1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
48 |
|
|
49 |
|
@Component |
50 |
|
@Named("org.xwiki.rest.internal.resources.attachments.AttachmentsResourceImpl") |
|
|
| 79.5% |
Uncovered Elements: 17 (83) |
Complexity: 17 |
Complexity Density: 0.31 |
|
51 |
|
public class AttachmentsResourceImpl extends BaseAttachmentsResource implements AttachmentsResource |
52 |
|
{ |
53 |
|
private static String FORM_FILENAME_FIELD = "filename"; |
54 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
55 |
2 |
@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 |
|
|
|
|
| 78.9% |
Uncovered Elements: 16 (76) |
Complexity: 15 |
Complexity Density: 0.3 |
|
69 |
1 |
@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 |
|
|
84 |
1 |
String attachmentName = null; |
85 |
|
|
86 |
|
|
87 |
1 |
String actualFileName = null; |
88 |
|
|
89 |
|
|
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 |
|
|
98 |
1 |
Enumeration e = bodyPart.getMatchingHeaders(new String[]{ "Content-disposition" }); |
99 |
2 |
while (e.hasMoreElements()) { |
100 |
1 |
Header h = (Header) e.nextElement(); |
101 |
|
|
102 |
|
|
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 |
|
|
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 |
|
} |