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.standard.entity; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.List; |
25 |
|
import java.util.ListIterator; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import org.apache.commons.lang3.StringUtils; |
29 |
|
import org.apache.commons.lang3.tuple.ImmutablePair; |
30 |
|
import org.apache.commons.lang3.tuple.Pair; |
31 |
|
import org.xwiki.model.EntityType; |
32 |
|
import org.xwiki.model.reference.EntityReference; |
33 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
34 |
|
import org.xwiki.model.reference.WikiReference; |
35 |
|
import org.xwiki.resource.CreateResourceReferenceException; |
36 |
|
import org.xwiki.resource.ResourceType; |
37 |
|
import org.xwiki.resource.UnsupportedResourceReferenceException; |
38 |
|
import org.xwiki.resource.entity.EntityResourceAction; |
39 |
|
import org.xwiki.resource.entity.EntityResourceReference; |
40 |
|
import org.xwiki.resource.internal.entity.EntityResourceActionLister; |
41 |
|
import org.xwiki.url.ExtendedURL; |
42 |
|
import org.xwiki.url.internal.AbstractResourceReferenceResolver; |
43 |
|
import org.xwiki.url.internal.standard.StandardURLConfiguration; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
|
|
| 96.6% |
Uncovered Elements: 3 (88) |
Complexity: 20 |
Complexity Density: 0.33 |
|
51 |
|
public abstract class AbstractEntityResourceReferenceResolver extends AbstractResourceReferenceResolver |
52 |
|
{ |
53 |
|
private static final String VIEW_ACTION = "view"; |
54 |
|
|
55 |
|
private static final String DOWNLOAD_ACTION = "download"; |
56 |
|
|
57 |
|
private static final String DELATTACHMENT_ACTION = "delattachment"; |
58 |
|
|
59 |
|
private static final String VIEWATTACHREV_ACTION = "viewattachrev"; |
60 |
|
|
61 |
|
private static final String DOWNLOADREV_ACTION = "downloadrev"; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private static final String SKIN_ACTION = "skin"; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
private static final List<String> FILE_ACTION_LIST = |
72 |
|
Arrays.asList(DOWNLOAD_ACTION, DELATTACHMENT_ACTION, VIEWATTACHREV_ACTION, DOWNLOADREV_ACTION, SKIN_ACTION); |
73 |
|
|
74 |
|
private StandardURLConfiguration configuration; |
75 |
|
|
76 |
|
private EntityResourceActionLister entityResourceActionLister; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private EntityReferenceResolver<EntityReference> defaultReferenceEntityReferenceResolver; |
82 |
|
|
83 |
|
protected abstract WikiReference extractWikiReference(ExtendedURL url); |
84 |
|
|
|
|
| 97% |
Uncovered Elements: 1 (33) |
Complexity: 6 |
Complexity Density: 0.24 |
|
85 |
21125 |
@Override... |
86 |
|
public EntityResourceReference resolve(ExtendedURL extendedURL, ResourceType type, Map<String, Object> parameters) |
87 |
|
throws CreateResourceReferenceException, UnsupportedResourceReferenceException |
88 |
|
{ |
89 |
21129 |
EntityResourceReference reference = null; |
90 |
|
|
91 |
|
|
92 |
21117 |
WikiReference wikiReference = extractWikiReference(extendedURL); |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
21108 |
List<String> pathSegments = extendedURL.getSegments(); |
97 |
21114 |
List<String> spaceNames = null; |
98 |
21121 |
String pageName = null; |
99 |
21115 |
String attachmentName = null; |
100 |
21109 |
String action = VIEW_ACTION; |
101 |
|
|
102 |
21109 |
if (pathSegments.size() != 0) { |
103 |
21083 |
String firstSegment = pathSegments.get(0); |
104 |
21091 |
action = firstSegment; |
105 |
|
|
106 |
|
|
107 |
21103 |
if (FILE_ACTION_LIST.contains(firstSegment) && pathSegments.size() >= 4) { |
108 |
|
|
109 |
2574 |
attachmentName = pathSegments.get(pathSegments.size() - 1); |
110 |
|
|
111 |
2580 |
pageName = pathSegments.get(pathSegments.size() - 2); |
112 |
|
|
113 |
2581 |
spaceNames = extractSpaceNames(pathSegments, 1, pathSegments.size() - 3); |
114 |
|
} else { |
115 |
|
|
116 |
18526 |
Pair<String, Integer> actionAndStartPosition = |
117 |
|
computeActionAndStartPosition(firstSegment, pathSegments); |
118 |
18528 |
action = actionAndStartPosition.getLeft(); |
119 |
18528 |
int startPosition = actionAndStartPosition.getRight(); |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
18526 |
if (pathSegments.size() - startPosition == 1) { |
127 |
6 |
spaceNames = Arrays.asList(pathSegments.get(startPosition)); |
128 |
|
} else { |
129 |
|
|
130 |
18520 |
pageName = pathSegments.get(pathSegments.size() - 1); |
131 |
|
|
132 |
18523 |
spaceNames = extractSpaceNames(pathSegments, startPosition, pathSegments.size() - 2); |
133 |
|
} |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
21114 |
if (reference == null) { |
138 |
21099 |
reference = new EntityResourceReference( |
139 |
|
buildEntityReference(wikiReference, spaceNames, pageName, attachmentName), |
140 |
|
EntityResourceAction.fromString(action)); |
141 |
|
} |
142 |
|
|
143 |
21103 |
copyParameters(extendedURL, reference); |
144 |
|
|
145 |
21104 |
return reference; |
146 |
|
} |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 5 |
Complexity Density: 0.45 |
|
148 |
18524 |
private Pair<String, Integer> computeActionAndStartPosition(String firstSegment, List<String> pathSegments)... |
149 |
|
{ |
150 |
18523 |
String action; |
151 |
18524 |
int startPosition; |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
18528 |
if (!this.entityResourceActionLister.listActions().contains(firstSegment)) { |
166 |
1808 |
if (pathSegments.size() > 0 && firstSegment.equals("resources") |
167 |
|
&& pathSegments.get(pathSegments.size() - 1).endsWith(".gwtrpc")) |
168 |
|
{ |
169 |
4 |
action = firstSegment; |
170 |
4 |
startPosition = 1; |
171 |
|
} else { |
172 |
1804 |
action = VIEW_ACTION; |
173 |
1804 |
startPosition = 0; |
174 |
|
} |
175 |
|
} else { |
176 |
16720 |
action = firstSegment; |
177 |
16719 |
startPosition = 1; |
178 |
|
} |
179 |
|
|
180 |
18527 |
return new ImmutablePair<>(action, startPosition); |
181 |
|
} |
182 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
183 |
21068 |
private List<String> extractSpaceNames(List<String> pathSegments, int startPosition, int stopPosition)... |
184 |
|
{ |
185 |
21095 |
if (stopPosition < 0) { |
186 |
0 |
return null; |
187 |
|
} |
188 |
|
|
189 |
21091 |
List<String> spaceNames = new ArrayList<>(); |
190 |
21078 |
ListIterator<String> iterator = pathSegments.listIterator(startPosition); |
191 |
21111 |
int total = stopPosition - startPosition + 1; |
192 |
21115 |
int count = 0; |
193 |
54134 |
while (count < total) { |
194 |
33021 |
spaceNames.add(iterator.next()); |
195 |
33015 |
count++; |
196 |
|
} |
197 |
21112 |
return spaceNames; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@param |
205 |
|
@param |
206 |
|
@param |
207 |
|
@return |
208 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 6 |
Complexity Density: 0.43 |
|
209 |
21046 |
private EntityReference buildEntityReference(WikiReference wikiReference, List<String> spaceNames, String pageName,... |
210 |
|
String attachmentName) |
211 |
|
{ |
212 |
21072 |
EntityReference reference = wikiReference; |
213 |
21108 |
EntityType entityType = EntityType.DOCUMENT; |
214 |
21092 |
if (spaceNames != null && !spaceNames.isEmpty()) { |
215 |
21085 |
EntityReference parent = reference; |
216 |
21100 |
for (String spaceName : spaceNames) { |
217 |
33018 |
if (!StringUtils.isEmpty(spaceName)) { |
218 |
33008 |
reference = new EntityReference(spaceName, EntityType.SPACE, parent); |
219 |
33017 |
parent = reference; |
220 |
|
} |
221 |
|
} |
222 |
|
} |
223 |
21123 |
if (!StringUtils.isEmpty(pageName)) { |
224 |
20453 |
reference = new EntityReference(pageName, EntityType.DOCUMENT, reference); |
225 |
|
} |
226 |
21133 |
if (!StringUtils.isEmpty(attachmentName)) { |
227 |
2602 |
reference = new EntityReference(attachmentName, EntityType.ATTACHMENT, reference); |
228 |
2602 |
entityType = EntityType.ATTACHMENT; |
229 |
|
} |
230 |
21131 |
return this.defaultReferenceEntityReferenceResolver.resolve(reference, entityType); |
231 |
|
} |
232 |
|
} |