001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import com.opensymphony.webwork.WebWorkStatics;
008: import com.opensymphony.webwork.dispatcher.mapper.ActionMapperFactory;
009: import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import javax.servlet.ServletConfig;
014: import javax.servlet.ServletException;
015: import javax.servlet.http.HttpServlet;
016: import javax.servlet.http.HttpServletRequest;
017: import javax.servlet.http.HttpServletResponse;
018: import java.io.IOException;
019:
020: /**
021: * Main dispatcher servlet in WebWork2 which acts as the controller in the MVC paradigm. <p>
022: * <p/>
023: * When a request enters the servlet the following things will happen: <ol>
024: * <p/>
025: * <li>The action name is parsed from the servlet path (i.e., /foo/bar/MyAction.action -> MyAction).</li>
026: * <li>A context consisting of the request, response, parameters, session and application
027: * properties is created.</li>
028: * <li>An XWork <tt>ActionProxy</tt> object is instantiated (wraps an <tt>Action</tt>) using the action name, path,
029: * and context then executed.</li>
030: * <li>Action output will channel back through the response to the user.</li></ol>
031: * <p/>
032: * Any errors occurring during the action execution will result in a
033: * {@link javax.servlet.http.HttpServletResponse#SC_INTERNAL_SERVER_ERROR} error and any resource errors
034: * (i.e., invalid action name or missing JSP page) will result in a
035: * {@link javax.servlet.http.HttpServletResponse#SC_NOT_FOUND} error. <p>
036: * <p/>
037: * Instead of traditional servlet init params this servlet will initialize itself using WebWork2 properties.
038: * The following properties are used upon initialization: <ul>
039: * <p/>
040: * <li><tt>webwork.configuration.xml.reload</tt>: if and only if set to <tt>true</tt> then the xml configuration
041: * files (action definitions, interceptor definitions, etc) will be reloaded for each request. This is
042: * useful for development but should be disabled for production deployment.</li>
043: * <li><tt>webwork.multipart.saveDir</tt>: The path used for temporarily uploaded files. Defaults to the
044: * temp path specified by the app server.</li>
045: * <li><tt>webwork.multipart.maxSize</tt>: sets the maximum allowable multipart request size
046: * in bytes. If the size was not specified then {@link java.lang.Integer#MAX_VALUE} will be used
047: * (essentially unlimited so be careful).</li></ul>
048: * <p/>
049: *
050: * @author <a href="mailto:rickard@middleware-company.com">Rickard �berg</a>
051: * @author <a href="mailto:matt@smallleap.com">Matt Baldree</a>
052: * @author Jason Carreira
053: * @author <a href="mailto:cameron@datacodex.net">Cameron Braid</a>
054: * @author Bill Lynch
055: * @see ServletDispatcherResult
056: * @deprecated use {@link FilterDispatcher} instead
057: */
058: public class ServletDispatcher extends HttpServlet implements
059: WebWorkStatics {
060:
061: /**
062: * Logger for this class.
063: */
064: protected static final Log LOG = LogFactory
065: .getLog(ServletDispatcher.class);
066:
067: /**
068: * Initalizes the servlet. Please read the {@link ServletDispatcher class documentation} for more
069: * detail. <p>
070: *
071: * @param servletConfig the ServletConfig object.
072: * @throws ServletException if an error occurs during initialization.
073: */
074: public void init(ServletConfig servletConfig)
075: throws ServletException {
076: super .init(servletConfig);
077: DispatcherUtils.initialize(getServletContext());
078: }
079:
080: /**
081: * Services the request by determining the desired action to load, building the action context and
082: * then executing the action. This handles all servlet requests including GETs and POSTs. <p>
083: * <p/>
084: * This method also transparently handles multipart requests.
085: *
086: * @param request the HttpServletRequest object.
087: * @param response the HttpServletResponse object.
088: * @throws ServletException if an error occurs while loading or executing the action.
089: */
090: public void service(HttpServletRequest request,
091: HttpServletResponse response) throws ServletException {
092: // prepare the request no matter what - this ensures that the proper character encoding
093: // is used before invoking the mapper (see WW-9127)
094: DispatcherUtils du = DispatcherUtils.getInstance();
095: du.prepare(request, response);
096:
097: try {
098: request = du.wrapRequest(request, getServletContext());
099: } catch (IOException e) {
100: String message = "Could not wrap servlet request with MultipartRequestWrapper!";
101: LOG.error(message, e);
102: throw new ServletException(message, e);
103: }
104:
105: ActionMapping mapping = ActionMapperFactory.getMapper()
106: .getMapping(request);
107: if (mapping == null) {
108: try {
109: response.sendError(404);
110: } catch (IOException e) {
111: LOG
112: .error(
113: "Could not send 404 after not finding any ActionMapping",
114: e);
115: }
116: return;
117: }
118:
119: du.serviceAction(request, response, getServletContext(),
120: mapping);
121: }
122: }
|