1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.doc; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.File; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.io.OutputStream; |
27 |
|
|
28 |
|
import org.apache.commons.fileupload.FileItem; |
29 |
|
import org.apache.commons.fileupload.disk.DiskFileItem; |
30 |
|
import org.apache.commons.io.IOUtils; |
31 |
|
import org.apache.commons.io.input.AutoCloseInputStream; |
32 |
|
import org.apache.commons.io.input.BoundedInputStream; |
33 |
|
import org.apache.commons.io.output.ProxyOutputStream; |
34 |
|
import org.xwiki.environment.Environment; |
35 |
|
import org.xwiki.store.UnexpectedException; |
36 |
|
|
37 |
|
import com.xpn.xwiki.web.Utils; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
|
|
|
| 89.2% |
Uncovered Elements: 11 (102) |
Complexity: 38 |
Complexity Density: 0.61 |
|
45 |
|
public class XWikiAttachmentContent implements Cloneable |
46 |
|
{ |
47 |
|
|
48 |
|
private static final byte[] NULLFILE = new byte[0]; |
49 |
|
|
50 |
|
|
51 |
|
private XWikiAttachment attachment; |
52 |
|
|
53 |
|
|
54 |
|
private boolean isContentDirty; |
55 |
|
|
56 |
|
|
57 |
|
private FileItem file; |
58 |
|
|
59 |
|
|
60 |
|
private XWikiDocument ownerDocument; |
61 |
|
|
62 |
|
|
63 |
|
@link |
64 |
|
|
65 |
|
@param |
66 |
|
@since |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
68 |
38542 |
public XWikiAttachmentContent(XWikiAttachmentContent original)... |
69 |
|
{ |
70 |
38542 |
this.file = original.file; |
71 |
38542 |
this.attachment = original.attachment; |
72 |
38542 |
this.isContentDirty = original.isContentDirty; |
73 |
38542 |
this.ownerDocument = original.ownerDocument; |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
@param |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
877 |
public XWikiAttachmentContent(XWikiAttachment attachment)... |
82 |
|
{ |
83 |
877 |
this.setAttachment(attachment); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
89 |
22 |
public XWikiAttachmentContent()... |
90 |
|
{ |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
95 |
10 |
protected XWikiAttachmentContent(XWikiAttachment attachment, FileItem f)... |
96 |
|
{ |
97 |
10 |
this.setAttachment(attachment); |
98 |
10 |
this.file = f; |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
@return |
103 |
|
@since |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
4 |
protected FileItem getFileItem()... |
106 |
|
{ |
107 |
4 |
return this.file; |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
@return |
112 |
|
@since |
113 |
|
|
|
|
| 75% |
Uncovered Elements: 3 (12) |
Complexity: 4 |
Complexity Density: 0.4 |
|
114 |
874 |
private static FileItem getNewFileItem()... |
115 |
|
{ |
116 |
874 |
final Environment env = Utils.getComponent(Environment.class); |
117 |
874 |
final File dir = new File(env.getTemporaryDirectory(), "attachment-cache"); |
118 |
874 |
try { |
119 |
874 |
if (!dir.mkdirs() && !dir.exists()) { |
120 |
0 |
throw new UnexpectedException("Failed to create directory for attachments " + dir); |
121 |
|
} |
122 |
874 |
final DiskFileItem dfi = new DiskFileItem(null, null, false, null, 10000, dir); |
123 |
|
|
124 |
874 |
dfi.getOutputStream().close(); |
125 |
|
|
126 |
874 |
dfi.getStoreLocation().deleteOnExit(); |
127 |
874 |
return dfi; |
128 |
|
} catch (IOException e) { |
129 |
0 |
throw new UnexpectedException("Failed to create new attachment temporary file.", e); |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
@return |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
138 |
1616 |
public long getId()... |
139 |
|
{ |
140 |
1616 |
return this.attachment.getId(); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
148 |
697 |
public void setId(long id)... |
149 |
|
{ |
150 |
|
|
151 |
|
|
152 |
|
} |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
38541 |
@Override... |
155 |
|
public Object clone() |
156 |
|
{ |
157 |
38541 |
return new XWikiAttachmentContent(this); |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
@return |
162 |
|
@deprecated@link |
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
164 |
914 |
@Deprecated... |
165 |
|
public byte[] getContent() |
166 |
|
{ |
167 |
914 |
if (this.file == null) { |
168 |
44 |
return NULLFILE; |
169 |
|
} |
170 |
870 |
return this.file.get(); |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@param |
177 |
|
@deprecated@link |
178 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
179 |
297 |
@Deprecated... |
180 |
|
public void setContent(byte[] content) |
181 |
|
{ |
182 |
297 |
try { |
183 |
297 |
byte[] internalContent = new byte[0]; |
184 |
297 |
if (content != null) { |
185 |
297 |
internalContent = content; |
186 |
|
} |
187 |
|
|
188 |
297 |
this.setContent(new ByteArrayInputStream(internalContent)); |
189 |
|
} catch (IOException e) { |
190 |
0 |
throw new RuntimeException("Failed to copy data to storage.", e); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
@return |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
197 |
3 |
public XWikiAttachment getAttachment()... |
198 |
|
{ |
199 |
3 |
return this.attachment; |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
205 |
39453 |
public void setAttachment(XWikiAttachment attachment)... |
206 |
|
{ |
207 |
39453 |
this.attachment = attachment; |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
@return |
214 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
1327 |
public boolean isContentDirty()... |
216 |
|
{ |
217 |
1327 |
return this.isContentDirty; |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
@param |
224 |
|
|
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
226 |
1580 |
public void setContentDirty(boolean contentDirty)... |
227 |
|
{ |
228 |
1580 |
this.isContentDirty = contentDirty; |
229 |
1581 |
if (contentDirty && this.ownerDocument != null) { |
230 |
4 |
this.ownerDocument.setMetaDataDirty(contentDirty); |
231 |
|
} |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
@return |
236 |
|
@since |
237 |
|
|
|
|
| 57.1% |
Uncovered Elements: 3 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
238 |
678 |
public InputStream getContentInputStream()... |
239 |
|
{ |
240 |
678 |
if (this.file == null) { |
241 |
0 |
return new ByteArrayInputStream(NULLFILE); |
242 |
|
} |
243 |
678 |
try { |
244 |
678 |
return new AutoCloseInputStream(this.file.getInputStream()); |
245 |
|
} catch (IOException e) { |
246 |
0 |
throw new RuntimeException("Failed to get InputStream", e); |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
@link |
254 |
|
|
255 |
|
|
256 |
|
@return |
257 |
|
@since |
258 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
259 |
874 |
public OutputStream getContentOutputStream()... |
260 |
|
{ |
261 |
874 |
final FileItem fi = getNewFileItem(); |
262 |
874 |
final XWikiAttachmentContent xac = this; |
263 |
874 |
final OutputStream fios; |
264 |
874 |
try { |
265 |
874 |
fios = fi.getOutputStream(); |
266 |
|
} catch (IOException e) { |
267 |
|
|
268 |
|
|
269 |
0 |
throw new RuntimeException("Exception getting attachment OutputStream.", e); |
270 |
|
} |
271 |
874 |
return (new ProxyOutputStream(fios) |
272 |
|
{ |
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
273 |
874 |
@Override... |
274 |
|
public void close() throws IOException |
275 |
|
{ |
276 |
874 |
super.close(); |
277 |
874 |
xac.file = fi; |
278 |
873 |
xac.setContentDirty(true); |
279 |
874 |
if (xac.attachment != null) { |
280 |
874 |
xac.attachment.setFilesize(xac.getSize()); |
281 |
|
} |
282 |
|
} |
283 |
|
}); |
284 |
|
} |
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
@param |
290 |
|
@param |
291 |
|
@throws |
292 |
|
@since |
293 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
294 |
1 |
public void setContent(InputStream is, int len) throws IOException... |
295 |
|
{ |
296 |
1 |
this.setContent(new BoundedInputStream(is, len)); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
@param |
303 |
|
@throws |
304 |
|
@since |
305 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
306 |
871 |
public void setContent(InputStream is) throws IOException... |
307 |
|
{ |
308 |
871 |
OutputStream fios = getContentOutputStream(); |
309 |
871 |
try { |
310 |
871 |
IOUtils.copy(is, fios); |
311 |
|
} finally { |
312 |
871 |
fios.close(); |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
|
@return |
318 |
|
@since |
319 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
320 |
1184 |
public int getSize()... |
321 |
|
{ |
322 |
1183 |
return (this.file != null) ? (int) this.file.getSize() : 0; |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
@param |
329 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
330 |
116476 |
public void setOwnerDocument(XWikiDocument ownerDocument)... |
331 |
|
{ |
332 |
116478 |
if (this.ownerDocument != ownerDocument) { |
333 |
116446 |
this.ownerDocument = ownerDocument; |
334 |
116446 |
if (this.isContentDirty && ownerDocument != null) { |
335 |
1508 |
ownerDocument.setMetaDataDirty(true); |
336 |
|
} |
337 |
|
} |
338 |
|
} |
339 |
|
} |