KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > server > AbstractHttpServletRequestWrapper


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.server;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import java.security.Principal JavaDoc;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.Locale JavaDoc;
30
31 import javax.servlet.RequestDispatcher JavaDoc;
32 import javax.servlet.ServletInputStream JavaDoc;
33 import javax.servlet.http.Cookie JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36
37 import org.apache.cactus.ServletURL;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 /**
42  * Abstract wrapper around {@link HttpServletRequest}. This class provides
43  * a common implementation of the wrapper for the different Servlet APIs.
44  * This is an implementation that delegates all the call to the
45  * {@link HttpServletRequest} object passed in the constructor except for
46  * some overidden methods which are use to simulate a URL. This is to be able
47  * to simulate any URL that would have been used to call the test method : if
48  * this was not done, the URL that would be returned (by calling the
49  * {@link HttpServletRequest#getRequestURI()} method or others alike) would be
50  * the URL of the Cactus redirector servlet and not a URL that the test case
51  * want to simulate.
52  *
53  * @version $Id: AbstractHttpServletRequestWrapper.java,v 1.2 2004/05/22 16:39:34 vmassol Exp $
54  */

55 public abstract class AbstractHttpServletRequestWrapper
56     implements HttpServletRequest JavaDoc
57 {
58     /**
59      * The logger
60      */

61     private static final Log LOGGER =
62         LogFactory.getLog(AbstractHttpServletRequestWrapper.class);
63
64     /**
65      * The real HTTP request
66      */

67     protected HttpServletRequest JavaDoc request;
68
69     /**
70      * The URL to simulate
71      */

72     protected ServletURL url;
73
74     /**
75      * Remote IP address to simulate (if any)
76      * @see #setRemoteIPAddress(String)
77      */

78     protected String JavaDoc remoteIPAddress;
79
80     /**
81      * Remote Host name to simulate (if any)
82      * @see #setRemoteHostName(String)
83      */

84     protected String JavaDoc remoteHostName;
85
86     /**
87      * Remote user to simulate (if any)
88      * @see #setRemoteUser(String)
89      */

90     protected String JavaDoc remoteUser;
91
92     // New methods not in the interface --------------------------------------
93

94     /**
95      * Construct an <code>HttpServletRequest</code> instance that delegates
96      * it's method calls to the request object passed as parameter and that
97      * uses the URL passed as parameter to simulate a URL from which the request
98      * would come from.
99      *
100      * @param theRequest the real HTTP request
101      * @param theURL the URL to simulate or <code>null</code> if none
102      */

103     public AbstractHttpServletRequestWrapper(HttpServletRequest JavaDoc theRequest,
104         ServletURL theURL)
105     {
106         this.request = theRequest;
107         this.url = theURL;
108     }
109
110     /**
111      * @return the original request object
112      */

113     public HttpServletRequest JavaDoc getOriginalRequest()
114     {
115         return this.request;
116     }
117
118     /**
119      * Simulates the remote IP address (ie the client IP address).
120      *
121      * @param theRemoteIPAddress the simulated IP address in string format.
122      * Exemple : "127.0.0.1"
123      */

124     public void setRemoteIPAddress(String JavaDoc theRemoteIPAddress)
125     {
126         this.remoteIPAddress = theRemoteIPAddress;
127     }
128
129     /**
130      * Simulates the remote host name(ie the client host name).
131      *
132      * @param theRemoteHostName the simulated host name in string format.
133      * Exemple : "atlantis"
134      */

135     public void setRemoteHostName(String JavaDoc theRemoteHostName)
136     {
137         this.remoteHostName = theRemoteHostName;
138     }
139
140     /**
141      * Sets the remote user name to simulate.
142      *
143      * @param theRemoteUser the simulated remote user name
144      */

145     public void setRemoteUser(String JavaDoc theRemoteUser)
146     {
147         this.remoteUser = theRemoteUser;
148     }
149
150     // Modified methods ------------------------------------------------------
151

152     /**
153      * @return the context path from the simulated URL or the real context path
154      * if a simulation URL has not been defined. The real context path
155      * will be returned if the context path defined in the simulated
156      * URL has a null value.
157      */

158     public String JavaDoc getContextPath()
159     {
160         String JavaDoc result = this.request.getContextPath();
161
162         if ((this.url != null) && (this.url.getContextPath() != null))
163         {
164             result = this.url.getContextPath();
165             LOGGER.debug("Using simulated context : [" + result + "]");
166         }
167
168         return result;
169     }
170
171     /**
172      * @return the path info from the simulated URL or the real path info
173      * if a simulation URL has not been defined.
174      */

175     public String JavaDoc getPathInfo()
176     {
177         String JavaDoc result;
178
179         if (this.url != null)
180         {
181             result = this.url.getPathInfo();
182             LOGGER.debug("Using simulated PathInfo : [" + result + "]");
183         }
184         else
185         {
186             result = this.request.getPathInfo();
187         }
188
189         return result;
190     }
191
192     /**
193      * @return the server name from the simulated URL or the real server name
194      * if a simulation URL has not been defined. If the server name
195      * defined in the simulation URL is null, return the real server
196      * name.
197      */

198     public String JavaDoc getServerName()
199     {
200         String JavaDoc result = this.request.getServerName();
201
202         if ((this.url != null) && (this.url.getHost() != null))
203         {
204             result = this.url.getHost();
205             LOGGER.debug("Using simulated server name : [" + result + "]");
206         }
207
208         return result;
209     }
210
211     /**
212      * @return the server port number from the simulated URL or the real server
213      * port number if a simulation URL has not been defined. If no
214      * port is defined in the simulation URL, then port 80 is returned.
215      * If the server name has been defined with a null value in
216      * in the simulation URL, return the real server port.
217      */

218     public int getServerPort()
219     {
220         int result = this.request.getServerPort();
221
222         if ((this.url != null) && (this.url.getServerName() != null))
223         {
224             result = (this.url.getPort() == -1) ? 80 : this.url.getPort();
225             LOGGER.debug("Using simulated server port : [" + result + "]");
226         }
227
228         return result;
229     }
230
231     /**
232      * @return the URI from the simulated URL or the real URI
233      * if a simulation URL has not been defined.
234      */

235     public String JavaDoc getRequestURI()
236     {
237         String JavaDoc result;
238
239         if (this.url != null)
240         {
241             result = getContextPath()
242                 + ((getServletPath() == null) ? "" : getServletPath())
243                 + ((getPathInfo() == null) ? "" : getPathInfo());
244
245             LOGGER.debug("Using simulated request URI : [" + result + "]");
246         }
247         else
248         {
249             result = this.request.getRequestURI();
250         }
251
252         return result;
253     }
254
255     /**
256      * @return the servlet path from the simulated URL or the real servlet path
257      * if a simulation URL has not been defined. The real servlet path
258      * will be returned if the servlet path defined in the simulated
259      * URL has a null value.
260      */

261     public String JavaDoc getServletPath()
262     {
263         String JavaDoc result = this.request.getServletPath();
264
265         if ((this.url != null) && (this.url.getServletPath() != null))
266         {
267             result = this.url.getServletPath();
268             LOGGER.debug("Using simulated servlet path : [" + result + "]");
269         }
270
271         return result;
272     }
273
274     /**
275      * @return any extra path information after the servlet name but
276      * before the query string, and translates it to a real path.
277      * Takes into account the simulated URL (if any).
278      */

279     public String JavaDoc getPathTranslated()
280     {
281         String JavaDoc pathTranslated;
282
283         if ((this.url != null) && (this.url.getPathInfo() != null))
284         {
285             String JavaDoc pathInfo = this.url.getPathInfo();
286             
287             // If getRealPath returns null then getPathTranslated should also
288
// return null (see section SRV.4.5 of the Servlet 2.3 spec).
289
if (this.request.getRealPath("/") == null)
290             {
291                 pathTranslated = null;
292             }
293             else
294             {
295                 // Compute the translated path using the root real path
296
String JavaDoc newPathInfo = (pathInfo.startsWith("/")
297                     ? pathInfo.substring(1) : pathInfo);
298
299                 if (this.request.getRealPath("/").endsWith("/"))
300                 {
301                     pathTranslated = this.request.getRealPath("/")
302                         + newPathInfo.replace('/', File.separatorChar);
303                 }
304                 else
305                 {
306                     pathTranslated = this.request.getRealPath("/")
307                         + File.separatorChar + newPathInfo.replace('/',
308                         File.separatorChar);
309                 }
310             }
311         }
312         else
313         {
314             pathTranslated = this.request.getPathTranslated();
315         }
316
317         return pathTranslated;
318     }
319
320     /**
321      * @return the query string from the simulated URL or the real query
322      * string if a simulation URL has not been defined.
323      */

324     public String JavaDoc getQueryString()
325     {
326         String JavaDoc result;
327
328         if (this.url != null)
329         {
330             result = this.url.getQueryString();
331             LOGGER.debug("Using simulated query string : [" + result + "]");
332         }
333         else
334         {
335             result = this.request.getQueryString();
336         }
337
338         return result;
339     }
340
341     /**
342      * @param thePath the path to the resource
343      * @return a wrapped request dispatcher instead of the real one, so that
344      * forward() and include() calls will use the wrapped dispatcher
345      * passing it the *original* request [this is needed for some
346      * servlet engine like Tomcat 3.x which do not support the new
347      * mechanism introduced by Servlet 2.3 Filters].
348      * @see HttpServletRequest#getRequestDispatcher(String)
349      */

350     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc thePath)
351     {
352         // I hate it, but we have to write some logic here ! Ideally we
353
// shouldn't have to do this as it is supposed to be done by the servlet
354
// engine. However as we are simulating the request URL, we have to
355
// provide it ... This is where we can see the limitation of Cactus
356
// (it has to mock some parts of the servlet engine) !
357
if (thePath == null)
358         {
359             return null;
360         }
361
362         RequestDispatcher JavaDoc dispatcher = null;
363         String JavaDoc fullPath;
364
365         // The spec says that the path can be relative, in which case it will
366
// be relative to the request. So for relative paths, we need to take
367
// into account the simulated URL (ServletURL).
368
if (thePath.startsWith("/"))
369         {
370             fullPath = thePath;
371         }
372         else
373         {
374             String JavaDoc pI = getPathInfo();
375
376             if (pI == null)
377             {
378                 fullPath = catPath(getServletPath(), thePath);
379             }
380             else
381             {
382                 fullPath = catPath(getServletPath() + pI, thePath);
383             }
384
385             if (fullPath == null)
386             {
387                 return null;
388             }
389         }
390
391         LOGGER.debug("Computed full path : [" + fullPath + "]");
392
393         dispatcher = new RequestDispatcherWrapper(
394             this.request.getRequestDispatcher(fullPath));
395
396         return dispatcher;
397     }
398
399     /**
400      * Will concatenate 2 paths, normalising it. For example :
401      * ( /a/b/c + d = /a/b/d, /a/b/c + ../d = /a/d ). Code borrowed from
402      * Tomcat 3.2.2 !
403      *
404      * @param theLookupPath the first part of the path
405      * @param thePath the part to add to the lookup path
406      * @return the concatenated thePath or null if an error occurs
407      */

