001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.commandline;
018:
019: import java.util.Collections;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Locale;
023: import java.util.Map;
024: import java.util.Vector;
025:
026: import org.apache.cocoon.Constants;
027: import org.apache.cocoon.environment.Cookie;
028: import org.apache.cocoon.environment.Environment;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.cocoon.environment.Session;
031: import org.apache.commons.collections.IteratorUtils;
032: import org.apache.commons.lang.SystemUtils;
033:
034: /**
035: * Creates a specific servlet request simulation from command line usage.
036: *
037: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
038: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
039: * @version $Id: CommandLineRequest.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041:
042: /*
043: * NOTE: method with a non-compliant implementation are marked with FIXME
044: * and should be fixed in the future if required
045: */
046: public class CommandLineRequest implements Request {
047:
048: private static class EmptyEnumeration implements Enumeration {
049: public boolean hasMoreElements() {
050: return false;
051: }
052:
053: public Object nextElement() {
054: return null;
055: }
056: }
057:
058: private Environment env;
059: private String contextPath;
060: private String servletPath;
061: private String pathInfo;
062: private Map attributes;
063: private Map parameters;
064: private Map headers;
065: private String characterEncoding;
066:
067: public CommandLineRequest(Environment env, String contextPath,
068: String servletPath, String pathInfo) {
069: this (env, contextPath, servletPath, pathInfo, null, null, null);
070: }
071:
072: public CommandLineRequest(Environment env, String contextPath,
073: String servletPath, String pathInfo, Map attributes) {
074: this (env, contextPath, servletPath, pathInfo, attributes, null,
075: null);
076: }
077:
078: public CommandLineRequest(Environment env, String contextPath,
079: String servletPath, String pathInfo, Map attributes,
080: Map parameters) {
081: this (env, contextPath, servletPath, pathInfo, attributes,
082: parameters, null);
083: }
084:
085: public CommandLineRequest(Environment env, String contextPath,
086: String servletPath, String pathInfo, Map attributes,
087: Map parameters, Map headers) {
088: this .env = env;
089: this .contextPath = contextPath;
090: this .servletPath = servletPath;
091: this .pathInfo = pathInfo;
092: this .attributes = (attributes == null ? new HashMap()
093: : attributes);
094: this .parameters = parameters;
095: this .headers = headers;
096: }
097:
098: /* (non-Javadoc)
099: * @see org.apache.cocoon.environment.Request#get(java.lang.String)
100: */
101: public Object get(String name) {
102: String[] values = this .getParameterValues(name);
103: if (values == null || values.length == 0) {
104: return null;
105: } else if (values.length == 1) {
106: return values[0];
107: } else {
108: Vector vect = new Vector(values.length);
109: for (int i = 0; i < values.length; i++) {
110: vect.add(values[i]);
111: }
112: return vect;
113: }
114: }
115:
116: public String getContextPath() {
117: return contextPath;
118: }
119:
120: public String getServletPath() {
121: return servletPath;
122: }
123:
124: public String getPathInfo() {
125: return pathInfo;
126: }
127:
128: public String getRequestURI() {
129: StringBuffer buffer = new StringBuffer();
130: if (servletPath != null)
131: buffer.append(servletPath);
132: if (contextPath != null)
133: buffer.append(contextPath);
134: if (pathInfo != null)
135: buffer.append(pathInfo);
136: return buffer.toString();
137: }
138:
139: // FIXME
140: public String getSitemapURI() {
141: return this .env.getURI();
142: }
143:
144: public String getSitemapURIPrefix() {
145: return this .env.getURIPrefix();
146: }
147:
148: public String getQueryString() {
149: return null;
150: } // use parameters instead
151:
152: public String getPathTranslated() {
153: return null;
154: } // FIXME (SM) this is legal but should we do something more?
155:
156: public Object getAttribute(String name) {
157: return this .attributes.get(name);
158: }
159:
160: public Enumeration getAttributeNames() {
161: return IteratorUtils.asEnumeration(this .attributes.keySet()
162: .iterator());
163: }
164:
165: public void setAttribute(String name, Object value) {
166: this .attributes.put(name, value);
167: }
168:
169: public void removeAttribute(String name) {
170: this .attributes.remove(name);
171: }
172:
173: public String getParameter(String name) {
174: if (this .parameters == null) {
175: return null;
176: }
177:
178: final Object value = this .parameters.get(name);
179: if (value instanceof String) {
180: return (String) value;
181: } else if (value == null) {
182: return null;
183: } else {
184: final String[] values = (String[]) value;
185: if (values.length == 0) {
186: return null;
187: }
188: return values[0];
189: }
190: }
191:
192: public Enumeration getParameterNames() {
193: return (this .parameters != null) ? IteratorUtils
194: .asEnumeration(this .parameters.keySet().iterator())
195: : null;
196: }
197:
198: public String[] getParameterValues(String name) {
199: final Object value = this .parameters.get(name);
200: if (value instanceof String) {
201: return new String[] { (String) value };
202: } else {
203: return (String[]) value;
204: }
205: }
206:
207: public String getHeader(String name) {
208: return (headers != null) ? (String) headers.get(name
209: .toLowerCase()) : null;
210: }
211:
212: public int getIntHeader(String name) {
213: String header = (headers != null) ? (String) headers.get(name
214: .toLowerCase()) : null;
215: return (header != null) ? Integer.parseInt(header) : -1;
216: }
217:
218: public long getDateHeader(String name) {
219: //FIXME
220: return 0;
221: }
222:
223: public Enumeration getHeaders(String name) {
224: // FIXME
225: return new EmptyEnumeration();
226: }
227:
228: public Enumeration getHeaderNames() {
229: if (headers != null) {
230: return IteratorUtils.asEnumeration(headers.keySet()
231: .iterator());
232: } else {
233: return new EmptyEnumeration();
234: }
235: }
236:
237: public String getCharacterEncoding() {
238: return characterEncoding;
239: }
240:
241: public int getContentLength() {
242: return -1;
243: }
244:
245: public String getContentType() {
246: return null;
247: }
248:
249: public String getProtocol() {
250: return "cli";
251: }
252:
253: public String getScheme() {
254: return "cli";
255: }
256:
257: public String getServerName() {
258: return Constants.COMPLETE_NAME;
259: }
260:
261: public int getServerPort() {
262: return -1;
263: }
264:
265: public String getRemoteAddr() {
266: return "127.0.0.1";
267: }
268:
269: public String getRemoteHost() {
270: return "localhost";
271: }
272:
273: public String getMethod() {
274: return "get";
275: }
276:
277: public String getRemoteUser() {
278: return SystemUtils.USER_NAME;
279: }
280:
281: public Cookie[] getCookies() {
282: return null;
283: }
284:
285: public Map getCookieMap() {
286: return Collections.unmodifiableMap(new HashMap());
287: }
288:
289: /**
290: * Returns the current session associated with this request,
291: * or if the request does not have a session, creates one.
292: *
293: * @return the <code>Session</code> associated
294: * with this request
295: *
296: * @see #getSession(boolean)
297: */
298: public Session getSession() {
299: return this .getSession(true);
300: }
301:
302: /**
303: * Returns the current <code>Session</code>
304: * associated with this request or, if if there is no
305: * current session and <code>create</code> is true, returns
306: * a new session.
307: *
308: * <p>If <code>create</code> is <code>false</code>
309: * and the request has no valid <code>Session</code>,
310: * this method returns <code>null</code>.
311: *
312: * <p>To make sure the session is properly maintained,
313: * you must call this method before
314: * the response is committed.
315: *
316: * @param create <code>true</code> to create a new session for this request
317: * if necessary;
318: * <code>false</code> to return <code>null</code> if there's
319: * no current session
320: *
321: * @return the <code>Session</code> associated with this request or
322: * <code>null</code> if <code>create</code> is <code>false</code>
323: * and the request has no valid session
324: *
325: * @see #getSession()
326: */
327: public Session getSession(boolean create) {
328: return CommandLineSession.getSession(create);
329: }
330:
331: /**
332: * Returns the session ID specified by the client. This may
333: * not be the same as the ID of the actual session in use.
334: * For example, if the request specified an old (expired)
335: * session ID and the server has started a new session, this
336: * method gets a new session with a new ID. If the request
337: * did not specify a session ID, this method returns
338: * <code>null</code>.
339: *
340: *
341: * @return a <code>String</code> specifying the session
342: * ID, or <code>null</code> if the request did
343: * not specify a session ID
344: *
345: * @see #isRequestedSessionIdValid()
346: */
347: public String getRequestedSessionId() {
348: return (CommandLineSession.getSession(false) != null) ? CommandLineSession
349: .getSession(false).getId()
350: : null;
351: }
352:
353: /**
354: * Checks whether the requested session ID is still valid.
355: *
356: * @return <code>true</code> if this
357: * request has an id for a valid session
358: * in the current session context;
359: * <code>false</code> otherwise
360: *
361: * @see #getRequestedSessionId()
362: * @see #getSession()
363: */
364: public boolean isRequestedSessionIdValid() {
365: return (CommandLineSession.getSession(false) != null);
366: }
367:
368: /**
369: * Checks whether the requested session ID came in as a cookie.
370: *
371: * @return <code>true</code> if the session ID
372: * came in as a
373: * cookie; otherwise, <code>false</code>
374: *
375: *
376: * @see #getSession()
377: */
378: public boolean isRequestedSessionIdFromCookie() {
379: return false;
380: }
381:
382: /**
383: * Checks whether the requested session ID came in as part of the
384: * request URL.
385: *
386: * @return <code>true</code> if the session ID
387: * came in as part of a URL; otherwise,
388: * <code>false</code>
389: *
390: *
391: * @see #getSession()
392: */
393: public boolean isRequestedSessionIdFromURL() {
394: return false;
395: }
396:
397: public Locale getLocale() {
398: return Locale.getDefault();
399: }
400:
401: public Enumeration getLocales() {
402: // FIXME
403: throw new RuntimeException(getClass().getName()
404: + ".getLocales() method not yet implemented!");
405: }
406:
407: public String getAuthType() {
408: return null;
409: }
410:
411: public boolean isSecure() {
412: return false;
413: }
414:
415: public boolean isUserInRole(String role) {
416: return false;
417: }
418:
419: public java.security.Principal getUserPrincipal() {
420: return null;
421: }
422:
423: public java.util.Map getParameterMap() {
424: return parameters;
425: }
426:
427: public void setCharacterEncoding(java.lang.String env)
428: throws java.io.UnsupportedEncodingException {
429: characterEncoding = env;
430: }
431:
432: public StringBuffer getRequestURL() {
433: return null;
434: }
435: }
|