1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.plugin.zipexplorer; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.BufferedInputStream; |
24 |
|
import java.io.DataInputStream; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.InputStream; |
27 |
|
import java.net.URLDecoder; |
28 |
|
import java.util.ArrayList; |
29 |
|
import java.util.HashMap; |
30 |
|
import java.util.List; |
31 |
|
import java.util.Map; |
32 |
|
import java.util.zip.ZipEntry; |
33 |
|
import java.util.zip.ZipInputStream; |
34 |
|
|
35 |
|
import org.apache.commons.io.IOUtils; |
36 |
|
import org.slf4j.Logger; |
37 |
|
import org.slf4j.LoggerFactory; |
38 |
|
|
39 |
|
import com.xpn.xwiki.XWikiContext; |
40 |
|
import com.xpn.xwiki.XWikiException; |
41 |
|
import com.xpn.xwiki.api.Api; |
42 |
|
import com.xpn.xwiki.api.Attachment; |
43 |
|
import com.xpn.xwiki.api.Document; |
44 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
45 |
|
import com.xpn.xwiki.objects.classes.ListItem; |
46 |
|
import com.xpn.xwiki.plugin.XWikiDefaultPlugin; |
47 |
|
import com.xpn.xwiki.plugin.XWikiPluginInterface; |
48 |
|
|
49 |
|
|
50 |
|
@link |
51 |
|
|
52 |
|
@version |
53 |
|
@deprecated |
54 |
|
|
55 |
|
@Deprecated |
|
|
| 83.3% |
Uncovered Elements: 21 (126) |
Complexity: 34 |
Complexity Density: 0.38 |
|
56 |
|
public class ZipExplorerPlugin extends XWikiDefaultPlugin |
57 |
|
{ |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private static final Logger LOG = LoggerFactory.getLogger(ZipExplorerPlugin.class); |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private static final String URL_SEPARATOR = "/"; |
69 |
|
|
70 |
|
|
71 |
|
@param |
72 |
|
@param |
73 |
|
@param |
74 |
|
|
75 |
|
@see |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
77 |
10 |
public ZipExplorerPlugin(String name, String className, XWikiContext context)... |
78 |
|
{ |
79 |
10 |
super(name, className, context); |
80 |
10 |
init(context); |
81 |
|
} |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0 |
@Override... |
84 |
|
public String getName() |
85 |
|
{ |
86 |
0 |
return "zipexplorer"; |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
0 |
@Override... |
90 |
|
public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) |
91 |
|
{ |
92 |
0 |
return new ZipExplorerPluginAPI((ZipExplorerPlugin) plugin, context); |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
@param |
101 |
|
@param |
102 |
|
@return |
103 |
|
|
104 |
|
@see |
105 |
|
|
|
|
| 76.3% |
Uncovered Elements: 9 (38) |
Complexity: 9 |
Complexity Density: 0.32 |
|
106 |
4 |
@Override... |
107 |
|
public XWikiAttachment downloadAttachment(XWikiAttachment attachment, XWikiContext context) |
108 |
|
{ |
109 |
4 |
String url = context.getRequest().getRequestURI(); |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
4 |
if (attachment.getReference().getDocumentReference().getSpaceReferences().size() > 1 |
117 |
|
|| !isValidZipURL(url, context.getAction().trim())) |
118 |
|
{ |
119 |
3 |
return attachment; |
120 |
|
} |
121 |
|
|
122 |
1 |
String filename = getFileLocationFromZipURL(url, context.getAction().trim()); |
123 |
|
|
124 |
|
|
125 |
1 |
XWikiAttachment newAttachment = new XWikiAttachment(); |
126 |
1 |
newAttachment.setDoc(attachment.getDoc()); |
127 |
1 |
newAttachment.setAuthor(attachment.getAuthor()); |
128 |
1 |
newAttachment.setDate(attachment.getDate()); |
129 |
|
|
130 |
1 |
InputStream stream = null; |
131 |
1 |
try { |
132 |
1 |
stream = new BufferedInputStream(attachment.getContentInputStream(context)); |
133 |
|
|
134 |
1 |
if (!isZipFile(stream)) { |
135 |
0 |
return attachment; |
136 |
|
} |
137 |
|
|
138 |
1 |
ZipInputStream zis = new ZipInputStream(stream); |
139 |
1 |
ZipEntry entry; |
140 |
|
|
141 |
? |
while ((entry = zis.getNextEntry()) != null) { |
142 |
1 |
String entryName = entry.getName(); |
143 |
|
|
144 |
1 |
if (entryName.equals(filename)) { |
145 |
1 |
newAttachment.setFilename(entryName); |
146 |
|
|
147 |
1 |
if (entry.getSize() == -1) { |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
1 |
byte[] data = IOUtils.toByteArray(zis); |
152 |
|
|
153 |
1 |
newAttachment.setContent(data); |
154 |
|
} else { |
155 |
0 |
newAttachment.setContent(zis, (int) entry.getSize()); |
156 |
|
} |
157 |
1 |
break; |
158 |
|
} |
159 |
|
} |
160 |
|
} catch (XWikiException e) { |
161 |
0 |
e.printStackTrace(); |
162 |
|
} catch (IOException e) { |
163 |
0 |
e.printStackTrace(); |
164 |
|
} finally { |
165 |
1 |
IOUtils.closeQuietly(stream); |
166 |
|
} |
167 |
1 |
return newAttachment; |
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
|
@param |
172 |
|
@param |
173 |
|
@param |
174 |
|
@return |
175 |
|
|
176 |
|
@see |
177 |
|
|
|
|
| 70.6% |
Uncovered Elements: 5 (17) |
Complexity: 5 |
Complexity Density: 0.38 |
|
178 |
2 |
public List<String> getFileList(Document document, String attachmentName, XWikiContext context)... |
179 |
|
{ |
180 |
2 |
List<String> zipList = new ArrayList<String>(); |
181 |
2 |
Attachment attachment = document.getAttachment(attachmentName); |
182 |
|
|
183 |
2 |
InputStream stream = null; |
184 |
2 |
try { |
185 |
2 |
stream = new ByteArrayInputStream(attachment.getContent()); |
186 |
|
|
187 |
2 |
if (isZipFile(stream)) { |
188 |
2 |
ZipInputStream zis = new ZipInputStream(stream); |
189 |
2 |
ZipEntry entry; |
190 |
? |
while ((entry = zis.getNextEntry()) != null) { |
191 |
4 |
zipList.add(entry.getName()); |
192 |
|
} |
193 |
|
} |
194 |
|
} catch (XWikiException e) { |
195 |
0 |
e.printStackTrace(); |
196 |
|
} catch (IOException e) { |
197 |
0 |
e.printStackTrace(); |
198 |
|
} |
199 |
2 |
return zipList; |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
@link |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@param |
219 |
|
@param |
220 |
|
@param |
221 |
|
@return@link |
222 |
|
|
223 |
|
@see |
224 |
|
|
|
|
| 95.7% |
Uncovered Elements: 1 (23) |
Complexity: 5 |
Complexity Density: 0.29 |
|
225 |
1 |
public List<ListItem> getFileTreeList(Document document, String attachmentName, XWikiContext context)... |
226 |
|
{ |
227 |
1 |
List<String> flatList = getFileList(document, attachmentName, context); |
228 |
1 |
Map<String, ListItem> fileTree = new HashMap<String, ListItem>(); |
229 |
1 |
List<ListItem> res = new ArrayList<ListItem>(); |
230 |
1 |
for (String url : flatList) { |
231 |
2 |
StringBuilder buf = new StringBuilder(url.length()); |
232 |
2 |
String parentBuf = ""; |
233 |
2 |
String[] aUrl = url.split(URL_SEPARATOR); |
234 |
5 |
for (int i = 0; i < aUrl.length; i++) { |
235 |
3 |
if (i == aUrl.length - 1 && !url.endsWith(URL_SEPARATOR)) { |
236 |
2 |
buf.append(aUrl[i]); |
237 |
|
} else { |
238 |
1 |
buf.append(aUrl[i] + URL_SEPARATOR); |
239 |
|
} |
240 |
3 |
ListItem item = new ListItem(buf.toString(), aUrl[i], parentBuf); |
241 |
3 |
if (!fileTree.containsKey(buf.toString())) { |
242 |
3 |
res.add(item); |
243 |
|
} |
244 |
3 |
fileTree.put(buf.toString(), item); |
245 |
3 |
parentBuf = buf.toString(); |
246 |
|
} |
247 |
|
} |
248 |
1 |
return res; |
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
|
@param |
253 |
|
@param |
254 |
|
@param |
255 |
|
@param |
256 |
|
@return |
257 |
|
|
258 |
|
@see |
259 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
260 |
1 |
public String getFileLink(Document document, String attachmentName, String fileName, XWikiContext context)... |
261 |
|
{ |
262 |
1 |
return document.getAttachmentURL(attachmentName) + URL_SEPARATOR + fileName; |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
@param |
267 |
|
@param |
268 |
|
@return |
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 5 |
Complexity Density: 0.45 |
|
275 |
14 |
protected String getFileLocationFromZipURL(String url, String action)... |
276 |
|
{ |
277 |
14 |
String path = url.substring(url.indexOf(URL_SEPARATOR + action)); |
278 |
13 |
int pos = 0; |
279 |
64 |
for (int i = 0; pos > -1 && i < 4; i++) { |
280 |
51 |
pos = path.indexOf(URL_SEPARATOR, pos + 1); |
281 |
|
} |
282 |
13 |
if (pos == -1) { |
283 |
6 |
return ""; |
284 |
|
} |
285 |
7 |
path = path.substring(pos + 1); |
286 |
|
|
287 |
|
|
288 |
|
|
289 |
7 |
try { |
290 |
7 |
path = URLDecoder.decode(path, "UTF-8"); |
291 |
|
} catch (IOException e) { |
292 |
|
|
293 |
|
|
294 |
|
|
295 |
0 |
LOG.error("Failed to decode URL path [" + path + "]", e); |
296 |
|
} |
297 |
|
|
298 |
7 |
return path; |
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
|
@param |
303 |
|
@return |
304 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.3 |
|
305 |
6 |
protected boolean isZipFile(InputStream filecontent)... |
306 |
|
{ |
307 |
6 |
int standardZipHeader = 0x504b0304; |
308 |
6 |
filecontent.mark(8); |
309 |
6 |
try { |
310 |
6 |
DataInputStream datastream = new DataInputStream(filecontent); |
311 |
6 |
int fileHeader = datastream.readInt(); |
312 |
5 |
return (standardZipHeader == fileHeader); |
313 |
|
} catch (IOException e) { |
314 |
|
|
315 |
|
} finally { |
316 |
|
|
317 |
6 |
try { |
318 |
6 |
filecontent.reset(); |
319 |
|
} catch (IOException e) { |
320 |
0 |
e.printStackTrace(); |
321 |
|
} |
322 |
|
} |
323 |
1 |
return false; |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
|
@param |
328 |
|
@param |
329 |
|
@return |
330 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
331 |
10 |
protected boolean isValidZipURL(String url, String action)... |
332 |
|
{ |
333 |
10 |
boolean isValidZipURL = false; |
334 |
10 |
try { |
335 |
|
|
336 |
|
|
337 |
10 |
String filenameInZip = getFileLocationFromZipURL(url, action); |
338 |
|
|
339 |
|
|
340 |
|
|
341 |
9 |
if (filenameInZip.length() > 0) { |
342 |
4 |
isValidZipURL = true; |
343 |
|
} |
344 |
|
} catch (Exception e) { |
345 |
|
|
346 |
|
|
347 |
|
} |
348 |
10 |
return isValidZipURL; |
349 |
|
} |
350 |
|
} |