KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > httpPresentation > servlet > ServletHttpPresentationRequest


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: ServletHttpPresentationRequest.java,v 1.2 2005/03/24 10:51:16 slobodan Exp $
23  */

24
25 package com.lutris.appserver.server.httpPresentation.servlet;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Stack JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import javax.servlet.http.Cookie JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35
36 import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
37 import com.lutris.appserver.server.httpPresentation.HttpPresentationIOException;
38 import com.lutris.appserver.server.httpPresentation.HttpPresentationInputStream;
39 import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest;
40
41 /**
42  * Servlet implementation of the object passed to Presentation objects that
43  * is used to access HTTP request data.
44  */

45 public class ServletHttpPresentationRequest
46     implements HttpPresentationRequest { //SV , DebugRequest
47

48     /*
49      * Servlet objects.
50      */

51     private HttpServletRequest JavaDoc request;
52     private HttpPresentationInputStream inputStream = null; // Lazy allocation.
53
private String JavaDoc presentationPath;
54     private String JavaDoc presentationPathInfo;
55
56     /*
57      * Indicates if sessionId is found in a request cookie
58      */

59     private boolean isRequestedSessionIdFromCookie = false;
60
61     /*
62      * Indicates if sessionId is found in a request url
63      */

64     private boolean isRequestedSessionIdFromUrl = false;
65
66     /**
67      * Construct an object associated with a servlet request.
68      * @param request Servlet request object that this object will front-end.
69      */

70     protected ServletHttpPresentationRequest(HttpServletRequest JavaDoc request) {
71         this.request = request;
72         StringBuffer JavaDoc presPath = new StringBuffer JavaDoc();
73         StringBuffer JavaDoc presPathInfo = new StringBuffer JavaDoc();
74
75         /*
76          * Split the request URI into the presentation path and path info.
77          */

78         StringTokenizer JavaDoc tokens =
79             new StringTokenizer JavaDoc(request.getRequestURI().trim(), "/\\", false);
80
81         while (tokens.hasMoreTokens()) {
82             String JavaDoc name = tokens.nextToken();
83         // A presentation object is found if a token ends with *.po or
84
// *.po; where the session id would follow the semi-colon
85
if (name.endsWith(".po")) {
86         presPath.append("/");
87         presPath.append(name);
88         break;
89         } else if (name.indexOf(".po;") != -1) {
90         presPath.append("/");
91         presPath.append(name.substring(0, name.indexOf(";")));
92         presPathInfo.append(name.substring(name.indexOf(";")));
93                 break;
94         } else {
95         presPath.append("/");
96         presPath.append(name);
97         }
98         }
99         if (presPath.length() == 0) {
100             presPath.append("/");
101         }
102
103         if (tokens.hasMoreTokens()) {
104             //presPathInfo = new StringBuffer();
105
while (tokens.hasMoreTokens()) {
106                 presPathInfo.append("/");
107                 presPathInfo.append(tokens.nextToken());
108             }
109         }
110         presentationPath = presPath.toString();
111         presentationPathInfo = presPathInfo.toString();
112
113     }
114
115     /**
116      * Returns the original HttpServletRequest.
117      */

118     public HttpServletRequest JavaDoc getHttpServletRequest() {
119     return this.request;
120     }
121
122     /**
123      * Returns the size of the request entity data, or -1 if not known. Same
124      * as the CGI variable CONTENT_LENGTH.
125      */

126     public int getContentLength() throws HttpPresentationException {
127         return request.getContentLength();
128     }
129
130     /*
131      * Returns the Internet Media Type of the request entity data, or null if
132      * not known. Same as the CGI variable CONTENT_TYPE.
133      */

134     public String JavaDoc getContentType() throws HttpPresentationException {
135         return request.getContentType();
136     }
137
138     /**
139      * Returns the protocol and version of the request as a string of
140      * the form <code>&lt;protocol&gt;/&lt;major version&gt;.&lt;minor
141      * version&gt</code>. Same as the CGI variable SERVER_PROTOCOL.
142      */

143     public String JavaDoc getProtocol() throws HttpPresentationException {
144         return request.getProtocol();
145     }
146
147     /**
148      * Returns the scheme of the URL used in this request, for example
149      * "http", "https", or "ftp". Different schemes have different
150      * rules for constructing URLs, as noted in RFC 1738. The URL used
151      * to create a request may be reconstructed using this scheme, the
152      * server name and port, and additional information such as URIs.
153      */

154     public String JavaDoc getScheme() {
155         return request.getScheme();
156     }
157
158     /**
159      * Returns the host name of the server that received the request.
160      * Same as the CGI variable SERVER_NAME.
161      */

162     public String JavaDoc getServerName() {
163         return request.getServerName();
164     }
165
166     /**
167      * Returns the port number on which this request was received.
168      * Same as the CGI variable SERVER_PORT.
169      */

170     public int getServerPort() {
171         return request.getServerPort();
172     }
173
174     /**
175      * Returns the IP address of the agent that sent the request. Same as the
176      * CGI variable REMOTE_ADDR.
177      */

178     public String JavaDoc getRemoteAddr() throws HttpPresentationException {
179         return request.getRemoteAddr();
180     }
181
182     /**
183      * Returns the fully qualified host name of the agent that sent the
184      * request. Same as the CGI variable REMOTE_HOST.
185      */

186     public String JavaDoc getRemoteHost() throws HttpPresentationException {
187         return request.getRemoteHost();
188     }
189
190     /**
191      * Applies alias rules to the specified virtual path and returns the
192      * corresponding real path, or null if the translation can not be
193      * performed for any reason. This resolves the path using the virtual
194      * docroot, if virtual hosting is enabled, and with the default docroot
195      * otherwise. Calling this method with the string "/" as an argument
196      * returns the document root.
197      *
198      * @param path the virtual path to be translated to a real path
199      */

200 /*
201     public String getRealPath(String path) throws HttpPresentationException {
202         return request.getRealPath(path);
203     }
204 */

205
206     /**
207      * Returns an input stream for reading the request body.
208      */

209     public HttpPresentationInputStream getInputStream() throws HttpPresentationException {
210         if (inputStream == null) {
211             try {
212                 inputStream = new ServletHttpPresentationInputStream(request.getInputStream());
213             } catch (IOException JavaDoc except) {
214                 // Want to throw a HttpPresentationIOException to record the fact that
215
// the client disconnected, but want to maintain the HttpPresentationException
216
// API.
217
throw new HttpPresentationException(new HttpPresentationIOException(except));
218             }
219         }
220         return inputStream;
221     }
222
223     /**
224      * Returns a string containing the lone value of the specified query
225      * parameter, or null if the parameter does not exist. Presentation
226      * writers should use this method only when they are sure that there is
227      * only one value for the parameter. If the parameter has (or could have)
228      * multiple values, then use getParameterValues. If a multiple valued
229      * parameter name is passed as an argument, the return value is
230      * implementation dependent.
231      *
232      * @param name the name of the parameter whose value is required.
233      * @see HttpPresentationRequest#getParameterValues
234      */

235     public String JavaDoc getParameter(String JavaDoc name) throws HttpPresentationException {
236         return request.getParameter(name);
237     }
238
239     /**
240      * Returns the values of the specified query parameter for the request as
241      * an array of strings, or a 0 length array if the named parameter does
242      * not exist.
243      *
244      * @param name the name of the parameter whose value is required.
245      */

246     public String JavaDoc[] getParameterValues(String JavaDoc name)
247     throws HttpPresentationException
248     {
249     Object JavaDoc values = request.getParameterValues(name);
250     if (values == null) {
251         return new String JavaDoc[0];
252     }
253         if (values instanceof String JavaDoc) {
254             String JavaDoc[] valArray = new String JavaDoc[1];
255             valArray[0] = (String JavaDoc)values;
256             return valArray;
257         }
258         return (String JavaDoc[])values;
259     }
260
261     /**
262      * Returns the parameter names for this request as an enumeration
263      * of strings, or an empty enumeration if there are no parameters.
264      */

265     public Enumeration JavaDoc getParameterNames() throws HttpPresentationException {
266         return request.getParameterNames();
267     }
268
269     /**
270      * Returns the method with which the request was made. The returned
271      * value can be "GET", "HEAD", "POST", or an extension method. Same
272      * as the CGI variable REQUEST_METHOD.
273      */

274     public String JavaDoc getMethod() throws HttpPresentationException {
275         return request.getMethod();
276     }
277
278     /**
279      * Returns the request URI.
280      */

281     public String JavaDoc getRequestURI() throws HttpPresentationException {
282         return request.getRequestURI();
283     }
284
285     /**
286      * Returns the presentation URI.
287      */

288     public String JavaDoc getPresentationURI() throws HttpPresentationException {
289         return request.getRequestURI();
290     }
291
292     /**
293      * Returns the part of the request URI that refers to the application
294      * object being invoked. Analogous to the CGI variable SCRIPT_NAME.
295      *
296      * @deprecated This method was named in a confusing manner; it
297      * returns the application, not presentation object path. Use
298      * <A HREF="getApplicationPath.html">getApplicationPath()</A>.
299      */

300     public String JavaDoc getPresentationPath() throws HttpPresentationException {
301         //return request.getServletPath();
302
return request.getContextPath();
303     }
304
305     /**
306      * Returns the part of the request URI that refers to the presentation
307      * object being invoked.
308      */

309     public String JavaDoc getPresentationObjectPath() throws HttpPresentationException {
310         return presentationPath;
311     }
312
313
314
315     /**
316      * Returns the part of the request URI after the presentation
317      * manager servlet, upto and including the presentation object .po,
318      * but not any path info.
319      *
320      * Includes bug fix submitted by Ben Warren
321      */

322     public String JavaDoc getPresentationObjectRelativePath() throws HttpPresentationException {
323         String JavaDoc reqPathInfo = request.getPathInfo();
324         String JavaDoc poPathInfo = getPathInfo();
325
326         // Return the relative path less path information.
327
if (poPathInfo == null) {
328             return "/";
329         }
330         if (poPathInfo.endsWith("/")) {
331             poPathInfo = poPathInfo.substring(0, poPathInfo.length()-1);
332         }
333         if (reqPathInfo == null) {
334             return poPathInfo;
335         }
336         if (!reqPathInfo.startsWith("/")) {
337             reqPathInfo = "/" + reqPathInfo;
338         }
339         if (reqPathInfo.endsWith("/")) {
340             reqPathInfo = reqPathInfo.substring(0,reqPathInfo.length()-1);
341         }
342
343         int pos;
344
345         //If the semicolon is the last char leave it. It is not
346
//followed by a session ID.
347
if ((pos = reqPathInfo.indexOf(';')) < 0 ||reqPathInfo.endsWith(";")) { //FIX
348
return reqPathInfo;
349         } else {
350             return reqPathInfo.substring(0, pos);
351         }
352     }
353
354
355     /**
356      * Returns the part of the request URI that refers to the application.
357      * Analogous to the CGI variable SCRIPT_NAME.
358      */

359     public String JavaDoc getApplicationPath() throws HttpPresentationException {
360         String JavaDoc contextPath = request.getContextPath();
361         //patch 25.06.2002.
362
String JavaDoc servletPath = request.getServletPath();
363         if (contextPath == null) {
364             contextPath = "/";
365         }
366 // else if (!contextPath.endsWith("/")) {
367
else if (!contextPath.endsWith("/") && !servletPath.startsWith("/") ) {
368             contextPath += "/";
369         }
370 // return contextPath + request.getServletPath();
371
return contextPath + servletPath;
372
373     }
374
375     /**
376      * Returns optional extra path information following the presentation
377      * path, but immediately preceding the query string. Returns null if
378      * not specified. Same as the CGI variable PATH_INFO.
379      */

380     public String JavaDoc getPathInfo() throws HttpPresentationException {
381         if (presentationPathInfo == null) {
382             return null;
383         } else {
384             return presentationPathInfo;
385         }
386     }
387
388     /**
389      * Returns extra path information translated to a real path. Returns
390      * null if no extra path information specified. Same as the CGI variable
391      * PATH_TRANSLATED.
392      */

393     public String JavaDoc getPathTranslated() throws HttpPresentationException {
394         return request.getPathTranslated();
395     }
396
397     /**
398      * Returns the query string part of the presentation URI, or null if none.
399      * Same as the CGI variable QUERY_STRING.
400      */

401     public String JavaDoc getQueryString() throws HttpPresentationException {
402         return request.getQueryString();
403     }
404
405     /**
406      * Returns the name of the user making this request, or null if not
407      * known. The user name is set with HTTP authentication. Whether
408      * the user name will continue to be sent with each subsequent
409      * communication is browser-dependent. Same as the CGI variable
410      * REMOTE_USER.
411      */

412     public String JavaDoc getRemoteUser() throws HttpPresentationException {
413         return request.getRemoteUser();
414     }
415
416     /**
417      * Returns the authentication scheme of the request, or null if none.
418      * Same as the CGI variable AUTH_TYPE.
419      */

420     public String JavaDoc getAuthType() throws HttpPresentationException {
421         return request.getAuthType();
422     }
423
424     /**
425      * Gets the array of cookies found in this request.
426      *
427      * @return The array of cookies found in this request.
428      */

429     public Cookie JavaDoc[] getCookies() throws HttpPresentationException {
430         return request.getCookies();
431     }
432
433
434     /*
435      * set the flag indicating whether the sessionId came from a cookie
436      *@param isFromCookie boolean indicating whether sessionId came from cookie
437      */

438     public void setRequestedSessionIdFromCookie(boolean isFromCookie)
439       throws HttpPresentationException {
440       isRequestedSessionIdFromCookie = isFromCookie;
441     }
442
443
444     /**
445      * Indicates whether client submitted their session id through a cookie
446      * @return true if client submitted their sessionId via a cookie,
447      * false otherwise
448      */

449     public boolean isRequestedSessionIdFromCookie()
450     throws HttpPresentationException {
451        return isRequestedSessionIdFromCookie;
452     }
453
454
455     /*
456      * set the flag indicating whether the sessionId came from a url
457      * @param isFromUrl boolean indicating whether sessionId came from url
458      */

459     public void setRequestedSessionIdFromUrl(boolean isFromUrl)
460       throws HttpPresentationException {
461       isRequestedSessionIdFromUrl = isFromUrl;
462     }
463
464
465
466     /**
467      * Indicates whether client submitted their sessionId through a
468      * rewritten url
469      * @return true if client submitted their sessionId via a rewritten url
470      */

471     public boolean isRequestedSessionIdFromUrl()
472     throws HttpPresentationException {
473     return isRequestedSessionIdFromUrl;
474     }
475
476     /**
477      * Returns the value of a header field, or null if not known.
478      * The case of the header field name is ignored.
479      * @param name the case-insensitive header field name
480      */

481     public String JavaDoc getHeader(String JavaDoc name) throws HttpPresentationException {
482         return request.getHeader(name);
483     }
484
485     /**
486      * Returns the value of an integer header field, or -1 if not found.
487      * The case of the header field name is ignored.
488      * @param name the case-insensitive header field name
489      */

490     public int getIntHeader(String JavaDoc name) throws HttpPresentationException {
491         return request.getIntHeader(name);
492     }
493
494     /**
495      * Returns the value of a date header field, or -1 if not found.
496      * The case of the header field name is ignored.
497      * @param name the case-insensitive header field name
498      */

499     public long getDateHeader(String JavaDoc name) throws HttpPresentationException {
500         return request.getDateHeader(name);
501     }
502
503     /**
504      * Returns an enumeration of strings representing the header names
505      * for this request. Some server implementations do not allow headers
506      * to be accessed in this way, in which case this method will return null.
507      */

508     public Enumeration JavaDoc getHeaderNames() throws HttpPresentationException {
509         return request.getHeaderNames();
510     }
511
512     /**
513      * Get the URI path for a file in the application. This converts a path
514      * to the file part of the URL. It adds in the reference to application
515      * servlet.
516      *
517      * @param file File with in the application. Currently this must
518      * be a path relative to the presentation prefix.
519      * @return The file path portion of the URL, starting with
520      * a <CODE>/</CODE>.
521      */

522     public String JavaDoc getAppFileURIPath(String JavaDoc file)
523     throws HttpPresentationException {
524
525         StringBuffer JavaDoc uriPath;
526         String JavaDoc presPath = getApplicationPath();
527
528         if (presPath.charAt(presPath.length()-1) == '/') {
529             uriPath = new StringBuffer JavaDoc(presPath.substring(0, presPath.length()-1));
530         } else {
531             uriPath = new StringBuffer JavaDoc(presPath);
532         }
533
534         // Convert system dependent file path into a URL file path.
535
Stack JavaDoc names = new Stack JavaDoc();
536         File JavaDoc fparse = new File JavaDoc(file);
537         String JavaDoc parent;
538         while ((parent = fparse.getParent()) != null) {
539             names.push(fparse.getName());
540             fparse = new File JavaDoc(parent);
541         }
542         names.push(fparse.getName());
543
544         while (!names.empty()) {
545             uriPath.append('/');
546             uriPath.append((String JavaDoc)names.pop());
547         }
548         return uriPath.toString();
549     }
550
551     /**
552      * Returns the total number of bytes processed as part of this request.
553      * This is not supported for Jolt Presentations and is necessary
554      * to support the DebugRequest interface.
555      *
556      * @see com.lutris.http.debug.DebugRequest
557      */

558     public int getTotalBytes() {
559     // FIX: When we have our own servlet implementation, we can
560
// FIX: support this.
561
return -1;
562     }
563
564     /**
565      * Returns the part of the request URI that refers to the presentation
566      * object being invoked. This is the same as getPresentationPath(),
567      * but is necessary to support the DebugRequest interface.
568      *
569      * @see com.lutris.http.debug.DebugRequest
570      */

571     public String JavaDoc getPath() throws HttpPresentationException {
572         return getPresentationPath();
573     }
574 }
575
Popular Tags