Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.renderers

File ReferenceResourceRenderer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
76% of files have more coverage

Code metrics

8
26
4
1
128
73
10
0.38
6.5
4
2.5

Classes

Class Line # Actions
ReferenceResourceRenderer 63 26 0% 10 38
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2011-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Resolves the referenced resource and triggers the rendering of the referenced resource node.
62    */
 
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 since 2.4, use {@link #ReferenceResourceRenderer(RenderingEngine, ServerConfiguration)}.
72    */
 
73  0 toggle @Deprecated
74    public ReferenceResourceRenderer() {
75  0 this(Components.getComponent(RenderingEngine.class), Components.getComponent(ServerConfiguration.class));
76    }
77   
 
78  0 toggle @Inject
79    public ReferenceResourceRenderer(RenderingEngine renderingEngine, ServerConfiguration servConf) {
80  0 this.engine = renderingEngine;
81  0 this.servConf = servConf;
82    }
83   
 
84  0 toggle @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    // Rendering the log information as comment.
112  0 try {
113  0 if (StringUtils.isNotEmpty(logMessage)) {
114    // Only render the error message on author instance
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   
 
125  0 toggle private String htmlCommentLogMessage(String logMessage) {
126  0 return "<!-- " + logMessage + " -->";
127    }
128    }