KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tasks > TaskHttpServletRequest


1 package com.sslexplorer.tasks;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.UnsupportedEncodingException JavaDoc;
6 import java.net.MalformedURLException JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.security.Principal JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.util.Hashtable JavaDoc;
11 import java.util.Locale JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import javax.servlet.RequestDispatcher JavaDoc;
17 import javax.servlet.ServletInputStream JavaDoc;
18 import javax.servlet.http.Cookie JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpSession JavaDoc;
21
22 import org.apache.struts.Globals;
23 import org.apache.struts.config.ModuleConfig;
24 import org.mortbay.util.MultiMap;
25
26 import com.sslexplorer.boot.Util;
27
28 public class TaskHttpServletRequest implements HttpServletRequest JavaDoc {
29     
30     public static final String JavaDoc ATTR_TASK = "task";
31     public static final String JavaDoc ATTR_TASK_FORWARD = "taskForward";
32     public static final String JavaDoc ATTR_TASK_PROGRESS_HANDLED_EXTERNALLY = "taskProgressHandledExternally";
33     
34     private StringBuffer JavaDoc url;
35     private String JavaDoc uri;
36     private Hashtable JavaDoc<String JavaDoc, String JavaDoc[]> params;
37     private Hashtable JavaDoc<String JavaDoc, Object JavaDoc> attributes;
38     private String JavaDoc query;
39     private String JavaDoc servletPath;
40     
41     private HttpSession JavaDoc session;
42     private Cookie JavaDoc[] cookies;
43     private String JavaDoc characterEncoding;
44     private String JavaDoc authType;
45     private Vector JavaDoc<String JavaDoc> headerNames;
46     private String JavaDoc contextPath;
47     private MultiMap headers;
48     private String JavaDoc pathInfo;
49     private String JavaDoc pathTranslated;
50     private String JavaDoc remoteUser;
51     private String JavaDoc requestedSessionId;
52     private Principal JavaDoc userPrincipal;
53     private boolean requestedSessionIdFromCookie;
54     private boolean requestedSessionIdFromURL;
55     private boolean requestedSessionIdValid;
56     private String JavaDoc localAddr;
57     private String JavaDoc localName;
58     private int localPort;
59     private Locale JavaDoc locale;
60     private Vector JavaDoc<Locale JavaDoc> locales;
61     private String JavaDoc protocol;
62     private String JavaDoc remoteAddr;
63     private String JavaDoc remoteHost;
64     private int remotePort;
65     private String JavaDoc scheme;
66     private String JavaDoc serverName;
67     private int serverPort;
68     private boolean secure;
69     
70     public TaskHttpServletRequest(HttpServletRequest JavaDoc wrapping, Task task) {
71         this.session = wrapping.getSession();
72         String JavaDoc location = wrapping.getParameter("url");
73         
74         cookies = wrapping.getCookies();
75         characterEncoding = wrapping.getCharacterEncoding();
76         authType = wrapping.getAuthType();
77         headerNames = new Vector JavaDoc<String JavaDoc>();
78         headers = new MultiMap();
79         for(Enumeration JavaDoc e = wrapping.getHeaderNames();
80             e.hasMoreElements(); ) {
81             String JavaDoc headerName = (String JavaDoc)e.nextElement();
82             for(Enumeration JavaDoc f = wrapping.getHeaders(headerName); f.hasMoreElements(); ) {
83                 String JavaDoc headerValue = (String JavaDoc)f.nextElement();
84                 headers.add(headerName, headerValue);
85             }
86         }
87         contextPath = wrapping.getContextPath();
88         pathInfo = wrapping.getPathInfo();
89         pathTranslated = wrapping.getPathTranslated();
90         remoteUser = wrapping.getRemoteUser(); // TODO check if needed
91
requestedSessionId = wrapping.getRequestedSessionId(); // TODO check if needed
92
userPrincipal = wrapping.getUserPrincipal(); // TODO check if needed
93
requestedSessionIdFromCookie = wrapping.isRequestedSessionIdFromCookie();
94         requestedSessionIdFromURL = wrapping.isRequestedSessionIdFromURL();
95         requestedSessionIdValid = wrapping.isRequestedSessionIdValid();
96         localAddr = wrapping.getLocalAddr();
97         localName = wrapping.getLocalName();
98         localPort = wrapping.getLocalPort();
99         locale = wrapping.getLocale();
100         locales = new Vector JavaDoc<Locale JavaDoc>();
101         for(Enumeration JavaDoc e = wrapping.getLocales(); e.hasMoreElements(); locales.add((Locale JavaDoc)e.nextElement()) );
102         protocol = wrapping.getProtocol();
103         remoteAddr = wrapping.getRemoteAddr();
104         remoteHost = wrapping.getRemoteHost();
105         remotePort = wrapping.getRemotePort();
106         scheme = wrapping.getScheme();
107         serverName = wrapping.getServerName();
108         serverPort = wrapping.getServerPort();
109         secure = wrapping.isSecure();
110         
111         // Extract the query (everything after ?)
112
int idx = location.indexOf('?');
113         query = null;
114         if(idx != -1) {
115             query = location.substring(idx + 1);
116         }
117         
118         // Extract the URI (everything before ?)
119
uri = location;
120         if(idx != -1) {
121             uri = uri.substring(0, idx);
122         }
123         
124         // Servlet path (same as URI?)
125
servletPath = uri;
126         
127         // Extract parameters
128
params = new Hashtable JavaDoc<String JavaDoc, String JavaDoc[]>();
129         if(query != null) {
130             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(query, "&");
131             while(t.hasMoreTokens()) {
132                 String JavaDoc token = t.nextToken();
133                 idx = token.indexOf('=');
134                 String JavaDoc name = token;
135                 String JavaDoc val = null;
136                 if(idx != -1) {
137                     name = token.substring(0, idx);
138                     val = token.substring(idx + 1);
139                 }
140                 else {
141                     val = "";
142                 }
143                 String JavaDoc[] vals = params.get(name);
144                 if(vals == null) {
145                     vals = new String JavaDoc[] { val };
146                 }
147                 else {
148                     String JavaDoc[] nvals = new String JavaDoc[vals.length + 1];
149                     System.arraycopy(vals, 0, nvals, 0, vals.length);
150                     nvals[vals.length] = val;
151                     vals = nvals;
152                 }
153                 params.put(name, vals);
154             }
155         }
156         
157         // Initialise attributes
158
attributes = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>();
159         
160         // Create the URL (the URL with protocol / host / post)
161
try {
162             URL JavaDoc u = new URL JavaDoc(new URL JavaDoc(wrapping.getRequestURL().toString()), uri);
163             url = new StringBuffer JavaDoc(u.toExternalForm());
164         } catch (MalformedURLException JavaDoc e) {
165         }
166         
167         setAttribute(ATTR_TASK, task);
168     }
169
170     public String JavaDoc getServletPath() {
171         return servletPath;
172     }
173
174     public String JavaDoc getQueryString() {
175         return query;
176     }
177
178     public String JavaDoc getRequestURI() {
179         return uri;
180     }
181
182     public StringBuffer JavaDoc getRequestURL() {
183         return url;
184     }
185
186     public String JavaDoc getParameter(String JavaDoc arg0) {
187         String JavaDoc[] vals = params.get(arg0);
188         return vals == null ? null : vals[0];
189     }
190
191     public Map JavaDoc getParameterMap() {
192         return params;
193     }
194
195     public Enumeration JavaDoc getParameterNames() {
196         return params.keys();
197     }
198
199     public String JavaDoc[] getParameterValues(String JavaDoc arg0) {
200         return params.get(arg0);
201     }
202
203     public String JavaDoc getMethod() {
204         return "GET";
205     }
206
207     public HttpSession JavaDoc getSession(boolean arg0) {
208         // We can never create a new session
209
if(arg0)
210             throw new IllegalStateException JavaDoc("Cannot create session");
211         return getSession();
212     }
213
214     public Object JavaDoc getAttribute(String JavaDoc arg0) {
215         return attributes.get(arg0);
216     }
217
218     public Enumeration JavaDoc getAttributeNames() {
219         return attributes.keys();
220     }
221
222     public String JavaDoc getCharacterEncoding() {
223         return characterEncoding;
224     }
225
226     public int getContentLength() {
227         return 0;
228     }
229
230     public String JavaDoc getContentType() {
231         return null;
232     }
233
234     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
235         return null;
236     }
237
238     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
239         return null;
240     }
241
242     public void removeAttribute(String JavaDoc arg0) {
243         attributes.remove(arg0);
244     }
245
246     public void setAttribute(String JavaDoc arg0, Object JavaDoc arg1) {
247         attributes.put(arg0, arg1);
248     }
249
250     public void setCharacterEncoding(String JavaDoc characterEncoding) throws UnsupportedEncodingException JavaDoc {
251         this.characterEncoding = characterEncoding;
252     }
253     
254     public Cookie JavaDoc[] getCookies() {
255         return cookies;
256     }
257
258     public String JavaDoc getAuthType() {
259         return authType;
260     }
261
262     public String JavaDoc getContextPath() {
263         return contextPath;
264     }
265
266     public long getDateHeader(String JavaDoc name) {
267         throw new IllegalArgumentException JavaDoc("Not implemented.");
268     }
269
270     public String JavaDoc getHeader(String JavaDoc name) {
271         return headers.getString(name);
272     }
273
274     public Enumeration JavaDoc getHeaderNames() {
275         return headerNames.elements();
276     }
277
278     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
279         return new Vector JavaDoc(headers.getValues(name)).elements();
280     }
281
282     public int getIntHeader(String JavaDoc name) {
283         return Integer.parseInt(getHeader(name));
284     }
285
286     public String JavaDoc getPathInfo() {
287         return pathInfo;
288     }
289
290     public String JavaDoc getPathTranslated() {
291         return pathTranslated;
292     }
293
294     public String JavaDoc getRemoteUser() {
295         return remoteUser;
296     }
297
298     public String JavaDoc getRequestedSessionId() {
299         return requestedSessionId;
300     }
301
302     public HttpSession JavaDoc getSession() {
303         return session;
304     }
305
306     public Principal JavaDoc getUserPrincipal() {
307         return userPrincipal;
308     }
309
310     public boolean isRequestedSessionIdFromCookie() {
311         return requestedSessionIdFromCookie;
312     }
313
314     public boolean isRequestedSessionIdFromURL() {
315         return requestedSessionIdFromURL;
316     }
317
318     public boolean isRequestedSessionIdFromUrl() {
319         return isRequestedSessionIdFromURL();
320     }
321
322     public boolean isRequestedSessionIdValid() {
323         return requestedSessionIdValid;
324     }
325
326     public boolean isUserInRole(String JavaDoc role) {
327         throw new IllegalArgumentException JavaDoc("Not implemented.");
328     }
329
330     public String JavaDoc getLocalAddr() {
331         return localAddr;
332     }
333
334     public String JavaDoc getLocalName() {
335         return localName;
336     }
337
338     public int getLocalPort() {
339         return localPort;
340     }
341
342     public Locale JavaDoc getLocale() {
343         return locale;
344     }
345
346     public Enumeration JavaDoc getLocales() {
347         return locales.elements();
348     }
349
350     public String JavaDoc getProtocol() {
351         return protocol;
352     }
353
354     public String JavaDoc getRealPath(String JavaDoc path) {
355         throw new IllegalArgumentException JavaDoc("Not implemented.");
356     }
357
358     public String JavaDoc getRemoteAddr() {
359         return remoteAddr;
360     }
361
362     public String JavaDoc getRemoteHost() {
363         return remoteHost;
364     }
365
366     public int getRemotePort() {
367         return remotePort;
368     }
369
370     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
371         throw new IllegalStateException JavaDoc("Request dispatcher not available.");
372     }
373
374     public String JavaDoc getScheme() {
375         return scheme;
376     }
377
378     public String JavaDoc getServerName() {
379         return serverName;
380     }
381
382     public int getServerPort() {
383         return serverPort;
384     }
385
386     public boolean isSecure() {
387         return secure;
388     }
389
390 }
391
Popular Tags