View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 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.util.Enumeration;
37  import java.util.NoSuchElementException;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletRequestWrapper;
40  
41  import org.apache.commons.lang.ObjectUtils;
42  import org.springframework.web.util.WebUtils;
43  
44  /**
45   * Request wrapper for overriding special attributes.
46   *
47   * @since 1.2
48   */
49  public class SpecialAttributeRequestWrapper extends HttpServletRequestWrapper {
50  
51      private static final String[] SPECIAL_ATTRIBUTE_NAMES = new String[]{
52              WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE,
53              WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE,
54              WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE,
55              WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE,
56              WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE,
57              WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE,
58              WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE,
59              WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE,
60              WebUtils.FORWARD_PATH_INFO_ATTRIBUTE,
61              WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE /*,
62              WebUtils.ERROR_STATUS_CODE_ATTRIBUTE,
63              WebUtils.ERROR_EXCEPTION_TYPE_ATTRIBUTE,
64              WebUtils.ERROR_MESSAGE_ATTRIBUTE,
65              WebUtils.ERROR_EXCEPTION_ATTRIBUTE,
66              WebUtils.ERROR_REQUEST_URI_ATTRIBUTE,
67              WebUtils.ERROR_SERVLET_NAME_ATTRIBUTE*/
68      };
69  
70      private final Object[] localValues = new Object[SPECIAL_ATTRIBUTE_NAMES.length];
71      private final Object[] originalValues = new Object[SPECIAL_ATTRIBUTE_NAMES.length];
72      private boolean enabled = true;
73  
74      public SpecialAttributeRequestWrapper(HttpServletRequest request) {
75          super(request);
76      }
77  
78      public boolean isEnabled() {
79          return enabled;
80      }
81  
82      public void setEnabled(boolean enabled) {
83          this.enabled = enabled;
84      }
85  
86      public boolean setSpecialAttribute(String name, Object value) {
87          for (int i = 0; i < SPECIAL_ATTRIBUTE_NAMES.length; i++) {
88              if (name.equals(SPECIAL_ATTRIBUTE_NAMES[i])) {
89                  localValues[i] = value;
90                  originalValues[i] = super.getAttribute(name);
91                  return true;
92              }
93          }
94          return false;
95      }
96  
97      @Override
98      public Object getAttribute(String name) {
99          if (enabled) {
100             for (int i = 0; i < SPECIAL_ATTRIBUTE_NAMES.length; i++) {
101                 if (localValues[i] != null && SPECIAL_ATTRIBUTE_NAMES[i].equals(name)) {
102                     if (shouldMasquerade(name)) {
103                         return localValues[i];
104                     } else {
105                         break;
106                     }
107                 }
108             }
109         }
110         return super.getAttribute(name);
111     }
112 
113     protected boolean shouldMasquerade(String name) {
114         for (int i = 0; i < SPECIAL_ATTRIBUTE_NAMES.length; i++) {
115             if (localValues[i] != null && !ObjectUtils.equals(super.getAttribute(SPECIAL_ATTRIBUTE_NAMES[i]), originalValues[i])) {
116                 return false;
117             }
118         }
119         return true;
120     }
121 
122     @Override
123     public Enumeration getAttributeNames() {
124 
125         return new Enumeration() {
126 
127             private int pos = 0;
128             private String next = null;
129             private Enumeration parentEnumeration = getRequest().getAttributeNames();
130 
131             @Override
132             public boolean hasMoreElements() {
133                 if (enabled) {
134                     while (pos < localValues.length) {
135                         if (localValues[pos] != null) {
136                             return true;
137                         }
138                         pos++;
139                     }
140                 }
141                 if (next != null) {
142                     return true;
143                 }
144                 next = findNext();
145                 return next != null;
146             }
147 
148             @Override
149             public Object nextElement() {
150                 if (enabled) {
151                     while (pos < localValues.length) {
152                         if (localValues[pos] != null) {
153                             String name = SPECIAL_ATTRIBUTE_NAMES[pos];
154                             pos++;
155                             return name;
156                         }
157                         pos++;
158                     }
159                 }
160                 if (next == null) {
161                     next = findNext();
162                     if (next == null) {
163                         throw new NoSuchElementException();
164                     }
165                 }
166                 Object result = next;
167                 next = null;
168                 return result;
169             }
170 
171             private String findNext() {
172                 while (parentEnumeration.hasMoreElements()) {
173                     String name = (String) parentEnumeration.nextElement();
174                     if (!enabled || !hasLocalValue(name)) {
175                         return name;
176                     }
177                 }
178                 return null;
179             }
180         };
181     }
182 
183     @Override
184     public void setAttribute(String name, Object o) {
185         if (!hasLocalValue(name)) {
186             super.setAttribute(name, o);
187         }
188     }
189 
190     @Override
191     public void removeAttribute(String name) {
192         if (!hasLocalValue(name)) {
193             super.removeAttribute(name);
194         }
195     }
196 
197     private boolean hasLocalValue(String name) {
198         for (int i = 0; i < SPECIAL_ATTRIBUTE_NAMES.length; i++) {
199             if (localValues[i] != null && name.equals(SPECIAL_ATTRIBUTE_NAMES[i])) {
200                 return true;
201             }
202         }
203         return false;
204     }
205 }