KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > commandline > CommandLineRequest


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

16 package org.apache.cocoon.environment.commandline;
17
18 import java.util.Collections JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.cocoon.Constants;
26 import org.apache.cocoon.environment.Cookie;
27 import org.apache.cocoon.environment.Environment;
28 import org.apache.cocoon.environment.Request;
29 import org.apache.cocoon.environment.Session;
30 import org.apache.commons.collections.IteratorUtils;
31 import org.apache.commons.lang.SystemUtils;
32
33 /**
34  * Creates a specific servlet request simulation from command line usage.
35  *
36  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
37  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
38  * @version $Id: CommandLineRequest.java 202262 2005-06-28 18:02:55Z vgritsenko $
39  */

40
41 /*
42  * NOTE: method with a non-compliant implementation are marked with FIXME
43  * and should be fixed in the future if required
44  */

45 public class CommandLineRequest implements Request {
46
47     private static class EmptyEnumeration implements Enumeration JavaDoc {
48         public boolean hasMoreElements() {
49             return false;
50         }
51         public Object JavaDoc nextElement() {
52             return null;
53         }
54     }
55
56     private Environment env;
57     private String JavaDoc contextPath;
58     private String JavaDoc servletPath;
59     private String JavaDoc pathInfo;
60     private Map JavaDoc attributes;
61     private Map JavaDoc parameters;
62     private Map JavaDoc headers;
63     private String JavaDoc characterEncoding;
64
65     public CommandLineRequest(Environment env,
66                               String JavaDoc contextPath,
67                               String JavaDoc servletPath,
68                               String JavaDoc pathInfo) {
69         this(env, contextPath, servletPath, pathInfo, null, null, null);
70     }
71
72     public CommandLineRequest(Environment env,
73                               String JavaDoc contextPath,
74                               String JavaDoc servletPath,
75                               String JavaDoc pathInfo,
76                               Map JavaDoc attributes) {
77         this(env, contextPath, servletPath, pathInfo, attributes, null, null);
78     }
79
80     public CommandLineRequest(Environment env,
81                               String JavaDoc contextPath,
82                               String JavaDoc servletPath,
83                               String JavaDoc pathInfo,
84                               Map JavaDoc attributes,
85                               Map JavaDoc parameters) {
86         this(env, contextPath, servletPath, pathInfo, attributes, parameters, null);
87     }
88
89     public CommandLineRequest(Environment env,
90                               String JavaDoc contextPath,
91                               String JavaDoc servletPath,
92                               String JavaDoc pathInfo,
93                               Map JavaDoc attributes,
94                               Map JavaDoc parameters,
95                               Map JavaDoc headers) {
96         this.env = env;
97         this.contextPath = contextPath;
98         this.servletPath = servletPath;
99         this.pathInfo = pathInfo;
100         this.attributes = (attributes == null ? new HashMap JavaDoc() : attributes);
101         this.parameters = parameters;
102         this.headers = headers;
103     }
104
105     /* (non-Javadoc)
106      * @see org.apache.cocoon.environment.Request#get(java.lang.String)
107      */

108     public Object JavaDoc get(String JavaDoc name) {
109         String JavaDoc[] values = this.getParameterValues(name);
110         if (values == null || values.length == 0) {
111             return null;
112         } else if (values.length == 1) {
113             return values[0];
114         } else {
115             Vector JavaDoc vect = new Vector JavaDoc(values.length);
116             for (int i = 0; i < values.length; i++) {
117                 vect.add(values[i]);
118             }
119             return vect;
120         }
121     }
122
123     public String JavaDoc getContextPath() { return contextPath; }
124     public String JavaDoc getServletPath() { return servletPath; }
125     public String JavaDoc getPathInfo() { return pathInfo; }
126     public String JavaDoc getRequestURI() {
127         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
128         if (servletPath != null) buffer.append(servletPath);
129         if (contextPath != null) buffer.append(contextPath);
130         if (pathInfo != null) buffer.append(pathInfo);
131         return buffer.toString();
132     }
133     // FIXME
134
public String JavaDoc getSitemapURI() {
135         return this.env.getURI();
136     }
137     public String JavaDoc getSitemapURIPrefix() {
138         return this.env.getURIPrefix();
139     }
140     public String JavaDoc getQueryString() { return null; } // use parameters instead
141
public String JavaDoc getPathTranslated() { return null; } // FIXME (SM) this is legal but should we do something more?
142

143     public Object JavaDoc getAttribute(String JavaDoc name) {
144         return this.attributes.get(name);
145     }
146     public Enumeration JavaDoc getAttributeNames() {
147         return IteratorUtils.asEnumeration(this.attributes.keySet().iterator());
148     }
149     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
150         this.attributes.put(name, value);
151     }
152     public void removeAttribute(String JavaDoc name) {
153         this.attributes.remove(name);
154     }
155
156     public String JavaDoc getParameter(String JavaDoc name) {
157         if (this.parameters == null) {
158             return null;
159         }
160
161         final Object JavaDoc value = this.parameters.get(name);
162         if (value instanceof String JavaDoc) {
163             return (String JavaDoc)value;
164         } else if (value == null) {
165             return null;
166         } else {
167             final String JavaDoc[] values = (String JavaDoc[]) value;
168             if (values.length == 0) {
169                 return null;
170             }
171             return values[0];
172         }
173     }
174
175     public Enumeration JavaDoc getParameterNames() {
176         return (this.parameters != null) ? IteratorUtils.asEnumeration(this.parameters.keySet().iterator()) : null;
177     }
178
179     public String JavaDoc[] getParameterValues(String JavaDoc name) {
180         final Object JavaDoc value = this.parameters.get(name);
181         if (value instanceof String JavaDoc) {
182             return new String JavaDoc[] { (String JavaDoc)value };
183         } else {
184             return (String JavaDoc[]) value;
185         }
186     }
187
188     public String JavaDoc getHeader(String JavaDoc name) {
189         return (headers != null) ? (String JavaDoc) headers.get(name.toLowerCase()) : null;
190     }
191
192     public int getIntHeader(String JavaDoc name) {
193         String JavaDoc header = (headers != null) ? (String JavaDoc) headers.get(name.toLowerCase()) : null;
194         return (header != null) ? Integer.parseInt(header) : -1;
195     }
196
197     public long getDateHeader(String JavaDoc name) {
198         //FIXME
199
return 0;
200     }
201
202     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
203         // FIXME
204
return new EmptyEnumeration();
205     }
206
207     public Enumeration JavaDoc getHeaderNames() {
208         if (headers != null) {
209             return IteratorUtils.asEnumeration(headers.keySet().iterator());
210         } else {
211             return new EmptyEnumeration();
212         }
213     }
214
215     public String JavaDoc getCharacterEncoding() { return characterEncoding; }
216     public int getContentLength() { return -1; }
217
218     public String JavaDoc getContentType() { return null; }
219     public String JavaDoc getProtocol() { return "cli"; }
220     public String JavaDoc getScheme() { return "cli"; }
221     public String JavaDoc getServerName() { return Constants.COMPLETE_NAME; }
222     public int getServerPort() { return -1; }
223     public String JavaDoc getRemoteAddr() { return "127.0.0.1"; }
224     public String JavaDoc getRemoteHost() { return "localhost"; }
225     public String JavaDoc getMethod() { return "get"; }
226     public String JavaDoc getRemoteUser() { return SystemUtils.USER_NAME; }
227
228     public Cookie[] getCookies() { return null; }
229     public Map JavaDoc getCookieMap() {
230         return Collections.unmodifiableMap(new HashMap JavaDoc());
231     }
232
233     /**
234      * Returns the current session associated with this request,
235      * or if the request does not have a session, creates one.
236      *
237      * @return the <code>Session</code> associated
238      * with this request
239      *
240      * @see #getSession(boolean)
241      */

