1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.resources.renderers; |
35 |
|
|
36 |
|
import info.magnolia.cms.beans.config.ServerConfiguration; |
37 |
|
import info.magnolia.context.MgnlContext; |
38 |
|
import info.magnolia.jcr.util.PropertyUtil; |
39 |
|
import info.magnolia.module.resources.ResourcesModule; |
40 |
|
import info.magnolia.objectfactory.Components; |
41 |
|
import info.magnolia.rendering.context.RenderingContext; |
42 |
|
import info.magnolia.rendering.engine.RenderException; |
43 |
|
import info.magnolia.rendering.engine.RenderingEngine; |
44 |
|
import info.magnolia.rendering.renderer.Renderer; |
45 |
|
import info.magnolia.rendering.util.AppendableWriter; |
46 |
|
|
47 |
|
import java.io.IOException; |
48 |
|
import java.util.Map; |
49 |
|
|
50 |
|
import javax.inject.Inject; |
51 |
|
import javax.jcr.Node; |
52 |
|
import javax.jcr.RepositoryException; |
53 |
|
import javax.jcr.Session; |
54 |
|
|
55 |
|
import org.apache.commons.lang3.StringUtils; |
56 |
|
import org.apache.jackrabbit.commons.JcrUtils; |
57 |
|
import org.slf4j.Logger; |
58 |
|
import org.slf4j.LoggerFactory; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
|
|
| 0% |
Uncovered Elements: 38 (38) |
Complexity: 10 |
Complexity Density: 0.38 |
|
63 |
|
public class ReferenceResourceRenderer implements Renderer { |
64 |
|
|
65 |
|
private static final Logger log = LoggerFactory.getLogger(ReferenceResourceRenderer.class); |
66 |
|
|
67 |
|
private final RenderingEngine engine; |
68 |
|
private final ServerConfiguration servConf; |
69 |
|
|
70 |
|
|
71 |
|
@deprecated@link |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
0 |
@Deprecated... |
74 |
|
public ReferenceResourceRenderer() { |
75 |
0 |
this(Components.getComponent(RenderingEngine.class), Components.getComponent(ServerConfiguration.class)); |
76 |
|
} |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
78 |
0 |
@Inject... |
79 |
|
public ReferenceResourceRenderer(RenderingEngine renderingEngine, ServerConfiguration servConf) { |
80 |
0 |
this.engine = renderingEngine; |
81 |
0 |
this.servConf = servConf; |
82 |
|
} |
83 |
|
|
|
|
| 0% |
Uncovered Elements: 30 (30) |
Complexity: 7 |
Complexity Density: 0.32 |
|
84 |
0 |
@Override... |
85 |
|
public void render(RenderingContext ctx, Map<String, Object> contextObjects) throws RenderException { |
86 |
0 |
String logMessage = ""; |
87 |
0 |
Node content = ctx.getCurrentContent(); |
88 |
|
|
89 |
0 |
String referencedPath = PropertyUtil.getString(content, "reference"); |
90 |
0 |
if (referencedPath != null) { |
91 |
0 |
Node referencedResource = null; |
92 |
0 |
try { |
93 |
0 |
Session session = MgnlContext.getJCRSession(ResourcesModule.DEFAULT_WORKSPACE); |
94 |
0 |
if (session.nodeExists(referencedPath)) { |
95 |
0 |
referencedResource = session.getNode(referencedPath); |
96 |
0 |
engine.render(referencedResource, ctx.getOutputProvider()); |
97 |
|
} else { |
98 |
0 |
logMessage = "The referenced resource '" + referencedPath + "' does not exist."; |
99 |
0 |
log.error(logMessage); |
100 |
|
|
101 |
|
} |
102 |
|
} catch (RepositoryException e) { |
103 |
0 |
logMessage = "Unable to render referenced resource node ." + JcrUtils.toString(referencedResource); |
104 |
0 |
throw new RenderException(logMessage, e); |
105 |
|
} |
106 |
|
} else { |
107 |
0 |
logMessage = "Reference resource " + JcrUtils.toString(content) + " does not contain any 'reference' property value targeting another resource node."; |
108 |
0 |
log.error(logMessage); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
0 |
try { |
113 |
0 |
if (StringUtils.isNotEmpty(logMessage)) { |
114 |
|
|
115 |
0 |
if (servConf.isAdmin()) { |
116 |
0 |
AppendableWriter out = ctx.getAppendable(); |
117 |
0 |
out.append(htmlCommentLogMessage(logMessage)); |
118 |
|
} |
119 |
|
} |
120 |
|
} catch (IOException e) { |
121 |
0 |
throw new RenderException(logMessage, e); |
122 |
|
} |
123 |
|
} |
124 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
0 |
private String htmlCommentLogMessage(String logMessage) {... |
126 |
0 |
return "<!-- " + logMessage + " -->"; |
127 |
|
} |
128 |
|
} |