1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.component.wiki.internal; |
21 |
|
|
22 |
|
import java.lang.reflect.InvocationHandler; |
23 |
|
import java.lang.reflect.Proxy; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Arrays; |
26 |
|
import java.util.List; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Provider; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.slf4j.Logger; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.component.manager.ComponentManager; |
36 |
|
import org.xwiki.component.util.ReflectionUtils; |
37 |
|
import org.xwiki.component.wiki.WikiComponent; |
38 |
|
import org.xwiki.component.wiki.WikiComponentBuilder; |
39 |
|
import org.xwiki.component.wiki.WikiComponentException; |
40 |
|
import org.xwiki.component.wiki.internal.bridge.WikiComponentBridge; |
41 |
|
import org.xwiki.model.reference.DocumentReference; |
42 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
43 |
|
import org.xwiki.security.authorization.Right; |
44 |
|
|
45 |
|
import com.xpn.xwiki.XWikiContext; |
46 |
|
import com.xpn.xwiki.XWikiException; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Singleton |
|
|
| 96.9% |
Uncovered Elements: 1 (32) |
Complexity: 4 |
Complexity Density: 0.14 |
|
56 |
|
public class DefaultWikiComponentBuilder implements WikiComponentBuilder, WikiComponentConstants |
57 |
|
{ |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@Inject |
62 |
|
private Logger logger; |
63 |
|
|
64 |
|
@Inject |
65 |
|
@Named("context") |
66 |
|
private ComponentManager contextComponentManager; |
67 |
|
|
68 |
|
|
69 |
|
@link |
70 |
|
|
71 |
|
@Inject |
72 |
|
private Provider<XWikiContext> xcontextProvider; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
private WikiComponentBridge componentBridge; |
79 |
|
|
80 |
|
@Inject |
81 |
|
private ContextualAuthorizationManager authorization; |
82 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
|
83 |
3876 |
@Override... |
84 |
|
public List<DocumentReference> getDocumentReferences() |
85 |
|
{ |
86 |
3876 |
List<DocumentReference> results = new ArrayList<DocumentReference>(); |
87 |
|
|
88 |
3876 |
String query = ", BaseObject as obj, StringProperty as role where obj.className=? and obj.name=doc.fullName " |
89 |
|
+ "and role.id.id=obj.id and role.id.name=? " |
90 |
|
+ "and (role.value <> '' or (role.value is not null and '' is null))"; |
91 |
3876 |
List<String> parameters = new ArrayList<String>(); |
92 |
3876 |
parameters.add(COMPONENT_CLASS); |
93 |
3876 |
parameters.add(COMPONENT_ROLE_TYPE_FIELD); |
94 |
|
|
95 |
3876 |
try { |
96 |
3876 |
XWikiContext xcontext = xcontextProvider.get(); |
97 |
3876 |
results.addAll(xcontext.getWiki().getStore().searchDocumentReferences(query, parameters, xcontext)); |
98 |
|
} catch (XWikiException e) { |
99 |
0 |
this.logger.error("Failed to search for existing wiki components [{}]", e.getMessage()); |
100 |
|
} |
101 |
|
|
102 |
3876 |
return results; |
103 |
|
} |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 2 |
Complexity Density: 0.11 |
|
105 |
2 |
@Override... |
106 |
|
public List<WikiComponent> buildComponents(DocumentReference reference) throws WikiComponentException |
107 |
|
{ |
108 |
2 |
List<WikiComponent> components = new ArrayList<WikiComponent>(); |
109 |
|
|
110 |
2 |
if (!this.authorization.hasAccess(Right.PROGRAM, reference)) { |
111 |
1 |
throw new WikiComponentException("Registering wiki components requires programming rights"); |
112 |
|
} |
113 |
|
|
114 |
1 |
DefaultWikiComponent rawComponent = |
115 |
|
new DefaultWikiComponent(reference, componentBridge.getAuthorReference(reference), |
116 |
|
componentBridge.getRoleType(reference), componentBridge.getRoleHint(reference), |
117 |
|
componentBridge.getScope(reference)); |
118 |
1 |
rawComponent.setHandledMethods(componentBridge.getHandledMethods(reference)); |
119 |
1 |
rawComponent.setImplementedInterfaces(componentBridge.getDeclaredInterfaces(reference)); |
120 |
1 |
rawComponent.setDependencies(componentBridge.getDependencies(reference)); |
121 |
1 |
rawComponent.setSyntax(componentBridge.getSyntax(reference)); |
122 |
|
|
123 |
|
|
124 |
1 |
InvocationHandler handler = new DefaultWikiComponentInvocationHandler(rawComponent, contextComponentManager); |
125 |
|
|
126 |
|
|
127 |
1 |
List<Class<?>> implementedInterfaces = new ArrayList<Class<?>>(); |
128 |
|
|
129 |
1 |
Class<?> roleTypeClass = ReflectionUtils.getTypeClass(rawComponent.getRoleType()); |
130 |
|
|
131 |
1 |
implementedInterfaces.add(ReflectionUtils.getTypeClass(roleTypeClass)); |
132 |
|
|
133 |
1 |
implementedInterfaces.addAll(rawComponent.getImplementedInterfaces()); |
134 |
|
|
135 |
1 |
implementedInterfaces.addAll(Arrays.asList(rawComponent.getClass().getInterfaces())); |
136 |
|
|
137 |
|
|
138 |
1 |
Class<?>[] implementedInterfacesArray = implementedInterfaces.toArray(new Class<?>[0]); |
139 |
1 |
WikiComponent component = (WikiComponent) Proxy.newProxyInstance(roleTypeClass.getClassLoader(), |
140 |
|
implementedInterfacesArray, handler); |
141 |
|
|
142 |
1 |
components.add(component); |
143 |
|
|
144 |
1 |
return components; |
145 |
|
} |
146 |
|
} |