1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.api

File Attachment.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

6
38
23
1
265
135
29
0.76
1.65
23
1.26

Classes

Class Line # Actions
Attachment 37 38 0% 29 26
0.611940361.2%
 

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  11932 toggle public Attachment(Document doc, XWikiAttachment attachment, XWikiContext context)
47    {
48  11932 super(context);
49  11932 this.doc = doc;
50  11932 this.attachment = attachment;
51    }
52   
53    /**
54    * @return the document the file is attached to
55    */
 
56  109 toggle public Document getDocument()
57    {
58  109 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    * @deprecated since 9.0RC1, use {@link #getLongSize()} instead
80    */
 
81  72 toggle @Deprecated
82    public int getFilesize()
83    {
84  72 long longSize = getLongSize();
85   
86  72 return longSize > (long) Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) longSize;
87    }
88   
89    /**
90    * @return the number of bytes in this attachment content
91    * @since 9.0RC1
92    */
 
93  399 toggle public long getLongSize()
94    {
95  399 return this.attachment.getLongSize();
96    }
97   
98    /**
99    * @return the attachment name
100    */
 
101  3106 toggle public String getFilename()
102    {
103  3106 return this.attachment.getFilename();
104    }
105   
106    /**
107    * @return the login of the person who attach the file
108    */
 
109  286 toggle public String getAuthor()
110    {
111  286 return this.attachment.getAuthor();
112    }
113   
114    /**
115    * @return the last version number of the document
116    */
 
117  101 toggle public String getVersion()
118    {
119  101 return this.attachment.getVersion();
120    }
121   
122    /**
123    * @return the RCS object version of the document
124    */
 
125  0 toggle public Version getRCSVersion()
126    {
127  0 return this.attachment.getRCSVersion();
128    }
129   
130    /**
131    * @return the list of comments
132    */
 
133  0 toggle public String getComment()
134    {
135  0 return this.attachment.getComment();
136    }
137   
138    /**
139    * @return the date of the last uploaded version
140    */
 
141  197 toggle public Date getDate()
142    {
143  197 return this.attachment.getDate();
144    }
145   
146    /**
147    * @return the content of the attachment
148    * @throws XWikiException
149    */
 
150  48 toggle public byte[] getContent() throws XWikiException
151    {
152  48 return getContentAsBytes();
153    }
154   
 
155  50 toggle public byte[] getContentAsBytes() throws XWikiException
156    {
157  50 try {
158  50 return IOUtils.toByteArray(this.attachment.getContentInputStream(getXWikiContext()));
159    } catch (IOException ex) {
160    // This really shouldn't happen, but it's not nice to throw exceptions from scriptable APIs
161  0 return new byte[0];
162    }
163    }
164   
 
165  31 toggle public InputStream getContentInputStream()
166    {
167  31 try {
168  31 return this.attachment.getContentInputStream(getXWikiContext());
169    } catch (XWikiException e) {
170  0 LOGGER.error("Failed to get attachment input stream", e);
171   
172  0 return null;
173    }
174    }
175   
 
176  2 toggle public String getContentAsString() throws XWikiException
177    {
178    // TODO: detect correct encoding for XML files?
179  2 return new String(getContentAsBytes());
180    }
181   
 
182  0 toggle public String getContentAsString(String charset) throws XWikiException
183    {
184    // TODO: detect correct encoding for XML files?
185  0 byte[] contentBytes = getContentAsBytes();
186  0 try {
187  0 return new String(contentBytes, charset);
188    } catch (UnsupportedEncodingException ex) {
189  0 return new String(contentBytes);
190    }
191    }
192   
193    /**
194    * Get an array containing the versions of the attachment.
195    * Versions are represented as a JRCS Version object.
196    * This gets versions directly from the database which is slower than {@link #getVersionList()}.
197    * WARNING: If there is an error loading content from the database, a single element array will
198    * be returned containing only the current version of the attachment.
199    * Consider using {@link #getVersionList()} instead.
200    *
201    * @return an array of Versions.
202    * @throws XWikiException this will never happen.
203    */
 
204  2 toggle public Version[] getVersions() throws XWikiException
205    {
206  2 this.attachment.loadArchive(getXWikiContext());
207  2 return this.attachment.getVersions();
208    }
209   
210    /**
211    * Get a list of attachment versions from 1.1 to the current.
212    * Versions are represented as a JRCS Version object.
213    * This gets versions by counting backward from the current version to 1.1
214    * which will be correct as long as the database is in a consistant state.
215    *
216    * @return a list of Versions.
217    * @throws XWikiException this will never happen.
218    */
 
219  0 toggle public List<Version> getVersionList() throws XWikiException
220    {
221  0 return this.attachment.getVersionList();
222    }
223   
224    /**
225    * @return the XWikiAttachment object (without the wrapping) if you have the programming right
226    * @see XWikiAttachment
227    */
 
228  0 toggle public XWikiAttachment getAttachment()
229    {
230  0 if (hasProgrammingRights()) {
231  0 return this.attachment;
232    } else {
233  0 return null;
234    }
235    }
236   
237    /**
238    * @return the mimetype of the attachment
239    */
 
240  469 toggle public String getMimeType()
241    {
242  469 return this.attachment.getMimeType(getXWikiContext());
243    }
244   
245    /**
246    * @return true if it's an image
247    */
 
248  2558 toggle public boolean isImage()
249    {
250  2558 return this.attachment.isImage(getXWikiContext());
251    }
252   
253    /**
254    * Allow to easily access any revision of an attachment.
255    *
256    * @param rev Version to access, in the "Major.minor" format.
257    * @return Attachment API object, or <tt>null</tt> if the requested version does not exist.
258    * @throws XWikiException In case of an error.
259    */
 
260  9 toggle public Attachment getAttachmentRevision(String rev) throws XWikiException
261    {
262  9 XWikiAttachment att = this.attachment.getAttachmentRevision(rev, getXWikiContext());
263  9 return att == null ? null : new Attachment(getDocument(), att, this.context);
264    }
265    }