1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.filter.xar.internal.input; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.InputStream; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Provider; |
27 |
|
|
28 |
|
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
31 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
32 |
|
import org.xwiki.filter.FilterEventParameters; |
33 |
|
import org.xwiki.filter.FilterException; |
34 |
|
import org.xwiki.filter.input.AbstractBeanInputFilterStream; |
35 |
|
import org.xwiki.filter.input.InputSource; |
36 |
|
import org.xwiki.filter.input.InputStreamInputSource; |
37 |
|
import org.xwiki.filter.input.ReaderInputSource; |
38 |
|
import org.xwiki.filter.xar.input.XARInputProperties; |
39 |
|
import org.xwiki.filter.xar.internal.XARFilterUtils; |
40 |
|
import org.xwiki.filter.xml.input.SourceInputSource; |
41 |
|
import org.xwiki.model.reference.EntityReference; |
42 |
|
|
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component(hints = {XARFilterUtils.ROLEHINT_13, XARFilterUtils.ROLEHINT_12, XARFilterUtils.ROLEHINT_11}) |
48 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
|
|
| 87.7% |
Uncovered Elements: 7 (57) |
Complexity: 18 |
Complexity Density: 0.45 |
|
49 |
|
public class XARInputFilterStream extends AbstractBeanInputFilterStream<XARInputProperties, XARInputFilter> |
50 |
|
{ |
51 |
|
@Inject |
52 |
|
private Provider<WikiReader> wikiReaderProvider; |
53 |
|
|
54 |
|
@Inject |
55 |
|
private Provider<DocumentLocaleReader> documentLocaleReaderProvider; |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
57 |
4287 |
@Override... |
58 |
|
public void close() throws IOException |
59 |
|
{ |
60 |
4288 |
this.properties.getSource().close(); |
61 |
|
} |
62 |
|
|
|
|
| 80% |
Uncovered Elements: 5 (25) |
Complexity: 9 |
Complexity Density: 0.47 |
|
63 |
4290 |
@Override... |
64 |
|
protected void read(Object filter, XARInputFilter proxyFilter) throws FilterException |
65 |
|
{ |
66 |
4290 |
InputSource inputSource = this.properties.getSource(); |
67 |
|
|
68 |
4290 |
if (this.properties.isForceDocument() || inputSource instanceof ReaderInputSource |
69 |
|
|| inputSource instanceof SourceInputSource) { |
70 |
4282 |
readDocument(filter, proxyFilter); |
71 |
8 |
} else if (inputSource instanceof InputStreamInputSource) { |
72 |
8 |
InputStream stream; |
73 |
8 |
try { |
74 |
8 |
stream = ((InputStreamInputSource) inputSource).getInputStream(); |
75 |
|
} catch (IOException e) { |
76 |
0 |
throw new FilterException("Failed to get input stream", e); |
77 |
|
} |
78 |
|
|
79 |
8 |
Boolean iszip; |
80 |
8 |
try { |
81 |
8 |
iszip = isZip(stream); |
82 |
|
} catch (IOException e) { |
83 |
0 |
throw new FilterException("Failed to read input stream", e); |
84 |
|
} finally { |
85 |
8 |
try { |
86 |
8 |
inputSource.close(); |
87 |
|
} catch (IOException e) { |
88 |
0 |
throw new FilterException("Failed to close the source", e); |
89 |
|
} |
90 |
|
} |
91 |
|
|
92 |
8 |
if (iszip == Boolean.FALSE) { |
93 |
1 |
readDocument(filter, proxyFilter); |
94 |
|
} else { |
95 |
7 |
readXAR(filter, proxyFilter); |
96 |
|
} |
97 |
|
} else { |
98 |
0 |
throw new FilterException(String.format("Unsupported input source of type [%s]", inputSource.getClass())); |
99 |
|
} |
100 |
|
} |
101 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
102 |
7 |
private void readXAR(Object filter, XARInputFilter proxyFilter) throws FilterException... |
103 |
|
{ |
104 |
7 |
WikiReader wikiReader = this.wikiReaderProvider.get(); |
105 |
7 |
wikiReader.setProperties(this.properties); |
106 |
|
|
107 |
7 |
try { |
108 |
7 |
wikiReader.read(filter, proxyFilter); |
109 |
|
} catch (Exception e) { |
110 |
0 |
throw new FilterException("Failed to read XAR package", e); |
111 |
|
} |
112 |
|
} |
113 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
114 |
4283 |
protected void readDocument(Object filter, XARInputFilter proxyFilter) throws FilterException... |
115 |
|
{ |
116 |
4283 |
DocumentLocaleReader documentReader = documentLocaleReaderProvider.get(); |
117 |
4283 |
documentReader.setProperties(this.properties); |
118 |
|
|
119 |
4283 |
try { |
120 |
4283 |
documentReader.read(filter, proxyFilter); |
121 |
|
} catch (Exception e) { |
122 |
0 |
throw new FilterException("Failed to read XAR XML document", e); |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
4282 |
if (documentReader.getSentSpaceReference() != null) { |
127 |
5080 |
for (EntityReference space = documentReader.getSentSpaceReference(); space != null; space = |
128 |
|
space.getParent()) { |
129 |
2542 |
proxyFilter.endWikiSpace(space.getName(), FilterEventParameters.EMPTY); |
130 |
|
} |
131 |
|
} |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
134 |
8 |
private Boolean isZip(InputStream stream) throws IOException... |
135 |
|
{ |
136 |
8 |
if (!stream.markSupported()) { |
137 |
|
|
138 |
3 |
return null; |
139 |
|
} |
140 |
|
|
141 |
5 |
final byte[] signature = new byte[12]; |
142 |
5 |
stream.mark(signature.length); |
143 |
5 |
int signatureLength = stream.read(signature); |
144 |
5 |
stream.reset(); |
145 |
|
|
146 |
5 |
return ZipArchiveInputStream.matches(signature, signatureLength); |
147 |
|
} |
148 |
|
} |