1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.repository; |
21 |
|
|
22 |
|
import java.io.UnsupportedEncodingException; |
23 |
|
import java.net.URI; |
24 |
|
import java.net.URISyntaxException; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
@version |
32 |
|
@since |
33 |
|
|
|
|
| 81.7% |
Uncovered Elements: 38 (208) |
Complexity: 57 |
Complexity Density: 0.47 |
|
34 |
|
public class UriBuilder implements Cloneable |
35 |
|
{ |
36 |
|
private String scheme; |
37 |
|
|
38 |
|
private String host; |
39 |
|
|
40 |
|
private Integer port; |
41 |
|
|
42 |
|
private String userInfo; |
43 |
|
|
44 |
|
private CharSequence path; |
45 |
|
|
46 |
|
private CharSequence query; |
47 |
|
|
48 |
|
private String fragment; |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
50 |
15 |
public UriBuilder(URI base, String path)... |
51 |
|
{ |
52 |
15 |
uri(base); |
53 |
15 |
path(path); |
54 |
|
} |
55 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
56 |
8 |
public UriBuilder(String base, String path)... |
57 |
|
{ |
58 |
8 |
try { |
59 |
8 |
uri(new URI(base)); |
60 |
|
} catch (URISyntaxException e) { |
61 |
0 |
throw new IllegalArgumentException("Invalid base URI [" + base + "]", e); |
62 |
|
} |
63 |
|
|
64 |
8 |
path(path); |
65 |
|
} |
66 |
|
|
|
|
| 81.2% |
Uncovered Elements: 6 (32) |
Complexity: 9 |
Complexity Density: 0.56 |
|
67 |
23 |
private void uri(URI uri) throws IllegalArgumentException... |
68 |
|
{ |
69 |
23 |
if (uri == null) { |
70 |
0 |
throw new IllegalArgumentException("The URI must not be null"); |
71 |
|
} |
72 |
|
|
73 |
23 |
if (uri.getScheme() != null) { |
74 |
23 |
this.scheme = uri.getScheme(); |
75 |
|
} |
76 |
23 |
if (uri.getHost() != null) { |
77 |
21 |
this.host = uri.getHost(); |
78 |
|
} |
79 |
23 |
if (uri.getPort() > 0) { |
80 |
6 |
this.port = uri.getPort(); |
81 |
|
} |
82 |
23 |
if (uri.getRawUserInfo() != null) { |
83 |
0 |
this.userInfo = uri.getRawUserInfo(); |
84 |
|
} |
85 |
23 |
if (uri.getRawPath() != null) { |
86 |
23 |
this.path = uri.getRawPath(); |
87 |
|
} |
88 |
23 |
if (uri.getRawQuery() != null) { |
89 |
1 |
this.query = uri.getRawQuery(); |
90 |
|
} |
91 |
23 |
if (uri.getRawFragment() != null) { |
92 |
1 |
this.fragment = uri.getRawFragment(); |
93 |
|
} |
94 |
|
} |
95 |
|
|
|
|
| 53.1% |
Uncovered Elements: 15 (32) |
Complexity: 11 |
Complexity Density: 0.61 |
|
96 |
23 |
private void path(String path)... |
97 |
|
{ |
98 |
23 |
if (path != null && !path.isEmpty()) { |
99 |
22 |
StringBuilder stringBuilder; |
100 |
22 |
if (this.path != null) { |
101 |
22 |
if (this.path instanceof String) { |
102 |
22 |
stringBuilder = new StringBuilder(this.path); |
103 |
22 |
this.path = stringBuilder; |
104 |
|
} else { |
105 |
0 |
stringBuilder = (StringBuilder) this.path; |
106 |
|
} |
107 |
|
} else { |
108 |
0 |
stringBuilder = new StringBuilder(); |
109 |
0 |
this.path = stringBuilder; |
110 |
|
} |
111 |
|
|
112 |
22 |
if (this.path.length() == 0 || this.path.charAt(this.path.length() - 1) != '/') { |
113 |
22 |
if (path.charAt(0) != '/') { |
114 |
5 |
stringBuilder.append('/'); |
115 |
|
} |
116 |
|
|
117 |
22 |
stringBuilder.append(path); |
118 |
|
} else { |
119 |
0 |
int i = 0; |
120 |
0 |
for (; i < path.length() && path.charAt(i) == '/'; ++i) { |
121 |
|
|
122 |
|
} |
123 |
|
|
124 |
0 |
if (i > 0) { |
125 |
0 |
stringBuilder.append(path.substring(i)); |
126 |
|
} else { |
127 |
0 |
stringBuilder.append(path); |
128 |
|
} |
129 |
|
} |
130 |
|
} |
131 |
|
} |
132 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
133 |
194 |
public static String encode(String toEncode)... |
134 |
|
{ |
135 |
194 |
String result = null; |
136 |
|
|
137 |
194 |
if (toEncode != null) { |
138 |
194 |
try { |
139 |
194 |
result = java.net.URLEncoder.encode(toEncode, "UTF-8"); |
140 |
|
} catch (UnsupportedEncodingException e) { |
141 |
|
|
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
194 |
return result; |
146 |
|
} |
147 |
|
|
|
|
| 92.3% |
Uncovered Elements: 2 (26) |
Complexity: 5 |
Complexity Density: 0.28 |
|
148 |
14 |
public UriBuilder queryParam(String name, Object... values) throws IllegalArgumentException... |
149 |
|
{ |
150 |
14 |
if (values == null) { |
151 |
0 |
throw new IllegalArgumentException("The values must not be null"); |
152 |
|
} |
153 |
|
|
154 |
14 |
String encodedName = encode(name); |
155 |
|
|
156 |
14 |
StringBuilder queryBuilder; |
157 |
14 |
if (this.query == null) { |
158 |
7 |
queryBuilder = new StringBuilder(); |
159 |
7 |
this.query = queryBuilder; |
160 |
7 |
} else if (this.query instanceof StringBuilder) { |
161 |
6 |
queryBuilder = (StringBuilder) this.query; |
162 |
|
} else { |
163 |
1 |
queryBuilder = new StringBuilder(this.query); |
164 |
1 |
this.query = queryBuilder; |
165 |
|
} |
166 |
|
|
167 |
14 |
for (Object value : values) { |
168 |
14 |
if (queryBuilder.length() > 0) { |
169 |
7 |
queryBuilder.append('&'); |
170 |
|
} |
171 |
14 |
queryBuilder.append(encodedName); |
172 |
14 |
queryBuilder.append('='); |
173 |
14 |
queryBuilder.append(encode(value.toString())); |
174 |
|
} |
175 |
|
|
176 |
14 |
return this; |
177 |
|
} |
178 |
|
|
|
|
| 85.4% |
Uncovered Elements: 7 (48) |
Complexity: 13 |
Complexity Density: 0.46 |
|
179 |
117 |
public URI build(Object... values)... |
180 |
|
{ |
181 |
117 |
final StringBuilder stb = new StringBuilder(); |
182 |
117 |
if (this.scheme != null) { |
183 |
117 |
stb.append(this.scheme); |
184 |
117 |
stb.append("://"); |
185 |
|
} |
186 |
117 |
if (this.userInfo != null) { |
187 |
0 |
stb.append(this.userInfo); |
188 |
0 |
stb.append('@'); |
189 |
|
} |
190 |
117 |
if (this.host != null) { |
191 |
115 |
stb.append(this.host); |
192 |
|
} |
193 |
117 |
if (this.port != null) { |
194 |
108 |
stb.append(':'); |
195 |
108 |
stb.append(this.port); |
196 |
|
} |
197 |
|
|
198 |
117 |
String resolvePath = formatPath(values); |
199 |
117 |
if (resolvePath != null) { |
200 |
117 |
if (stb.length() > 0) { |
201 |
117 |
if (resolvePath.length() == 0 || resolvePath.charAt(0) != '/') { |
202 |
1 |
stb.append('/'); |
203 |
|
} |
204 |
|
} |
205 |
117 |
stb.append(resolvePath); |
206 |
|
} |
207 |
|
|
208 |
117 |
String query = this.query != null ? this.query.toString() : null; |
209 |
117 |
if (query != null) { |
210 |
8 |
stb.append('?'); |
211 |
8 |
stb.append(query); |
212 |
|
} |
213 |
117 |
if (this.fragment != null) { |
214 |
1 |
stb.append('#'); |
215 |
1 |
stb.append(this.fragment); |
216 |
|
} |
217 |
|
|
218 |
117 |
try { |
219 |
117 |
return new URI(stb.toString()); |
220 |
|
} catch (URISyntaxException e) { |
221 |
0 |
throw new IllegalArgumentException("Failed to build the URI", e); |
222 |
|
} |
223 |
|
} |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
225 |
2369 |
public static boolean isUnreserved(char character)... |
226 |
|
{ |
227 |
2369 |
return Character.isLetter(character) || Character.isDigit(character) || (character == '-') |
228 |
|
|| (character == '.') || (character == '_') || (character == '~'); |
229 |
|
} |
230 |
|
|
|
|
| 90% |
Uncovered Elements: 4 (40) |
Complexity: 9 |
Complexity Density: 0.38 |
|
231 |
117 |
private String formatPath(Object[] values)... |
232 |
|
{ |
233 |
117 |
final StringBuilder result = new StringBuilder(); |
234 |
|
|
235 |
117 |
StringBuilder varBuffer = null; |
236 |
117 |
char c; |
237 |
117 |
boolean inVariable = false; |
238 |
117 |
final int patternLength = this.path.length(); |
239 |
117 |
int valueId = 0; |
240 |
7219 |
for (int i = 0; i < patternLength; i++) { |
241 |
7102 |
c = this.path.charAt(i); |
242 |
|
|
243 |
7102 |
if (inVariable) { |
244 |
2369 |
if (isUnreserved(c)) { |
245 |
|
|
246 |
2203 |
varBuffer.append(c); |
247 |
166 |
} else if (c == '}') { |
248 |
|
|
249 |
166 |
if (varBuffer.length() == 0) { |
250 |
|
|
251 |
|
} else { |
252 |
166 |
Object varValue = values[valueId++]; |
253 |
|
|
254 |
166 |
String varValueString = (varValue == null) ? null : varValue.toString(); |
255 |
|
|
256 |
166 |
result.append(encode(varValueString)); |
257 |
|
|
258 |
|
|
259 |
166 |
varBuffer = new StringBuilder(); |
260 |
|
} |
261 |
166 |
inVariable = false; |
262 |
|
} else { |
263 |
|
|
264 |
|
} |
265 |
|
} else { |
266 |
4733 |
if (c == '{') { |
267 |
166 |
inVariable = true; |
268 |
166 |
varBuffer = new StringBuilder(); |
269 |
4567 |
} else if (c == '}') { |
270 |
|
|
271 |
|
} else { |
272 |
4567 |
result.append(c); |
273 |
|
} |
274 |
|
} |
275 |
|
} |
276 |
|
|
277 |
117 |
return result.toString(); |
278 |
|
} |
279 |
|
|
280 |
|
|
281 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
282 |
0 |
@Override... |
283 |
|
public String toString() |
284 |
|
{ |
285 |
0 |
return build().toString(); |
286 |
|
} |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
288 |
32 |
@Override... |
289 |
|
public UriBuilder clone() |
290 |
|
{ |
291 |
32 |
UriBuilder clone = null; |
292 |
|
|
293 |
32 |
try { |
294 |
32 |
clone = (UriBuilder) super.clone(); |
295 |
|
} catch (CloneNotSupportedException e) { |
296 |
|
|
297 |
|
} |
298 |
|
|
299 |
32 |
return clone; |
300 |
|
} |
301 |
|
} |