View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.beans.runtime.MultipartForm;
37  import info.magnolia.cms.core.AggregationState;
38  import info.magnolia.cms.security.Security;
39  import info.magnolia.objectfactory.Components;
40  
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import javax.servlet.ServletContext;
45  import javax.servlet.ServletException;
46  import javax.servlet.ServletRequest;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  import javax.servlet.http.HttpSession;
50  import javax.servlet.jsp.PageContext;
51  import java.io.IOException;
52  import java.io.Writer;
53  import java.util.Enumeration;
54  import java.util.HashMap;
55  import java.util.Map;
56  import java.util.Stack;
57  
58  
59  /**
60   * @author Sameer Charles
61   * @version $Id: WebContextImpl.java 34573 2010-05-20 07:49:54Z had $
62   */
63  public class WebContextImpl extends UserContextImpl implements WebContext {
64      private static final Logger log = LoggerFactory.getLogger(WebContextImpl.class);
65  
66      private static final long serialVersionUID = 222L;
67  
68      private HttpServletRequest request;
69  
70      private HttpServletResponse response;
71  
72      private ServletContext servletContext;
73  
74      /**
75       * the jsp page context.
76       */
77      private PageContext pageContext;
78  
79      protected AggregationState aggregationState;
80  
81      private Stack<HttpServletResponse> responseStack = new Stack<HttpServletResponse>();
82  
83      private Stack<HttpServletRequest> requestStack = new Stack<HttpServletRequest>();
84  
85      /**
86       * Use init to initialize the object.
87       */
88      public WebContextImpl() {
89      }
90  
91      public void init(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) {
92          this.request = request;
93          this.response = response;
94          this.servletContext = servletContext;
95          //reset();
96          //setUser(getAnonymousUser());
97          setAttributeStrategy(new RequestAttributeStrategy(this));
98          setRepositoryStrategy(new DefaultRepositoryStrategy(this));
99      }
100 
101     public AggregationState getAggregationState() {
102         if (aggregationState == null) {
103             aggregationState = Components.getComponentProvider().newInstance(AggregationState.class);
104         }
105         return aggregationState;
106     }
107 
108     /**
109      * This will only reset the original URI/URL by calling {@link AggregationState#resetURIs()}
110      */
111     public void resetAggregationState() {
112         getAggregationState().resetURIs();
113     }
114 
115     /**
116      * Get form object assembled by <code>MultipartRequestFilter</code>
117      * @return multipart form object
118      */
119     public MultipartForm getPostedForm() {
120         return (MultipartForm) getAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, LOCAL_SCOPE);
121     }
122 
123     /**
124      * Get parameter value as string.
125      * @return parameter value
126      */
127     public String getParameter(String name) {
128         return this.request.getParameter(name);
129     }
130 
131     /**
132      * Get parameter values as string[].
133      * @return parameter values
134      */
135     public String[] getParameterValues(String name) {
136         return this.request.getParameterValues(name);
137     }
138 
139     /**
140      * Get parameter values as a Map<String, String> (unlike HttpServletRequest.getParameterMap() which returns a Map<String,
141      * String[]>, so don't expect to retrieve multiple-valued form parameters here)
142      * @return parameter values
143      */
144     public Map<String, String> getParameters() {
145         Map<String, String> map = new HashMap<String, String>();
146         Enumeration<String> paramEnum = this.request.getParameterNames();
147         while (paramEnum.hasMoreElements()) {
148             final String name = paramEnum.nextElement();
149             map.put(name, this.request.getParameter(name));
150         }
151         return map;
152     }
153 
154     /**
155      * Avoid the call to this method where ever possible.
156      * @return Returns the request.
157      */
158     public HttpServletRequest getRequest() {
159         return this.request;
160     }
161 
162     public HttpServletResponse getResponse() {
163         return response;
164     }
165 
166     public String getContextPath() {
167         return this.request.getContextPath();
168     }
169 
170     /**
171      * Does an include using the request that was set when setting up this context, or using the
172      * request wrapped by the pageContext if existing.
173      */
174     public void include(final String path, final Writer out) throws ServletException, IOException {
175         try {
176             final ServletRequest requestToUse = /*pageContext != null ? pageContext.getRequest() :*/ this.getRequest();
177             final HttpServletResponse responseToUse = /*(pageContext != null && pageContext.getResponse() instanceof HttpServletResponse) ? (HttpServletResponse) pageContext.getResponse() :*/ response;
178             final WriterResponseWrapper wrappedResponse = new WriterResponseWrapper(responseToUse, out);
179 
180             requestToUse.getRequestDispatcher(path).include(requestToUse, wrappedResponse);
181         }
182         catch (ServletException e) {
183             throw new RuntimeException(e);
184         }
185         catch (IOException e) {
186             throw new RuntimeException(e);
187         }
188     }
189 
190     /**
191      * {@inheritDoc}
192      */
193     public PageContext getPageContext() {
194         return pageContext;
195     }
196 
197     /**
198      * {@inheritDoc}
199      */
200     public void setPageContext(PageContext pageContext) {
201         this.pageContext = pageContext;
202     }
203 
204     /**
205      * {@inheritDoc}
206      */
207     public ServletContext getServletContext() {
208         return servletContext;
209     }
210 
211     public void login() {
212         setRepositoryStrategy(new DefaultRepositoryStrategy(this));
213     }
214 
215     /**
216      * Closes opened JCR sessions and invalidates the current HttpSession.
217      * @see #release()
218      */
219     public void logout() {
220         releaseJCRSessions();
221 
222         HttpSession session = this.request.getSession(false);
223         if (session != null) {
224             session.invalidate();
225         }
226         login(Security.getAnonymousUser());
227     }
228 
229     /**
230      * Closes opened JCR sessions.
231      */
232     public void release() {
233         releaseJCRSessions();
234         this.request = null;
235         this.response = null;
236     }
237 
238     protected void releaseJCRSessions() {
239         getRepositoryStrategy().release();
240     }
241 
242     /* (non-Javadoc)
243      * @see info.magnolia.context.WebContext#pop()
244      */
245     public void pop() {
246         request = requestStack.pop();
247         response = responseStack.pop();
248     }
249 
250     /* (non-Javadoc)
251      * @see info.magnolia.context.WebContext#push(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
252      */
253     public void push(HttpServletRequest request, HttpServletResponse response) {
254         requestStack.push(this.request);
255         this.request = request;
256         responseStack.push(this.response);
257         this.response = response;
258     }
259 }