001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.wrapper;
018:
019: import java.security.Principal;
020: import java.util.Enumeration;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Locale;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import org.apache.cocoon.environment.Cookie;
028: import org.apache.cocoon.environment.Environment;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.cocoon.environment.Session;
031:
032: /**
033: * This is a wrapper class for the <code>Request</code> object.
034: * It has the same properties except that the url and the parameters
035: * are different.
036: *
037: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
038: * @version $Id: RequestWrapper.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public final class RequestWrapper implements Request {
041:
042: /** The real {@link Request} object */
043: private final Request req;
044:
045: /** The query string */
046: private String queryString;
047:
048: /** The request parameters */
049: private final RequestParameters parameters;
050:
051: /** The environment */
052: private final Environment environment;
053:
054: /** raw mode? **/
055: private final boolean rawMode;
056:
057: /** The request uri */
058: private String requestURI;
059:
060: /**
061: * Constructor
062: */
063: public RequestWrapper(Request request, String requestURI,
064: String queryString, Environment env) {
065: this (request, requestURI, queryString, env, false);
066: }
067:
068: /**
069: * Constructor
070: */
071: public RequestWrapper(Request request, String requestURI,
072: String queryString, Environment env, boolean rawMode) {
073: this .environment = env;
074: this .req = request;
075: this .queryString = queryString;
076: this .parameters = new RequestParameters(queryString);
077: this .rawMode = rawMode;
078: if (this .req.getQueryString() != null && this .rawMode == false) {
079: if (this .queryString == null)
080: this .queryString = this .req.getQueryString();
081: else
082: this .queryString += '&' + this .req.getQueryString();
083: }
084: this .requestURI = this .req.getRequestURI();
085: }
086:
087: public Object get(String name) {
088: return this .req.get(name);
089: }
090:
091: public Object getAttribute(String name) {
092: return this .req.getAttribute(name);
093: }
094:
095: public Enumeration getAttributeNames() {
096: return this .req.getAttributeNames();
097: }
098:
099: public String getCharacterEncoding() {
100: return this .req.getCharacterEncoding();
101: }
102:
103: public void setCharacterEncoding(String enc)
104: throws java.io.UnsupportedEncodingException {
105: this .req.setCharacterEncoding(enc);
106: }
107:
108: public int getContentLength() {
109: return this .req.getContentLength();
110: }
111:
112: public String getContentType() {
113: return this .req.getContentType();
114: }
115:
116: public String getParameter(String name) {
117: String value = this .parameters.getParameter(name);
118: if (value == null && this .rawMode == false)
119: return this .req.getParameter(name);
120: else
121: return value;
122: }
123:
124: public Enumeration getParameterNames() {
125: if (this .rawMode == false) {
126: // put all parameter names into a set
127: Set parameterNames = new HashSet();
128: Enumeration names = this .parameters.getParameterNames();
129: while (names.hasMoreElements()) {
130: parameterNames.add(names.nextElement());
131: }
132: names = this .req.getParameterNames();
133: while (names.hasMoreElements()) {
134: parameterNames.add(names.nextElement());
135: }
136: return new EnumerationFromIterator(parameterNames
137: .iterator());
138: } else {
139: return this .parameters.getParameterNames();
140: }
141: }
142:
143: static final class EnumerationFromIterator implements Enumeration {
144: private Iterator iter;
145:
146: EnumerationFromIterator(Iterator iter) {
147: this .iter = iter;
148: }
149:
150: public boolean hasMoreElements() {
151: return iter.hasNext();
152: }
153:
154: public Object nextElement() {
155: return iter.next();
156: }
157: }
158:
159: public String[] getParameterValues(String name) {
160: if (this .rawMode == false) {
161: String[] values = this .parameters.getParameterValues(name);
162: String[] inherited = this .req.getParameterValues(name);
163: if (inherited == null)
164: return values;
165: if (values == null)
166: return inherited;
167: String[] allValues = new String[values.length
168: + inherited.length];
169: System.arraycopy(values, 0, allValues, 0, values.length);
170: System.arraycopy(inherited, 0, allValues, values.length,
171: inherited.length);
172: return allValues;
173: } else {
174: return this .parameters.getParameterValues(name);
175: }
176: }
177:
178: public String getProtocol() {
179: return this .req.getProtocol();
180: }
181:
182: public String getScheme() {
183: return this .req.getScheme();
184: }
185:
186: public String getServerName() {
187: return this .req.getServerName();
188: }
189:
190: public int getServerPort() {
191: return this .req.getServerPort();
192: }
193:
194: public String getRemoteAddr() {
195: return this .req.getRemoteAddr();
196: }
197:
198: public String getRemoteHost() {
199: return this .req.getRemoteHost();
200: }
201:
202: public void setAttribute(String name, Object o) {
203: this .req.setAttribute(name, o);
204: }
205:
206: /**
207: * Remove one attriube
208: */
209: public void removeAttribute(String name) {
210: this .req.removeAttribute(name);
211: }
212:
213: public Locale getLocale() {
214: return this .req.getLocale();
215: }
216:
217: public Enumeration getLocales() {
218: return this .req.getLocales();
219: }
220:
221: public boolean isSecure() {
222: return this .req.isSecure();
223: }
224:
225: public Cookie[] getCookies() {
226: return this .req.getCookies();
227: }
228:
229: public Map getCookieMap() {
230: return this .req.getCookieMap();
231: }
232:
233: public long getDateHeader(String name) {
234: return this .req.getDateHeader(name);
235: }
236:
237: public String getHeader(String name) {
238: return this .req.getHeader(name);
239: }
240:
241: public Enumeration getHeaders(String name) {
242: return this .req.getHeaders(name);
243: }
244:
245: public Enumeration getHeaderNames() {
246: return this .req.getHeaderNames();
247: }
248:
249: public String getMethod() {
250: return this .req.getMethod();
251: }
252:
253: public String getPathInfo() {
254: return this .req.getPathInfo();
255: }
256:
257: public String getPathTranslated() {
258: return this .req.getPathTranslated();
259: }
260:
261: public String getContextPath() {
262: return this .req.getContextPath();
263: }
264:
265: public String getQueryString() {
266: return this .queryString;
267: }
268:
269: public String getRemoteUser() {
270: return this .req.getRemoteUser();
271: }
272:
273: public String getRequestedSessionId() {
274: return this .req.getRequestedSessionId();
275: }
276:
277: public String getRequestURI() {
278: return this .requestURI;
279: }
280:
281: public String getSitemapURI() {
282: return this .environment.getURI();
283: }
284:
285: public String getSitemapURIPrefix() {
286: return this .environment.getURIPrefix();
287: }
288:
289: public String getServletPath() {
290: return this .req.getServletPath();
291: }
292:
293: public Session getSession(boolean create) {
294: return this .req.getSession(create);
295: }
296:
297: public Session getSession() {
298: return this .req.getSession();
299: }
300:
301: public boolean isRequestedSessionIdValid() {
302: return this .req.isRequestedSessionIdValid();
303: }
304:
305: public boolean isRequestedSessionIdFromCookie() {
306: return this .req.isRequestedSessionIdFromCookie();
307: }
308:
309: public boolean isRequestedSessionIdFromURL() {
310: return this .req.isRequestedSessionIdFromURL();
311: }
312:
313: public boolean isRequestedSessionIdFromUrl() {
314: return this .req.isRequestedSessionIdFromURL();
315: }
316:
317: public Principal getUserPrincipal() {
318: return this .req.getUserPrincipal();
319: }
320:
321: public boolean isUserInRole(String role) {
322: return this .req.isUserInRole(role);
323: }
324:
325: public String getAuthType() {
326: return this .req.getAuthType();
327: }
328:
329: public void setRequestURI(String prefix, String uri) {
330: StringBuffer buffer = new StringBuffer(this .getContextPath());
331: buffer.append('/');
332: buffer.append(prefix);
333: buffer.append(uri);
334: this.requestURI = buffer.toString();
335: }
336: }
|