View Javadoc

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