1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
54 |
|
|
|
|
| 82.4% |
Uncovered Elements: 6 (34) |
Complexity: 7 |
Complexity Density: 0.28 |
|
55 |
|
public class XWikiAttachmentEventGenerator |
56 |
|
extends AbstractBeanEntityEventGenerator<XWikiAttachment, XWikiAttachmentFilter, DocumentInstanceInputProperties> |
57 |
|
{ |
58 |
|
|
59 |
|
|
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 |
|
|
|
|
| 81.8% |
Uncovered Elements: 6 (33) |
Complexity: 7 |
Complexity Density: 0.28 |
|
70 |
21 |
@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 |
|
|
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 |
|
|
116 |
|
|
117 |
21 |
attachmentFilter.onWikiAttachment(attachment.getFilename(), content, size, attachmentParameters); |
118 |
|
} |
119 |
|
} |