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.display; |
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.MetaDataBlock; |
41 |
|
import org.xwiki.rendering.block.XDOM; |
42 |
|
import org.xwiki.rendering.listener.MetaData; |
43 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
44 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
45 |
|
import org.xwiki.rendering.macro.display.DisplayMacroParameters; |
46 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
|
53 |
|
@Component |
54 |
|
@Named("display") |
55 |
|
@Singleton |
|
|
| 78% |
Uncovered Elements: 13 (59) |
Complexity: 14 |
Complexity Density: 0.33 |
|
56 |
|
public class DisplayMacro extends AbstractMacro<DisplayMacroParameters> |
57 |
|
{ |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private static final String DESCRIPTION = "Display other pages into the current page."; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@Inject |
67 |
|
private DocumentAccessBridge documentAccessBridge; |
68 |
|
|
69 |
|
|
70 |
|
@link |
71 |
|
|
72 |
|
@Inject |
73 |
|
@Named("macro") |
74 |
|
private DocumentReferenceResolver<String> macroDocumentReferenceResolver; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@Inject |
81 |
|
private EntityReferenceSerializer<String> defaultEntityReferenceSerializer; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@Inject |
87 |
|
@Named("configured") |
88 |
|
private DocumentDisplayer documentDisplayer; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
private ThreadLocal<Stack<Object>> displaysBeingExecuted = new ThreadLocal<Stack<Object>>(); |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
98 |
7 |
public DisplayMacro()... |
99 |
|
{ |
100 |
7 |
super("Display", DESCRIPTION, DisplayMacroParameters.class); |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
7 |
setPriority(10); |
105 |
7 |
setDefaultCategory(DEFAULT_CATEGORY_CONTENT); |
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
0 |
@Override... |
109 |
|
public boolean supportsInlineMode() |
110 |
|
{ |
111 |
0 |
return true; |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
@param |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
3 |
public void setDocumentAccessBridge(DocumentAccessBridge documentAccessBridge)... |
120 |
|
{ |
121 |
3 |
this.documentAccessBridge = documentAccessBridge; |
122 |
|
} |
123 |
|
|
|
|
| 75.7% |
Uncovered Elements: 9 (37) |
Complexity: 6 |
Complexity Density: 0.19 |
|
124 |
7 |
@Override... |
125 |
|
public List<Block> execute(DisplayMacroParameters parameters, String content, MacroTransformationContext context) |
126 |
|
throws MacroExecutionException |
127 |
|
{ |
128 |
|
|
129 |
7 |
if (parameters.getReference() == null) { |
130 |
1 |
throw new MacroExecutionException( |
131 |
|
"You must specify a 'reference' parameter pointing to the entity to display."); |
132 |
|
} |
133 |
|
|
134 |
6 |
DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters); |
135 |
|
|
136 |
6 |
checkRecursiveDisplay(context.getCurrentMacroBlock(), includedReference); |
137 |
|
|
138 |
5 |
if (!this.documentAccessBridge.isDocumentViewable(includedReference)) { |
139 |
0 |
throw new MacroExecutionException("Current user [" + this.documentAccessBridge.getCurrentUserReference() |
140 |
|
+ "] doesn't have view rights on document [" |
141 |
|
+ this.defaultEntityReferenceSerializer.serialize(includedReference) + "]"); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
5 |
DocumentModelBridge documentBridge; |
146 |
5 |
try { |
147 |
5 |
documentBridge = this.documentAccessBridge.getDocument(includedReference); |
148 |
|
} catch (Exception e) { |
149 |
0 |
throw new MacroExecutionException( |
150 |
|
"Failed to load Document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]", |
151 |
|
e); |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
5 |
DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters(); |
157 |
5 |
displayParameters.setContentTransformed(true); |
158 |
5 |
displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed()); |
159 |
5 |
displayParameters.setSectionId(parameters.getSection()); |
160 |
5 |
displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed()); |
161 |
5 |
displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax()); |
162 |
|
|
163 |
5 |
Stack<Object> references = this.displaysBeingExecuted.get(); |
164 |
5 |
if (references == null) { |
165 |
5 |
references = new Stack<Object>(); |
166 |
5 |
this.displaysBeingExecuted.set(references); |
167 |
|
} |
168 |
5 |
references.push(includedReference); |
169 |
|
|
170 |
5 |
XDOM result; |
171 |
5 |
try { |
172 |
5 |
result = this.documentDisplayer.display(documentBridge, displayParameters); |
173 |
|
} catch (Exception e) { |
174 |
5 |
throw new MacroExecutionException(e.getMessage(), e); |
175 |
|
} finally { |
176 |
5 |
references.pop(); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
0 |
MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData()); |
182 |
0 |
String source = this.defaultEntityReferenceSerializer.serialize(includedReference); |
183 |
0 |
metadata.getMetaData().addMetaData(MetaData.SOURCE, source); |
184 |
0 |
metadata.getMetaData().addMetaData(MetaData.BASE, source); |
185 |
|
|
186 |
0 |
return Arrays.<Block>asList(metadata); |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
@param |
193 |
|
@param |
194 |
|
@throws |
195 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
196 |
6 |
private void checkRecursiveDisplay(Block currrentBlock, DocumentReference documentReference)... |
197 |
|
throws MacroExecutionException |
198 |
|
{ |
199 |
|
|
200 |
6 |
Stack<Object> references = this.displaysBeingExecuted.get(); |
201 |
6 |
if (references != null && references.contains(documentReference)) { |
202 |
1 |
throw new MacroExecutionException("Found recursive display of document [" + documentReference + "]"); |
203 |
|
} |
204 |
|
} |
205 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
206 |
6 |
private DocumentReference resolve(MacroBlock block, DisplayMacroParameters parameters)... |
207 |
|
throws MacroExecutionException |
208 |
|
{ |
209 |
6 |
String reference = parameters.getReference(); |
210 |
|
|
211 |
6 |
if (reference == null) { |
212 |
0 |
throw new MacroExecutionException( |
213 |
|
"You must specify a 'reference' parameter pointing to the entity to include."); |
214 |
|
} |
215 |
|
|
216 |
6 |
return this.macroDocumentReferenceResolver.resolve(reference, block); |
217 |
|
} |
218 |
|
} |