KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > cocoon > standalone > CocoonObjectFactory


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 JavaDoc;
7 import java.io.IOException JavaDoc;
8
9 import java.net.MalformedURLException JavaDoc;
10 import java.net.URL JavaDoc;
11
12 import javax.servlet.ServletContext JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15
16
17 /**
18  * @author Yanick Duchesne
19  * <dl>
20  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
21  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
22  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class CocoonObjectFactory {
26   public static final String JavaDoc DEFAULT_ENCODING = "ISO-8859-1";
27
28   public static HttpEnvironment getEnvironment(HttpServletRequest JavaDoc req,
29     HttpServletResponse JavaDoc res, ServletContext JavaDoc ctx, String JavaDoc containerEncoding,
30     String JavaDoc defaultFormEncoding) throws IOException JavaDoc {
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 JavaDoc getUriFromRequest(HttpServletRequest JavaDoc request) {
40     String JavaDoc uri = request.getServletPath();
41
42     if (uri == null) {
43       uri = "";
44     }
45
46     String JavaDoc pathInfo = request.getPathInfo();
47
48     if (pathInfo != null) {
49       // VG: WebLogic fix: Both uri and pathInfo starts with '/'
50
// This problem exists only in WL6.1sp2, not in WL6.0sp2 or WL7.0b.
51
if ((uri.length() > 0) && (uri.charAt(0) == '/')) {
52         uri = uri.substring(1);
53       }
54
55       uri += pathInfo;
56     }
57
58     // if (uri.length() == 0) {
59
// /* empty relative URI
60
// -> HTTP-redirect from /cocoon to /cocoon/ to avoid
61
// StringIndexOutOfBoundsException when calling
62
// "".charAt(0)
63
// else process URI normally
64
// */
65
// String prefix = request.getRequestURI();
66
// if (prefix == null) {
67
// prefix = "";
68
// }
69
//
70
// res.sendRedirect(res.encodeRedirectURL(prefix + "/"));
71
// return;
72
// }
73
if (uri.charAt(0) == '/') {
74       uri = uri.substring(1);
75     }
76
77     return uri;
78   }
79
80   private static String JavaDoc getRootFromContext(ServletContext JavaDoc ctx)
81     throws IOException JavaDoc {
82     String JavaDoc servletContextPath = ctx.getRealPath("/");
83
84     // // first init the work-directory for the logger.
85
// // this is required if we are running inside a war file!
86
// final String workDirParam = getInitParameter("work-directory");
87
// if (workDirParam != null) {
88
// if (this.servletContextPath == null) {
89
// // No context path : consider work-directory as absolute
90
// this.workDir = new File(workDirParam);
91
// } else {
92
// // Context path exists : is work-directory absolute ?
93
// File workDirParamFile = new File(workDirParam);
94
// if (workDirParamFile.isAbsolute()) {
95
// // Yes : keep it as is
96
// this.workDir = workDirParamFile;
97
// } else {
98
// // No : consider it relative to context path
99
// this.workDir = new File(servletContextPath , workDirParam);
100
// }
101
// }
102
// } else {
103
// this.workDir = (File) this.servletContext.getAttribute("javax.servlet.context.tempdir");
104
// this.workDir = new File(workDir, "cocoon-files");
105
// }
106
// this.workDir.mkdirs();
107
//
108
// initLogger();
109
String JavaDoc path = servletContextPath;
110
111     if (path == null) {
112       try {
113         URL JavaDoc url = ctx.getResource("/WEB-INF");
114
115         if (url != null) {
116           path = url.toExternalForm();
117         } else {
118           path = new File JavaDoc(System.getProperty("user.dir")).toURL()
119                                                          .toExternalForm();
120         }
121       } catch (MalformedURLException JavaDoc me) {
122         throw new MalformedURLException JavaDoc("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 JavaDoc servletContextURL;
130
131     try {
132       if (path.indexOf(':') > 1) {
133         servletContextURL = path;
134       } else {
135         servletContextURL = new File JavaDoc(path).toURL().toExternalForm();
136       }
137     } catch (MalformedURLException JavaDoc me) {
138       // VG: Novell has absolute file names starting with the
139
// volume name which is easily more then one letter.
140
// Examples: sys:/apache/cocoon or sys:\apache\cocoon
141
try {
142         servletContextURL = new File JavaDoc(path).toURL().toExternalForm();
143       } catch (MalformedURLException JavaDoc ignored) {
144         throw new MalformedURLException JavaDoc(
145           "Unable to determine servlet context URL. - " + me.getMessage());
146       }
147     }
148
149     return servletContextPath;
150   }
151 }
152
Popular Tags