408     private String JavaDoc catPath(String JavaDoc theLookupPath, String JavaDoc thePath)
409     {
410         // Cut off the last slash and everything beyond
411
int index = theLookupPath.lastIndexOf("/");
412
413         theLookupPath = theLookupPath.substring(0, index);
414
415         // Deal with .. by chopping dirs off the lookup thePath
416
while (thePath.startsWith("../"))
417         {
418             if (theLookupPath.length() > 0)
419             {
420                 index = theLookupPath.lastIndexOf("/");
421                 theLookupPath = theLookupPath.substring(0, index);
422             }
423             else
424             {
425                 // More ..'s than dirs, return null
426
return null;
427             }
428
429             index = thePath.indexOf("../") + 3;
430             thePath = thePath.substring(index);
431         }
432
433         return theLookupPath + "/" + thePath;
434     }
435
436     /**
437      * @return the simulated remote IP address if any or the real one.
438      *
439      * @see HttpServletRequest#getRemoteAddr()
440      */

441     public String JavaDoc getRemoteAddr()
442     {
443         String JavaDoc remoteIPAddress;
444
445         if (this.remoteIPAddress != null)
446         {
447             remoteIPAddress = this.remoteIPAddress;
448         }
449         else
450         {
451             remoteIPAddress = this.request.getRemoteAddr();
452         }
453
454         return remoteIPAddress;
455     }
456
457     /**
458      * @return the simulated remote host name if any or the real one.
459      *
460      * @see HttpServletRequest#getRemoteHost()
461      */

