1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.classloader.internal.protocol.attachmentjar; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.UnsupportedEncodingException; |
24 |
|
import java.net.URL; |
25 |
|
import java.net.URLConnection; |
26 |
|
import java.net.URLDecoder; |
27 |
|
import java.net.URLStreamHandler; |
28 |
|
|
29 |
|
import javax.inject.Inject; |
30 |
|
import javax.inject.Named; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
34 |
|
import org.xwiki.classloader.ExtendedURLStreamHandler; |
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.model.reference.AttachmentReference; |
37 |
|
import org.xwiki.model.reference.AttachmentReferenceResolver; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
45 |
|
@Component |
46 |
|
@Named("attachmentjar") |
47 |
|
@Singleton |
|
|
| 93.8% |
Uncovered Elements: 1 (16) |
Complexity: 5 |
Complexity Density: 0.45 |
|
48 |
|
public class AttachmentURLStreamHandler extends URLStreamHandler implements ExtendedURLStreamHandler |
49 |
|
{ |
50 |
|
private static final String ATTACHMENT_JAR_PROTOCOL = "attachmentjar"; |
51 |
|
|
52 |
|
private static final String ATTACHMENT_JAR_PREFIX = ATTACHMENT_JAR_PROTOCOL + "://"; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Inject |
58 |
|
@Named("current") |
59 |
|
private AttachmentReferenceResolver<String> attachmentReferenceResolver; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@Inject |
65 |
|
private DocumentAccessBridge documentAccessBridge; |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
7 |
@Override... |
68 |
|
public String getProtocol() |
69 |
|
{ |
70 |
7 |
return ATTACHMENT_JAR_PROTOCOL; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
@inheritDoc |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@see |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
80 |
7 |
@Override... |
81 |
|
protected URLConnection openConnection(URL url) throws IOException |
82 |
|
{ |
83 |
|
|
84 |
7 |
AttachmentReference attachmentReference = this.attachmentReferenceResolver.resolve( |
85 |
|
getAttachmentReference(url)); |
86 |
|
|
87 |
6 |
return new AttachmentURLConnection(url, attachmentReference, this.documentAccessBridge); |
88 |
|
} |
89 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
90 |
7 |
private String getAttachmentReference(URL url)... |
91 |
|
{ |
92 |
|
|
93 |
7 |
String urlAsString = url.toString(); |
94 |
7 |
if (!urlAsString.startsWith(ATTACHMENT_JAR_PREFIX)) { |
95 |
1 |
throw new RuntimeException("An attachment JAR URL should start with [" |
96 |
|
+ ATTACHMENT_JAR_PREFIX + "], got [" + urlAsString + "]"); |
97 |
|
} |
98 |
|
|
99 |
6 |
String attachmentReference = urlAsString.substring(ATTACHMENT_JAR_PREFIX.length()); |
100 |
6 |
try { |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
6 |
attachmentReference = URLDecoder.decode(attachmentReference, "UTF-8"); |
106 |
|
} catch (UnsupportedEncodingException e) { |
107 |
|
|
108 |
|
|
109 |
0 |
throw new RuntimeException("Failed to URL decode [" + attachmentReference + "] using UTF-8.", e); |
110 |
|
} |
111 |
|
|
112 |
6 |
return attachmentReference; |
113 |
|
} |
114 |
|
} |