KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > helpers > CustomRequestWrapper


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 /**
7  * Created by The eXo Platform SARL
8  * Author : Mestrallet Benjamin
9  * benjmestrallet@users.sourceforge.net
10  * Date: Jul 29, 2003
11  * Time: 2:24:57 AM
12  */

13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers;
14
15 import java.io.BufferedReader JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.commons.lang.StringUtils ;
25 import org.exoplatform.Constants;
26 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.utils.CustomRequestWrapperUtil;
27
28 import javax.servlet.ServletInputStream JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
31 import javax.servlet.http.HttpSession JavaDoc;
32
33
34
35 /**
36  * This wrapper manages the incoming request to only provide the
37  * attributes and parameters that are in the name space of
38  * the receiving portlet.
39  * <br>
40  * This is done using a correct encoding and decoding
41  * windowId?attributeName
42  */

43 public class CustomRequestWrapper extends HttpServletRequestWrapper JavaDoc {
44
45   private String JavaDoc pathInfo;
46   private String JavaDoc servletPath;
47   private String JavaDoc query;
48   private String JavaDoc windowId;
49   private boolean redirected;
50   private HttpSession JavaDoc sharedSession;
51   private String JavaDoc contextPath;
52   private Map JavaDoc parameterMap ;
53
54   public CustomRequestWrapper(HttpServletRequest JavaDoc httpServletRequest) {
55     super(httpServletRequest);
56   }
57
58   public void fillCustomRequestWrapper(HttpServletRequest JavaDoc httpServletRequest,
59                                        String JavaDoc windowId) {
60     super.setRequest(httpServletRequest);
61     this.windowId = windowId;
62     this.parameterMap = httpServletRequest.getParameterMap() ;
63   }
64
65   public void emptyCustomRequestWrapper() {
66     this.windowId = null;
67   }
68   
69   public Enumeration JavaDoc getAttributeNames() {
70     Enumeration JavaDoc e = super.getAttributeNames();
71     Vector JavaDoc v = new Vector JavaDoc();
72     while (e.hasMoreElements()) {
73       String JavaDoc s = (String JavaDoc) e.nextElement();
74       s = CustomRequestWrapperUtil.decodeRequestAttribute(windowId, s);
75       v.add(s);
76     }
77     return v.elements();
78   }
79
80   public Object JavaDoc getAttribute(String JavaDoc s) {
81     return super.getAttribute(CustomRequestWrapperUtil.encodeAttribute(windowId, s));
82   }
83
84   public void removeAttribute(String JavaDoc s) {
85     super.removeAttribute(CustomRequestWrapperUtil.encodeAttribute(windowId, s));
86   }
87
88   public void setAttribute(String JavaDoc s, Object JavaDoc o) {
89     super.setAttribute(CustomRequestWrapperUtil.encodeAttribute(windowId, s), o);
90   }
91   
92   public Map JavaDoc getParameterMap() {
93     Map JavaDoc superMap = super.getParameterMap();
94     if(redirected){
95       Map JavaDoc filteredMap = new HashMap JavaDoc();
96       Set JavaDoc keys = superMap.keySet();
97       for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
98         String JavaDoc element = (String JavaDoc) iter.next();
99         if(!element.startsWith(Constants.PARAMETER_ENCODER))
100           filteredMap.put(element, superMap.get(element));
101       }
102       return filteredMap;
103     }
104     return superMap;
105   }
106
107   public void setParameterMap(Map JavaDoc map) {
108     this.parameterMap = map ;
109   }
110   
111   public boolean isRedirected() {
112     return redirected;
113   }
114
115   public void setRedirected(boolean b) {
116     redirected = b;
117   }
118
119   public int getContentLength() {
120     if(redirected)
121       return 0;
122     return super.getContentLength();
123   }
124   
125   public StringBuffer JavaDoc getRequestURL() {
126     if(redirected)
127       return null;
128     return super.getRequestURL();
129   }
130
131   public String JavaDoc getCharacterEncoding() {
132     //if(redirected)
133
// return null;
134
return super.getCharacterEncoding();
135   }
136
137   public String JavaDoc getContentType() {
138     if(redirected)
139       return null;
140     return super.getContentType();
141   }
142
143   public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
144     if(redirected)
145       return null;
146     return super.getInputStream();
147   }
148
149   public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
150     if(redirected)
151       return null;
152     return super.getReader();
153   }
154
155   public String JavaDoc getRealPath(String JavaDoc arg0) {
156     if(redirected)
157       return null;
158     return super.getRealPath(arg0);
159   }
160
161   public String JavaDoc getRemoteAddr() {
162     if(redirected)
163       return null;
164     return super.getRemoteAddr();
165   }
166
167   public String JavaDoc getRemoteHost() {
168     if(redirected)
169       return null;
170     return super.getRemoteHost();
171   }
172
173   public void setCharacterEncoding(String JavaDoc arg0)
174     throws UnsupportedEncodingException JavaDoc {
175     if(redirected)
176         return;
177     super.setCharacterEncoding(arg0);
178   }
179   
180   public String JavaDoc getProtocol() {
181     if(redirected)
182       return null;
183     return super.getProtocol();
184   }
185   
186   public HttpSession JavaDoc getSession(){
187     return getSession(true);
188   }
189   
190   public HttpSession JavaDoc getSession(boolean b){
191     if(/*redirected &&*/ sharedSession != null)
192       return sharedSession;
193     else{
194       return super.getSession(b);
195     }
196   }
197
198   public void setSharedSession(HttpSession JavaDoc session) {
199     sharedSession = session;
200   }
201
202   public boolean isRequestedSessionIdValid() {
203     if(/*redirected &&*/ sharedSession != null){
204       return true;
205     }
206     return super.isRequestedSessionIdValid();
207   }
208
209   public void setContextPath(String JavaDoc string) {
210     contextPath = string;
211   }
212
213   public String JavaDoc getContextPath() {
214     if(redirected && contextPath != null){
215       return contextPath;
216     }
217     return super.getContextPath();
218   }
219
220   public void setRedirectedPath(String JavaDoc path) {
221    if(path == null || path.length() == 0)
222      path = "/";
223     
224     String JavaDoc[] key = StringUtils.split(path, "?") ;
225     String JavaDoc firstPart = "";
226     if(key.length > 1){
227       query = key[1];
228       firstPart = key[0];
229     } else {
230       firstPart = path;
231     }
232     
233     if(firstPart.indexOf("/", 1)>0){
234       servletPath = firstPart.substring(0, firstPart.indexOf("/", 1));
235       pathInfo = firstPart.substring(firstPart.indexOf("/", 1));
236     } else {
237       servletPath = firstPart;
238           pathInfo = null;
239     }
240   }
241   
242   public String JavaDoc getPathInfo() {
243     if(redirected){
244       return pathInfo;
245     }
246     return super.getPathInfo();
247   }
248   
249   public String JavaDoc getServletPath() {
250     if(redirected){
251       return servletPath;
252     }
253     return super.getServletPath();
254   }
255   
256   public String JavaDoc getQueryString() {
257     if(redirected){
258       return query;
259     }
260     return super.getQueryString();
261   }
262
263   public String JavaDoc getRequestURI() {
264     if(redirected){
265       return getContextPath() +
266                  ((servletPath == null) ? "" : servletPath) +
267                  ((pathInfo == null) ? "" : pathInfo);
268     }
269     return super.getRequestURI();
270   }
271
272 }
273
Popular Tags