1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.filter.output

File XWikiAttachmentOutputFilterStream.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

14
25
2
1
105
64
12
0.48
12.5
2
6

Classes

Class Line # Actions
XWikiAttachmentOutputFilterStream 44 25 0% 12 9
0.780487878%
 

Contributing tests

This file is covered by 21 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 com.xpn.xwiki.internal.filter.output;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.util.Date;
25   
26    import org.apache.commons.lang3.math.NumberUtils;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.component.annotation.InstantiationStrategy;
29    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
30    import org.xwiki.filter.FilterEventParameters;
31    import org.xwiki.filter.FilterException;
32    import org.xwiki.filter.event.model.WikiAttachmentFilter;
33    import org.xwiki.filter.event.xwiki.XWikiWikiAttachmentFilter;
34   
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiAttachment;
37   
38    /**
39    * @version $Id: f044b894b75258d8cf0b63a011eb2ea837b61305 $
40    * @since 9.0RC1
41    */
42    @Component
43    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
44    public class XWikiAttachmentOutputFilterStream extends AbstractEntityOutputFilterStream<XWikiAttachment>
45    {
46    // Events
47   
 
48  7 toggle private void setVersion(FilterEventParameters parameters)
49    {
50  7 if (parameters.containsKey(WikiAttachmentFilter.PARAMETER_REVISION)) {
51  6 String version = getString(WikiAttachmentFilter.PARAMETER_REVISION, parameters, null);
52  6 if (version != null) {
53  6 if (VALID_VERSION.matcher(version).matches()) {
54  6 this.entity.setVersion(version);
55  0 } else if (NumberUtils.isDigits(version)) {
56  0 this.entity.setVersion(version + ".1");
57    } else {
58    // TODO: log something, probably a warning
59    }
60    }
61    }
62    }
63   
 
64  449 toggle @Override
65    public void onWikiAttachment(String name, InputStream content, Long size, FilterEventParameters parameters)
66    throws FilterException
67    {
68  449 if (this.entity == null) {
69  449 this.entity = new XWikiAttachment();
70    }
71   
72  449 this.entity.setFilename(name);
73   
74  449 try {
75  449 this.entity.setContent(content);
76    } catch (IOException e) {
77  0 throw new FilterException("Failed to set attachment content", e);
78    }
79   
80    // Author
81   
82  449 this.entity
83    .setAuthorReference(getUserReference(WikiAttachmentFilter.PARAMETER_REVISION_AUTHOR, parameters, null));
84   
85    // Revision
86   
87  449 if (this.properties == null || this.properties.isVersionPreserved()) {
88  7 setVersion(parameters);
89  7 this.entity.setComment(getString(WikiAttachmentFilter.PARAMETER_REVISION_COMMENT, parameters, ""));
90  7 this.entity.setDate(getDate(WikiAttachmentFilter.PARAMETER_REVISION_DATE, parameters, new Date()));
91  7 this.entity.setMimeType(getString(WikiAttachmentFilter.PARAMETER_MIMETYPE, parameters, null));
92   
93  7 String revisions = getString(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS, parameters, null);
94  7 if (revisions != null) {
95  1 try {
96  1 this.entity.setArchive(revisions);
97    } catch (XWikiException e) {
98  0 throw new FilterException("Failed to set attachment archive", e);
99    }
100    }
101   
102  7 this.entity.setMetaDataDirty(false);
103    }
104    }
105    }