KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > DefaultControllerParser


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller;
8
9
10 import java.io.IOException JavaDoc;
11 import java.net.URISyntaxException JavaDoc;
12
13 import javax.servlet.RequestDispatcher JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17
18 import org.apache.log4j.Logger;
19
20 import com.inversoft.error.ErrorList;
21 import com.inversoft.verge.mvc.MVCException;
22 import com.inversoft.verge.mvc.MVCRequest;
23 import com.inversoft.verge.mvc.MVCURLTools;
24
25
26 /**
27  * <p>
28  * This class is the default controller parser for the
29  * Verge MVC system. This implements the {@link
30  * ControllerParser ControllerParser} interface and supplies
31  * a parse method that reads the HttpServletRequest
32  * parameters to determine what controller to call.
33  * </p>
34  *
35  * <p>
36  * The HttpServletRequest will extra path information in
37  * the request URL (the information following the context
38  * path). This information helps the default Verge controller
39  * to determine which of the MVC systems are being used. This
40  * information also contains data that is relavent only to
41  * the specific MVC systems. The {@link ControllerMVCInfo}
42  * class is used to decode the path information and store
43  * it for the remainder of the MVC request. It is left up
44  * to the MVC systems to use this class and any other mechanism
45  * they wish to determine how to execute.
46  * </p>
47  *
48  * <p>
49  * In addition, the Verge controller system contains support
50  * for proxies in a clustered environment as well as other
51  * such setups where URLs generated are actually different
52  * than the current server. This is only done for redirect
53  * handling of controller {@link Result Results}. This is
54  * done by making use of the {@link
55  * com.inversoft.verge.util.url.URLGenerator} system with
56  * a category named <code>controller</code>
57  * </p>
58  *
59  * @author Brian Pontarelli
60  * @since 2.0
61  * @version 2.0
62  */

63 public class DefaultControllerParser implements ControllerParser {
64
65     /**
66      * This classes logger
67      */

68     private static final Logger logger = Logger.getLogger(DefaultControllerParser.class);
69
70     /**
71      * The name of the URLGenerator category used when creating redirect URLs
72      */

73     public static final String JavaDoc URL_CATEGORY_NAME = "controller";
74
75
76     /**
77      * Constructor for DefaultControllerParser.
78      */

79     public DefaultControllerParser() {
80         super();
81     }
82
83
84     /**
85      * Sets up all the controller information stuff into the MVCRequest. For
86      * example, this tries to build the ControllerMVCInfo from the HTTP servlet
87      * request. It also tries to create an Action, etc, etc.
88      * Builds the ControllerMVCInfo object from the HttpServletRequest and
89      * returns it. If it can not be found, null is returned to signal that no
90      * controller information was sent in the request and that the controller
91      * part of the MVC handling should be skipped. If the meta data is built,
92      * this method gives the controller implementation a chance to set the
93      * attributes of the meta data by calling the {@link
94      * ControllerHandler#preExecute(MVCRequest) preExecute} method on the
95      * ControllerHandler.
96      */

97     public void preExecute(MVCRequest mvcRequest) throws MVCException {
98
99         // Allow the implementation to set attributes
100
HttpServletRequest JavaDoc request = mvcRequest.getRequest();
101         ControllerMVCInfo info = MVCURLTools.decodeURL(request);
102         mvcRequest.setControllerInfo(info);
103         info.getHandler().preExecute(mvcRequest);
104     }
105
106     /**
107      * Parses out the request parameters to determine what controller to call.
108      *
109      * @param mvcRequest The MVCRequest that is built as part of the
110      * MVCMediators mediation routine
111      */

112     public void execute(MVCRequest mvcRequest) throws MVCException {
113
114         // If there is no meta data, bail
115
ControllerMVCInfo info = mvcRequest.getControllerInfo();
116         if (info == null) {
117             return;
118         }
119
120         ControllerHandler handler = info.getHandler();
121         Result result = handler.execute(mvcRequest);
122         handleResult(mvcRequest, result);
123     }
124
125     /**
126      * Using the {@link Result Result} object returned from the {@link
127      * ControllerHandler ControllerHandler}, this method either forwards or sends
128      * a redirect. The URL that is used is retrieved from a call to {@link
129      * Result#getGeneratedURL(HttpServletRequest) Result#getGeneratedURL} method.
130      * This means that applying any categories or other URL transformations
131      * should happen there. Unless this method is overridden to handle whatever
132      * needs to be done.
133      *
134      * @param mvcRequest The MVCRequest to get the Http objects from
135      * @param result The result to handle, can be null
136      * @throws MVCException If there was any problems handling the result
137      */

138     protected void handleResult(MVCRequest mvcRequest, Result result)
139     throws MVCException {
140         HttpServletRequest JavaDoc request = mvcRequest.getRequest();
141         HttpServletResponse JavaDoc response = mvcRequest.getResponse();
142         if (result != null) {
143
144             if (result.isForward()) {
145
146                 String JavaDoc url = null;
147                 try {
148                     url = result.getGeneratedURL(request);
149
150                     if (logger.isDebugEnabled()) {
151                         logger.debug("Forwarding to URL: " + url);
152                     }
153
154                     RequestDispatcher JavaDoc rd = request.getRequestDispatcher(url);
155                     rd.forward(request, response);
156                 } catch (ServletException JavaDoc se) {
157                     ErrorList el = new ErrorList();
158                     el.addError("This exception is generally the result of an" +
159                         "exception thrown by the JSP page. Most containers " +
160                         "smother these exceptions and thrown a SerlvetException " +
161                         "making it very difficult to determine the root cause.");
162
163                     MVCException mvce = new MVCException("Error forwarding to:" +
164                         url, se);
165                     mvce.setErrors(el);
166                     throw mvce;
167                 } catch (IOException JavaDoc ioe) {
168                     throw new MVCException("Error forwarding to: " + url, ioe);
169                 } catch (URISyntaxException JavaDoc use) {
170                     throw new MVCException("Error forwarding to: " + result.getURL(), use);
171                 }
172             } else {
173                 String JavaDoc url = null;
174                 try {
175                     url = result.getGeneratedURL(request);
176                     url = response.encodeRedirectURL(url);
177                     response.sendRedirect(url);
178                 } catch (IOException JavaDoc ioe) {
179                     throw new MVCException("Error redirecting to:" + url, ioe);
180                 } catch (URISyntaxException JavaDoc use) {
181                     throw new MVCException("Error redirecting to: " + result.getURL(), use);
182                 }
183             }
184         }
185     }
186 }
Popular Tags