1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.pdf.impl; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.OutputStream; |
25 |
|
import java.net.URI; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Provider; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.apache.fop.apps.io.ResourceResolverFactory; |
33 |
|
import org.apache.xmlgraphics.io.Resource; |
34 |
|
import org.apache.xmlgraphics.io.ResourceResolver; |
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.formula.ImageData; |
37 |
|
import org.xwiki.formula.ImageStorage; |
38 |
|
import org.xwiki.model.EntityType; |
39 |
|
import org.xwiki.model.reference.AttachmentReference; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWikiContext; |
42 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
43 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
44 |
|
import com.xpn.xwiki.web.Utils; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@link |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@version |
58 |
|
@since |
59 |
|
|
60 |
|
@Component(roles = PDFResourceResolver.class) |
61 |
|
@Singleton |
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 7 |
Complexity Density: 0.37 |
|
62 |
|
public class PDFResourceResolver implements ResourceResolver |
63 |
|
{ |
64 |
|
private static final String TEX_ACTION = "/tex/"; |
65 |
|
|
66 |
|
@Inject |
67 |
|
private Provider<XWikiContext> xcontextProvider; |
68 |
|
|
69 |
|
private ResourceResolver standardResolver = ResourceResolverFactory.createDefaultResourceResolver(); |
70 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 6 |
Complexity Density: 0.33 |
|
71 |
0 |
@Override... |
72 |
|
public Resource getResource(URI uri) throws IOException |
73 |
|
{ |
74 |
0 |
XWikiContext xcontext = xcontextProvider.get(); |
75 |
|
|
76 |
0 |
Map<String, AttachmentReference> attachmentMap = |
77 |
|
(Map<String, AttachmentReference>) xcontext.get(PdfURLFactory.PDF_EXPORT_CONTEXT_KEY); |
78 |
|
|
79 |
0 |
if (attachmentMap != null) { |
80 |
0 |
String uriString = uri.toString(); |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
0 |
if (uriString.contains(TEX_ACTION)) { |
89 |
|
|
90 |
0 |
AttachmentReference reference = attachmentMap.get(uriString.replace(TEX_ACTION, "/download/")); |
91 |
0 |
if (reference != null) { |
92 |
|
|
93 |
0 |
ImageStorage storage = Utils.getComponent(ImageStorage.class); |
94 |
0 |
ImageData image = storage.get(reference.getName()); |
95 |
0 |
return new Resource(new ByteArrayInputStream(image.getData())); |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
0 |
AttachmentReference reference = attachmentMap.get(uriString); |
101 |
0 |
if (reference != null) { |
102 |
0 |
try { |
103 |
0 |
XWikiDocument xdoc = |
104 |
|
xcontext.getWiki().getDocument(reference.extractReference(EntityType.DOCUMENT), xcontext); |
105 |
|
|
106 |
0 |
XWikiAttachment attachment = |
107 |
|
xdoc.getAttachment(reference.extractReference(EntityType.ATTACHMENT).getName()); |
108 |
0 |
return new Resource(attachment.getContentInputStream(xcontext)); |
109 |
|
} catch (Exception e) { |
110 |
0 |
throw new IOException(String.format("Failed to resolve export URI [%s]", uriString), e); |
111 |
|
} |
112 |
|
} |
113 |
|
} |
114 |
|
|
115 |
0 |
return this.standardResolver.getResource(uri); |
116 |
|
} |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
0 |
@Override... |
119 |
|
public OutputStream getOutputStream(URI uri) throws IOException |
120 |
|
{ |
121 |
|
|
122 |
0 |
return this.standardResolver.getOutputStream(uri); |
123 |
|
} |
124 |
|
|
125 |
|
} |