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.jar; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.net.MalformedURLException; |
24 |
|
import java.net.URL; |
25 |
|
import java.net.URLConnection; |
26 |
|
import java.net.URLStreamHandler; |
27 |
|
import java.net.URLStreamHandlerFactory; |
28 |
|
import java.util.regex.Matcher; |
29 |
|
import java.util.regex.Pattern; |
30 |
|
|
31 |
|
import javax.inject.Inject; |
32 |
|
import javax.inject.Named; |
33 |
|
import javax.inject.Singleton; |
34 |
|
|
35 |
|
import org.xwiki.classloader.ExtendedURLStreamHandler; |
36 |
|
import org.xwiki.classloader.internal.protocol.jar.JarURLConnection.JarOpener; |
37 |
|
import org.xwiki.component.annotation.Component; |
38 |
|
|
39 |
|
import edu.emory.mathcs.util.classloader.ResourceUtils; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@Component |
57 |
|
@Named("jar") |
58 |
|
@Singleton |
|
|
| 54.5% |
Uncovered Elements: 20 (44) |
Complexity: 10 |
Complexity Density: 0.32 |
|
59 |
|
public class JarURLStreamHandler extends URLStreamHandler implements ExtendedURLStreamHandler |
60 |
|
{ |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final Pattern ABSOLUTE_JAR_URL_PATTERN = |
66 |
|
Pattern.compile("jar:(.*)!(/(?:.*/)?)((?:[^/#]+)?)((?:#.*)?)"); |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
private static final String JAR_PROTOCOL = "jar"; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private static final String JAR_PROTOCOL_SEPARATOR = "!"; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private JarOpener opener = new JarProxy(); |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
@Inject |
89 |
|
private URLStreamHandlerFactory handlerFactory; |
90 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
0 |
@Override... |
92 |
|
public String getProtocol() |
93 |
|
{ |
94 |
0 |
return JAR_PROTOCOL; |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
6 |
@Override... |
98 |
|
public URLConnection openConnection(URL url) throws IOException |
99 |
|
{ |
100 |
6 |
return new JarURLConnection(url, this.opener, this.handlerFactory); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
@inheritDoc |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
@see |
111 |
|
|
|
|
| 53.8% |
Uncovered Elements: 18 (39) |
Complexity: 8 |
Complexity Density: 0.28 |
|
112 |
88 |
@Override... |
113 |
|
protected void parseURL(URL u, String spec, int start, int limit) |
114 |
|
{ |
115 |
88 |
Matcher matcher = ABSOLUTE_JAR_URL_PATTERN.matcher(spec); |
116 |
88 |
if (matcher.matches()) { |
117 |
|
|
118 |
0 |
String base = matcher.group(1); |
119 |
0 |
try { |
120 |
|
|
121 |
0 |
new URL(base); |
122 |
|
} catch (MalformedURLException e) { |
123 |
0 |
throw new IllegalArgumentException(e.toString()); |
124 |
|
} |
125 |
0 |
String path = matcher.group(2) + matcher.group(3); |
126 |
0 |
path = ResourceUtils.canonizePath(path); |
127 |
0 |
String ref = matcher.group(4); |
128 |
0 |
if (ref.length() == 0) { |
129 |
0 |
ref = null; |
130 |
|
} else { |
131 |
0 |
ref = ref.substring(1); |
132 |
|
} |
133 |
0 |
setURL(u, JAR_PROTOCOL, "", -1, "", "", base + JAR_PROTOCOL_SEPARATOR + path, null, ref); |
134 |
|
} else { |
135 |
88 |
matcher = ABSOLUTE_JAR_URL_PATTERN.matcher(u.toString()); |
136 |
88 |
if (matcher.matches()) { |
137 |
88 |
String ref = spec.substring(limit); |
138 |
88 |
if (ref.length() == 0) { |
139 |
88 |
ref = null; |
140 |
|
} else { |
141 |
0 |
ref = ref.substring(1); |
142 |
|
} |
143 |
88 |
String newSpec = spec.substring(start, limit); |
144 |
88 |
String base = matcher.group(1); |
145 |
88 |
String path; |
146 |
88 |
if (newSpec.length() > 0 && newSpec.charAt(0) == '/') { |
147 |
3 |
path = newSpec; |
148 |
|
} else { |
149 |
85 |
String cxtDir = matcher.group(2); |
150 |
85 |
path = cxtDir + newSpec; |
151 |
|
} |
152 |
88 |
path = ResourceUtils.canonizePath(path); |
153 |
88 |
setURL(u, JAR_PROTOCOL, "", -1, "", "", base + JAR_PROTOCOL_SEPARATOR + path, null, ref); |
154 |
|
} else { |
155 |
0 |
throw new IllegalArgumentException("Neither URL nor the spec are valid JAR URLs"); |
156 |
|
} |
157 |
|
} |
158 |
|
} |
159 |
|
} |