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

File AttachmentResourceImpl.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

10
31
3
1
122
81
11
0.35
10.33
3
3.67

Classes

Class Line # Actions
AttachmentResourceImpl 43 31 0% 11 5
0.886363688.6%
 

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 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 $Id: 6998b856371a0127ca9eca689f6275b71165a267 $
40    */
41    @Component
42    @Named("org.xwiki.rest.internal.resources.attachments.AttachmentResourceImpl")
 
43    public class AttachmentResourceImpl extends BaseAttachmentsResource implements AttachmentResource
44    {
 
45  42 toggle @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   
 
64  46 toggle @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    /* Attach the file */
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   
 
90  2 toggle @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    }