| 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.bridge; |
| 21 |
|
|
| 22 |
|
import java.lang.reflect.Type; |
| 23 |
|
import java.util.ArrayList; |
| 24 |
|
import java.util.HashMap; |
| 25 |
|
import java.util.List; |
| 26 |
|
import java.util.Map; |
| 27 |
|
|
| 28 |
|
import javax.inject.Inject; |
| 29 |
|
import javax.inject.Provider; |
| 30 |
|
import javax.inject.Singleton; |
| 31 |
|
|
| 32 |
|
import org.apache.commons.lang3.StringUtils; |
| 33 |
|
import org.slf4j.Logger; |
| 34 |
|
import org.xwiki.component.annotation.Component; |
| 35 |
|
import org.xwiki.component.descriptor.ComponentDescriptor; |
| 36 |
|
import org.xwiki.component.descriptor.DefaultComponentDescriptor; |
| 37 |
|
import org.xwiki.component.util.ReflectionUtils; |
| 38 |
|
import org.xwiki.component.wiki.WikiComponentException; |
| 39 |
|
import org.xwiki.component.wiki.WikiComponentScope; |
| 40 |
|
import org.xwiki.component.wiki.internal.WikiComponentConstants; |
| 41 |
|
import org.xwiki.model.reference.DocumentReference; |
| 42 |
|
import org.xwiki.rendering.block.XDOM; |
| 43 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 44 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
| 45 |
|
import org.xwiki.security.authorization.Right; |
| 46 |
|
|
| 47 |
|
import com.xpn.xwiki.XWikiContext; |
| 48 |
|
import com.xpn.xwiki.XWikiException; |
| 49 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 50 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
@version |
| 56 |
|
@since |
| 57 |
|
|
| 58 |
|
@Component |
| 59 |
|
@Singleton |
| |
|
| 86.8% |
Uncovered Elements: 10 (76) |
Complexity: 21 |
Complexity Density: 0.4 |
|
| 60 |
|
public class DefaultWikiComponentBridge implements WikiComponentConstants, WikiComponentBridge |
| 61 |
|
{ |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@Inject |
| 66 |
|
protected Logger logger; |
| 67 |
|
|
| 68 |
|
@Inject |
| 69 |
|
private Provider<XWikiContext> xcontextProvider; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
@Inject |
| 75 |
|
private ContentParser renderingBridge; |
| 76 |
|
|
| 77 |
|
@Inject |
| 78 |
|
private ContextualAuthorizationManager authorization; |
| 79 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 80 |
2 |
@Override... |
| 81 |
|
public Syntax getSyntax(DocumentReference reference) throws WikiComponentException |
| 82 |
|
{ |
| 83 |
2 |
XWikiDocument componentDocument = this.getDocument(reference); |
| 84 |
2 |
return componentDocument.getSyntax(); |
| 85 |
|
} |
| 86 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 87 |
3 |
@Override... |
| 88 |
|
public Type getRoleType(DocumentReference reference) throws WikiComponentException |
| 89 |
|
{ |
| 90 |
3 |
BaseObject componentObject = getComponentObject(reference); |
| 91 |
2 |
String role = componentObject.getStringValue(COMPONENT_ROLE_TYPE_FIELD); |
| 92 |
2 |
Type roleType; |
| 93 |
|
|
| 94 |
2 |
try { |
| 95 |
2 |
roleType = ReflectionUtils.unserializeType(role, Thread.currentThread().getContextClassLoader()); |
| 96 |
|
} catch (ClassNotFoundException e) { |
| 97 |
1 |
throw new WikiComponentException(String.format("The role type [%s] does not exist", role), e); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
1 |
return roleType; |
| 101 |
|
} |
| 102 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 103 |
1 |
@Override... |
| 104 |
|
public String getRoleHint(DocumentReference reference) throws WikiComponentException |
| 105 |
|
{ |
| 106 |
1 |
BaseObject componentObject = getComponentObject(reference); |
| 107 |
1 |
return StringUtils.defaultIfEmpty(componentObject.getStringValue(COMPONENT_ROLE_HINT_FIELD), "default"); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 110 |
1 |
@Override... |
| 111 |
|
public DocumentReference getAuthorReference(DocumentReference reference) throws WikiComponentException |
| 112 |
|
{ |
| 113 |
1 |
XWikiDocument componentDocument = this.getDocument(reference); |
| 114 |
1 |
return componentDocument.getAuthorReference(); |
| 115 |
|
} |
| 116 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 117 |
2 |
@Override... |
| 118 |
|
public WikiComponentScope getScope(DocumentReference reference) throws WikiComponentException |
| 119 |
|
{ |
| 120 |
2 |
BaseObject componentObject = getComponentObject(reference); |
| 121 |
2 |
return WikiComponentScope.fromString(componentObject.getStringValue(COMPONENT_SCOPE_FIELD)); |
| 122 |
|
} |
| 123 |
|
|
| |
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 124 |
1 |
@Override... |
| 125 |
|
public Map<String, XDOM> getHandledMethods(DocumentReference reference) throws WikiComponentException |
| 126 |
|
{ |
| 127 |
1 |
Map<String, XDOM> handledMethods = new HashMap<String, XDOM>(); |
| 128 |
1 |
XWikiDocument componentDocument = this.getDocument(reference); |
| 129 |
1 |
if (componentDocument.getObjectNumbers(METHOD_CLASS) > 0) { |
| 130 |
1 |
for (BaseObject method : componentDocument.getObjects(METHOD_CLASS)) { |
| 131 |
1 |
if (!StringUtils.isBlank(method.getStringValue(METHOD_NAME_FIELD))) { |
| 132 |
1 |
handledMethods.put(method.getStringValue(METHOD_NAME_FIELD), renderingBridge.parse( |
| 133 |
|
method.getStringValue(METHOD_CODE_FIELD), this.getSyntax(reference), reference)); |
| 134 |
|
} |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
1 |
return handledMethods; |
| 138 |
|
} |
| 139 |
|
|
| |
|
| 78.6% |
Uncovered Elements: 3 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 140 |
1 |
@Override... |
| 141 |
|
public List<Class< ? >> getDeclaredInterfaces(DocumentReference reference) throws WikiComponentException |
| 142 |
|
{ |
| 143 |
1 |
List<Class< ? >> interfaces = new ArrayList<Class< ? >>(); |
| 144 |
1 |
XWikiDocument componentDocument = this.getDocument(reference); |
| 145 |
1 |
if (componentDocument.getObjectNumbers(INTERFACE_CLASS) > 0) { |
| 146 |
1 |
for (BaseObject iface : componentDocument.getObjects(INTERFACE_CLASS)) { |
| 147 |
1 |
if (!StringUtils.isBlank(iface.getStringValue(INTERFACE_NAME_FIELD))) { |
| 148 |
1 |
try { |
| 149 |
1 |
Class< ? > implemented = Class.forName(iface.getStringValue(INTERFACE_NAME_FIELD)); |
| 150 |
1 |
interfaces.add(implemented); |
| 151 |
|
} catch (Exception e) { |
| 152 |
0 |
this.logger.warn("Interface [{}] not found, declared for wiki component [{}]", |
| 153 |
|
iface.getStringValue(INTERFACE_NAME_FIELD), componentDocument.getDocumentReference()); |
| 154 |
|
} |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
1 |
return interfaces; |
| 159 |
|
} |
| 160 |
|
|
| |
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
| 161 |
1 |
@Override... |
| 162 |
|
public Map<String, ComponentDescriptor> getDependencies(DocumentReference reference) throws WikiComponentException |
| 163 |
|
{ |
| 164 |
1 |
Map<String, ComponentDescriptor> dependencies = new HashMap<String, ComponentDescriptor>(); |
| 165 |
1 |
XWikiDocument componentDocument = this.getDocument(reference); |
| 166 |
1 |
if (componentDocument.getObjectNumbers(DEPENDENCY_CLASS) > 0) { |
| 167 |
1 |
for (BaseObject dependency : componentDocument.getObjects(DEPENDENCY_CLASS)) { |
| 168 |
1 |
try { |
| 169 |
1 |
DefaultComponentDescriptor cd = new DefaultComponentDescriptor(); |
| 170 |
1 |
cd.setRoleType(ReflectionUtils.unserializeType( |
| 171 |
|
dependency.getStringValue(COMPONENT_ROLE_TYPE_FIELD), Thread.currentThread() |
| 172 |
|
.getContextClassLoader())); |
| 173 |
1 |
cd.setRoleHint(dependency.getStringValue(COMPONENT_ROLE_HINT_FIELD)); |
| 174 |
1 |
dependencies.put(dependency.getStringValue(DEPENDENCY_BINDING_NAME_FIELD), cd); |
| 175 |
|
} catch (Exception e) { |
| 176 |
0 |
this.logger.warn("Interface [{}] not found, declared as dependency for wiki component [{}]", |
| 177 |
|
dependency.getStringValue(COMPONENT_ROLE_TYPE_FIELD), componentDocument.getDocumentReference()); |
| 178 |
|
} |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
|
| 182 |
1 |
return dependencies; |
| 183 |
|
} |
| 184 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 185 |
0 |
@Override... |
| 186 |
|
public boolean hasProgrammingRights(DocumentReference reference) throws WikiComponentException |
| 187 |
|
{ |
| 188 |
0 |
return this.authorization.hasAccess(Right.PROGRAM, reference); |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
@param |
| 195 |
|
@return |
| 196 |
|
@throws |
| 197 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 198 |
6 |
private BaseObject getComponentObject(DocumentReference reference) throws WikiComponentException... |
| 199 |
|
{ |
| 200 |
6 |
XWikiDocument componentDocument = this.getDocument(reference); |
| 201 |
6 |
BaseObject componentObject = componentDocument.getObject(COMPONENT_CLASS); |
| 202 |
|
|
| 203 |
6 |
if (componentObject == null) { |
| 204 |
1 |
throw new WikiComponentException(String.format("No component object could be found in document [%s]", |
| 205 |
|
reference)); |
| 206 |
|
} |
| 207 |
|
|
| 208 |
5 |
return componentObject; |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
@param |
| 215 |
|
@return |
| 216 |
|
@throws |
| 217 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 218 |
12 |
protected XWikiDocument getDocument(DocumentReference reference) throws WikiComponentException... |
| 219 |
|
{ |
| 220 |
12 |
try { |
| 221 |
12 |
XWikiContext xcontext = this.xcontextProvider.get(); |
| 222 |
12 |
return xcontext.getWiki().getDocument(reference, xcontext); |
| 223 |
|
} catch (XWikiException e) { |
| 224 |
0 |
throw new WikiComponentException(String.format("Failed to retrieve the document [%s]", reference), e); |
| 225 |
|
} |
| 226 |
|
} |
| 227 |
|
} |