1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.support

File IncludeRequestWrapper.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
27
5
1
134
79
9
0.33
5.4
5
1.8

Classes

Class Line # Actions
IncludeRequestWrapper 55 27 0% 9 3
0.92592.5%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 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  9 toggle public IncludeRequestWrapper(HttpServletRequest request, String requestUri, String contextPath, String servletPath, Object pathInfo, String queryString) {
68  9 super(request);
69  9 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  9 ServletRequest wrapper = request;
82  11 while (wrapper instanceof HttpServletRequestWrapper) {
83  2 if (wrapper instanceof IncludeRequestWrapper) {
84  2 IncludeRequestWrapper includeRequestWrapper = (IncludeRequestWrapper) wrapper;
85  2 previousChainState.add(includeRequestWrapper);
86  2 previousChainState.add(includeRequestWrapper.isEnabled());
87  2 includeRequestWrapper.setEnabled(false);
88    }
89  2 wrapper = ((HttpServletRequestWrapper) wrapper).getRequest();
90    }
91    }
92   
 
93  1 toggle public void restore() {
94  2 for (int i = 0; i < previousChainState.size(); ) {
95  1 IncludeRequestWrapper includeRequestWrapper = (IncludeRequestWrapper) previousChainState.get(i++);
96  1 includeRequestWrapper.setEnabled((Boolean) previousChainState.get(i++));
97    }
98    }
99   
 
100  2 toggle @Override
101    public RequestDispatcher getRequestDispatcher(final String path) {
102   
103  2 if (!isEnabled()) {
104  0 return super.getRequestDispatcher(path);
105    }
106   
107  2 final RequestDispatcher dispatcher = super.getRequestDispatcher(path);
108   
109  2 return new RequestDispatcher() {
110   
 
111  1 toggle @Override
112    public void forward(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
113  1 boolean previousValue = isEnabled();
114  1 setEnabled(false);
115  1 try {
116  1 dispatcher.forward(servletRequest, servletResponse);
117    } finally {
118  1 setEnabled(previousValue);
119    }
120    }
121   
 
122  1 toggle @Override
123    public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
124  1 boolean previousValue = isEnabled();
125  1 setEnabled(false);
126  1 try {
127  1 dispatcher.include(servletRequest, servletResponse);
128    } finally {
129  1 setEnabled(previousValue);
130    }
131    }
132    };
133    }
134    }