1 16 package org.apache.cocoon.environment.commandline; 17 18 import java.util.Collections ; 19 import java.util.Enumeration ; 20 import java.util.HashMap ; 21 import java.util.Locale ; 22 import java.util.Map ; 23 import java.util.Vector ; 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 40 41 45 public class CommandLineRequest implements Request { 46 47 private static class EmptyEnumeration implements Enumeration { 48 public boolean hasMoreElements() { 49 return false; 50 } 51 public Object nextElement() { 52 return null; 53 } 54 } 55 56 private Environment env; 57 private String contextPath; 58 private String servletPath; 59 private String pathInfo; 60 private Map attributes; 61 private Map parameters; 62 private Map headers; 63 private String characterEncoding; 64 65 public CommandLineRequest(Environment env, 66 String contextPath, 67 String servletPath, 68 String pathInfo) { 69 this(env, contextPath, servletPath, pathInfo, null, null, null); 70 } 71 72 public CommandLineRequest(Environment env, 73 String contextPath, 74 String servletPath, 75 String pathInfo, 76 Map attributes) { 77 this(env, contextPath, servletPath, pathInfo, attributes, null, null); 78 } 79 80 public CommandLineRequest(Environment env, 81 String contextPath, 82 String servletPath, 83 String pathInfo, 84 Map attributes, 85 Map parameters) { 86 this(env, contextPath, servletPath, pathInfo, attributes, parameters, null); 87 } 88 89 public CommandLineRequest(Environment env, 90 String contextPath, 91 String servletPath, 92 String pathInfo, 93 Map attributes, 94 Map parameters, 95 Map headers) { 96 this.env = env; 97 this.contextPath = contextPath; 98 this.servletPath = servletPath; 99 this.pathInfo = pathInfo; 100 this.attributes = (attributes == null ? new HashMap () : attributes); 101 this.parameters = parameters; 102 this.headers = headers; 103 } 104 105 108 public Object get(String name) { 109 String [] 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 vect = new Vector (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 getContextPath() { return contextPath; } 124 public String getServletPath() { return servletPath; } 125 public String getPathInfo() { return pathInfo; } 126 public String getRequestURI() { 127 StringBuffer buffer = new StringBuffer (); 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 public String getSitemapURI() { 135 return this.env.getURI(); 136 } 137 public String getSitemapURIPrefix() { 138 return this.env.getURIPrefix(); 139 } 140 public String getQueryString() { return null; } public String getPathTranslated() { return null; } 143 public Object getAttribute(String name) { 144 return this.attributes.get(name); 145 } 146 public Enumeration getAttributeNames() { 147 return IteratorUtils.asEnumeration(this.attributes.keySet().iterator()); 148 } 149 public void setAttribute(String name, Object value) { 150 this.attributes.put(name, value); 151 } 152 public void removeAttribute(String name) { 153 this.attributes.remove(name); 154 } 155 156 public String getParameter(String name) { 157 if (this.parameters == null) { 158 return null; 159 } 160 161 final Object value = this.parameters.get(name); 162 if (value instanceof String ) { 163 return (String )value; 164 } else if (value == null) { 165 return null; 166 } else { 167 final String [] values = (String []) value; 168 if (values.length == 0) { 169 return null; 170 } 171 return values[0]; 172 } 173 } 174 175 public Enumeration getParameterNames() { 176 return (this.parameters != null) ? IteratorUtils.asEnumeration(this.parameters.keySet().iterator()) : null; 177 } 178 179 public String [] getParameterValues(String name) { 180 final Object value = this.parameters.get(name); 181 if (value instanceof String ) { 182 return new String [] { (String )value }; 183 } else { 184 return (String []) value; 185 } 186 } 187 188 public String getHeader(String name) { 189 return (headers != null) ? (String ) headers.get(name.toLowerCase()) : null; 190 } 191 192 public int getIntHeader(String name) { 193 String header = (headers != null) ? (String ) headers.get(name.toLowerCase()) : null; 194 return (header != null) ? Integer.parseInt(header) : -1; 195 } 196 197 public long getDateHeader(String name) { 198 return 0; 200 } 201 202 public Enumeration getHeaders(String name) { 203 return new EmptyEnumeration(); 205 } 206 207 public Enumeration getHeaderNames() { 208 if (headers != null) { 209 return IteratorUtils.asEnumeration(headers.keySet().iterator()); 210 } else { 211 return new EmptyEnumeration(); 212 } 213 } 214 215 public String getCharacterEncoding() { return characterEncoding; } 216 public int getContentLength() { return -1; } 217 218 public String getContentType() { return null; } 219 public String getProtocol() { return "cli"; } 220 public String getScheme() { return "cli"; } 221 public String getServerName() { return Constants.COMPLETE_NAME; } 222 public int getServerPort() { return -1; } 223 public String getRemoteAddr() { return "127.0.0.1"; } 224 public String getRemoteHost() { return "localhost"; } 225 public String getMethod() { return "get"; } 226 public String getRemoteUser() { return SystemUtils.USER_NAME; } 227 228 public Cookie[] getCookies() { return null; } 229 public Map getCookieMap() { 230 return Collections.unmodifiableMap(new HashMap ()); 231 } 232 233 242 public Session getSession() { 243 return this.getSession(true); 244 } 245 246 271 public Session getSession(boolean create) { 272 return CommandLineSession.getSession(create); 273 } 274 275 291 public String getRequestedSessionId() { 292 return (CommandLineSession.getSession(false) != null) ? 293 CommandLineSession.getSession(false).getId() : null; 294 } 295 296 307 public boolean isRequestedSessionIdValid() { 308 return (CommandLineSession.getSession(false) != null); 309 } 310 311 321 public boolean isRequestedSessionIdFromCookie() { 322 return false; 323 } 324 325 336 public boolean isRequestedSessionIdFromURL() { 337 return false; 338 } 339 340 public Locale getLocale() { return Locale.getDefault(); } 341 public Enumeration getLocales() { 342 throw new RuntimeException (getClass().getName() + ".getLocales() method not yet implemented!"); 344 } 345 346 public String getAuthType() { return null; } 347 public boolean isSecure() { return false; } 348 public boolean isUserInRole(String role) { return false; } 349 public java.security.Principal getUserPrincipal() { return null; } 350 351 public java.util.Map getParameterMap() { return parameters; } 352 public void setCharacterEncoding(java.lang.String env) 353 throws java.io.UnsupportedEncodingException { characterEncoding = env; } 354 public StringBuffer getRequestURL() { return null; } 355 } 356 | Popular Tags |