View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.context;
35  
36  import info.magnolia.cms.util.ServletUtil;
37  
38  import java.io.Serializable;
39  import java.util.Enumeration;
40  import java.util.HashMap;
41  import java.util.Map;
42  
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpSession;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * An AttributeStrategy which delegates to the given WebContext's request.
51   */
52  public class RequestAttributeStrategy implements AttributeStrategy {
53      private static final Logger log = LoggerFactory.getLogger(RequestAttributeStrategy.class);
54  
55      private final WebContext ctx;
56  
57      public RequestAttributeStrategy(WebContext ctx) {
58          this.ctx = ctx;
59      }
60  
61      public HttpServletRequest getRequest() {
62          return ctx.getRequest();
63      }
64  
65      @Override
66      public Object getAttribute(String name, int scope) {
67          switch (scope) {
68          case Context.LOCAL_SCOPE:
69              Object obj = getRequest().getAttribute(name);
70              if (obj == null) {
71                  obj = getRequest().getParameter(name);
72              }
73              if (obj == null) {
74                  // we also expose some of the request properties as attributes
75                  if (WebContext.ATTRIBUTE_REQUEST_CHARACTER_ENCODING.equals(name)) {
76                      obj = getRequest().getCharacterEncoding();
77                  } else if (WebContext.ATTRIBUTE_REQUEST_URI.equals(name)) {
78                      obj = ServletUtil.stripPathParameters(getRequest().getRequestURI());
79                  }
80              }
81              return obj;
82          case Context.SESSION_SCOPE:
83              HttpSession httpsession = getRequest().getSession(false);
84              if (httpsession == null) {
85                  return null;
86              }
87              return httpsession.getAttribute(name);
88          case Context.APPLICATION_SCOPE:
89              return MgnlContext.getSystemContext().getAttribute(name, Context.APPLICATION_SCOPE);
90          default:
91              log.error("illegal scope passed");
92              return null;
93          }
94      }
95  
96      @Override
97      public Map getAttributes(int scope) {
98          Map map = new HashMap();
99          Enumeration keysEnum;
100         switch (scope) {
101         case Context.LOCAL_SCOPE:
102             // add parameters
103             Enumeration paramEnum = getRequest().getParameterNames();
104             while (paramEnum.hasMoreElements()) {
105                 final String name = (String) paramEnum.nextElement();
106                 map.put(name, getRequest().getParameter(name));
107             }
108             // attributes have higher priority
109             keysEnum = getRequest().getAttributeNames();
110             while (keysEnum.hasMoreElements()) {
111                 String key = (String) keysEnum.nextElement();
112                 Object value = getAttribute(key, scope);
113                 map.put(key, value);
114             }
115             return map;
116         case Context.SESSION_SCOPE:
117             HttpSession httpsession = getRequest().getSession(false);
118             if (httpsession == null) {
119                 return map;
120             }
121             keysEnum = httpsession.getAttributeNames();
122             while (keysEnum.hasMoreElements()) {
123                 String key = (String) keysEnum.nextElement();
124                 Object value = getAttribute(key, scope);
125                 map.put(key, value);
126             }
127             return map;
128         case Context.APPLICATION_SCOPE:
129             return MgnlContext.getSystemContext().getAttributes(Context.APPLICATION_SCOPE);
130         default:
131             log.error("no illegal scope passed");
132             return map;
133         }
134     }
135 
136     @Override
137     public void removeAttribute(String name, int scope) {
138         switch (scope) {
139         case Context.LOCAL_SCOPE:
140             getRequest().removeAttribute(name);
141             break;
142         case Context.SESSION_SCOPE:
143             HttpSession httpsession = getRequest().getSession(false);
144             if (httpsession != null) {
145                 httpsession.removeAttribute(name);
146             }
147             break;
148         case Context.APPLICATION_SCOPE:
149             MgnlContext.getSystemContext().removeAttribute(name, Context.APPLICATION_SCOPE);
150             break;
151         default:
152             log.error("no illegal scope passed");
153         }
154     }
155 
156     @Override
157     public void setAttribute(String name, Object value, int scope) {
158         if (value == null) {
159             removeAttribute(name, scope);
160             return;
161         }
162 
163         switch (scope) {
164         case Context.LOCAL_SCOPE:
165             getRequest().setAttribute(name, value);
166             break;
167         case Context.SESSION_SCOPE:
168             if (!(value instanceof Serializable)) {
169                 log.warn("Trying to store a non-serializable attribute in session: {}. Object type is {}", name, value.getClass().getName(), new Throwable("This stacktrace has been added to provide debugging information"));
170                 return;
171             }
172 
173             HttpSession httpsession = getRequest().getSession(false);
174             if (httpsession == null) {
175                 log.debug("Session initialized in order to set attribute '{}' to '{}'. You should avoid using session when possible!", name, value);
176                 httpsession = getRequest().getSession(true);
177             }
178 
179             httpsession.setAttribute(name, value);
180             break;
181         case Context.APPLICATION_SCOPE:
182             MgnlContext.getSystemContext().setAttribute(name, value, Context.APPLICATION_SCOPE);
183             break;
184         default:
185             getRequest().setAttribute(name, value);
186             log.debug("Undefined scope, setting attribute [{}] in request scope", name);
187         }
188     }
189 
190 }