1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.filter.xar.internal.input

File XARInputFilterStream.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
40
5
1
148
105
18
0.45
8
5
3.6

Classes

Class Line # Actions
XARInputFilterStream 49 40 0% 18 7
0.87719387.7%
 

Contributing tests

This file is covered by 52 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 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 $Id: c3cf3885cea5fdc5812de89053f694fe7b97c4e5 $
45    * @since 6.2M1
46    */
47    @Component(hints = {XARFilterUtils.ROLEHINT_13, XARFilterUtils.ROLEHINT_12, XARFilterUtils.ROLEHINT_11})
48    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
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   
 
57  4287 toggle @Override
58    public void close() throws IOException
59    {
60  4288 this.properties.getSource().close();
61    }
62   
 
63  4290 toggle @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   
 
102  7 toggle 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   
 
114  4283 toggle 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    // Close remaining opened spaces
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   
 
134  8 toggle private Boolean isZip(InputStream stream) throws IOException
135    {
136  8 if (!stream.markSupported()) {
137    // ZIP by default
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    }