00001
package com.quadcap.http.server22;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.IOException;
00042
00043
import javax.servlet.RequestDispatcher;
00044
import javax.servlet.Servlet;
00045
import javax.servlet.ServletContext;
00046
import javax.servlet.ServletException;
00047
import javax.servlet.ServletRequest;
00048
import javax.servlet.ServletResponse;
00049
00050
import com.quadcap.util.Debug;
00051
00052 public class HttpDispatcher implements RequestDispatcher {
00053 WebApplication context;
00054 WebServlet servlet;
00055 String
contextPath =
"";
00056 String
subPath =
"";
00057 String
servletPath =
"";
00058 String
pathInfo = null;
00059
00060 public HttpDispatcher(
WebApplication context, String contextRelativePath) {
00061
this.context = context;
00062
this.
contextPath = context.
getContextPath();
00063
this.subPath = contextRelativePath;
00064
this.servletPath = context.
resolveDirectory(contextRelativePath);
00065 }
00066
00067 public HttpDispatcher(
WebServlet servlet, String uri) {
00068
this.context = servlet.
getWebApplication();
00069
this.servlet = servlet;
00070
this.contextPath =
"";
00071
this.servletPath =
"/servlet/" + uri;
00072
this.subPath =
servletPath;
00073 }
00074
00075 public void forward(ServletRequest request, ServletResponse response)
00076
throws ServletException, IOException
00077 {
00078 String p =
pathInfo == null ?
"" : pathInfo;
00079 ((
HttpRequest)request).setURI(
contextPath +
servletPath + p);
00080
if (
Trace.level() > 1) {
00081
Debug.println(
"[" +
context.
getRootPath() +
"]: forward" +
00082 ((
HttpRequest)request).getRequestURI());
00083 }
00084 ((
HttpRequest)request).setRequestDispatcher(
this);
00085 response.reset();
00086
servlet.
service(request, response);
00087 }
00088
00089
00090 public void include(ServletRequest request, ServletResponse response)
00091
throws ServletException, IOException
00092 {
00093 String p =
pathInfo == null ?
"" : pathInfo;
00094 ((
HttpRequest)request).setURI(
contextPath +
servletPath + p);
00095 ((
HttpRequest)request).setRequestDispatcher(
this);
00096
if (
Trace.level() > 1) {
00097
Debug.println(
"[" +
context.
getRootPath() +
"]: include" +
00098 ((
HttpRequest)request).getRequestURI());
00099 }
00100
servlet.
service(request, response);
00101 }
00102
00103 public final void service(ServletRequest request, ServletResponse response)
00104
throws ServletException, IOException
00105 {
00106
if ((
subPath.length() == 0 ||
00107
subPath.charAt(
subPath.length()-1) !=
'/')
00108 &&
context.
isDirectory(
subPath)) {
00109
00110
00111 ((
HttpResponse)response).sendRedirect(
contextPath +
subPath +
"/");
00112 }
else {
00113 ((
HttpRequest)request).setRequestDispatcher(
this);
00114
servlet.
service(request, response);
00115 }
00116 }
00117
00118 public void setServlet(
WebServlet servlet, String servletPath) {
00119
this.servlet = servlet;
00120
this.servletPath = servletPath;
00121 }
00122
00123 public void setContextPath(String path) {
00124
this.contextPath =
contextPath;
00125 }
00126
00127 public String
getContextPath() {
00128
return contextPath;
00129 }
00130
00131 public String
getServletPath() {
00132
return servletPath;
00133 }
00134
00135 void setServletPath(String path) {
00136
servletPath = path;
00137 }
00138
00139 public String
getPathInfo() {
00140
return pathInfo;
00141 }
00142
00143 public void setPathInfo(String pathInfo) {
00144
this.pathInfo = pathInfo;
00145 }
00146
00147 public WebApplication getContext() {
00148
return context;
00149 }
00150 }