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

File SpecialAttributeRequestWrapper.java

 

Coverage histogram

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

Code metrics

36
49
9
1
168
112
30
0.61
5.44
9
3.33
4.1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
SpecialAttributeRequestWrapper 47 49 4.1% 30 4
0.957446895.7%
 

Contributing tests

This file is covered by 26 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.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  30 toggle public SpecialAttributeRequestWrapper(HttpServletRequest request) {
55  30 super(request);
56    }
57   
 
58    toggle public boolean isEnabled() {
59    return enabled;
60    }
61   
 
62    toggle public void setEnabled(boolean enabled) {
63    this.enabled = enabled;
64    }
65   
 
66  27 toggle protected void setOverriddenAttributes(String commonPrefix, String[] names, Object[] values) {
67  27 this.commonPrefix = commonPrefix;
68  27 this.names = names;
69  27 this.values = values;
70    }
71   
 
72  55 toggle @Override
73    public Object getAttribute(String name) {
74  55 if (enabled && name.startsWith(commonPrefix)) {
75  94 for (int i = 0; i < names.length; i++) {
76  92 if (names[i].equals(name)) {
77  36 return values[i];
78    }
79    }
80    }
81  19 return super.getAttribute(name);
82    }
83   
 
84  9 toggle public Enumeration getAttributeNames() {
85   
86  9 if (!enabled) {
87  1 return super.getAttributeNames();
88    }
89   
90  8 return new Enumeration() {
91   
92    private int pos = 0;
93    private String next = null;
94    private Enumeration parentEnumeration = SpecialAttributeRequestWrapper.super.getAttributeNames();
95   
 
96  8 toggle @Override
97    public boolean hasMoreElements() {
98  8 if (next != null) {
99  0 return true;
100    }
101  9 while (pos < names.length) {
102  4 if (values[pos] != null) {
103  3 next = names[pos++];
104  3 return true;
105    }
106  1 pos++;
107    }
108  6 while (parentEnumeration.hasMoreElements()) {
109  2 String name = (String) parentEnumeration.nextElement();
110  2 if (!isAttributeOverridden(name)) {
111  1 next = name;
112  1 return true;
113    }
114    }
115  4 return false;
116    }
117   
 
118  13 toggle @Override
119    public String nextElement() {
120  13 if (next != null) {
121  4 String value = next;
122  4 next = null;
123  4 return value;
124    }
125  10 while (pos < names.length) {
126  4 if (values[pos] != null) {
127  3 return names[pos++];
128    }
129  1 pos++;
130    }
131  6 while (parentEnumeration.hasMoreElements()) {
132  3 String name = (String) parentEnumeration.nextElement();
133  3 if (!isAttributeOverridden(name)) {
134  3 return name;
135    }
136    }
137  3 throw new NoSuchElementException();
138    }
139    };
140    }
141   
 
142  2 toggle @Override
143    public void setAttribute(String name, Object o) {
144  2 if (enabled && isAttributeOverridden(name)) {
145  1 return;
146    }
147  1 super.setAttribute(name, o);
148    }
149   
 
150  2 toggle @Override
151    public void removeAttribute(String name) {
152  2 if (enabled && isAttributeOverridden(name)) {
153  1 return;
154    }
155  1 super.removeAttribute(name);
156    }
157   
 
158  9 toggle private boolean isAttributeOverridden(String name) {
159  9 if (name.startsWith(commonPrefix)) {
160  9 for (String overriddenName : names) {
161  4 if (name.equals(overriddenName)) {
162  3 return true;
163    }
164    }
165    }
166  6 return false;
167    }
168    }