1 package com.quadcap.http.server22; 2 3 40 41 import java.io.IOException ; 42 43 import javax.servlet.RequestDispatcher ; 44 import javax.servlet.Servlet ; 45 import javax.servlet.ServletContext ; 46 import javax.servlet.ServletException ; 47 import javax.servlet.ServletRequest ; 48 import javax.servlet.ServletResponse ; 49 50 import com.quadcap.util.Debug; 51 52 public class HttpDispatcher implements RequestDispatcher { 53 WebApplication context; 54 WebServlet servlet; 55 String contextPath = ""; 56 String subPath = ""; 57 String servletPath = ""; 58 String pathInfo = null; 59 60 public HttpDispatcher(WebApplication context, String 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 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 request, ServletResponse response) 76 throws ServletException , IOException 77 { 78 String 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 request, ServletResponse response) 91 throws ServletException , IOException 92 { 93 String 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 request, ServletResponse response) 104 throws ServletException , IOException 105 { 106 if ((subPath.length() == 0 || 107 subPath.charAt(subPath.length()-1) != '/') 108 && context.isDirectory(subPath)) { 109 ((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 servletPath) { 119 this.servlet = servlet; 120 this.servletPath = servletPath; 121 } 122 123 public void setContextPath(String path) { 124 this.contextPath = contextPath; 125 } 126 127 public String getContextPath() { 128 return contextPath; 129 } 130 131 public String getServletPath() { 132 return servletPath; 133 } 134 135 void setServletPath(String path) { 136 servletPath = path; 137 } 138 139 public String getPathInfo() { 140 return pathInfo; 141 } 142 143 public void setPathInfo(String pathInfo) { 144 this.pathInfo = pathInfo; 145 } 146 147 public WebApplication getContext() { 148 return context; 149 } 150 } 151 | Popular Tags |