462     public String JavaDoc getRemoteHost()
463     {
464         String JavaDoc remoteHostName;
465
466         if (this.remoteHostName != null)
467         {
468             remoteHostName = this.remoteHostName;
469         }
470         else
471         {
472             remoteHostName = this.request.getRemoteHost();
473         }
474
475         return remoteHostName;
476     }
477
478     /**
479      * @return the simulated remote user name if any or the real one.
480      *
481      * @see HttpServletRequest#getRemoteUser()
482      */

483     public String JavaDoc getRemoteUser()
484     {
485         String JavaDoc remoteUser;
486
487         if (this.remoteUser != null)
488         {
489             remoteUser = this.remoteUser;
490         }
491         else
492         {
493             remoteUser = this.request.getRemoteUser();
494         }
495
496         return remoteUser;
497     }
498
499     // Not modified methods --------------------------------------------------
500

501     /**
502      * @see HttpServletRequest#isRequestedSessionIdFromURL()
503      */

504     public boolean isRequestedSessionIdFromURL()
505     {
506         return this.request.isRequestedSessionIdFromURL();
507     }
508
509     /**
510      * @see HttpServletRequest#isRequestedSessionIdFromUrl()
511      */

512     public boolean isRequestedSessionIdFromUrl()
513     {
514         return this.request.isRequestedSessionIdFromURL();
515     }
516
517     /**
518      * @see HttpServletRequest#isUserInRole(String)
519      */

