001: package com.quadcap.http.server22;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import javax.servlet.RequestDispatcher;
044: import javax.servlet.Servlet;
045: import javax.servlet.ServletContext;
046: import javax.servlet.ServletException;
047: import javax.servlet.ServletRequest;
048: import javax.servlet.ServletResponse;
049:
050: import com.quadcap.util.Debug;
051:
052: public class HttpDispatcher implements RequestDispatcher {
053: WebApplication context;
054: WebServlet servlet;
055: String contextPath = "";
056: String subPath = "";
057: String servletPath = "";
058: String pathInfo = null;
059:
060: public HttpDispatcher(WebApplication context,
061: String contextRelativePath) {
062: this .context = context;
063: this .contextPath = context.getContextPath();
064: this .subPath = contextRelativePath;
065: this .servletPath = context
066: .resolveDirectory(contextRelativePath);
067: }
068:
069: public HttpDispatcher(WebServlet servlet, String uri) {
070: this .context = servlet.getWebApplication();
071: this .servlet = servlet;
072: this .contextPath = "";
073: this .servletPath = "/servlet/" + uri;
074: this .subPath = servletPath;
075: }
076:
077: public void forward(ServletRequest request, ServletResponse response)
078: throws ServletException, IOException {
079: String p = pathInfo == null ? "" : pathInfo;
080: ((HttpRequest) request).setURI(contextPath + servletPath + p);
081: if (Trace.level() > 1) {
082: Debug.println("[" + context.getRootPath() + "]: forward"
083: + ((HttpRequest) request).getRequestURI());
084: }
085: ((HttpRequest) request).setRequestDispatcher(this );
086: response.reset();
087: servlet.service(request, response);
088: }
089:
090: public void include(ServletRequest request, ServletResponse response)
091: throws ServletException, IOException {
092: String p = pathInfo == null ? "" : pathInfo;
093: ((HttpRequest) request).setURI(contextPath + servletPath + p);
094: ((HttpRequest) request).setRequestDispatcher(this );
095: if (Trace.level() > 1) {
096: Debug.println("[" + context.getRootPath() + "]: include"
097: + ((HttpRequest) request).getRequestURI());
098: }
099: servlet.service(request, response);
100: }
101:
102: public final void service(ServletRequest request,
103: ServletResponse response) throws ServletException,
104: IOException {
105: if ((subPath.length() == 0 || subPath
106: .charAt(subPath.length() - 1) != '/')
107: && context.isDirectory(subPath)) {
108: // ---- Send a redirect from GET '/foo' to GET '/foo/' if foo is
109: // ---- a directory.
110: ((HttpResponse) response).sendRedirect(contextPath
111: + 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: }
|