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 javax.inject.Named; |
23 |
|
import javax.ws.rs.WebApplicationException; |
24 |
|
import javax.ws.rs.core.Response; |
25 |
|
import javax.ws.rs.core.Response.Status; |
26 |
|
|
27 |
|
import org.xwiki.component.annotation.Component; |
28 |
|
import org.xwiki.rest.XWikiRestException; |
29 |
|
import org.xwiki.rest.internal.Utils; |
30 |
|
import org.xwiki.rest.internal.resources.BaseAttachmentsResource; |
31 |
|
import org.xwiki.rest.resources.attachments.AttachmentResource; |
32 |
|
|
33 |
|
import com.xpn.xwiki.XWikiException; |
34 |
|
import com.xpn.xwiki.api.Document; |
35 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
36 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
37 |
|
|
38 |
|
|
39 |
|
@version |
40 |
|
|
41 |
|
@Component |
42 |
|
@Named("org.xwiki.rest.internal.resources.attachments.AttachmentResourceImpl") |
|
|
| 88.6% |
Uncovered Elements: 5 (44) |
Complexity: 11 |
Complexity Density: 0.35 |
|
43 |
|
public class AttachmentResourceImpl extends BaseAttachmentsResource implements AttachmentResource |
44 |
|
{ |
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
45 |
42 |
@Override... |
46 |
|
public Response getAttachment(String wikiName, String spaceName, String pageName, String attachmentName) |
47 |
|
throws XWikiRestException |
48 |
|
{ |
49 |
42 |
try { |
50 |
42 |
DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false); |
51 |
42 |
Document doc = documentInfo.getDocument(); |
52 |
|
|
53 |
42 |
final com.xpn.xwiki.api.Attachment xwikiAttachment = doc.getAttachment(attachmentName); |
54 |
42 |
if (xwikiAttachment == null) { |
55 |
10 |
throw new WebApplicationException(Status.NOT_FOUND); |
56 |
|
} |
57 |
|
|
58 |
32 |
return Response.ok().type(xwikiAttachment.getMimeType()).entity(xwikiAttachment.getContent()).build(); |
59 |
|
} catch (XWikiException e) { |
60 |
0 |
throw new XWikiRestException(e); |
61 |
|
} |
62 |
|
} |
63 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
64 |
46 |
@Override... |
65 |
|
public Response putAttachment(String wikiName, String spaceName, String pageName, String attachmentName, |
66 |
|
byte[] content) throws XWikiRestException |
67 |
|
{ |
68 |
46 |
try { |
69 |
46 |
DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, true); |
70 |
|
|
71 |
46 |
Document doc = documentInfo.getDocument(); |
72 |
|
|
73 |
46 |
if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) { |
74 |
1 |
throw new WebApplicationException(Status.UNAUTHORIZED); |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
45 |
AttachmentInfo attachmentInfo = storeAttachment(doc, attachmentName, content); |
79 |
|
|
80 |
45 |
if (attachmentInfo.isAlreadyExisting()) { |
81 |
3 |
return Response.status(Status.ACCEPTED).entity(attachmentInfo.getAttachment()).build(); |
82 |
|
} else { |
83 |
42 |
return Response.created(uriInfo.getAbsolutePath()).entity(attachmentInfo.getAttachment()).build(); |
84 |
|
} |
85 |
|
} catch (XWikiException e) { |
86 |
0 |
throw new XWikiRestException(e); |
87 |
|
} |
88 |
|
} |
89 |
|
|
|
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
90 |
2 |
@Override... |
91 |
|
public void deleteAttachment(String wikiName, String spaceName, String pageName, String attachmentName) |
92 |
|
throws XWikiRestException |
93 |
|
{ |
94 |
2 |
try { |
95 |
2 |
DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, true); |
96 |
|
|
97 |
2 |
Document doc = documentInfo.getDocument(); |
98 |
|
|
99 |
2 |
if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) { |
100 |
1 |
throw new WebApplicationException(Status.UNAUTHORIZED); |
101 |
|
} |
102 |
|
|
103 |
1 |
com.xpn.xwiki.api.Attachment xwikiAttachment = doc.getAttachment(attachmentName); |
104 |
1 |
if (xwikiAttachment == null) { |
105 |
0 |
throw new WebApplicationException(Status.NOT_FOUND); |
106 |
|
} |
107 |
|
|
108 |
1 |
XWikiDocument xwikiDocument = |
109 |
|
Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), |
110 |
|
Utils.getXWikiContext(componentManager)); |
111 |
1 |
XWikiAttachment baseXWikiAttachment = xwikiDocument.getAttachment(attachmentName); |
112 |
|
|
113 |
1 |
xwikiDocument.removeAttachment(baseXWikiAttachment); |
114 |
|
|
115 |
1 |
Utils.getXWiki(componentManager).saveDocument(xwikiDocument, |
116 |
|
"Deleted attachment [" + baseXWikiAttachment.getFilename() + "]", |
117 |
|
Utils.getXWikiContext(componentManager)); |
118 |
|
} catch (XWikiException e) { |
119 |
0 |
throw new XWikiRestException(e); |
120 |
|
} |
121 |
|
} |
122 |
|
} |