1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.resource.internal.entity; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.InputStream; |
24 |
|
import java.io.StringReader; |
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
|
29 |
|
import javax.inject.Inject; |
30 |
|
import javax.inject.Named; |
31 |
|
import javax.inject.Provider; |
32 |
|
import javax.inject.Singleton; |
33 |
|
|
34 |
|
import org.apache.commons.lang3.StringUtils; |
35 |
|
import org.jdom2.Document; |
36 |
|
import org.jdom2.Element; |
37 |
|
import org.jdom2.JDOMException; |
38 |
|
import org.jdom2.input.SAXBuilder; |
39 |
|
import org.xml.sax.EntityResolver; |
40 |
|
import org.xml.sax.InputSource; |
41 |
|
import org.xml.sax.SAXException; |
42 |
|
import org.xwiki.component.annotation.Component; |
43 |
|
import org.xwiki.component.manager.ComponentLookupException; |
44 |
|
import org.xwiki.component.manager.ComponentManager; |
45 |
|
import org.xwiki.component.phase.Initializable; |
46 |
|
import org.xwiki.component.phase.InitializationException; |
47 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
48 |
|
import org.xwiki.environment.Environment; |
49 |
|
import org.xwiki.resource.ResourceReferenceHandler; |
50 |
|
import org.xwiki.resource.entity.EntityResourceAction; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@link |
55 |
|
|
56 |
|
@version |
57 |
|
@since |
58 |
|
|
59 |
|
@Component |
60 |
|
@Singleton |
|
|
| 92.9% |
Uncovered Elements: 2 (28) |
Complexity: 7 |
Complexity Density: 0.32 |
|
61 |
|
public class DefaultEntityResourceActionLister implements EntityResourceActionLister, Initializable |
62 |
|
{ |
63 |
|
private static final String STRUTS_CONFIG_RESOURCE = "/WEB-INF/struts-config.xml"; |
64 |
|
|
65 |
|
private List<String> strutsActionNames; |
66 |
|
|
67 |
|
@Inject |
68 |
|
private Environment environment; |
69 |
|
|
70 |
|
@Inject |
71 |
|
@Named("context") |
72 |
|
private Provider<ComponentManager> contextComponentManagerProvider; |
73 |
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 3 |
Complexity Density: 0.23 |
|
74 |
89 |
@Override... |
75 |
|
public void initialize() throws InitializationException |
76 |
|
{ |
77 |
|
|
78 |
89 |
List<String> actionNames = new ArrayList<>(); |
79 |
89 |
SAXBuilder builder = new SAXBuilder(); |
80 |
|
|
81 |
|
|
82 |
89 |
builder.setEntityResolver(new EntityResolver() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
33 |
@Override public InputSource resolveEntity(String publicId, String systemId)... |
84 |
|
throws SAXException, IOException |
85 |
|
{ |
86 |
33 |
return new InputSource(new StringReader("")); |
87 |
|
} |
88 |
|
}); |
89 |
|
|
90 |
|
|
91 |
89 |
InputStream strutsConfigStream = this.environment.getResourceAsStream(getStrutsConfigResource()); |
92 |
|
|
93 |
89 |
if (strutsConfigStream != null) { |
94 |
|
|
95 |
33 |
Document document; |
96 |
33 |
try { |
97 |
33 |
document = builder.build(strutsConfigStream); |
98 |
|
} catch (JDOMException | IOException e) { |
99 |
0 |
throw new InitializationException( |
100 |
|
String.format("Failed to parse Struts Config file [%s]", getStrutsConfigResource()), e); |
101 |
|
} |
102 |
33 |
Element mappingElement = document.getRootElement().getChild("action-mappings"); |
103 |
33 |
for (Element element : mappingElement.getChildren("action")) { |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
1781 |
actionNames.add(StringUtils.strip(element.getAttributeValue("path"), "/")); |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
89 |
this.strutsActionNames = actionNames; |
112 |
|
} |
113 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
114 |
18505 |
@Override... |
115 |
|
public List<String> listActions() |
116 |
|
{ |
117 |
18505 |
List<String> actionNames = new ArrayList<>(this.strutsActionNames); |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
18502 |
try { |
122 |
18502 |
Map<String, ResourceReferenceHandler<EntityResourceAction>> componentMap = |
123 |
|
this.contextComponentManagerProvider.get().getInstanceMap( |
124 |
|
new DefaultParameterizedType(null, ResourceReferenceHandler.class, EntityResourceAction.class)); |
125 |
18507 |
for (String key : componentMap.keySet()) { |
126 |
1 |
actionNames.add(key); |
127 |
|
} |
128 |
|
} catch (ComponentLookupException e) { |
129 |
|
|
130 |
0 |
throw new RuntimeException("Failed to locate Resource Reference Handlers", e); |
131 |
|
} |
132 |
|
|
133 |
18505 |
return actionNames; |
134 |
|
} |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
89 |
protected String getStrutsConfigResource()... |
137 |
|
{ |
138 |
89 |
return STRUTS_CONFIG_RESOURCE; |
139 |
|
} |
140 |
|
} |