1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.api

File Attachment.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

4
36
22
1
252
129
27
0.75
1.64
22
1.23

Classes

Class Line # Actions
Attachment 37 36 0% 27 27
0.564516156.5%
 

Contributing tests

This file is covered by 2 tests. .

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.api;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.io.UnsupportedEncodingException;
25    import java.util.Date;
26    import java.util.List;
27   
28    import org.apache.commons.io.IOUtils;
29    import org.slf4j.Logger;
30    import org.slf4j.LoggerFactory;
31    import org.suigeneris.jrcs.rcs.Version;
32   
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.XWikiException;
35    import com.xpn.xwiki.doc.XWikiAttachment;
36   
 
37    public class Attachment extends Api
38    {
39    /** Logging helper object. */
40    private static final Logger LOGGER = LoggerFactory.getLogger(Attachment.class);
41   
42    private Document doc;
43   
44    private XWikiAttachment attachment;
45   
 
46  3816 toggle public Attachment(Document doc, XWikiAttachment attachment, XWikiContext context)
47    {
48  3816 super(context);
49  3816 this.doc = doc;
50  3816 this.attachment = attachment;
51    }
52   
53    /**
54    * @return the document the file is attached to
55    */
 
56  75 toggle public Document getDocument()
57    {
58  75 return this.doc;
59    }
60   
61    /**
62    * @return the document Id of the attachment
63    */
 
64  0 toggle public long getId()
65    {
66  0 return this.attachment.getId();
67    }
68   
69    /**
70    * @return the id of the document the file is attached to
71    */
 
72  0 toggle public long getDocId()
73    {
74  0 return this.doc.getId();
75    }
76   
77    /**
78    * @return the Attachment size
79    */
 
80  85 toggle public int getFilesize()
81    {
82  85 return this.attachment.getFilesize();
83    }
84   
85    /**
86    * @return the attachment name
87    */
 
88  661 toggle public String getFilename()
89    {
90  661 return this.attachment.getFilename();
91    }
92   
93    /**
94    * @return the login of the person who attach the file
95    */
 
96  81 toggle public String getAuthor()
97    {
98  81 return this.attachment.getAuthor();
99    }
100   
101    /**
102    * @return the last version number of the document
103    */
 
104  76 toggle public String getVersion()
105    {
106  76 return this.attachment.getVersion();
107    }
108   
109    /**
110    * @return the RCS object version of the document
111    */
 
112  0 toggle public Version getRCSVersion()
113    {
114  0 return this.attachment.getRCSVersion();
115    }
116   
117    /**
118    * @return the list of comments
119    */
 
120  0 toggle public String getComment()
121    {
122  0 return this.attachment.getComment();
123    }
124   
125    /**
126    * @return the date of the last uploaded version
127    */
 
128  75 toggle public Date getDate()
129    {
130  75 return this.attachment.getDate();
131    }
132   
133    /**
134    * @return the content of the attachment
135    * @throws XWikiException
136    */
 
137  60 toggle public byte[] getContent() throws XWikiException
138    {
139  60 return getContentAsBytes();
140    }
141   
 
142  60 toggle public byte[] getContentAsBytes() throws XWikiException
143    {
144  60 try {
145  60 return IOUtils.toByteArray(this.attachment.getContentInputStream(getXWikiContext()));
146    } catch (IOException ex) {
147    // This really shouldn't happen, but it's not nice to throw exceptions from scriptable APIs
148  0 return new byte[0];
149    }
150    }
151   
 
152  4 toggle public InputStream getContentInputStream()
153    {
154  4 try {
155  4 return this.attachment.getContentInputStream(getXWikiContext());
156    } catch (XWikiException e) {
157  0 LOGGER.error("Failed to get attachment input stream", e);
158   
159  0 return null;
160    }
161    }
162   
 
163  0 toggle public String getContentAsString() throws XWikiException
164    {
165    // TODO: detect correct encoding for XML files?
166  0 return new String(getContentAsBytes());
167    }
168   
 
169  0 toggle public String getContentAsString(String charset) throws XWikiException
170    {
171    // TODO: detect correct encoding for XML files?
172  0 byte[] contentBytes = getContentAsBytes();
173  0 try {
174  0 return new String(contentBytes, charset);
175    } catch (UnsupportedEncodingException ex) {
176  0 return new String(contentBytes);
177    }
178    }
179   
180    /**
181    * Get an array containing the versions of the attachment.
182    * Versions are represented as a JRCS Version object.
183    * This gets versions directly from the database which is slower than {@link #getVersionList()}.
184    * WARNING: If there is an error loading content from the database, a single element array will
185    * be returned containing only the current version of the attachment.
186    * Consider using {@link #getVersionList()} instead.
187    *
188    * @return an array of Versions.
189    * @throws XWikiException this will never happen.
190    */
 
191  1 toggle public Version[] getVersions() throws XWikiException
192    {
193  1 this.attachment.loadArchive(getXWikiContext());
194  1 return this.attachment.getVersions();
195    }
196   
197    /**
198    * Get a list of attachment versions from 1.1 to the current.
199    * Versions are represented as a JRCS Version object.
200    * This gets versions by counting backward from the current version to 1.1
201    * which will be correct as long as the database is in a consistant state.
202    *
203    * @return a list of Versions.
204    * @throws XWikiException this will never happen.
205    */
 
206  0 toggle public List<Version> getVersionList() throws XWikiException
207    {
208  0 return this.attachment.getVersionList();
209    }
210   
211    /**
212    * @return the XWikiAttachment object (without the wrapping) if you have the programming right
213    * @see XWikiAttachment
214    */
 
215  0 toggle public XWikiAttachment getAttachment()
216    {
217  0 if (hasProgrammingRights()) {
218  0 return this.attachment;
219    } else {
220  0 return null;
221    }
222    }
223   
224    /**
225    * @return the mimetype of the attachment
226    */
 
227  127 toggle public String getMimeType()
228    {
229  127 return this.attachment.getMimeType(getXWikiContext());
230    }
231   
232    /**
233    * @return true if it's an image
234    */
 
235  298 toggle public boolean isImage()
236    {
237  298 return this.attachment.isImage(getXWikiContext());
238    }
239   
240    /**
241    * Allow to easily access any revision of an attachment.
242    *
243    * @param rev Version to access, in the "Major.minor" format.
244    * @return Attachment API object, or <tt>null</tt> if the requested version does not exist.
245    * @throws XWikiException In case of an error.
246    */
 
247  8 toggle public Attachment getAttachmentRevision(String rev) throws XWikiException
248    {
249  8 XWikiAttachment att = this.attachment.getAttachmentRevision(rev, getXWikiContext());
250  8 return att == null ? null : new Attachment(getDocument(), att, this.context);
251    }
252    }