520     public boolean isUserInRole(String JavaDoc theRole)
521     {
522         return this.request.isUserInRole(theRole);
523     }
524
525     /**
526      * @see HttpServletRequest#isRequestedSessionIdValid()
527      */

528     public boolean isRequestedSessionIdValid()
529     {
530         return this.request.isRequestedSessionIdValid();
531     }
532
533     /**
534      * @see HttpServletRequest#isRequestedSessionIdFromCookie()
535      */

536     public boolean isRequestedSessionIdFromCookie()
537     {
538         return this.request.isRequestedSessionIdFromCookie();
539     }
540
541     /**
542      * @see HttpServletRequest#getLocales()
543      */

544     public Enumeration JavaDoc getLocales()
545     {
546         return this.request.getLocales();
547     }
548
549     /**
550      * @see HttpServletRequest#getHeader(String)
551      */

552     public String JavaDoc getHeader(String JavaDoc theName)
553     {
554         return this.request.getHeader(theName);
555     }
556
557     /**
558      * @see HttpServletRequest#getHeaders(String)
559      */

560     public Enumeration JavaDoc getHeaders(String JavaDoc theName)
561     {
562         return this.request.getHeaders(theName);
563     }
564
565     /**
566      * @see HttpServletRequest#getHeaderNames()
567      */

568     public Enumeration JavaDoc getHeaderNames()
569     {
570         return this.request.getHeaderNames();
571     }
572
573     /**
574      * @see HttpServletRequest#getScheme()
575      */

576     public String JavaDoc getScheme()
577     {
578         return this.request.getScheme();
579     }
580
581     /**
582      * @see HttpServletRequest#getAuthType()
583      */

584     public String JavaDoc getAuthType()
585     {
586         return this.request.getAuthType();
587     }
588
589     /**
590      * @see HttpServletRequest#getRealPath(String)
591      */

592     public String JavaDoc getRealPath(String JavaDoc thePath)
593     {
594         return this.request.getRealPath(thePath);
595     }
596
597     /**
598      * @see HttpServletRequest#getSession()
599      */

600     public HttpSession JavaDoc getSession()
601     {
602         return this.request.getSession();
603     }
604
605     /**
606      * @see HttpServletRequest#getSession(boolean)
607      */

608     public HttpSession JavaDoc getSession(boolean isCreate)
609     {
610         return this.request.getSession(isCreate);
611     }
612
613     /**
614      * @see HttpServletRequest#getReader()
615      */

