View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 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 java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import javax.servlet.RequestDispatcher;
41  import javax.servlet.ServletException;
42  import javax.servlet.ServletRequest;
43  import javax.servlet.ServletResponse;
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletRequestWrapper;
46  
47  import org.springframework.web.util.WebUtils;
48  
49  /**
50   * Request wrapper that makes the request appear like an include operation. Exposes the special request attributes that
51   * needs to be present during an include.
52   *
53   * @since 1.2
54   */
55  public class IncludeRequestWrapper extends SpecialAttributeRequestWrapper {
56  
57      private static final String INCLUDE_ATTRIBUTES_COMMON_PREFIX = "javax.servlet.include.";
58      private static final String[] INCLUDE_ATTRIBUTE_NAMES = new String[]{
59              WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE,
60              WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE,
61              WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE,
62              WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE,
63              WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE};
64  
65      private final List<Object> previousChainState = new ArrayList<Object>(48);
66  
67      public IncludeRequestWrapper(HttpServletRequest request, String requestUri, String contextPath, String servletPath, Object pathInfo, String queryString) {
68          super(request);
69          setOverriddenAttributes(
70                  INCLUDE_ATTRIBUTES_COMMON_PREFIX,
71                  INCLUDE_ATTRIBUTE_NAMES,
72                  new Object[]{
73                          requestUri,
74                          contextPath,
75                          servletPath,
76                          pathInfo,
77                          queryString}
78          );
79  
80          // Disable all IncludeRequestWrappers already in chain to increase performance, they won't have any effect anyway
81          ServletRequest wrapper = request;
82          while (wrapper instanceof HttpServletRequestWrapper) {
83              if (wrapper instanceof IncludeRequestWrapper) {
84                  IncludeRequestWrapper includeRequestWrapper = (IncludeRequestWrapper) wrapper;
85                  previousChainState.add(includeRequestWrapper);
86                  previousChainState.add(includeRequestWrapper.isEnabled());
87                  includeRequestWrapper.setEnabled(false);
88              }
89              wrapper = ((HttpServletRequestWrapper) wrapper).getRequest();
90          }
91      }
92  
93      public void restore() {
94          for (int i = 0; i < previousChainState.size(); ) {
95              IncludeRequestWrapper includeRequestWrapper = (IncludeRequestWrapper) previousChainState.get(i++);
96              includeRequestWrapper.setEnabled((Boolean) previousChainState.get(i++));
97          }
98      }
99  
100     @Override
101     public RequestDispatcher getRequestDispatcher(final String path) {
102 
103         if (!isEnabled()) {
104             return super.getRequestDispatcher(path);
105         }
106 
107         final RequestDispatcher dispatcher = super.getRequestDispatcher(path);
108 
109         return new RequestDispatcher() {
110 
111             @Override
112             public void forward(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
113                 boolean previousValue = isEnabled();
114                 setEnabled(false);
115                 try {
116                     dispatcher.forward(servletRequest, servletResponse);
117                 } finally {
118                     setEnabled(previousValue);
119                 }
120             }
121 
122             @Override
123             public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
124                 boolean previousValue = isEnabled();
125                 setEnabled(false);
126                 try {
127                     dispatcher.include(servletRequest, servletResponse);
128                 } finally {
129                     setEnabled(previousValue);
130                 }
131             }
132         };
133     }
134 }