242     public Session getSession() {
243         return this.getSession(true);
244     }
245
246     /**
247      * Returns the current <code>Session</code>
248      * associated with this request or, if if there is no
249      * current session and <code>create</code> is true, returns
250      * a new session.
251      *
252      * <p>If <code>create</code> is <code>false</code>
253      * and the request has no valid <code>Session</code>,
254      * this method returns <code>null</code>.
255      *
256      * <p>To make sure the session is properly maintained,
257      * you must call this method before
258      * the response is committed.
259      *
260      * @param create <code>true</code> to create a new session for this request
261      * if necessary;
262      * <code>false</code> to return <code>null</code> if there's
263      * no current session
264      *
265      * @return the <code>Session</code> associated with this request or
266      * <code>null</code> if <code>create</code> is <code>false</code>
267      * and the request has no valid session
268      *
269      * @see #getSession()
270      */

271     public Session getSession(boolean create) {
272         return CommandLineSession.getSession(create);
273     }
274
275     /**
276      * Returns the session ID specified by the client. This may
277      * not be the same as the ID of the actual session in use.
278      * For example, if the request specified an old (expired)
279      * session ID and the server has started a new session, this
280      * method gets a new session with a new ID. If the request
281      * did not specify a session ID, this method returns
282      * <code>null</code>.
283      *
284      *
285      * @return a <code>String</code> specifying the session
286      * ID, or <code>null</code> if the request did
287      * not specify a session ID
288      *
289      * @see #isRequestedSessionIdValid()
290      */