616     public BufferedReader JavaDoc getReader() throws IOException JavaDoc
617     {
618         return this.request.getReader();
619     }
620
621     /**
622      * @see HttpServletRequest#getContentLength()
623      */

624     public int getContentLength()
625     {
626         return this.request.getContentLength();
627     }
628
629     /**
630      * @see HttpServletRequest#getParameterValues(String)
631      */

632     public String JavaDoc[] getParameterValues(String JavaDoc theName)
633     {
634         return this.request.getParameterValues(theName);
635     }
636
637     /**
638      * @see HttpServletRequest#getContentType()
639      */

640     public String JavaDoc getContentType()
641     {
642         return this.request.getContentType();
643     }
644
645     /**
646      * @see HttpServletRequest#getLocale()
647      */

648     public Locale JavaDoc getLocale()
649     {
650         return this.request.getLocale();
651     }
652
653     /**
654      * @see HttpServletRequest#removeAttribute(String)
655      */

656     public void removeAttribute(String JavaDoc theName)
657     {
658         this.request.removeAttribute(theName);
659     }
660
661     /**
662      * @see HttpServletRequest#getParameter(String)
663      */

664     public String JavaDoc getParameter(String JavaDoc theName)
665     {
666         return this.request.getParameter(theName);
667     }
668
669     /**
670      * @see HttpServletRequest#getInputStream()
671      */

672     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc
673     {
674         return this.request.getInputStream();
675     }
676
677     /**
678      * @see HttpServletRequest#getUserPrincipal()
679      */

680     public Principal JavaDoc getUserPrincipal()
681     {
682         return this.request.getUserPrincipal();
683     }
684
685     /**
686      * @see HttpServletRequest#isSecure()
687      */

688     public boolean isSecure()
689     {
690         return this.request.isSecure();
691     }
692
693     /**
694      * @see HttpServletRequest#getCharacterEncoding()
695      */

696     public String JavaDoc getCharacterEncoding()
697     {
698         return this.request.getCharacterEncoding();
699     }
700
701     /**
702      * @see HttpServletRequest#getParameterNames()
703      */

704     public Enumeration JavaDoc getParameterNames()
705     {
706         return this.request.getParameterNames();
707     }
708
709     /**
710      * @see HttpServletRequest#getMethod()
711      */

712     public String JavaDoc getMethod()
713     {
714         return this.request.getMethod();
715     }
716
717     /**
718      * @see HttpServletRequest#setAttribute(String, Object)
719      */

720     public void setAttribute(String JavaDoc theName, Object JavaDoc theAttribute)
721     {
722         this.request.setAttribute(theName, theAttribute);
723     }
724
725     /**
726      * @see HttpServletRequest#getAttribute(String)
727      */

728     public Object JavaDoc getAttribute(String JavaDoc theName)
729     {
730         return this.request.getAttribute(theName);
731     }
732
733     /**
734      * @see HttpServletRequest#getIntHeader(String)
735      */

736     public int getIntHeader(String JavaDoc theName)
737     {
738         return this.request.getIntHeader(theName);
739     }
740
741     /**
742      * @see HttpServletRequest#getDateHeader(String)
743      */

744     public long getDateHeader(String JavaDoc theName)
745     {
746         return this.request.getDateHeader(theName);
747     }
748
749     /**
750      * @see HttpServletRequest#getAttributeNames()
751      */

752     public Enumeration JavaDoc getAttributeNames()
753     {
754         return this.request.getAttributeNames();
755     }
756
757     /**
758      * @see HttpServletRequest#getRequestedSessionId()
759      */

760     public String JavaDoc getRequestedSessionId()
761     {
762         return this.request.getRequestedSessionId();
763     }
764
765     /**
766      * @see HttpServletRequest#getCookies()
767      */

768     public Cookie JavaDoc[] getCookies()
769     {
770         return this.request.getCookies();
771     }
772
773     /**
774      * @see HttpServletRequest#getProtocol()
775      */

776     public String JavaDoc getProtocol()
777     {
778         return this.request.getProtocol();
779     }
780 }
781
Popular Tags