1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.url.internal.container; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.LinkedHashSet; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
import javax.servlet.http.HttpServletRequest; |
31 |
|
|
32 |
|
import org.apache.commons.lang3.StringUtils; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.component.phase.Initializable; |
35 |
|
import org.xwiki.container.Container; |
36 |
|
import org.xwiki.container.servlet.ServletRequest; |
37 |
|
import org.xwiki.environment.Environment; |
38 |
|
import org.xwiki.environment.internal.ServletEnvironment; |
39 |
|
import org.xwiki.url.ExtendedURL; |
40 |
|
import org.xwiki.url.URLNormalizer; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
50 |
|
@Component |
51 |
|
@Named("contextpath+actionservletpath") |
52 |
|
@Singleton |
|
|
| 100% |
Uncovered Elements: 0 (46) |
Complexity: 12 |
Complexity Density: 0.46 |
|
53 |
|
public class ContextAndActionURLNormalizer implements URLNormalizer<ExtendedURL>, Initializable |
54 |
|
{ |
55 |
|
|
56 |
|
private static final String URL_SEGMENT_DELIMITER = "/"; |
57 |
|
|
58 |
|
|
59 |
|
private static final String IGNORED_MAPPING_CHARACTERS = "/*"; |
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
private Container container; |
64 |
|
|
65 |
|
|
66 |
|
@Inject |
67 |
|
private Environment environment; |
68 |
|
|
69 |
|
|
70 |
|
private String defaultServletMapping = "bin"; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private Collection<String> validServletMappings = new LinkedHashSet<>(); |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
78 |
16 |
@Override... |
79 |
|
public void initialize() |
80 |
|
{ |
81 |
16 |
if (this.environment instanceof ServletEnvironment) { |
82 |
14 |
for (String mapping : ((ServletEnvironment) this.environment).getServletContext() |
83 |
|
.getServletRegistration("action").getMappings()) { |
84 |
43 |
this.validServletMappings.add(StringUtils.strip(mapping, IGNORED_MAPPING_CHARACTERS)); |
85 |
|
} |
86 |
|
} |
87 |
16 |
if (!this.validServletMappings.isEmpty()) { |
88 |
14 |
this.defaultServletMapping = this.validServletMappings.iterator().next(); |
89 |
|
} |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
92 |
7 |
@Override... |
93 |
|
public ExtendedURL normalize(ExtendedURL partialURL) |
94 |
|
{ |
95 |
7 |
String contextPath = StringUtils.strip(getContextPath(), URL_SEGMENT_DELIMITER); |
96 |
7 |
if (contextPath == null) { |
97 |
1 |
throw new RuntimeException(String.format("Failed to normalize the URL [%s] since the " |
98 |
|
+ "application's Servlet context couldn't be computed.", partialURL)); |
99 |
|
} |
100 |
6 |
List<String> segments = new ArrayList<>(); |
101 |
6 |
if (StringUtils.isNotEmpty(contextPath)) { |
102 |
4 |
segments.add(contextPath); |
103 |
|
} |
104 |
|
|
105 |
6 |
String servletPath = getActionServletMapping(); |
106 |
6 |
if (StringUtils.isNotEmpty(servletPath)) { |
107 |
5 |
segments.add(servletPath); |
108 |
|
} |
109 |
|
|
110 |
6 |
segments.addAll(partialURL.getSegments()); |
111 |
|
|
112 |
6 |
return new ExtendedURL(segments, partialURL.getParameters()); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
115 |
10 |
protected String getContextPath()... |
116 |
|
{ |
117 |
10 |
if (this.environment instanceof ServletEnvironment) { |
118 |
8 |
return ((ServletEnvironment) this.environment).getServletContext().getContextPath(); |
119 |
|
} |
120 |
2 |
return null; |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
@return |
128 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
129 |
14 |
protected String getActionServletMapping()... |
130 |
|
{ |
131 |
14 |
String result = this.defaultServletMapping; |
132 |
14 |
if (this.container.getRequest() instanceof ServletRequest) { |
133 |
4 |
HttpServletRequest hsRequest = ((ServletRequest) this.container.getRequest()).getHttpServletRequest(); |
134 |
4 |
result = StringUtils.strip(hsRequest.getServletPath(), IGNORED_MAPPING_CHARACTERS); |
135 |
|
|
136 |
4 |
if (!this.validServletMappings.contains(result)) { |
137 |
|
|
138 |
1 |
result = this.defaultServletMapping; |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
14 |
return result; |
143 |
|
} |
144 |
|
} |