1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau;
18
19 import static org.apache.juneau.commons.utils.CollectionUtils.*;
20 import static org.apache.juneau.commons.utils.StringUtils.*;
21 import static org.apache.juneau.commons.utils.ThrowableUtils.*;
22 import static org.apache.juneau.commons.utils.Utils.*;
23
24 import java.util.function.Supplier;
25
26 import org.apache.juneau.annotation.*;
27 import org.apache.juneau.collections.*;
28 import org.apache.juneau.commons.collections.*;
29 import org.apache.juneau.parser.*;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 @Bean
48 public class UriContext {
49
50
51
52
53
54
55
56 public static final UriContext DEFAULT = new UriContext();
57
58
59
60
61
62
63
64
65
66 public static UriContext of(String s) {
67 try {
68 return new UriContext(s);
69 } catch (ParseException e) {
70 throw toRex(e);
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public static UriContext of(String authority, String contextRoot, String servletPath, String pathInfo) {
88 return new UriContext(authority, contextRoot, servletPath, pathInfo);
89 }
90
91 private static String getParent(String uri) {
92 var i = uri.lastIndexOf('/');
93 if (i <= 1)
94 return "/";
95 return uri.substring(0, i);
96 }
97
98 @SuppressWarnings("javadoc")
99 public final String authority, contextRoot, servletPath, pathInfo, parentPath;
100
101
102 private final Supplier<String> aContextRoot, rContextRoot, aServletPath, rResource, aPathInfo, rPath;
103
104
105
106
107
108
109
110 public UriContext() {
111 this(null, null, null, null);
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public UriContext(String s) throws ParseException {
128 var m = JsonMap.ofJson(s);
129 this.authority = nullIfEmpty(trimSlashes(m.getString("authority")));
130 this.contextRoot = nullIfEmpty(trimSlashes(m.getString("contextRoot")));
131 this.servletPath = nullIfEmpty(trimSlashes(m.getString("servletPath")));
132 this.pathInfo = nullIfEmpty(trimSlashes(m.getString("pathInfo")));
133 this.parentPath = this.pathInfo == null || this.pathInfo.indexOf('/') == -1 ? null : this.pathInfo.substring(0, this.pathInfo.lastIndexOf('/'));
134 this.rContextRoot = mem(() -> findRContextRoot());
135 this.rResource = mem(() -> findRResource());
136 this.rPath = mem(() -> findRPath());
137 this.aContextRoot = mem(() -> findAContextRoot());
138 this.aServletPath = mem(() -> findAServletPath());
139 this.aPathInfo = mem(() -> findAPathInfo());
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 @Beanc
161 public UriContext(@Name("authority") String authority, @Name("contextRoot") String contextRoot, @Name("servletPath") String servletPath, @Name("pathInfo") String pathInfo) {
162 this.authority = nullIfEmpty(trimSlashes(authority));
163 this.contextRoot = nullIfEmpty(trimSlashes(contextRoot));
164 this.servletPath = nullIfEmpty(trimSlashes(servletPath));
165 this.pathInfo = nullIfEmpty(trimSlashes(pathInfo));
166 this.parentPath = this.pathInfo == null || this.pathInfo.indexOf('/') == -1 ? null : this.pathInfo.substring(0, this.pathInfo.lastIndexOf('/'));
167 this.rContextRoot = mem(() -> findRContextRoot());
168 this.rResource = mem(() -> findRResource());
169 this.rPath = mem(() -> findRPath());
170 this.aContextRoot = mem(() -> findAContextRoot());
171 this.aServletPath = mem(() -> findAServletPath());
172 this.aPathInfo = mem(() -> findAPathInfo());
173 }
174
175 private String findRContextRoot() {
176 return contextRoot == null ? "/" : ('/' + contextRoot);
177 }
178
179 private String findRResource() {
180
181 if (contextRoot == null)
182 return (
183 servletPath == null
184 ? "/"
185 : ('/' + servletPath)
186 );
187 return (
188 servletPath == null
189 ? ('/' + contextRoot)
190 : ('/' + contextRoot + '/' + servletPath)
191 );
192
193 }
194
195 private String findRPath() {
196
197 if (contextRoot == null) {
198 if (servletPath == null)
199 return (
200 pathInfo == null
201 ? "/"
202 : ('/' + pathInfo)
203 );
204 return (
205 pathInfo == null
206 ? ('/' + servletPath)
207 : ('/' + servletPath + '/' + pathInfo)
208 );
209 }
210 if (servletPath == null)
211 return (
212 pathInfo == null
213 ? ('/' + contextRoot)
214 : ('/' + contextRoot + '/' + pathInfo)
215 );
216 return (
217 pathInfo == null
218 ? ('/' + contextRoot + '/' + servletPath)
219 : ('/' + contextRoot + '/' + servletPath + '/' + pathInfo)
220 );
221
222 }
223
224 private String findAContextRoot() {
225
226 if (authority == null)
227 return rContextRoot.get();
228 return (
229 contextRoot == null
230 ? authority
231 : (authority + '/' + contextRoot)
232 );
233
234 }
235
236 private String findAServletPath() {
237
238 if (authority == null)
239 return rResource.get();
240 if (contextRoot == null)
241 return (
242 servletPath == null
243 ? authority
244 : authority + '/' + servletPath
245 );
246 return (
247 servletPath == null
248 ? (authority + '/' + contextRoot)
249 : (authority + '/' + contextRoot + '/' + servletPath)
250 );
251
252 }
253
254 private String findAPathInfo() {
255
256 if (authority == null)
257 return rPath.get();
258 if (contextRoot == null) {
259 if (servletPath == null)
260 return (
261 pathInfo == null
262 ? authority : (authority + '/' + pathInfo)
263 );
264 return (
265 pathInfo == null
266 ? (authority + '/' + servletPath)
267 : (authority + '/' + servletPath + '/' + pathInfo)
268 );
269 }
270 if (servletPath == null)
271 return (
272 pathInfo == null
273 ? authority + '/' + contextRoot
274 : (authority + '/' + contextRoot + '/' + pathInfo)
275 );
276 return (
277 pathInfo == null
278 ? (authority + '/' + contextRoot + '/' + servletPath)
279 : (authority + '/' + contextRoot + '/' + servletPath + '/' + pathInfo)
280 );
281
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297 public String getAbsoluteAuthority() { return authority == null ? "/" : authority; }
298
299
300
301
302
303
304
305
306
307
308
309 public String getAbsoluteContextRoot() {
310 return aContextRoot.get();
311 }
312
313
314
315
316
317
318
319
320
321
322
323 public String getAbsolutePathInfo() {
324 return aPathInfo.get();
325 }
326
327
328
329
330
331
332 public String getAbsolutePathInfoParent() { return getParent(getAbsolutePathInfo()); }
333
334
335
336
337
338
339
340
341
342
343
344 public String getAbsoluteServletPath() {
345 return aServletPath.get();
346 }
347
348
349
350
351
352
353 public String getAbsoluteServletPathParent() { return getParent(getAbsoluteServletPath()); }
354
355
356
357
358
359
360
361
362
363
364
365 public String getRootRelativeContextRoot() {
366 return rContextRoot.get();
367 }
368
369
370
371
372
373
374
375
376
377
378
379 public String getRootRelativePathInfo() {
380 return rPath.get();
381 }
382
383
384
385
386
387
388 public String getRootRelativePathInfoParent() { return getParent(getRootRelativePathInfo()); }
389
390
391
392
393
394
395
396
397
398
399
400 public String getRootRelativeServletPath() {
401 return rResource.get();
402 }
403
404
405
406
407
408
409 public String getRootRelativeServletPathParent() { return getParent(getRootRelativeServletPath()); }
410
411 protected FluentMap<String,Object> properties() {
412
413 return filteredBeanPropertyMap()
414 .a("aContextRoot", aContextRoot.get())
415 .a("aPathInfo", aPathInfo.get())
416 .a("aServletPath", aServletPath.get())
417 .a("authority", authority)
418 .a("contextRoot", contextRoot)
419 .a("parentPath", parentPath)
420 .a("pathInfo", pathInfo)
421 .a("rContextRoot", rContextRoot.get())
422 .a("rResource", rResource.get())
423 .a("servletPath", servletPath)
424 .a("rPath", rPath.get());
425
426 }
427
428 @Override
429 public String toString() {
430 return r(properties());
431 }
432 }