| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.rendering.internal.macro.include; |
| 21 |
|
|
| 22 |
|
import java.util.Arrays; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Stack; |
| 25 |
|
|
| 26 |
|
import javax.inject.Inject; |
| 27 |
|
import javax.inject.Named; |
| 28 |
|
import javax.inject.Singleton; |
| 29 |
|
|
| 30 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 31 |
|
import org.xwiki.bridge.DocumentModelBridge; |
| 32 |
|
import org.xwiki.component.annotation.Component; |
| 33 |
|
import org.xwiki.display.internal.DocumentDisplayer; |
| 34 |
|
import org.xwiki.display.internal.DocumentDisplayerParameters; |
| 35 |
|
import org.xwiki.model.reference.DocumentReference; |
| 36 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 37 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
| 38 |
|
import org.xwiki.rendering.block.Block; |
| 39 |
|
import org.xwiki.rendering.block.MacroBlock; |
| 40 |
|
import org.xwiki.rendering.block.MacroMarkerBlock; |
| 41 |
|
import org.xwiki.rendering.block.MetaDataBlock; |
| 42 |
|
import org.xwiki.rendering.block.XDOM; |
| 43 |
|
import org.xwiki.rendering.listener.MetaData; |
| 44 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
| 45 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
| 46 |
|
import org.xwiki.rendering.macro.include.IncludeMacroParameters; |
| 47 |
|
import org.xwiki.rendering.macro.include.IncludeMacroParameters.Context; |
| 48 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
@version |
| 52 |
|
@since |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
@Component |
| 56 |
|
@Named("include") |
| 57 |
|
@Singleton |
| |
|
| 85.4% |
Uncovered Elements: 14 (96) |
Complexity: 24 |
Complexity Density: 0.39 |
|
| 58 |
|
public class IncludeMacro extends AbstractMacro<IncludeMacroParameters> |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
private static final String DESCRIPTION = "Include other pages into the current page."; |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
@Inject |
| 69 |
|
private DocumentAccessBridge documentAccessBridge; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
@link |
| 73 |
|
|
| 74 |
|
@Inject |
| 75 |
|
@Named("macro") |
| 76 |
|
private DocumentReferenceResolver<String> macroDocumentReferenceResolver; |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
@Inject |
| 83 |
|
private EntityReferenceSerializer<String> defaultEntityReferenceSerializer; |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
@Inject |
| 89 |
|
@Named("configured") |
| 90 |
|
private DocumentDisplayer documentDisplayer; |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
private ThreadLocal<Stack<Object>> inclusionsBeingExecuted = new ThreadLocal<Stack<Object>>(); |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 100 |
29 |
public IncludeMacro()... |
| 101 |
|
{ |
| 102 |
29 |
super("Include", DESCRIPTION, IncludeMacroParameters.class); |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
29 |
setPriority(10); |
| 107 |
29 |
setDefaultCategory(DEFAULT_CATEGORY_CONTENT); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 110 |
87 |
@Override... |
| 111 |
|
public boolean supportsInlineMode() |
| 112 |
|
{ |
| 113 |
87 |
return true; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
@param |
| 120 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 121 |
3 |
public void setDocumentAccessBridge(DocumentAccessBridge documentAccessBridge)... |
| 122 |
|
{ |
| 123 |
3 |
this.documentAccessBridge = documentAccessBridge; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
@param |
| 130 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 131 |
1 |
public void setDocumentDisplayer(DocumentDisplayer documentDisplayer)... |
| 132 |
|
{ |
| 133 |
1 |
this.documentDisplayer = documentDisplayer; |
| 134 |
|
} |
| 135 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 8 (48) |
Complexity: 9 |
Complexity Density: 0.25 |
|
| 136 |
964 |
@Override... |
| 137 |
|
public List<Block> execute(IncludeMacroParameters parameters, String content, MacroTransformationContext context) |
| 138 |
|
throws MacroExecutionException |
| 139 |
|
{ |
| 140 |
|
|
| 141 |
964 |
if (parameters.getReference() == null) { |
| 142 |
1 |
throw new MacroExecutionException( |
| 143 |
|
"You must specify a 'reference' parameter pointing to the entity to include."); |
| 144 |
|
} |
| 145 |
|
|
| 146 |
963 |
DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters); |
| 147 |
|
|
| 148 |
963 |
checkRecursiveInclusion(context.getCurrentMacroBlock(), includedReference); |
| 149 |
|
|
| 150 |
961 |
if (!this.documentAccessBridge.isDocumentViewable(includedReference)) { |
| 151 |
0 |
throw new MacroExecutionException(String.format( |
| 152 |
|
"Current user [%s] doesn't have view rights on document [%s]", |
| 153 |
|
this.documentAccessBridge.getCurrentUserReference(), |
| 154 |
|
this.defaultEntityReferenceSerializer.serialize(includedReference))); |
| 155 |
|
} |
| 156 |
|
|
| 157 |
961 |
Context parametersContext = parameters.getContext(); |
| 158 |
|
|
| 159 |
|
|
| 160 |
961 |
DocumentModelBridge documentBridge; |
| 161 |
961 |
try { |
| 162 |
961 |
documentBridge = this.documentAccessBridge.getDocument(includedReference); |
| 163 |
|
} catch (Exception e) { |
| 164 |
0 |
throw new MacroExecutionException("Failed to load Document [" |
| 165 |
|
+ this.defaultEntityReferenceSerializer.serialize(includedReference) + "]", e); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
961 |
DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters(); |
| 177 |
961 |
displayParameters.setContentTransformed(parametersContext == Context.NEW); |
| 178 |
961 |
displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed()); |
| 179 |
961 |
displayParameters.setSectionId(parameters.getSection()); |
| 180 |
961 |
displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed()); |
| 181 |
961 |
displayParameters.setTransformationContextRestricted(context.getTransformationContext().isRestricted()); |
| 182 |
961 |
displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax()); |
| 183 |
|
|
| 184 |
961 |
Stack<Object> references = this.inclusionsBeingExecuted.get(); |
| 185 |
3 |
if (parametersContext == Context.NEW) { |
| 186 |
3 |
if (references == null) { |
| 187 |
3 |
references = new Stack<Object>(); |
| 188 |
3 |
this.inclusionsBeingExecuted.set(references); |
| 189 |
|
} |
| 190 |
3 |
references.push(includedReference); |
| 191 |
|
} |
| 192 |
|
|
| 193 |
961 |
XDOM result; |
| 194 |
961 |
try { |
| 195 |
961 |
result = this.documentDisplayer.display(documentBridge, displayParameters); |
| 196 |
|
} catch (Exception e) { |
| 197 |
6 |
throw new MacroExecutionException(e.getMessage(), e); |
| 198 |
|
} finally { |
| 199 |
3 |
if (parametersContext == Context.NEW) { |
| 200 |
3 |
references.pop(); |
| 201 |
|
} |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
955 |
MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData()); |
| 207 |
955 |
String source = this.defaultEntityReferenceSerializer.serialize(includedReference); |
| 208 |
955 |
metadata.getMetaData().addMetaData(MetaData.SOURCE, source); |
| 209 |
955 |
if (parametersContext == Context.NEW) { |
| 210 |
0 |
metadata.getMetaData().addMetaData(MetaData.BASE, source); |
| 211 |
|
} |
| 212 |
|
|
| 213 |
955 |
return Arrays.<Block> asList(metadata); |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
@param |
| 220 |
|
@param |
| 221 |
|
@throws |
| 222 |
|
|
| |
|
| 88.9% |
Uncovered Elements: 2 (18) |
Complexity: 6 |
Complexity Density: 0.6 |
|
| 223 |
3273 |
private void checkRecursiveInclusion(Block currrentBlock, DocumentReference documentReference)... |
| 224 |
|
throws MacroExecutionException |
| 225 |
|
{ |
| 226 |
|
|
| 227 |
3273 |
Stack<Object> references = this.inclusionsBeingExecuted.get(); |
| 228 |
3273 |
if (references != null && references.contains(documentReference)) { |
| 229 |
1 |
throw new MacroExecutionException("Found recursive inclusion of document [" + documentReference + "]"); |
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
|
| 233 |
3272 |
Block parentBlock = currrentBlock.getParent(); |
| 234 |
|
|
| 235 |
3272 |
if (parentBlock != null) { |
| 236 |
597 |
if (parentBlock instanceof MacroMarkerBlock) { |
| 237 |
597 |
MacroMarkerBlock parentMacro = (MacroMarkerBlock) parentBlock; |
| 238 |
|
|
| 239 |
597 |
if (isRecursive(parentMacro, documentReference)) { |
| 240 |
1 |
throw new MacroExecutionException("Found recursive inclusion of document [" + documentReference |
| 241 |
|
+ "]"); |
| 242 |
|
} |
| 243 |
|
} |
| 244 |
|
|
| 245 |
0 |
checkRecursiveInclusion(parentBlock, documentReference); |
| 246 |
|
} |
| 247 |
|
} |
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
@param |
| 253 |
|
@param |
| 254 |
|
@return |
| 255 |
|
|
| 256 |
|
|
| |
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 257 |
597 |
private boolean isRecursive(MacroMarkerBlock parentMacro, DocumentReference documentReference)... |
| 258 |
|
{ |
| 259 |
597 |
if (parentMacro.getId().equals("include")) { |
| 260 |
215 |
String reference = parentMacro.getParameter("reference"); |
| 261 |
215 |
if (reference == null) { |
| 262 |
0 |
reference = parentMacro.getParameter("document"); |
| 263 |
|
} |
| 264 |
|
|
| 265 |
215 |
return documentReference.equals(this.macroDocumentReferenceResolver.resolve(reference, parentMacro)); |
| 266 |
|
} |
| 267 |
|
|
| 268 |
382 |
return false; |
| 269 |
|
} |
| 270 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 271 |
963 |
private DocumentReference resolve(MacroBlock block, IncludeMacroParameters parameters)... |
| 272 |
|
throws MacroExecutionException |
| 273 |
|
{ |
| 274 |
963 |
String reference = parameters.getReference(); |
| 275 |
|
|
| 276 |
963 |
if (reference == null) { |
| 277 |
0 |
throw new MacroExecutionException( |
| 278 |
|
"You must specify a 'reference' parameter pointing to the entity to include."); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
963 |
return this.macroDocumentReferenceResolver.resolve(reference, block); |
| 282 |
|
} |
| 283 |
|
} |