View Javadoc

1   /*
2    * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted under the terms of either of the following
6    * Open Source licenses:
7    *
8    * The GNU General Public License, version 2, or any later version, as
9    * published by the Free Software Foundation
10   * (http://www.fsf.org/copyleft/gpl.html);
11   *
12   *  or
13   *
14   * The Semiotek Public License (http://webmacro.org/LICENSE.)
15   *
16   * This software is provided "as is", with NO WARRANTY, not even the
17   * implied warranties of fitness to purpose, or merchantability. You
18   * assume all risks and liabilities associated with its use.
19   *
20   * See www.webmacro.org for more information on the WebMacro project.
21   */
22  
23  
24  package org.webmacro.servlet;
25  
26  import javax.servlet.ServletContext;
27  import javax.servlet.http.HttpServletRequest;
28  
29  
30  /***
31   * <p>
32   * Provided to mimic the CGI environment within the WebMacro script
33   * language via introspection. The data in this class duplicates
34   * information already available in request, but makes it available
35   * in a familiar form.
36   * </p><p>
37   * From the WebMacro script language you can refer to the properties
38   * contained in this class with names that exactly duplicate the names
39   * familiar to CGI programmers. eg: REQUEST_METHOD, PATH_INFO, etc.
40   * </p>
41   */
42  final public class CGI_Impersonator
43  {
44  
45      /***
46       * This is the request object from the WebContext
47       */
48      final HttpServletRequest requst_;
49  
50      final ServletContext sc_;
51  
52      /***
53       * Use the supplied HttpServletRequest to produce the results
54       * below. Really this class just forwards methods to this sub
55       * object in order to provide a familiar interface to CGI programmers.
56       */
57      CGI_Impersonator (WebContext wc)
58      {
59          requst_ = wc.getRequest();
60          // this is not very nice, but I don't see any other
61          // possibility to get the servlet context. We should
62          // provide a method in WebContext to hide this from our users.
63          sc_ = ((ServletBroker) wc.getBroker()).getServletContext();
64      }
65  
66      /***
67       * Return the name of the server
68       */
69      final public String getSERVER_NAME ()
70      {
71          return requst_.getServerName();
72      }
73  
74      /***
75       * Return the server info
76       */
77      final public String getSERVER_SOFTWARE ()
78      {
79          return sc_.getServerInfo();
80      }
81  
82  
83      /***
84       * Return the server protocol
85       */
86      final public String getSERVER_PROTOCOL ()
87      {
88          return requst_.getProtocol();
89      }
90  
91      /***
92       * Return the server port
93       */
94      final public Integer getSERVER_PORT ()
95      {
96          return new Integer(requst_.getServerPort());
97      }
98  
99      /***
100      * Return what type of REQUEST this was: GET, POST, etc.
101      */
102     final public String getREQUEST_METHOD ()
103     {
104         return requst_.getMethod();
105     }
106 
107     /***
108      * What portion of the URL appeared as additional path beyond
109      * the SCRIPT_NAME portion.
110      * @return The path info.
111      */
112     final public String getPATH_INFO ()
113     {
114         return requst_.getPathInfo();
115     }
116 
117     /***
118      * Same as PATH_INFO but translated to a real path
119      */
120     final public String getPATH_TRANSLATED ()
121     {
122         return requst_.getPathTranslated();
123     }
124 
125     /***
126      * What portion of the URL represents the servlet being run.
127      * @return The servlet being run.
128      */
129     final public String getSCRIPT_NAME ()
130     {
131         return requst_.getServletPath();
132     }
133 
134     /***
135      * What is the root of documents served by this servlet
136      *
137      * WARNING: the method called (getRealPath) is deprecated in Servlet 2.2
138      *
139      */
140     final public String getDOCUMENT_ROOT ()
141     {
142         return sc_.getRealPath("/");
143     }
144 
145     /***
146      * In a GET request, return the query string that was submitted, if any
147      */
148     final public String getQUERY_STRING ()
149     {
150         return requst_.getQueryString();
151     }
152 
153     /***
154      * Return the remote host connected to this request
155      */
156     final public String getREMOTE_HOST ()
157     {
158         return requst_.getRemoteHost();
159     }
160 
161     /***
162      * Return the remove address connected to this servlet
163      */
164     final public String getREMOTE_ADDR ()
165     {
166         return requst_.getRemoteAddr();
167     }
168 
169     /***
170      * Type of authorization for this request
171      */
172     final public String getAUTH_TYPE ()
173     {
174         return requst_.getAuthType();
175     }
176 
177     /***
178      * Name of the remote user if it was supplied with the HTTP request
179      */
180     final public String getREMOTE_USER ()
181     {
182         return requst_.getRemoteUser();
183     }
184 
185     /***
186      * Get the content type submitted to this request
187      */
188     final public String getCONTENT_TYPE ()
189     {
190         return requst_.getContentType();
191     }
192 
193     /***
194      * Get the content length submitted to this request
195      */
196     final public Integer getCONTENT_LENGTH ()
197     {
198         return new Integer(requst_.getContentLength());
199     }
200 
201     /***
202      * What type of data is accepted by the client
203      */
204     final public String getHTTP_ACCEPT ()
205     {
206         return requst_.getHeader("Accept");
207     }
208 
209     /***
210      * Get the user agent (browser) connected to this request
211      */
212     final public String getHTTP_USER_AGENT ()
213     {
214         return requst_.getHeader("User-Agent");
215     }
216 
217     /***
218      * Get the URL that the request claims to have visited prior to this one
219      */
220     final public String getHTTP_REFERER ()
221     {
222         return requst_.getHeader("Referer");
223     }
224 
225 }
226