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.util.Enumeration;
37  import java.util.NoSuchElementException;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletRequestWrapper;
41  
42  /**
43   * Request wrapper for overriding special attributes.
44   *
45   * @since 1.2
46   */
47  public class SpecialAttributeRequestWrapper extends HttpServletRequestWrapper {
48  
49      private String commonPrefix;
50      private String[] names;
51      private Object[] values;
52      private boolean enabled = true;
53  
54      public SpecialAttributeRequestWrapper(HttpServletRequest request) {
55          super(request);
56      }
57  
58      public boolean isEnabled() {
59          return enabled;
60      }
61  
62      public void setEnabled(boolean enabled) {
63          this.enabled = enabled;
64      }
65  
66      protected void setOverriddenAttributes(String commonPrefix, String[] names, Object[] values) {
67          this.commonPrefix = commonPrefix;
68          this.names = names;
69          this.values = values;
70      }
71  
72      @Override
73      public Object getAttribute(String name) {
74          if (enabled && name.startsWith(commonPrefix)) {
75              for (int i = 0; i < names.length; i++) {
76                  if (names[i].equals(name)) {
77                      return values[i];
78                  }
79              }
80          }
81          return super.getAttribute(name);
82      }
83  
84      public Enumeration getAttributeNames() {
85  
86          if (!enabled) {
87              return super.getAttributeNames();
88          }
89  
90          return new Enumeration() {
91  
92              private int pos = 0;
93              private String next = null;
94              private Enumeration parentEnumeration = SpecialAttributeRequestWrapper.super.getAttributeNames();
95  
96              @Override
97              public boolean hasMoreElements() {
98                  if (next != null) {
99                      return true;
100                 }
101                 while (pos < names.length) {
102                     if (values[pos] != null) {
103                         next = names[pos++];
104                         return true;
105                     }
106                     pos++;
107                 }
108                 while (parentEnumeration.hasMoreElements()) {
109                     String name = (String) parentEnumeration.nextElement();
110                     if (!isAttributeOverridden(name)) {
111                         next = name;
112                         return true;
113                     }
114                 }
115                 return false;
116             }
117 
118             @Override
119             public String nextElement() {
120                 if (next != null) {
121                     String value = next;
122                     next = null;
123                     return value;
124                 }
125                 while (pos < names.length) {
126                     if (values[pos] != null) {
127                         return names[pos++];
128                     }
129                     pos++;
130                 }
131                 while (parentEnumeration.hasMoreElements()) {
132                     String name = (String) parentEnumeration.nextElement();
133                     if (!isAttributeOverridden(name)) {
134                         return name;
135                     }
136                 }
137                 throw new NoSuchElementException();
138             }
139         };
140     }
141 
142     @Override
143     public void setAttribute(String name, Object o) {
144         if (enabled && isAttributeOverridden(name)) {
145             return;
146         }
147         super.setAttribute(name, o);
148     }
149 
150     @Override
151     public void removeAttribute(String name) {
152         if (enabled && isAttributeOverridden(name)) {
153             return;
154         }
155         super.removeAttribute(name);
156     }
157 
158     private boolean isAttributeOverridden(String name) {
159         if (name.startsWith(commonPrefix)) {
160             for (String overriddenName : names) {
161                 if (name.equals(overriddenName)) {
162                     return true;
163                 }
164             }
165         }
166         return false;
167     }
168 }