View Javadoc

1   /**
2    * This file Copyright (c) 2010-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.module.blossom.support;
35  
36  import javax.servlet.http.HttpServletRequest;
37  
38  import org.apache.commons.lang.StringUtils;
39  import org.springframework.web.util.WebUtils;
40  
41  /**
42   * Request wrapper that makes the request appear like a forward operation. Also exposes the special attributes for a
43   * forward request if they are not already set.
44   *
45   * @since 1.2
46   */
47  public class ForwardRequestWrapper extends SpecialAttributeRequestWrapper {
48  
49      private final String requestUri;
50      private final String originalRequestUri;
51      private final String contextPath;
52      private final String originalContextPath;
53      private final String servletPath;
54      private final String originalServletPath;
55      private final String pathInfo;
56      private final String originalPathInfo;
57  
58      public ForwardRequestWrapper(HttpServletRequest request, String requestUri, String contextPath, String servletPath, String pathInfo) {
59          super(request);
60          this.requestUri = requestUri;
61          this.originalRequestUri = request.getRequestURI();
62          this.servletPath = servletPath;
63          this.originalServletPath = request.getServletPath();
64          this.contextPath = contextPath;
65          this.originalContextPath = request.getContextPath();
66          this.pathInfo = pathInfo;
67          this.originalPathInfo = request.getPathInfo();
68          if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) == null) {
69              setSpecialAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, request.getRequestURI());
70              setSpecialAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE, request.getContextPath());
71              setSpecialAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE, request.getServletPath());
72              setSpecialAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE, request.getPathInfo());
73              setSpecialAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, request.getQueryString());
74          }
75      }
76  
77      @Override
78      public String getRequestURI() {
79          String currentRequestUri = super.getRequestURI();
80          if (!StringUtils.equals(currentRequestUri, originalRequestUri)) {
81              return currentRequestUri;
82          }
83          if (!StringUtils.equals(super.getServletPath(), originalServletPath)) {
84              return currentRequestUri;
85          }
86          if (!StringUtils.equals(super.getPathInfo(), originalPathInfo)) {
87              return currentRequestUri;
88          }
89          if (!StringUtils.equals(super.getContextPath(), originalContextPath)) {
90              return currentRequestUri;
91          }
92          return requestUri;
93      }
94  
95      @Override
96      public String getContextPath() {
97          String currentContextPath = super.getContextPath();
98          if (!StringUtils.equals(super.getRequestURI(), originalRequestUri)) {
99              return currentContextPath;
100         }
101         if (!StringUtils.equals(super.getServletPath(), originalServletPath)) {
102             return currentContextPath;
103         }
104         if (!StringUtils.equals(super.getPathInfo(), originalPathInfo)) {
105             return currentContextPath;
106         }
107         if (!StringUtils.equals(currentContextPath, originalContextPath)) {
108             return currentContextPath;
109         }
110         return contextPath;
111     }
112 
113     @Override
114     public String getServletPath() {
115         String currentServletPath = super.getServletPath();
116         if (!StringUtils.equals(super.getRequestURI(), originalRequestUri)) {
117             return currentServletPath;
118         }
119         if (!StringUtils.equals(currentServletPath, originalServletPath)) {
120             return currentServletPath;
121         }
122         if (!StringUtils.equals(super.getPathInfo(), originalPathInfo)) {
123             return currentServletPath;
124         }
125         if (!StringUtils.equals(super.getContextPath(), originalContextPath)) {
126             return currentServletPath;
127         }
128         return servletPath;
129     }
130 
131     @Override
132     public String getPathInfo() {
133         String superPathInfo = super.getPathInfo();
134         if (!StringUtils.equals(super.getRequestURI(), originalRequestUri)) {
135             return superPathInfo;
136         }
137         if (!StringUtils.equals(super.getServletPath(), originalServletPath)) {
138             return superPathInfo;
139         }
140         if (!StringUtils.equals(superPathInfo, originalPathInfo)) {
141             return superPathInfo;
142         }
143         if (!StringUtils.equals(super.getContextPath(), originalContextPath)) {
144             return superPathInfo;
145         }
146         return pathInfo;
147     }
148 }