KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > Dispatcher


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms;
14
15 import java.io.IOException JavaDoc;
16 import java.text.MessageFormat JavaDoc;
17
18 import javax.servlet.RequestDispatcher JavaDoc;
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.apache.log4j.Logger;
25
26
27 /**
28  * @author Sameer Charles
29  * @version 2.0
30  */

31 public final class Dispatcher {
32
33     /**
34      * Logger.
35      */

36     private static Logger log = Logger.getLogger(Dispatcher.class);
37
38     /**
39      * Utility class, don't instantiate.
40      */

41     private Dispatcher() {
42         // unused
43
}
44
45     /**
46      * Dispatches the current requested to the handler JSP / Servlet
47      * @param req HttpServletRequest
48      * @param res HttpServletResponse
49      * @param sc ServletContext
50      * @throws ServletException
51      * @throws IOException
52      */

53     public static void dispatch(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res, ServletContext JavaDoc sc)
54         throws ServletException JavaDoc, IOException JavaDoc {
55         if (sc == null) {
56             log.error("null ServletContext received - aborting request"); //$NON-NLS-1$
57
return;
58         }
59         String JavaDoc requestReceiver = (String JavaDoc) req.getAttribute(Aggregator.REQUEST_RECEIVER);
60
61         if (requestReceiver == null) {
62             log.error("requestReceiver is missing, returning a 404 error"); //$NON-NLS-1$
63
res.sendError(404);
64             return;
65         }
66
67         if (log.isDebugEnabled()) {
68             log.debug(MessageFormat.format("Dispatching request for [{0}] - forward to [{1}]", //$NON-NLS-1$
69
new Object JavaDoc[]{req.getRequestURL(), requestReceiver}));
70         }
71
72         if (res.isCommitted()) {
73             log.error(MessageFormat.format("Can''t forward to [{0}] for request [{1}]. Response is already committed.", //$NON-NLS-1$
74
new Object JavaDoc[]{requestReceiver, req.getRequestURL()}));
75             return;
76         }
77
78         RequestDispatcher JavaDoc rd = sc.getRequestDispatcher(requestReceiver);
79         rd.forward(req, res);
80         rd = null;
81     }
82 }
83
Popular Tags