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

File XWikiAttachmentEventGenerator.java

 

Coverage histogram

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

Code metrics

8
25
1
1
119
74
7
0.28
25
1
7

Classes

Class Line # Actions
XWikiAttachmentEventGenerator 55 25 0% 7 6
0.823529482.4%
 

Contributing tests

This file is covered by 1 test. .

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.input;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.InputStream;
24    import java.lang.reflect.ParameterizedType;
25   
26    import javax.inject.Inject;
27    import javax.inject.Provider;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.util.DefaultParameterizedType;
34    import org.xwiki.filter.FilterEventParameters;
35    import org.xwiki.filter.FilterException;
36    import org.xwiki.filter.event.model.WikiAttachmentFilter;
37    import org.xwiki.filter.event.xwiki.XWikiWikiAttachmentFilter;
38    import org.xwiki.filter.instance.input.DocumentInstanceInputProperties;
39    import org.xwiki.filter.instance.input.EntityEventGenerator;
40    import org.xwiki.filter.instance.internal.input.AbstractBeanEntityEventGenerator;
41   
42    import com.xpn.xwiki.XWikiContext;
43    import com.xpn.xwiki.XWikiException;
44    import com.xpn.xwiki.doc.XWikiAttachment;
45    import com.xpn.xwiki.doc.XWikiAttachmentArchive;
46    import com.xpn.xwiki.internal.filter.XWikiAttachmentFilter;
47   
48    /**
49    * @version $Id: 5058c67f4730c52612e17664ab149607ab0b8480 $
50    * @since 6.2M1
51    */
52    @Component
53    @Singleton
54    // TODO: add support for real revision events (instead of the jrcs archive )
 
55    public class XWikiAttachmentEventGenerator
56    extends AbstractBeanEntityEventGenerator<XWikiAttachment, XWikiAttachmentFilter, DocumentInstanceInputProperties>
57    {
58    /**
59    * The role of this component.
60    */
61    public static final ParameterizedType ROLE = new DefaultParameterizedType(null, EntityEventGenerator.class,
62    XWikiAttachment.class, DocumentInstanceInputProperties.class);
63   
64    @Inject
65    private Provider<XWikiContext> xcontextProvider;
66   
67    @Inject
68    private Logger logger;
69   
 
70  21 toggle @Override
71    public void write(XWikiAttachment attachment, Object filter, XWikiAttachmentFilter attachmentFilter,
72    DocumentInstanceInputProperties properties) throws FilterException
73    {
74  21 XWikiContext xcontext = this.xcontextProvider.get();
75   
76  21 FilterEventParameters attachmentParameters = new FilterEventParameters();
77   
78  21 if (attachment.getAuthor() != null) {
79  21 attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_AUTHOR, attachment.getAuthor());
80    }
81  21 attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_COMMENT, attachment.getComment());
82  21 attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_DATE, attachment.getDate());
83  21 attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION, attachment.getVersion());
84   
85  21 if (StringUtils.isNotEmpty(attachment.getMimeType())) {
86  1 attachmentParameters.put(WikiAttachmentFilter.PARAMETER_MIMETYPE, attachment.getMimeType());
87    }
88   
89  21 if (properties.isWithJRCSRevisions()) {
90  17 try {
91    // We need to make sure content is loaded
92  17 XWikiAttachmentArchive archive;
93  17 archive = attachment.loadArchive(xcontext);
94  17 if (archive != null) {
95  17 attachmentParameters.put(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS,
96    archive.getArchiveAsString());
97    }
98    } catch (XWikiException e) {
99  0 this.logger.error("Attachment [{}] has malformed history", attachment.getReference(), e);
100    }
101    }
102   
103  21 InputStream content;
104  21 Long size;
105  21 try {
106  21 content = attachment.getContentInputStream(xcontext);
107  21 size = Long.valueOf(attachment.getFilesize());
108    } catch (XWikiException e) {
109  0 this.logger.error("Failed to get content of attachment [{}]", attachment.getReference(), e);
110   
111  0 content = new ByteArrayInputStream(new byte[0]);
112  0 size = 0L;
113    }
114   
115    // WikiAttachment
116   
117  21 attachmentFilter.onWikiAttachment(attachment.getFilename(), content, size, attachmentParameters);
118    }
119    }