View Javadoc
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      @Deprecated
74      public ReferenceResourceRenderer() {
75          this(Components.getComponent(RenderingEngine.class), Components.getComponent(ServerConfiguration.class));
76      }
77  
78      @Inject
79      public ReferenceResourceRenderer(RenderingEngine renderingEngine, ServerConfiguration servConf) {
80          this.engine = renderingEngine;
81          this.servConf = servConf;
82      }
83  
84      @Override
85      public void render(RenderingContext ctx, Map<String, Object> contextObjects) throws RenderException {
86          String logMessage = "";
87          Node content = ctx.getCurrentContent();
88  
89          String referencedPath = PropertyUtil.getString(content, "reference");
90          if (referencedPath != null) {
91              Node referencedResource = null;
92              try {
93                  Session session = MgnlContext.getJCRSession(ResourcesModule.DEFAULT_WORKSPACE);
94                  if (session.nodeExists(referencedPath)) {
95                      referencedResource = session.getNode(referencedPath);
96                      engine.render(referencedResource, ctx.getOutputProvider());
97                  } else {
98                      logMessage = "The referenced resource '" + referencedPath + "' does not exist.";
99                      log.error(logMessage);
100 
101                 }
102             } catch (RepositoryException e) {
103                 logMessage = "Unable to render referenced resource node ." + JcrUtils.toString(referencedResource);
104                 throw new RenderException(logMessage, e);
105             }
106         } else {
107             logMessage = "Reference resource " + JcrUtils.toString(content) + " does not contain any 'reference' property value targeting another resource node.";
108             log.error(logMessage);
109         }
110 
111         // Rendering the log information as comment.
112         try {
113             if (StringUtils.isNotEmpty(logMessage)) {
114                 // Only render the error message on author instance
115                 if (servConf.isAdmin()) {
116                     AppendableWriter out = ctx.getAppendable();
117                     out.append(htmlCommentLogMessage(logMessage));
118                 }
119             }
120         } catch (IOException e) {
121             throw new RenderException(logMessage, e);
122         }
123     }
124 
125     private String htmlCommentLogMessage(String logMessage) {
126         return "<!-- " + logMessage + " -->";
127     }
128 }