1 package org.sapia.soto.state.cocoon.standalone; 2 3 import org.apache.cocoon.environment.http.HttpContext; 4 import org.apache.cocoon.environment.http.HttpEnvironment; 5 6 import java.io.File ; 7 import java.io.IOException ; 8 9 import java.net.MalformedURLException ; 10 import java.net.URL ; 11 12 import javax.servlet.ServletContext ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 16 17 25 public class CocoonObjectFactory { 26 public static final String DEFAULT_ENCODING = "ISO-8859-1"; 27 28 public static HttpEnvironment getEnvironment(HttpServletRequest req, 29 HttpServletResponse res, ServletContext ctx, String containerEncoding, 30 String defaultFormEncoding) throws IOException { 31 HttpEnvironment env = new HttpEnvironment(getUriFromRequest(req), 32 getRootFromContext(ctx), req, res, ctx, new HttpContext(ctx), 33 (containerEncoding == null) ? DEFAULT_ENCODING : containerEncoding, 34 (defaultFormEncoding == null) ? DEFAULT_ENCODING : defaultFormEncoding); 35 36 return env; 37 } 38 39 private static String getUriFromRequest(HttpServletRequest request) { 40 String uri = request.getServletPath(); 41 42 if (uri == null) { 43 uri = ""; 44 } 45 46 String pathInfo = request.getPathInfo(); 47 48 if (pathInfo != null) { 49 if ((uri.length() > 0) && (uri.charAt(0) == '/')) { 52 uri = uri.substring(1); 53 } 54 55 uri += pathInfo; 56 } 57 58 if (uri.charAt(0) == '/') { 74 uri = uri.substring(1); 75 } 76 77 return uri; 78 } 79 80 private static String getRootFromContext(ServletContext ctx) 81 throws IOException { 82 String servletContextPath = ctx.getRealPath("/"); 83 84 String path = servletContextPath; 110 111 if (path == null) { 112 try { 113 URL url = ctx.getResource("/WEB-INF"); 114 115 if (url != null) { 116 path = url.toExternalForm(); 117 } else { 118 path = new File (System.getProperty("user.dir")).toURL() 119 .toExternalForm(); 120 } 121 } catch (MalformedURLException me) { 122 throw new MalformedURLException ("Unable to get resource 'WEB-INF' - " + 123 me.getMessage()); 124 } 125 126 path = path.substring(0, path.length() - "WEB-INF".length()); 127 } 128 129 String servletContextURL; 130 131 try { 132 if (path.indexOf(':') > 1) { 133 servletContextURL = path; 134 } else { 135 servletContextURL = new File (path).toURL().toExternalForm(); 136 } 137 } catch (MalformedURLException me) { 138 try { 142 servletContextURL = new File (path).toURL().toExternalForm(); 143 } catch (MalformedURLException ignored) { 144 throw new MalformedURLException ( 145 "Unable to determine servlet context URL. - " + me.getMessage()); 146 } 147 } 148 149 return servletContextPath; 150 } 151 } 152 | Popular Tags |