KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ServletDispatcher


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import com.opensymphony.webwork.WebWorkStatics;
8 import com.opensymphony.webwork.dispatcher.mapper.ActionMapperFactory;
9 import com.opensymphony.webwork.dispatcher.mapper.ActionMapping;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import javax.servlet.ServletConfig JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServlet JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18 import java.io.IOException JavaDoc;
19
20 /**
21  * Main dispatcher servlet in WebWork2 which acts as the controller in the MVC paradigm. <p>
22  * <p/>
23  * When a request enters the servlet the following things will happen: <ol>
24  * <p/>
25  * <li>The action name is parsed from the servlet path (i.e., /foo/bar/MyAction.action -> MyAction).</li>
26  * <li>A context consisting of the request, response, parameters, session and application
27  * properties is created.</li>
28  * <li>An XWork <tt>ActionProxy</tt> object is instantiated (wraps an <tt>Action</tt>) using the action name, path,
29  * and context then executed.</li>
30  * <li>Action output will channel back through the response to the user.</li></ol>
31  * <p/>
32  * Any errors occurring during the action execution will result in a
33  * {@link javax.servlet.http.HttpServletResponse#SC_INTERNAL_SERVER_ERROR} error and any resource errors
34  * (i.e., invalid action name or missing JSP page) will result in a
35  * {@link javax.servlet.http.HttpServletResponse#SC_NOT_FOUND} error. <p>
36  * <p/>
37  * Instead of traditional servlet init params this servlet will initialize itself using WebWork2 properties.
38  * The following properties are used upon initialization: <ul>
39  * <p/>
40  * <li><tt>webwork.configuration.xml.reload</tt>: if and only if set to <tt>true</tt> then the xml configuration
41  * files (action definitions, interceptor definitions, etc) will be reloaded for each request. This is
42  * useful for development but should be disabled for production deployment.</li>
43  * <li><tt>webwork.multipart.saveDir</tt>: The path used for temporarily uploaded files. Defaults to the
44  * temp path specified by the app server.</li>
45  * <li><tt>webwork.multipart.maxSize</tt>: sets the maximum allowable multipart request size
46  * in bytes. If the size was not specified then {@link java.lang.Integer#MAX_VALUE} will be used
47  * (essentially unlimited so be careful).</li></ul>
48  * <p/>
49  *
50  * @author <a HREF="mailto:rickard@middleware-company.com">Rickard �berg</a>
51  * @author <a HREF="mailto:matt@smallleap.com">Matt Baldree</a>
52  * @author Jason Carreira
53  * @author <a HREF="mailto:cameron@datacodex.net">Cameron Braid</a>
54  * @author Bill Lynch
55  * @see ServletDispatcherResult
56  * @deprecated use {@link FilterDispatcher} instead
57  */

58 public class ServletDispatcher extends HttpServlet JavaDoc implements WebWorkStatics {
59     //~ Static fields/initializers /////////////////////////////////////////////
60

61     /**
62      * Logger for this class.
63      */

64     protected static final Log LOG = LogFactory.getLog(ServletDispatcher.class);
65
66     /**
67      * Initalizes the servlet. Please read the {@link ServletDispatcher class documentation} for more
68      * detail. <p>
69      *
70      * @param servletConfig the ServletConfig object.
71      * @throws ServletException if an error occurs during initialization.
72      */

73     public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc {
74         super.init(servletConfig);
75         DispatcherUtils.initialize(getServletContext());
76     }
77
78     /**
79      * Services the request by determining the desired action to load, building the action context and
80      * then executing the action. This handles all servlet requests including GETs and POSTs. <p>
81      * <p/>
82      * This method also transparently handles multipart requests.
83      *
84      * @param request the HttpServletRequest object.
85      * @param response the HttpServletResponse object.
86      * @throws ServletException if an error occurs while loading or executing the action.
87      */

88     public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
89         ActionMapping mapping = ActionMapperFactory.getMapper().getMapping(request);
90         if (mapping == null) {
91             try {
92                 response.sendError(404);
93             } catch (IOException JavaDoc e) {
94                 LOG.error("Could not send 404 after not finding any ActionMapping", e);
95             }
96             return;
97         }
98
99         DispatcherUtils du = DispatcherUtils.getInstance();
100         du.prepare(request, response);
101
102         try {
103             request = du.wrapRequest(request, getServletContext());
104         } catch (IOException JavaDoc e) {
105             String JavaDoc message = "Could not wrap servlet request with MultipartRequestWrapper!";
106             LOG.error(message, e);
107             du.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, new ServletException JavaDoc(message, e));
108             return;
109         }
110
111         du.serviceAction(request, response, getServletContext(), mapping);
112     }
113 }
114
Popular Tags