001: /*
002: * de.jwic.web.DispatcherServlet
003: * $Id: DispatcherServlet.java,v 1.6 2007/03/09 10:11:25 lordsam Exp $
004: */
005: package de.jwic.web;
006:
007: import java.io.FileNotFoundException;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.Map;
011: import java.util.StringTokenizer;
012:
013: import javax.servlet.ServletConfig;
014: import javax.servlet.ServletContext;
015: import javax.servlet.ServletException;
016: import javax.servlet.http.HttpServlet;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.log4j.PropertyConfigurator;
023:
024: import de.jwic.base.ConfigurationTool;
025: import de.jwic.base.IApplicationSetup;
026: import de.jwic.base.JWicRuntime;
027: import de.jwic.base.XmlApplicationSetup;
028: import de.jwic.upload.Upload;
029:
030: /**
031: * Dispatches incoming request to an existing or new JWic session.
032: *
033: * <p>The DispatcherServlet can initialize a log4j system if required. To do so,
034: * place a log4j.properties file somewhere (WEB-INF is a good place) and point
035: * to it using the init-param "log4j-init-file" in the servlet definition.</p>
036: * <p>Sample:
037: * <pre>
038: * <init-param>
039: * <param-name>log4j-init-file</param-name>
040: * <param-value>WEB-INF/log4j.properties</param-value>
041: * </init-param>
042: * </pre>
043: * </p>
044: *
045: * @author Florian Lippisch
046: * @version $Revision: 1.6 $
047: */
048: public class DispatcherServlet extends HttpServlet implements
049: IApplicationSetupProvider {
050:
051: private static final long serialVersionUID = 2L;
052:
053: public final static String INIT_LOG4JINIT = "log4j-init-file";
054: public final static String INIT_SETROOTDIR = "setRootDir";
055: public final static String INIT_INTERCEPTORS = "servlet.interceptors";
056: public final static String INIT_AUTHENTICATOR = "authenticator";
057: public final static String INIT_LOGIN_URL = "login";
058: public final static String INIT_UPLOAD_LIMIT = "uploadlimit";
059:
060: protected final Log log = LogFactory.getLog(getClass());
061:
062: private JWicRuntime jRuntime = null;
063: private ServletInterceptor[] interceptors = null;
064:
065: private WebEngine engine = null;
066: private long uploadLimit = 4 * 1024 * 1024;
067:
068: /* (non-Javadoc)
069: * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
070: */
071: protected void service(HttpServletRequest req,
072: HttpServletResponse res) throws ServletException,
073: IOException {
074:
075: // invoke all configured interceptors
076: if (interceptors != null) {
077: for (int i = 0; i < interceptors.length; i++) {
078: try {
079: if (interceptors[i] != null) {
080: interceptors[i].preHandle(req, res);
081: }
082: } catch (Throwable thr) {
083: log
084: .error(
085: "Error executing interceptor.preHandle "
086: + interceptors[i]
087: .getClass()
088: .getName(), thr);
089: }
090: }
091: }
092:
093: // Store the context path initialy in the JWicRuntime to allow
094: // other tools to build full path links.
095: if (jRuntime.getContextPath() == null) {
096: jRuntime.setContextPath(req.getContextPath());
097: }
098:
099: super .service(req, res);
100:
101: // invoke all configured interceptors
102: if (interceptors != null) {
103: for (int i = 0; i < interceptors.length; i++) {
104: try {
105: if (interceptors[i] != null) {
106: interceptors[i].postHandle(req, res);
107: }
108: } catch (Throwable thr) {
109: log
110: .error(
111: "Error executing interceptor.postHandle "
112: + interceptors[i]
113: .getClass()
114: .getName(), thr);
115: }
116: }
117: }
118:
119: }
120:
121: /* (non-Javadoc)
122: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
123: */
124: protected void doGet(HttpServletRequest req, HttpServletResponse res)
125: throws ServletException, IOException {
126:
127: log.debug("incoming GET request: " + req.getRequestURI());
128: try {
129:
130: engine.handleRequest(req, res, null);
131:
132: } catch (Exception e) {
133: log.error("Error in doGet()", e);
134: engine.displayError(req, res, e, null);
135: }
136:
137: }
138:
139: /* (non-Javadoc)
140: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
141: */
142: protected void doPost(HttpServletRequest req,
143: HttpServletResponse res) throws ServletException,
144: IOException {
145:
146: Upload upload = null;
147: if (req.getContentType().startsWith("multipart")) {
148: // parse data using "multipart" mode
149: upload = new Upload(req, ".", uploadLimit, 1 * 1024 * 1024);
150: // get parameters from the stream and set them as AgoraRequest parameters.
151: // fill webform
152: Map fields = upload.getParams();
153: req = new UploadHttpServletRequest(req, fields);
154: }
155:
156: engine.handleRequest(req, res, upload);
157:
158: }
159:
160: /* (non-Javadoc)
161: * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
162: */
163: public void init(ServletConfig cfg) throws ServletException {
164:
165: super .init(cfg);
166:
167: ServletContext srvCtx = getServletContext();
168: String webAppRoot = srvCtx.getRealPath("/");
169:
170: /* Set the web application root directory as system.property named by "setRootDir" param */
171: String propertyName = getInitParameter(INIT_SETROOTDIR);
172: if (propertyName != null) {
173: System.setProperty(propertyName, webAppRoot);
174: }
175:
176: /* Setup the log4j system if specified in the web.xml */
177: String file = getInitParameter(INIT_LOG4JINIT);
178: if (file != null) {
179: PropertyConfigurator.configure(webAppRoot + file);
180: }
181:
182: /* Setup the runtime */
183: jRuntime = JWicRuntime.getJWicRuntime();
184: jRuntime.setRootPath(webAppRoot);
185: jRuntime.setSavePath(srvCtx
186: .getRealPath("WEB-INF/jwic/sessions"));
187:
188: ConfigurationTool.setRootPath(webAppRoot);
189:
190: // because some WebServers need a leading slash while others do
191: // only work with a relative path, we try both ways.
192: InputStream in = getServletContext().getResourceAsStream(
193: "WEB-INF/jwic-setup.xml");
194: if (in == null) {
195: in = getServletContext().getResourceAsStream(
196: "/WEB-INF/jwic-setup.xml");
197: }
198: if (in != null) {
199: jRuntime.setupRuntime(in);
200: } else {
201: throw new ServletException(
202: "/WEB-INF/jwic-setup.xml not found.");
203: }
204:
205: try {
206: engine = new WebEngine(this , webAppRoot);
207: } catch (Exception e1) {
208: log.error("Can not instanciate WebEngine.", e1);
209: throw new ServletException(
210: "Can not instanciate WebEngine: " + e1);
211: }
212:
213: // init Authentication
214: String authenticationClass = getInitParameter(INIT_AUTHENTICATOR);
215: String loginPage = getInitParameter(INIT_LOGIN_URL);
216: if (authenticationClass != null) {
217: // use custom class
218: try {
219: IAuthenticator authenticator = (IAuthenticator) Class
220: .forName(authenticationClass).newInstance();
221: engine.setAuthenticator(authenticator);
222: engine.setLoginPage(loginPage);
223:
224: } catch (Exception e) {
225: log.error("Error creating Authentication '"
226: + authenticationClass + "'", e);
227: }
228: } else {
229: log.warn("No authenticator configured.");
230: }
231:
232: // set the upload limit
233: try {
234: String upLimit = getInitParameter(INIT_UPLOAD_LIMIT);
235: if (upLimit != null) {
236: uploadLimit = Long.parseLong(upLimit);
237: }
238: } catch (NumberFormatException nfe) {
239: log.warn("Upload-Limit parameter can not be read: ", nfe);
240: }
241:
242: // Initialize the interceptors
243: String intClasses = getInitParameter(INIT_INTERCEPTORS);
244: if (intClasses != null && intClasses.length() != 0) {
245: StringTokenizer stk = new StringTokenizer(intClasses, ";");
246: interceptors = new ServletInterceptor[stk.countTokens()];
247: int i = 0;
248: while (stk.hasMoreTokens()) {
249: String classname = stk.nextToken();
250: try {
251: interceptors[i++] = (ServletInterceptor) Class
252: .forName(classname).newInstance();
253: } catch (Throwable t) {
254: log.error("Error creating ServletInterceptor '"
255: + classname + "'", t);
256: }
257: }
258: }
259:
260: }
261:
262: /**
263: * Destroy the servlet and the JWicRuntime.
264: */
265: public void destroy() {
266:
267: log.info("DispatcherServlet.destroy()");
268: JWicRuntime.getJWicRuntime().destroy();
269:
270: }
271:
272: /* (non-Javadoc)
273: * @see de.jwic.web.IApplicationSetupProvider#createApplicationSetup(javax.servlet.http.HttpServletRequest)
274: */
275: public IApplicationSetup createApplicationSetup(
276: HttpServletRequest request) throws IOException {
277:
278: InputStream in = getServletContext().getResourceAsStream(
279: request.getServletPath());
280: if (in == null) {
281: throw new FileNotFoundException(request.getServletPath());
282: }
283: return new XmlApplicationSetup(in);
284: }
285:
286: }
|