KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > context > PartialPageRequest


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.context;
25
26 import java.util.Collections JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
35
36 import org.riotfamily.common.collection.IteratorEnumeration;
37 import org.springframework.util.Assert;
38
39 public class PartialPageRequest extends HttpServletRequestWrapper JavaDoc {
40
41     private static final String JavaDoc CONTEXT_KEY =
42             PartialPageRequest.class.getName() + ".contextKey";
43
44     private PageRequestContext context;
45
46     private HashMap JavaDoc attributes = new HashMap JavaDoc();
47
48     public PartialPageRequest(HttpServletRequest JavaDoc request,
49             PageRequestContext context) {
50
51         super(request);
52         this.context = context;
53     }
54
55     public String JavaDoc getMethod() {
56         return context.getMethod();
57     }
58
59     public String JavaDoc getPathInfo() {
60         return context.getPathInfo();
61     }
62
63     public String JavaDoc getServletPath() {
64         return context.getServletPath();
65     }
66
67     public String JavaDoc getQueryString() {
68         return context.getQueryString();
69     }
70
71     public String JavaDoc getRequestURI() {
72         return context.getRequestURI();
73     }
74
75     public StringBuffer JavaDoc getRequestURL() {
76         StringBuffer JavaDoc url = new StringBuffer JavaDoc(getScheme());
77         url.append("://").append(getServerName()).append(':').append(getServerPort());
78         url.append(getRequestURI());
79         return url;
80     }
81
82     public String JavaDoc getPathTranslated() {
83         return (getPathInfo() != null ? getRealPath(getPathInfo()) : null);
84     }
85
86     public String JavaDoc getParameter(String JavaDoc name) {
87         Assert.notNull(name, "Parameter name must not be null");
88         String JavaDoc[] arr = (String JavaDoc[]) getParameterMap().get(name);
89         return (arr != null && arr.length > 0 ? arr[0] : null);
90     }
91
92     public Enumeration JavaDoc getParameterNames() {
93         return Collections.enumeration(getParameterMap().keySet());
94     }
95
96     public String JavaDoc[] getParameterValues(String JavaDoc name) {
97         Assert.notNull(name, "Parameter name must not be null");
98         return (String JavaDoc[]) getParameterMap().get(name);
99     }
100
101     public Map JavaDoc getParameterMap() {
102         HashMap JavaDoc params = new HashMap JavaDoc();
103         params.putAll(context.getParameters());
104         params.putAll(super.getParameterMap());
105         return params;
106     }
107
108     public Object JavaDoc getAttribute(String JavaDoc name) {
109         if (name.equals(CONTEXT_KEY)) {
110             return context.getKey();
111         }
112         if (name.startsWith("javax.servlet.")) {
113             return super.getAttribute(name);
114         }
115         if (attributes.containsKey(name)) {
116             return attributes.get(name);
117         }
118         return context.getAttributes().get(name);
119     }
120
121     public Enumeration JavaDoc getAttributeNames() {
122         HashSet JavaDoc names = new HashSet JavaDoc();
123         names.addAll(context.getAttributes().keySet());
124         Iterator JavaDoc it = attributes.entrySet().iterator();
125         while (it.hasNext()) {
126             Map.Entry JavaDoc attribute = (Map.Entry JavaDoc) it.next();
127             if (attribute.getValue() != null) {
128                 names.add(attribute.getKey());
129             }
130             else {
131                 // A null value in the local attributes map means that
132
// the attribute has been removed ...
133
names.remove(attribute.getKey());
134             }
135         }
136         return new IteratorEnumeration(names.iterator());
137     }
138
139     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
140         Assert.notNull(name, "Attribute name must not be null");
141         attributes.put(name, value);
142     }
143
144     public void removeAttribute(String JavaDoc name) {
145         Assert.notNull(name, "Attribute name must not be null");
146         // Instead of removing the attribute we set it to null, which marks
147
// it as removed ...
148
attributes.put(name, null);
149     }
150
151     public static boolean isWrapped(HttpServletRequest JavaDoc request) {
152         return request.getAttribute(CONTEXT_KEY) != null;
153     }
154
155     public static boolean isWrapped(HttpServletRequest JavaDoc request,
156             Object JavaDoc contextKey) {
157
158         return contextKey.equals(request.getAttribute(CONTEXT_KEY));
159     }
160
161 }
Popular Tags