1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File DownloadRevAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
86% of files have more coverage

Code metrics

10
43
2
1
116
81
11
0.26
21.5
2
5.5

Classes

Class Line # Actions
DownloadRevAction 36 43 0% 11 55
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import java.io.IOException;
23   
24    import org.apache.commons.io.IOUtils;
25    import org.xwiki.model.EntityType;
26    import org.xwiki.resource.ResourceReference;
27    import org.xwiki.resource.ResourceReferenceManager;
28    import org.xwiki.resource.entity.EntityResourceReference;
29   
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.XWikiException;
32    import com.xpn.xwiki.doc.XWikiAttachment;
33    import com.xpn.xwiki.doc.XWikiDocument;
34    import com.xpn.xwiki.plugin.XWikiPluginManager;
35   
 
36    public class DownloadRevAction extends DownloadAction
37    {
 
38  0 toggle @Override
39    public String render(XWikiContext context) throws XWikiException
40    {
41  0 XWikiRequest request = context.getRequest();
42  0 XWikiResponse response = context.getResponse();
43  0 XWikiDocument doc = context.getDoc();
44  0 String rev = request.getParameter("rev");
45  0 String filename = getFileName();
46  0 XWikiAttachment attachment;
47   
48  0 if (context.getWiki().hasAttachmentRecycleBin(context) && request.getParameter("rid") != null) {
49  0 int recycleId = Integer.parseInt(request.getParameter("rid"));
50  0 attachment = new XWikiAttachment(doc, filename);
51  0 attachment =
52    context.getWiki().getAttachmentRecycleBinStore().restoreFromRecycleBin(attachment, recycleId, context,
53    true);
54  0 } else if (request.getParameter("id") != null) {
55  0 int id = Integer.parseInt(request.getParameter("id"));
56  0 attachment = doc.getAttachmentList().get(id);
57    } else {
58  0 attachment = doc.getAttachment(filename);
59    }
60  0 if (attachment == null) {
61  0 Object[] args = { filename };
62  0 throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
63    XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment {0} not found", null, args);
64    }
65   
66  0 synchronized (attachment) {
67  0 try {
68  0 attachment = attachment.getAttachmentRevision(rev, context);
69  0 if (attachment == null) {
70  0 throw new XWikiException();
71    }
72    } catch (XWikiException e) {
73  0 String url = context.getDoc().getURL("viewattachrev", true, context);
74  0 url += "/" + filename;
75  0 if (request.getParameter("rid") != null) {
76  0 url += "?rid=" + request.getParameter("rid");
77    }
78  0 try {
79  0 context.getResponse().sendRedirect(url);
80  0 return null;
81    } catch (IOException ioe) {
82  0 ioe.printStackTrace();
83    }
84    }
85    }
86   
87  0 XWikiPluginManager plugins = context.getWiki().getPluginManager();
88  0 attachment = plugins.downloadAttachment(attachment, context);
89   
90    // Choose the right content type
91  0 String mimetype = attachment.getMimeType(context);
92  0 response.setContentType(mimetype);
93   
94  0 response.setDateHeader("Last-Modified", attachment.getDate().getTime());
95    // Sending the content of the attachment
96  0 try {
97  0 response.setContentLength(attachment.getContentSize(context));
98  0 IOUtils.copy(attachment.getContentInputStream(context), response.getOutputStream());
99    } catch (IOException e) {
100  0 throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
101    XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e);
102    }
103  0 return null;
104    }
105   
106    /**
107    * @return the filename of the attachment.
108    */
 
109  0 toggle private String getFileName()
110    {
111    // Extract the Attachment file name from the parsed request URL that was done before this Action is called
112  0 ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
113  0 EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
114  0 return entityResource.getEntityReference().extractReference(EntityType.ATTACHMENT).getName();
115    }
116    }