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.File; |
23 |
|
import java.io.FileOutputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.io.UnsupportedEncodingException; |
27 |
|
import java.net.URL; |
28 |
|
import java.net.URLEncoder; |
29 |
|
import java.util.List; |
30 |
|
import java.util.Map; |
31 |
|
|
32 |
|
import org.apache.commons.io.FilenameUtils; |
33 |
|
import org.apache.commons.io.IOUtils; |
34 |
|
import org.apache.commons.lang3.StringUtils; |
35 |
|
import org.slf4j.Logger; |
36 |
|
import org.slf4j.LoggerFactory; |
37 |
|
import org.xwiki.model.reference.DocumentReference; |
38 |
|
|
39 |
|
import com.xpn.xwiki.XWiki; |
40 |
|
import com.xpn.xwiki.XWikiContext; |
41 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
42 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
43 |
|
import com.xpn.xwiki.internal.model.LegacySpaceResolver; |
44 |
|
import com.xpn.xwiki.web.Utils; |
45 |
|
import com.xpn.xwiki.web.XWikiServletURLFactory; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
|
|
| 0% |
Uncovered Elements: 105 (105) |
Complexity: 29 |
Complexity Density: 0.38 |
|
55 |
|
public class FileSystemURLFactory extends XWikiServletURLFactory |
56 |
|
{ |
57 |
|
|
58 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(FileSystemURLFactory.class); |
59 |
|
|
60 |
|
|
61 |
|
private static final char SEPARATOR = '/'; |
62 |
|
|
63 |
|
private LegacySpaceResolver legacySpaceResolver = Utils.getComponent(LegacySpaceResolver.class); |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
65 |
0 |
@Override... |
66 |
|
public URL createAttachmentURL(String filename, String spaces, String name, String action, String querystring, |
67 |
|
String wiki, XWikiContext context) |
68 |
|
{ |
69 |
0 |
try { |
70 |
0 |
return getURL(wiki, spaces, name, filename, null, context); |
71 |
|
} catch (Exception ex) { |
72 |
0 |
LOGGER.warn("Failed to save image for PDF export", ex); |
73 |
0 |
return super.createAttachmentURL(filename, spaces, name, action, null, wiki, context); |
74 |
|
} |
75 |
|
} |
76 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
77 |
0 |
@Override... |
78 |
|
public URL createAttachmentRevisionURL(String filename, String spaces, String name, String revision, String wiki, |
79 |
|
XWikiContext context) |
80 |
|
{ |
81 |
0 |
try { |
82 |
0 |
return getURL(wiki, spaces, name, filename, revision, context); |
83 |
|
} catch (Exception ex) { |
84 |
0 |
LOGGER.warn("Failed to save image for PDF export: " + ex.getMessage()); |
85 |
0 |
return super.createAttachmentRevisionURL(filename, spaces, name, revision, wiki, context); |
86 |
|
} |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
89 |
0 |
@Override... |
90 |
|
public URL createSkinURL(String filename, String skin, XWikiContext context) |
91 |
|
{ |
92 |
0 |
try { |
93 |
0 |
Map<String, File> usedFiles = getFileMapping(context); |
94 |
0 |
String key = getSkinfileKey(filename, skin); |
95 |
0 |
if (!usedFiles.containsKey(key)) { |
96 |
0 |
if (!copyResource("/skins/" + skin + '/' + filename, key, usedFiles, context)) { |
97 |
|
|
98 |
0 |
return super.createSkinURL(filename, skin, context); |
99 |
|
} |
100 |
|
} |
101 |
0 |
return usedFiles.get(key).toURI().toURL(); |
102 |
|
} catch (Exception ex) { |
103 |
|
|
104 |
0 |
return super.createSkinURL(filename, skin, context); |
105 |
|
} |
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
108 |
0 |
@Override... |
109 |
|
public URL createResourceURL(String filename, boolean forceSkinAction, XWikiContext context) |
110 |
|
{ |
111 |
0 |
try { |
112 |
0 |
Map<String, File> usedFiles = getFileMapping(context); |
113 |
0 |
String key = getResourceKey(filename); |
114 |
0 |
if (!usedFiles.containsKey(key)) { |
115 |
0 |
if (!copyResource("/resources/" + filename, key, usedFiles, context)) { |
116 |
0 |
return super.createResourceURL(filename, forceSkinAction, context); |
117 |
|
} |
118 |
|
} |
119 |
0 |
return usedFiles.get(key).toURI().toURL(); |
120 |
|
} catch (Exception ex) { |
121 |
|
|
122 |
0 |
return super.createResourceURL(filename, forceSkinAction, context); |
123 |
|
} |
124 |
|
} |
125 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
126 |
0 |
@Override... |
127 |
|
public String getURL(URL url, XWikiContext context) |
128 |
|
{ |
129 |
0 |
if (url == null) { |
130 |
0 |
return ""; |
131 |
|
} |
132 |
0 |
return url.toString(); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
@param |
139 |
|
@param |
140 |
|
|
141 |
|
@param |
142 |
|
@param |
143 |
|
@param |
144 |
|
@param |
145 |
|
@return |
146 |
|
@throws |
147 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
148 |
0 |
private URL getURL(String wiki, String spaces, String name, String filename, String revision, XWikiContext context)... |
149 |
|
throws Exception |
150 |
|
{ |
151 |
0 |
Map<String, File> usedFiles = getFileMapping(context); |
152 |
0 |
List<String> spaceNames = this.legacySpaceResolver.resolve(spaces); |
153 |
0 |
String key = getAttachmentKey(spaceNames, name, filename, revision); |
154 |
0 |
if (!usedFiles.containsKey(key)) { |
155 |
0 |
File file = getTemporaryFile(key, context); |
156 |
0 |
LOGGER.debug("Temporary PDF export file [{}]", file.toString()); |
157 |
0 |
XWikiDocument doc = context.getWiki().getDocument(new DocumentReference( |
158 |
|
StringUtils.defaultString(wiki, context.getWikiId()), spaceNames, name), context); |
159 |
0 |
XWikiAttachment attachment = doc.getAttachment(filename); |
160 |
0 |
if (StringUtils.isNotEmpty(revision)) { |
161 |
0 |
attachment = attachment.getAttachmentRevision(revision, context); |
162 |
|
} |
163 |
0 |
FileOutputStream fos = new FileOutputStream(file); |
164 |
0 |
IOUtils.copy(attachment.getContentInputStream(context), fos); |
165 |
0 |
fos.close(); |
166 |
0 |
usedFiles.put(key, file); |
167 |
|
} |
168 |
0 |
return usedFiles.get(key).toURI().toURL(); |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
@param |
176 |
|
|
177 |
|
@param |
178 |
|
@param |
179 |
|
@param |
180 |
|
@return |
181 |
|
|
182 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
183 |
0 |
private boolean copyResource(String resourceName, String key, Map<String, File> usedFiles, XWikiContext context)... |
184 |
|
{ |
185 |
0 |
try { |
186 |
0 |
InputStream data = context.getWiki().getResourceAsStream(resourceName); |
187 |
0 |
if (data != null) { |
188 |
|
|
189 |
0 |
File file = getTemporaryFile(key, context); |
190 |
0 |
FileOutputStream fos = new FileOutputStream(file); |
191 |
0 |
IOUtils.copy(data, fos); |
192 |
0 |
fos.close(); |
193 |
0 |
usedFiles.put(key, file); |
194 |
0 |
return true; |
195 |
|
} |
196 |
|
} catch (Exception ex) { |
197 |
|
|
198 |
|
} |
199 |
0 |
return false; |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
@param |
206 |
|
@param |
207 |
|
@param |
208 |
|
@return |
209 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
|
210 |
0 |
private String getAttachmentKey(List<String> spaceNames, String name, String filename, String revision)... |
211 |
|
{ |
212 |
0 |
StringBuilder builder = new StringBuilder(); |
213 |
|
|
214 |
0 |
try { |
215 |
0 |
builder.append("attachment").append(SEPARATOR); |
216 |
0 |
for (String spaceName : spaceNames) { |
217 |
0 |
builder.append(URLEncoder.encode(spaceName, XWiki.DEFAULT_ENCODING)); |
218 |
0 |
builder.append(SEPARATOR); |
219 |
|
} |
220 |
0 |
builder.append(URLEncoder.encode(name, XWiki.DEFAULT_ENCODING)).append(SEPARATOR); |
221 |
0 |
builder.append(URLEncoder.encode(filename, XWiki.DEFAULT_ENCODING)).append(SEPARATOR); |
222 |
0 |
builder.append(URLEncoder.encode(StringUtils.defaultString(revision), XWiki.DEFAULT_ENCODING)); |
223 |
0 |
return builder.toString(); |
224 |
|
} catch (UnsupportedEncodingException e) { |
225 |
|
|
226 |
0 |
throw new RuntimeException(String.format("Failed to compute unique Attachment key for spaces [%s[, " |
227 |
|
+ "page [%s], filename [%s], revision [%s], while exporting.", StringUtils.join(spaceNames, ", "), |
228 |
|
name, filename, revision), e); |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
@param |
236 |
|
@return |
237 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
238 |
0 |
private String getResourceKey(String filename)... |
239 |
|
{ |
240 |
0 |
try { |
241 |
0 |
return "resource" + SEPARATOR + URLEncoder.encode(filename, XWiki.DEFAULT_ENCODING); |
242 |
|
} catch (UnsupportedEncodingException ex) { |
243 |
|
|
244 |
0 |
return filename; |
245 |
|
} |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
@param |
252 |
|
@param |
253 |
|
@return |
254 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
255 |
0 |
private String getSkinfileKey(String filename, String skin)... |
256 |
|
{ |
257 |
0 |
try { |
258 |
0 |
return "skin" + SEPARATOR + URLEncoder.encode(skin, XWiki.DEFAULT_ENCODING) + SEPARATOR |
259 |
|
+ URLEncoder.encode(filename, XWiki.DEFAULT_ENCODING); |
260 |
|
} catch (UnsupportedEncodingException ex) { |
261 |
|
|
262 |
0 |
return skin + SEPARATOR + filename; |
263 |
|
} |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
@param |
270 |
|
@return |
271 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
272 |
0 |
private Map<String, File> getFileMapping(XWikiContext context)... |
273 |
|
{ |
274 |
0 |
@SuppressWarnings("unchecked") |
275 |
|
Map<String, File> usedFiles = (Map<String, File>) context.get("pdfexport-file-mapping"); |
276 |
0 |
return usedFiles; |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
@param |
283 |
|
@param |
284 |
|
@return |
285 |
|
@throws |
286 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
287 |
0 |
private File getTemporaryFile(String key, XWikiContext context) throws IOException... |
288 |
|
{ |
289 |
0 |
File tempdir = (File) context.get("pdfexportdir"); |
290 |
0 |
String prefix = "pdf"; |
291 |
0 |
String suffix = "." + FilenameUtils.getExtension(key); |
292 |
0 |
try { |
293 |
0 |
return File.createTempFile(prefix, suffix, tempdir); |
294 |
|
} catch (IOException e) { |
295 |
0 |
throw new IOException("Failed to create temporary PDF export file with prefix [" + prefix + "], suffix [" |
296 |
|
+ suffix + "] in directory [" + tempdir + "]", e); |
297 |
|
} |
298 |
|
} |
299 |
|
} |