1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rest.internal.resources.attachments

File AttachmentsResourceImpl.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

26
55
2
1
165
114
17
0.31
27.5
2
8.5

Classes

Class Line # Actions
AttachmentsResourceImpl 51 55 0% 17 17
0.7951807479.5%
 

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 org.xwiki.rest.internal.resources.attachments;
21   
22    import java.io.ByteArrayOutputStream;
23    import java.io.InputStream;
24    import java.util.Enumeration;
25    import java.util.List;
26   
27    import javax.inject.Named;
28    import javax.mail.BodyPart;
29    import javax.mail.Header;
30    import javax.mail.Multipart;
31    import javax.ws.rs.WebApplicationException;
32    import javax.ws.rs.core.Response;
33    import javax.ws.rs.core.Response.Status;
34   
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.rest.XWikiRestException;
37    import org.xwiki.rest.internal.Utils;
38    import org.xwiki.rest.internal.resources.BaseAttachmentsResource;
39    import org.xwiki.rest.model.jaxb.Attachments;
40    import org.xwiki.rest.resources.attachments.AttachmentResource;
41    import org.xwiki.rest.resources.attachments.AttachmentsResource;
42   
43    import com.xpn.xwiki.XWikiException;
44    import com.xpn.xwiki.api.Document;
45   
46    /**
47    * @version $Id: 43c29eb9e7313eb3a9623e4c17d0233819821ff7 $
48    */
49    @Component
50    @Named("org.xwiki.rest.internal.resources.attachments.AttachmentsResourceImpl")
 
51    public class AttachmentsResourceImpl extends BaseAttachmentsResource implements AttachmentsResource
52    {
53    private static String FORM_FILENAME_FIELD = "filename";
54   
 
55  2 toggle @Override
56    public Attachments getAttachments(String wikiName, String spaceName, String pageName, Integer start, Integer number,
57    Boolean withPrettyNames) throws XWikiRestException
58    {
59  2 try {
60  2 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
61  2 Document doc = documentInfo.getDocument();
62   
63  2 return getAttachmentsForDocument(doc, start, number, withPrettyNames);
64    } catch (XWikiException e) {
65  0 throw new XWikiRestException(e);
66    }
67    }
68   
 
69  1 toggle @Override
70    public Response addAttachment(String wikiName, String spaceName, String pageName, Multipart multipart)
71    throws XWikiRestException
72    {
73  1 try {
74  1 List<String> spaces = parseSpaceSegments(spaceName);
75  1 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, true, true);
76   
77  1 Document doc = documentInfo.getDocument();
78   
79  1 if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
80  0 throw new WebApplicationException(Status.UNAUTHORIZED);
81    }
82   
83    /* The name to be used */
84  1 String attachmentName = null;
85   
86    /* The actual filename of the sent file */
87  1 String actualFileName = null;
88   
89    /* The specified file name using a form field */
90  1 String overriddenFileName = null;
91  1 String contentType = null;
92  1 InputStream inputStream = null;
93   
94  2 for (int i = 0; i < multipart.getCount(); i++) {
95  1 BodyPart bodyPart = multipart.getBodyPart(i);
96   
97    /* Get the content disposition headers */
98  1 Enumeration e = bodyPart.getMatchingHeaders(new String[]{ "Content-disposition" });
99  2 while (e.hasMoreElements()) {
100  1 Header h = (Header) e.nextElement();
101   
102    /* Parse header data. Normally headers are in the form form-data; key="value"; ... */
103  1 if (h.getValue().startsWith("form-data")) {
104  1 String[] fieldData = h.getValue().split(";");
105  1 for (String s : fieldData) {
106  3 String[] pair = s.split("=");
107  3 if (pair.length == 2) {
108  2 String key = pair[0].trim();
109  2 String value = pair[1].replace("\"", "").trim();
110   
111  2 if ("name".equals(key)) {
112  1 if (FORM_FILENAME_FIELD.equals(value)) {
113  0 overriddenFileName = bodyPart.getContent().toString();
114    }
115  1 } else if ("filename".equals(key)) {
116  1 actualFileName = value;
117  1 contentType = bodyPart.getContentType();
118  1 inputStream = bodyPart.getInputStream();
119    }
120    }
121    }
122    }
123    }
124    }
125   
126  1 if (overriddenFileName != null) {
127  0 attachmentName = overriddenFileName;
128    } else {
129  1 attachmentName = actualFileName;
130    }
131   
132  1 if (attachmentName == null) {
133  0 throw new WebApplicationException(Status.BAD_REQUEST);
134    }
135   
136  1 byte[] buffer = new byte[4096];
137  1 ByteArrayOutputStream baos = new ByteArrayOutputStream();
138  1 while (true) {
139  1 int read = inputStream.read(buffer);
140  1 if (read != 4096) {
141  1 if (read != -1) {
142  1 baos.write(buffer, 0, read);
143    }
144   
145  1 break;
146    } else {
147  0 baos.write(buffer);
148    }
149    }
150  1 baos.flush();
151   
152    /* Attach the file */
153  1 AttachmentInfo attachmentInfo = storeAttachment(doc, attachmentName, baos.toByteArray());
154   
155  1 if (attachmentInfo.isAlreadyExisting()) {
156  0 return Response.status(Status.ACCEPTED).entity(attachmentInfo.getAttachment()).build();
157    } else {
158  1 return Response.created(Utils.createURI(uriInfo.getBaseUri(), AttachmentResource.class, wikiName,
159    spaces, pageName, attachmentName)).entity(attachmentInfo.getAttachment()).build();
160    }
161    } catch (Exception e) {
162  0 throw new XWikiRestException(e);
163    }
164    }
165    }