Quadcap Embeddable Server

com/quadcap/http/server22/HttpDispatcher.java

Go to the documentation of this file.
00001 package com.quadcap.http.server22; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 // ---- Send a redirect from GET '/foo' to GET '/foo/' if foo is 00110 // ---- a directory. 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 }