KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > server22 > HttpDispatcher


1 package com.quadcap.http.server22;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import javax.servlet.RequestDispatcher JavaDoc;
44 import javax.servlet.Servlet JavaDoc;
45 import javax.servlet.ServletContext JavaDoc;
46 import javax.servlet.ServletException JavaDoc;
47 import javax.servlet.ServletRequest JavaDoc;
48 import javax.servlet.ServletResponse JavaDoc;
49
50 import com.quadcap.util.Debug;
51
52 public class HttpDispatcher implements RequestDispatcher JavaDoc {
53     WebApplication context;
54     WebServlet servlet;
55     String JavaDoc contextPath = "";
56     String JavaDoc subPath = "";
57     String JavaDoc servletPath = "";
58     String JavaDoc pathInfo = null;
59
60     public HttpDispatcher(WebApplication context, String JavaDoc contextRelativePath) {
61         this.context = context;
62         this.contextPath = context.getContextPath();
63         this.subPath = contextRelativePath;
64     this.servletPath = context.resolveDirectory(contextRelativePath);
65     }
66
67     public HttpDispatcher(WebServlet servlet, String JavaDoc uri) {
68         this.context = servlet.getWebApplication();
69         this.servlet = servlet;
70         this.contextPath = "";
71         this.servletPath = "/servlet/" + uri;
72         this.subPath = servletPath;
73     }
74
75     public void forward(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
76     throws ServletException JavaDoc, IOException JavaDoc
77     {
78         String JavaDoc p = pathInfo == null ? "" : pathInfo;
79     ((HttpRequest)request).setURI(contextPath + servletPath + p);
80         if (Trace.level() > 1) {
81             Debug.println("[" + context.getRootPath() + "]: forward" +
82                           ((HttpRequest)request).getRequestURI());
83         }
84         ((HttpRequest)request).setRequestDispatcher(this);
85         response.reset();
86     servlet.service(request, response);
87     }
88
89
90     public void include(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
91     throws ServletException JavaDoc, IOException JavaDoc
92     {
93         String JavaDoc p = pathInfo == null ? "" : pathInfo;
94     ((HttpRequest)request).setURI(contextPath + servletPath + p);
95         ((HttpRequest)request).setRequestDispatcher(this);
96         if (Trace.level() > 1) {
97             Debug.println("[" + context.getRootPath() + "]: include" +
98                           ((HttpRequest)request).getRequestURI());
99         }
100     servlet.service(request, response);
101     }
102
103     public final void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
104     throws ServletException JavaDoc, IOException JavaDoc
105     {
106         if ((subPath.length() == 0 ||
107              subPath.charAt(subPath.length()-1) != '/')
108             && context.isDirectory(subPath)) {
109             // ---- Send a redirect from GET '/foo' to GET '/foo/' if foo is
110
// ---- a directory.
111
((HttpResponse)response).sendRedirect(contextPath + subPath + "/");
112         } else {
113             ((HttpRequest)request).setRequestDispatcher(this);
114             servlet.service(request, response);
115         }
116     }
117
118     public void setServlet(WebServlet servlet, String JavaDoc servletPath) {
119         this.servlet = servlet;
120         this.servletPath = servletPath;
121     }
122
123     public void setContextPath(String JavaDoc path) {
124         this.contextPath = contextPath;
125     }
126
127     public String JavaDoc getContextPath() {
128         return contextPath;
129     }
130
131     public String JavaDoc getServletPath() {
132         return servletPath;
133     }
134
135     void setServletPath(String JavaDoc path) {
136         servletPath = path;
137     }
138
139     public String JavaDoc getPathInfo() {
140         return pathInfo;
141     }
142
143     public void setPathInfo(String JavaDoc pathInfo) {
144         this.pathInfo = pathInfo;
145     }
146
147     public WebApplication getContext() {
148         return context;
149     }
150 }
151
Popular Tags