291     public String JavaDoc getRequestedSessionId() {
292         return (CommandLineSession.getSession(false) != null) ?
293                 CommandLineSession.getSession(false).getId() : null;
294     }
295
296     /**
297      * Checks whether the requested session ID is still valid.
298      *
299      * @return <code>true</code> if this
300      * request has an id for a valid session
301      * in the current session context;
302      * <code>false</code> otherwise
303      *
304      * @see #getRequestedSessionId()
305      * @see #getSession()
306      */

307     public boolean isRequestedSessionIdValid() {
308         return (CommandLineSession.getSession(false) != null);
309     }
310
311     /**
312      * Checks whether the requested session ID came in as a cookie.
313      *
314      * @return <code>true</code> if the session ID
315      * came in as a
316      * cookie; otherwise, <code>false</code>
317      *
318      *
319      * @see #getSession()
320      */

321     public boolean isRequestedSessionIdFromCookie() {
322         return false;
323     }
324
325     /**
326      * Checks whether the requested session ID came in as part of the
327      * request URL.
328      *
329      * @return <code>true</code> if the session ID
330      * came in as part of a URL; otherwise,
331      * <code>false</code>
332      *
333      *
334      * @see #getSession()
335      */

336     public boolean isRequestedSessionIdFromURL() {
337         return false;
338     }
339
340     public Locale JavaDoc getLocale() { return Locale.getDefault(); }
341     public Enumeration JavaDoc getLocales() {
342         // FIXME
343
throw new RuntimeException JavaDoc (getClass().getName() + ".getLocales() method not yet implemented!");
344     }
345
346     public String JavaDoc getAuthType() { return null; }
347     public boolean isSecure() { return false; }
348     public boolean isUserInRole(String JavaDoc role) { return false; }
349     public java.security.Principal JavaDoc getUserPrincipal() { return null; }
350
351     public java.util.Map JavaDoc getParameterMap() { return parameters; }
352     public void setCharacterEncoding(java.lang.String JavaDoc env)
353                           throws java.io.UnsupportedEncodingException JavaDoc { characterEncoding = env; }
354     public StringBuffer JavaDoc getRequestURL() { return null; }
355 }
356
Popular Tags