1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.filter.xar.internal.input

File XARInputFilterStream.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
41% 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 62 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  12665 toggle @Override
58    public void close() throws IOException
59    {
60  12665 this.properties.getSource().close();
61    }
62   
 
63  12665 toggle @Override
64    protected void read(Object filter, XARInputFilter proxyFilter) throws FilterException
65    {
66  12665 InputSource inputSource = this.properties.getSource();
67   
68  12665 if (this.properties.isForceDocument() || inputSource instanceof ReaderInputSource
69    || inputSource instanceof SourceInputSource) {
70  12650 readDocument(filter, proxyFilter);
71  15 } else if (inputSource instanceof InputStreamInputSource) {
72  15 InputStream stream;
73  15 try {
74  15 stream = ((InputStreamInputSource) inputSource).getInputStream();
75    } catch (IOException e) {
76  0 throw new FilterException("Failed to get input stream", e);
77    }
78   
79  15 Boolean iszip;
80  15 try {
81  15 iszip = isZip(stream);
82    } catch (IOException e) {
83  0 throw new FilterException("Failed to read input stream", e);
84    } finally {
85  15 try {
86  15 inputSource.close();
87    } catch (IOException e) {
88  0 throw new FilterException("Failed to close the source", e);
89    }
90    }
91   
92  15 if (iszip == Boolean.FALSE) {
93  1 readDocument(filter, proxyFilter);
94    } else {
95  14 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  14 toggle private void readXAR(Object filter, XARInputFilter proxyFilter) throws FilterException
103    {
104  14 WikiReader wikiReader = this.wikiReaderProvider.get();
105  14 wikiReader.setProperties(this.properties);
106   
107  14 try {
108  14 wikiReader.read(filter, proxyFilter);
109    } catch (Exception e) {
110  0 throw new FilterException("Failed to read XAR package", e);
111    }
112    }
113   
 
114  12651 toggle protected void readDocument(Object filter, XARInputFilter proxyFilter) throws FilterException
115    {
116  12651 DocumentLocaleReader documentReader = documentLocaleReaderProvider.get();
117  12651 documentReader.setProperties(this.properties);
118   
119  12651 try {
120  12651 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  12651 if (documentReader.getSentSpaceReference() != null) {
127  16291 for (EntityReference space = documentReader.getSentSpaceReference(); space != null; space =
128    space.getParent()) {
129  8639 proxyFilter.endWikiSpace(space.getName(), FilterEventParameters.EMPTY);
130    }
131    }
132    }
133   
 
134  15 toggle private Boolean isZip(InputStream stream) throws IOException
135    {
136  15 if (!stream.markSupported()) {
137    // ZIP by default
138  14 return null;
139    }
140   
141  1 final byte[] signature = new byte[12];
142  1 stream.mark(signature.length);
143  1 int signatureLength = stream.read(signature);
144  1 stream.reset();
145   
146  1 return ZipArchiveInputStream.matches(signature, signatureLength);
147    }
148    }