View Javadoc

1   /**
2    * This file Copyright (c) 2011-2013 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.templating.elements;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.context.WebContext;
39  import info.magnolia.rendering.context.RenderingContext;
40  import info.magnolia.rendering.engine.RenderException;
41  
42  import java.util.HashMap;
43  import java.util.Map;
44  import java.util.Map.Entry;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.lang.StringUtils;
50  
51  /**
52   * Abstract base class for elements that operate on a specified piece of content.
53   * 
54   * @version $Id$
55   */
56  public abstract class AbstractContentTemplatingElement extends AbstractTemplatingElement {
57  
58      // TODO should also support a JSP ContentMap
59      private Node content;
60      private String workspace;
61      private String nodeIdentifier;
62      private String path;
63      private final Map<String, Object> savedCtxAttributes = new HashMap<String, Object>();
64  
65      public AbstractContentTemplatingElement(ServerConfiguration server, RenderingContext renderingContext) {
66          super(server, renderingContext);
67      }
68  
69      protected String getNodePath(Node node) throws RenderException {
70          try {
71              return node.getSession().getWorkspace().getName() + ":" + node.getPath();
72          } catch (RepositoryException e) {
73              throw new RenderException("Can't construct node path for node " + node);
74          }
75      }
76  
77      /**
78       * Returns the content passed to the element (content or workspace/path attribute) or null if nothing was passed.
79       */
80      protected Node getPassedContent() throws RenderException {
81  
82          // TODO should we default workspace to 'website' ?
83          // TODO should we be strict and fail on invalid combinations ?
84  
85          // TODO we can safely keep the node around after we've resolved it
86  
87          if (content != null) {
88              return content;
89          }
90          if (StringUtils.isNotEmpty(workspace)) {
91              if (StringUtils.isNotEmpty(nodeIdentifier)) {
92                  try {
93                      return MgnlContext.getJCRSession(workspace).getNodeByIdentifier(nodeIdentifier);
94                  } catch (RepositoryException e) {
95                      throw new RenderException("Can't read content from workspace [" + workspace + "] with identifier [" + nodeIdentifier + "].");
96                  }
97              }
98              if (StringUtils.isNotEmpty(path)) {
99                  try {
100                     return MgnlContext.getJCRSession(workspace).getNode(path);
101                 } catch (RepositoryException e) {
102                     throw new RenderException("Can't read content from workspace [" + workspace + "] with path [" + path + "].");
103                 }
104             }
105             throw new IllegalArgumentException("Need to specify either uuid or path in combination with workspace");
106         }
107 
108         return null;
109     }
110 
111     public Node getContent() {
112         return content;
113     }
114 
115     public void setContent(Node node) {
116         this.content = node;
117     }
118 
119     public String getWorkspace() {
120         return workspace;
121     }
122 
123     public void setWorkspace(String workspace) {
124         this.workspace = workspace;
125     }
126 
127     public String getNodeIdentifier() {
128         return nodeIdentifier;
129     }
130 
131     public void setNodeIdentifier(String nodeIdentifier) {
132         this.nodeIdentifier = nodeIdentifier;
133     }
134 
135     public String getPath() {
136         return path;
137     }
138 
139     public void setPath(String path) {
140         this.path = path;
141     }
142 
143     /**
144      * Sets attributes in web context under the specified scope. If an attribute already exists its value will be overwritten
145      * with the new one and the old value saved for subsequent restore.
146      * 
147      * @param scope one of {@link info.magnolia.context.Context#APPLICATION_SCOPE} {@link info.magnolia.context.Context#SESSION_SCOPE} {@link info.magnolia.context.Context#LOCAL_SCOPE}.
148      */
149     protected void setAttributesInWebContext(final Map<String, Object> attributes, int scope) {
150         if (attributes == null) {
151             return;
152         }
153         switch (scope) {
154         case WebContext.APPLICATION_SCOPE:
155         case WebContext.SESSION_SCOPE:
156         case WebContext.LOCAL_SCOPE:
157             break;
158         default:
159             throw new IllegalArgumentException("Scope is not valid. Use one of the scopes defined in info.magnolia.context.WebContext");
160         }
161         final WebContext webContext = MgnlContext.getWebContext();
162         for (Entry<String, Object> entry : attributes.entrySet()) {
163             final String key = entry.getKey();
164             if (webContext.containsKey(key)) {
165                 savedCtxAttributes.put(key, webContext.get(key));
166             }
167             webContext.setAttribute(key, entry.getValue(), scope);
168         }
169     }
170 
171     /**
172      * Restores the original values of attributes in web context under the specified scope.
173      * 
174      * @param scope one of {@link info.magnolia.context.Context#APPLICATION_SCOPE} {@link info.magnolia.context.Context#SESSION_SCOPE} {@link info.magnolia.context.Context#LOCAL_SCOPE}.
175      */
176     protected void restoreAttributesInWebContext(final Map<String, Object> attributes, int scope) {
177         if (attributes == null) {
178             return;
179         }
180         switch (scope) {
181         case WebContext.APPLICATION_SCOPE:
182         case WebContext.SESSION_SCOPE:
183         case WebContext.LOCAL_SCOPE:
184             break;
185         default:
186             throw new IllegalArgumentException("Scope is not valid. Use one of the scopes defined in info.magnolia.context.WebContext");
187         }
188         final WebContext webContext = MgnlContext.getWebContext();
189 
190         for (Entry<String, Object> entry : attributes.entrySet()) {
191             final String key = entry.getKey();
192             if (webContext.containsKey(key) && savedCtxAttributes.get(key) != null) {
193                 webContext.setAttribute(key, savedCtxAttributes.get(key), scope);
194                 continue;
195             }
196             webContext.removeAttribute(key, scope);
197         }
198     }
199 
200     /**
201      * Override to set conditions for rendering of cms:comments.
202      */
203     protected boolean renderComments() {
204         return isAdmin();
205     }
206 }