You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by cr...@locus.apache.org on 2000/08/12 00:40:26 UTC

cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector Constants.java HttpRequestBase.java HttpRequestWrapper.java HttpResponseBase.java HttpResponseWrapper.java LocalStrings.properties RequestBase.java RequestStream.java RequestWrapper.java ResponseBase.java ResponseStream.java ResponseWrapper.java

craigmcc    00/08/11 15:40:26

  Added:       catalina/src/share/org/apache/catalina/connector
                        Constants.java HttpRequestBase.java
                        HttpRequestWrapper.java HttpResponseBase.java
                        HttpResponseWrapper.java LocalStrings.properties
                        RequestBase.java RequestStream.java
                        RequestWrapper.java ResponseBase.java
                        ResponseStream.java ResponseWrapper.java
  Log:
  Migrate to Tomcat 4.0.
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/Constants.java
  
  Index: Constants.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/Constants.java,v 1.1 2000/08/11 22:40:18 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:18 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.catalina.connector;
  
  
  /**
   * Static constants for this package.
   */
  
  public final class Constants {
  
      public static final String Package = "org.apache.catalina.connector";
      public static final String SessionCookie = "JSESSIONID";
      public static final String SessionParameter = "jsessionid";
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpRequestBase.java
  
  Index: HttpRequestBase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpRequestBase.java,v 1.1 2000/08/11 22:40:18 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:18 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.connector;
  
  
  import java.io.IOException;
  import java.security.Principal;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.Hashtable;
  import java.util.Locale;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletInputStream;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpUtils;
  import org.apache.catalina.HttpRequest;
  import org.apache.catalina.Manager;
  import org.apache.catalina.Realm;
  import org.apache.catalina.Session;
  import org.apache.catalina.util.Enumerator;
  
  
  /**
   * Convenience base implementation of the <b>HttpRequest</b> interface, which
   * can be used for the Request implementation required by most Connectors that
   * implement the HTTP protocol.  Only the connector-specific methods need to
   * be implemented.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:18 $
   */
  
  public class HttpRequestBase
      extends RequestBase
      implements HttpRequest, HttpServletRequest {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The authentication type used for this request.
       */
      protected String authType = null;
  
  
      /**
       * The context path for this request.
       */
      protected String contextPath = "";
  
  
      /**
       * The set of cookies associated with this Request.
       */
      protected ArrayList cookies = new ArrayList();
  
  
      /**
       * An empty collection to use for returning empty Enumerations.  Do not
       * add any elements to this collection!
       */
      protected static ArrayList empty = new ArrayList();
  
  
      /**
       * The set of SimpleDateFormat formats to use in getDateHeader().
       */
      protected static SimpleDateFormat formats[] = {
  	new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
  	new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US),
  	new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US)
      };
  
  
      /**
       * The HTTP headers associated with this Request, keyed by name.  The
       * values are ArrayLists of the corresponding header values.
       */
      protected HashMap headers = new HashMap();
  
  
      /**
       * Descriptive information about this HttpRequest implementation.
       */
      protected static final String info =
  	"org.apache.catalina.connector.HttpRequestBase/1.0";
  
  
      /**
       * The request method associated with this Request.
       */
      protected String method = null;
  
  
      /**
       * The parsed parameters for this request.  This is populated only if
       * parameter information is requested via one of the
       * <code>getParameter()</code> family of method calls.  The key is the
       * parameter name, while the value is a String array of values for this
       * parameter.
       */
      protected HashMap parameters = null;
  
  
      /**
       * The path information for this request.
       */
      protected String pathInfo = null;
  
  
      /**
       * The query string for this request.
       */
      protected String queryString = null;
  
  
      /**
       * Was the requested session ID received in a cookie?
       */
      protected boolean requestedSessionCookie = false;
  
  
      /**
       * The requested session ID (if any) for this request.
       */
      protected String requestedSessionId = null;
  
  
      /**
       * Was the requested session ID received in a URL?
       */
      protected boolean requestedSessionURL = false;
  
  
      /**
       * The request URI associated with this request.
       */
      protected String requestURI = null;
  
  
      /**
       * Was this request received on a secure channel?
       */
      protected boolean secure = false;
  
  
      /**
       * The servlet path for this request.
       */
      protected String servletPath = null;
  
  
      /**
       * The currently active session for this request.
       */
      protected Session session = null;
  
  
      /**
       * The Principal who has been authenticated for this Request.
       */
      protected Principal userPrincipal = null;
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * Return descriptive information about this Request implementation and
       * the corresponding version number, in the format
       * <code>&lt;description&gt;/&lt;version&gt;</code>.
       */
      public String getInfo() {
  
  	return (info);
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Add a Cookie to the set of Cookies associated with this Request.
       *
       * @param cookie The new cookie
       */
      public void addCookie(Cookie cookie) {
  
  	synchronized (cookies) {
  	    cookies.add(cookie);
  	}
  
      }
  
  
      /**
       * Add a Header to the set of Headers associated with this Request.
       *
       * @param name The new header name
       * @param value The new header value
       */
      public void addHeader(String name, String value) {
  
  	name = name.toLowerCase();
  	synchronized (headers) {
  	    ArrayList values = (ArrayList) headers.get(name);
  	    if (values == null) {
  		values = new ArrayList();
  		headers.put(name, values);
  	    }
  	    values.add(value);
  	}
  
      }
  
  
      /**
       * Clear the collection of Cookies associated with this Request.
       */
      public void clearCookies() {
  
  	synchronized (cookies) {
  	    cookies.clear();
  	}
  
      }
  
  
      /**
       * Clear the collection of Headers associated with this Request.
       */
      public void clearHeaders() {
  
  	headers.clear();
  
      }
  
  
      /**
       * Clear the collection of Locales associated with this Request.
       */
      public void clearLocales() {
  
  	locales.clear();
  
      }
  
  
      /**
       * Release all object references, and initialize instance variables, in
       * preparation for reuse of this object.
       */
      public void recycle() {
  
  	super.recycle();
  	authType = null;
  	contextPath = "";
  	cookies.clear();
  	headers.clear();
  	method = null;
  	parameters = null;
  	pathInfo = null;
  	queryString = null;
  	requestedSessionCookie = false;
  	requestedSessionId = null;
  	requestedSessionURL = false;
  	requestURI = null;
  	secure = false;
  	servletPath = null;
  	session = null;
  	userPrincipal = null;
  
      }
  
  
      /**
       * Set the authentication type used for this request, if any; otherwise
       * set the type to <code>null</code>.  Typical values are "BASIC",
       * "DIGEST", or "SSL".
       *
       * @param type The authentication type used
       */
      public void setAuthType(String type) {
  
  	this.authType = authType;
  
      }
  
  
      /**
       * Set the context path for this Request.  This will normally be called
       * when the associated Context is mapping the Request to a particular
       * Wrapper.
       *
       * @param path The context path
       */
      public void setContextPath(String path) {
  
  	if (path == null)
  	    this.contextPath = "";
  	else
  	    this.contextPath = path;
  
      }
  
  
      /**
       * Set the HTTP request method used for this Request.
       *
       * @param method The request method
       */
      public void setMethod(String method) {
  
  	this.method = method;
  
      }
  
  
      /**
       * Set the path information for this Request.  This will normally be called
       * when the associated Context is mapping the Request to a particular
       * Wrapper.
       *
       * @param path The path information
       */
      public void setPathInfo(String path) {
  
  	this.pathInfo = path;
  
      }
  
  
      /**
       * Set the query string for this Request.  This will normally be called
       * by the HTTP Connector, when it parses the request headers.
       *
       * @param query The query string
       */
      public void setQueryString(String query) {
  
  	this.queryString = query;
  
      }
  
  
      /**
       * Set a flag indicating whether or not the requested session ID for this
       * request came in through a cookie.  This is normally called by the
       * HTTP Connector, when it parses the request headers.
       *
       * @param flag The new flag
       */
      public void setRequestedSessionCookie(boolean flag) {
  
  	this.requestedSessionCookie = flag;
  
      }
  
  
      /**
       * Set the requested session ID for this request.  This is normally called
       * by the HTTP Connector, when it parses the request headers.
       *
       * @param id The new session id
       */
      public void setRequestedSessionId(String id) {
  
  	this.requestedSessionId = id;
  
      }
  
  
      /**
       * Set a flag indicating whether or not the requested session ID for this
       * request came in through a URL.  This is normally called by the
       * HTTP Connector, when it parses the request headers.
       *
       * @param flag The new flag
       */
      public void setRequestedSessionURL(boolean flag) {
  
  	this.requestedSessionURL = flag;
  
      }
  
  
      /**
       * Set the unparsed request URI for this Request.  This will normally
       * be called by the HTTP Connector, when it parses the request headers.
       *
       * @param uri The request URI
       */
      public void setRequestURI(String uri) {
  
  	this.requestURI = uri;
  
      }
  
  
      /**
       * Set the flag indicating whether this Request was received on a secure
       * communications link or not.  This will normally be called by the HTTP
       * Connector, when it parses the request headers.
       *
       * @param secure The new secure flag
       */
      public void setSecure(boolean secure) {
  
  	this.secure = secure;
  
      }
  
  
      /**
       * Set the servlet path for this Request.  This will normally be called
       * when the associated Context is mapping the Request to a particular
       * Wrapper.
       *
       * @param path The servlet path
       */
      public void setServletPath(String path) {
  
  	this.servletPath = path;
  
      }
  
  
      /**
       * Set the Principal who has been authenticated for this Request.  This
       * value is also used to calculate the value to be returned by the
       * <code>getRemoteUser()</code> method.
       *
       * @param principal The user Principal
       */
      public void setUserPrincipal(Principal principal) {
  
  	this.userPrincipal = principal;
  
      }
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * Parse the parameters of this request, if it has not already occurred.
       * If parameters are present in both the query string and the request
       * content, they are merged.
       */
      protected void parseParameters() {
  
  	if (parameters != null)
  	    return;
  
  	HashMap results = new HashMap();
  
  	// Parse any query string parameters for this request
  	Hashtable queryParameters = null;
  	try {
  	    queryParameters = HttpUtils.parseQueryString(getQueryString());
  	} catch (IllegalArgumentException e) {
  	    ;
  	}
  
  	// Parse any posted parameters in the input stream
  	Hashtable postParameters = null;
  	if ("POST".equals(getMethod()) &&
  	    "application/x-www-form-urlencoded".equals(getContentType())) {
  	    try {
  		ServletInputStream is = getInputStream();
  		postParameters =
  		    HttpUtils.parsePostData(getContentLength(), is);
  	    } catch (IllegalArgumentException e) {
  		;
  	    } catch (IOException e) {
  		;
  	    }
  	}
  
  	// Process the query parameters first
  	if (queryParameters != null) {
  	    Enumeration queryKeys = queryParameters.keys();
  	    while (queryKeys.hasMoreElements()) {
  		String queryKey = (String) queryKeys.nextElement();
  		Object queryValues = queryParameters.get(queryKey);
  		ArrayList values = new ArrayList();
  		if (queryValues instanceof String)
  		    values.add((String) queryValues);
  		else if (queryValues instanceof String[]) {
  		    String queryStrings[] = (String[]) queryValues;
  		    for (int i = 0; i < queryStrings.length; i++)
  			values.add(queryStrings[i]);
  		}
  		results.put(queryKey,
  			    (String[]) values.toArray(new String[0]));
  	    }
  	}
  
  	// Process the post parameters second
  	if (postParameters != null) {
  	    Enumeration postKeys = postParameters.keys();
  	    while (postKeys.hasMoreElements()) {
  		String postKey = (String) postKeys.nextElement();
  		Object postValues = postParameters.get(postKey);
  		ArrayList values = new ArrayList();
  		String queryValues[] = (String[]) results.get(postKey);
  		if (queryValues != null) {
  		    for (int i = 0; i < queryValues.length; i++)
  			values.add(queryValues[i]);
  		}
  		if (postValues instanceof String)
  		    values.add((String) postValues);
  		else if (postValues instanceof String[]) {
  		    String postStrings[] = (String[]) postValues;
  		    for (int i = 0; i < postStrings.length; i++)
  			values.add(postStrings[i]);
  		}
  		results.put(postKey,
  			    (String[]) values.toArray(new String[0]));
  	    }
  	}
  
  	// Store the final results
  	parameters = results;
  
      }
  
  
      // ------------------------------------------------- ServletRequest Methods
  
  
      /**
       * Return the value of the specified request parameter, if any; otherwise,
       * return <code>null</code>.  If there is more than one value defined,
       * return only the first one.
       *
       * @param name Name of the desired request parameter
       */
      public String getParameter(String name) {
  
  	parseParameters();
  
  	synchronized (parameters) {
  	    String values[] = (String[]) parameters.get(name);
  	    if (values != null)
  		return (values[0]);
  	    else
  		return (null);
  	}
  
      }
  
  
      /**
       * Return the names of all defined request parameters for this request.
       */
      public Enumeration getParameterNames() {
  
  	parseParameters();
  
  	synchronized (parameters) {
  	    return (new Enumerator(parameters.keySet()));
  	}
  
      }
  
  
      /**
       * Return the defined values for the specified request parameter, if any;
       * otherwise, return <code>null</code>.
       *
       * @param name Name of the desired request parameter
       */
      public String[] getParameterValues(String name) {
  
  	parseParameters();
  
  	synchronized (parameters) {
  	    String values[] = (String[]) parameters.get(name);
  	    if (values != null)
  		return (values);
  	    else
  		return (null);
  	}
  
      }
  
  
      /**
       * Return a RequestDispatcher that wraps the resource at the specified
       * path, which may be interpreted as relative to the current request path.
       *
       * @param path Path of the resource to be wrapped
       */
      public RequestDispatcher getRequestDispatcher(String path) {
  
  	// If the path is already context-relative, just pass it through
  	if (path == null)
  	    return (null);
  	else if (path.startsWith("/"))
  	    return (context.getServletContext().getRequestDispatcher(path));
  
  	// Convert a request-relative path to a context-relative one
  	String relative = getServletPath() + "/../" + path;
  	// FIXME -- Canonicalize any ".." directory references!
  	return (context.getServletContext().getRequestDispatcher(relative));
  
      }
  
  
      /**
       * Was this request received on a secure connection?
       */
      public boolean isSecure() {
  
  	return (secure);
  
      }
  
  
      // --------------------------------------------- HttpServletRequest Methods
  
  
      /**
       * Return the authentication type used for this Request.
       */
      public String getAuthType() {
  
  	return (authType);
  
      }
  
  
      /**
       * Return the portion of the request URI used to select the Context
       * of the Request.
       */
      public String getContextPath() {
  
  	return (contextPath);
  
      }
  
  
      /**
       * Return the set of Cookies received with this Request.
       */
      public Cookie[] getCookies() {
  
  	synchronized (cookies) {
  	    Cookie results[] = new Cookie[cookies.size()];
  	    return ((Cookie[]) cookies.toArray(results));
  	}
  
      }
  
  
      /**
       * Return the value of the specified date header, if any; otherwise
       * return -1.
       *
       * @param name Name of the requested date header
       *
       * @exception IllegalArgumentException if the specified header value
       *  cannot be converted to a date
       */
      public long getDateHeader(String name) {
  
  	String value = getHeader(name);
  	if (value == null)
  	    return (-1L);
  
  	// Work around a bug in SimpleDateFormat in pre-JDK1.2b4
  	// (Bug Parade bug #4106807)
  	value += " ";
  
  	// Attempt to convert the date header in a variety of formats
  	for (int i = 0; i < formats.length; i++) {
  	    try {
  		Date date = formats[i].parse(value);
  		return (date.getTime());
  	    } catch (ParseException e) {
  		;
  	    }
  	}
  	throw new IllegalArgumentException(value);
  
      }
  
  
      /**
       * Return the first value of the specified header, if any; otherwise,
       * return <code>null</code>
       *
       * @param name Name of the requested header
       */
      public String getHeader(String name) {
  
  	name = name.toLowerCase();
  	synchronized (headers) {
  	    ArrayList values = (ArrayList) headers.get(name);
  	    if (values != null)
  		return ((String) values.get(0));
  	    else
  		return (null);
  	}
  
      }
  
  
      /**
       * Return all of the values of the specified header, if any; otherwise,
       * return an empty enumeration.
       *
       * @param name Name of the requested header
       */
      public Enumeration getHeaders(String name) {
  
  	name = name.toLowerCase();
  	synchronized (headers) {
  	    ArrayList values = (ArrayList) headers.get(name);
  	    if (values != null)
  		return (new Enumerator(values));
  	    else
  		return (new Enumerator(empty));
  	}
  
      }
  
  
      /**
       * Return the names of all headers received with this request.
       */
      public Enumeration getHeaderNames() {
  
  	synchronized (headers) {
  	    return (new Enumerator(headers.keySet()));
  	}
  
      }
  
  
      /**
       * Return the value of the specified header as an integer, or -1 if there
       * is no such header for this request.
       *
       * @param name Name of the requested header
       *
       * @exception IllegalArgumentException if the specified header value
       *  cannot be converted to an integer
       */
      public int getIntHeader(String name) {
  
  	String value = getHeader(name);
  	if (value == null)
  	    return (-1);
  	else
  	    return (Integer.parseInt(value));
  
      }
  
  
      /**
       * Return the HTTP request method used in this Request.
       */
      public String getMethod() {
  
  	return (method);
  
      }
  
  
      /**
       * Return the path information associated with this Request.
       */
      public String getPathInfo() {
  
  	return (pathInfo);
  
      }
  
  
      /**
       * Return the extra path information for this request, translated
       * to a real path.
       */
      public String getPathTranslated() {
  
  	if (pathInfo == null)
  	    return (null);
  	else
  	    return (context.getServletContext().getRealPath(pathInfo));
  
      }
  
  
      /**
       * Return the query string associated with this request.
       */
      public String getQueryString() {
  
  	return (queryString);
  
      }
  
  
      /**
       * Return the name of the remote user that has been authenticated
       * for this Request.
       */
      public String getRemoteUser() {
  
  	if (userPrincipal != null)
  	    return (userPrincipal.getName());
  	else
  	    return (null);
  
      }
  
  
      /**
       * Return the session identifier included in this request, if any.
       */
      public String getRequestedSessionId() {
  
  	return (requestedSessionId);
  
      }
  
  
      /**
       * Return the request URI for this request.
       */
      public String getRequestURI() {
  
  	return (requestURI);
  
      }
  
  
      /**
       * Return the portion of the request URI used to select the servlet
       * that will process this request.
       */
      public String getServletPath() {
  
  	return (servletPath);
  
      }
  
  
      /**
       * Return the session associated with this Request, creating one
       * if necessary.
       */
      public HttpSession getSession() {
  
  	return (getSession(true));
  
      }
  
  
      /**
       * Return the session associated with this Request, creating one
       * if necessary and requested.
       *
       * @param create Create a new session if one does not exist
       */
      public HttpSession getSession(boolean create) {
  
  	// Return the current session if it exists and is valid
  	if ((session != null) && !session.isValid())
  	    session = null;
  	if (session != null)
  	    return (session.getSession());
  
  	// Return the requested session if it exists and is valid
  	Manager manager = context.getManager();
  	if ((manager != null) && (requestedSessionId != null)) {
  	    try {
  		session = manager.findSession(requestedSessionId);
  	    } catch (IOException e) {
  		session = null;
  	    }
  	    if ((session != null) && !session.isValid())
  	        session = null;
  	    if (session != null) {
  		session.access();
  		return (session.getSession());
  	    }
  	}
  
  	// Create a new session if requested
  	if (!create)
  	    return (null);
  	session = manager.createSession();
  	if (session != null)
  	    return (session.getSession());
  	else
  	    return (null);
  
      }
  
  
      /**
       * Return <code>true</code> if the session identifier included in this
       * request came from a cookie.
       */
      public boolean isRequestedSessionIdFromCookie() {
  
  	if (requestedSessionId != null)
  	    return (requestedSessionCookie);
  	else
  	    return (false);
  
      }
  
  
      /**
       * Return <code>true</code> if the session identifier included in this
       * request came from the request URI.
       */
      public boolean isRequestedSessionIdFromURL() {
  
  	if (requestedSessionId != null)
  	    return (requestedSessionURL);
  	else
  	    return (false);
  
      }
  
  
      /**
       * Return <code>true</code> if the session identifier included in this
       * request came from the request URI.
       *
       * @deprecated As of Version 2.1 of the Java Servlet API, use
       *  <code>isRequestedSessionIdFromURL()</code> instead.
       */
      public boolean isRequestedSessionIdFromUrl() {
  
  	return (isRequestedSessionIdFromURL());
  
      }
  
  
      /**
       * Return <code>true</code> if the session identifier included in this
       * request identifies a valid session.
       */
      public boolean isRequestedSessionIdValid() {
  
  	if (requestedSessionId == null)
  	    return (false);
  	Manager manager = context.getManager();
  	if (manager == null)
  	    return (false);
  	Session session = null;
  	try {
  	    session = manager.findSession(requestedSessionId);
  	} catch (IOException e) {
  	    session = null;
  	}
  	if ((session != null) && session.isValid())
  	    return (true);
  	else
  	    return (false);
  
      }
  
  
      /**
       * Return <code>true</code> if the authenticated user principal
       * possesses the specified role name.
       *
       * @param role Role name to be validated
       */
      public boolean isUserInRole(String role) {
  
  	// Respect role name translations in the deployment descriptor
  	String realRole = context.findRoleMapping(role);
  
  	// Determine whether the current user has this role
  	if (userPrincipal == null)
  	    return (false);
  	Realm realm = context.getRealm();
  	if (realm == null)
  	    return (false);
  
  	return (realm.hasRole(userPrincipal, realRole));
  
      }
  
  
      /**
       * Return the principal that has been authenticated for this Request.
       */
      public Principal getUserPrincipal() {
  
  	return (userPrincipal);
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpRequestWrapper.java
  
  Index: HttpRequestWrapper.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpRequestWrapper.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.connector;
  
  
  import java.security.Principal;
  import java.util.Locale;
  import javax.servlet.http.Cookie;
  import org.apache.catalina.HttpRequest;
  
  
  /**
   * Abstract convenience class that wraps a Catalina-internal <b>HttpRequest</b>
   * object.  By default, all methods are delegated to the wrapped request,
   * but subclasses can override individual methods as required to provide the
   * functionality that they require.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public abstract class HttpRequestWrapper
      extends RequestWrapper
      implements HttpRequest {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a wrapper for the specified request.
       *
       * @param request The request to be wrapped
       */
      public HttpRequestWrapper(HttpRequest request) {
  
  	super(request);
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Add a Cookie to the set of Cookies associated with this Request.
       *
       * @param cookie The new cookie
       */
      public void addCookie(Cookie cookie) {
  
  	((HttpRequest) request).addCookie(cookie);
  
      }
  
  
      /**
       * Add a Header to the set of Headers associated with this Request.
       *
       * @param name The new header name
       * @param value The new header value
       */
      public void addHeader(String name, String value) {
  
  	((HttpRequest) request).addHeader(name, value);
  
      }
  
  
      /**
       * Add a Locale to the set of preferred Locales for this Request.  The
       * first added Locale will be the first one returned by getLocales().
       *
       * @param locale The new preferred Locale
       */
      public void addLocale(Locale locale) {
  
  	((HttpRequest) request).addLocale(locale);
  
      }
  
  
      /**
       * Clear the collection of Cookies associated with this Request.
       */
      public void clearCookies() {
  
  	((HttpRequest) request).clearCookies();
  
      }
  
  
      /**
       * Clear the collection of Headers associated with this Request.
       */
      public void clearHeaders() {
  
  	((HttpRequest) request).clearHeaders();
  
      }
  
  
      /**
       * Clear the collection of Locales associated with this Request.
       */
      public void clearLocales() {
  
  	((HttpRequest) request).clearLocales();
  
      }
  
  
      /**
       * Set the authentication type used for this request, if any; otherwise
       * set the type to <code>null</code>.  Typical values are "BASIC",
       * "DIGEST", or "SSL".
       *
       * @param type The authentication type used
       */
      public void setAuthType(String type) {
  
  	((HttpRequest) request).setAuthType(type);
  
      }
  
  
      /**
       * Set the context path for this Request.  This will normally be called
       * when the associated Context is mapping the Request to a particular
       * Wrapper.
       *
       * @param path The context path
       */
      public void setContextPath(String path) {
  
  	((HttpRequest) request).setContextPath(path);
  
      }
  
  
      /**
       * Set the HTTP request method used for this Request.
       *
       * @param method The request method
       */
      public void setMethod(String method) {
  
  	((HttpRequest) request).setMethod(method);
  
      }
  
  
      /**
       * Set the query string for this Request.  This will normally be called
       * by the HTTP Connector, when it parses the request headers.
       *
       * @param query The query string
       */
      public void setQueryString(String query) {
  
  	((HttpRequest) request).setQueryString(query);
  
      }
  
  
      /**
       * Set the path information for this Request.  This will normally be called
       * when the associated Context is mapping the Request to a particular
       * Wrapper.
       *
       * @param path The path information
       */
      public void setPathInfo(String path) {
  
  	((HttpRequest) request).setPathInfo(path);
  
      }
  
  
      /**
       * Set a flag indicating whether or not the requested session ID for this
       * request came in through a cookie.  This is normally called by the
       * HTTP Connector, when it parses the request headers.
       *
       * @param flag The new flag
       */
      public void setRequestedSessionCookie(boolean flag) {
  
  	((HttpRequest) request).setRequestedSessionCookie(flag);
  
      }
  
  
      /**
       * Set the requested session ID for this request.  This is normally called
       * by the HTTP Connector, when it parses the request headers.
       *
       * @param id The new session id
       */
      public void setRequestedSessionId(String id) {
  
  	((HttpRequest) request).setRequestedSessionId(id);
  
      }
  
  
      /**
       * Set a flag indicating whether or not the requested session ID for this
       * request came in through a URL.  This is normally called by the
       * HTTP Connector, when it parses the request headers.
       *
       * @param flag The new flag
       */
      public void setRequestedSessionURL(boolean flag) {
  
  	((HttpRequest) request).setRequestedSessionURL(flag);
  
      }
  
  
      /**
       * Set the unparsed request URI for this Request.  This will normally be
       * called by the HTTP Connector, when it parses the request headers.
       *
       * @param uri The request URI
       */
      public void setRequestURI(String uri) {
  
  	((HttpRequest) request).setRequestURI(uri);
  
      }
  
  
      /**
       * Set the servlet path for this Request.  This will normally be called
       * when the associated Context is mapping the Request to a particular
       * Wrapper.
       *
       * @param path The servlet path
       */
      public void setServletPath(String path) {
  
  	((HttpRequest) request).setServletPath(path);
  
      }
  
  
      /**
       * Set the Principal who has been authenticated for this Request.  This
       * value is also used to calculate the value to be returned by the
       * <code>getRemoteUser()</code> method.
       *
       * @param principal The user Principal
       */
      public void setUserPrincipal(Principal principal) {
  
  	((HttpRequest) request).setUserPrincipal(principal);
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpResponseBase.java
  
  Index: HttpResponseBase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpResponseBase.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.connector;
  
  
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Locale;
  import java.util.TimeZone;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  import javax.servlet.http.HttpUtils;
  import org.apache.catalina.HttpResponse;
  import org.apache.catalina.Logger;
  import org.apache.catalina.util.CookieTools;
  import org.apache.catalina.util.RequestUtil;
  
  
  /**
   * Convenience base implementation of the <b>HttpResponse</b> interface, which
   * can be used for the <code>Response</code> implementation required by most
   * <code>Connectors</code> that deal with HTTP.  Only the connector-specific
   * methods need to be implemented.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public class HttpResponseBase
      extends ResponseBase
      implements HttpResponse, HttpServletResponse {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The set of Cookies associated with this Response.
       */
      protected ArrayList cookies = new ArrayList();
  
  
      /**
       * The date format we will use for creating date headers.
       */
      protected static final SimpleDateFormat format =
  	new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",Locale.US);
      static {
  	format.setTimeZone(TimeZone.getTimeZone("GMT"));
      };
  
  
      /**
       * The HTTP headers explicitly added via addHeader(), but not including
       * those to be added with setContentLength(), setContentType(), and so on.
       * This collection is keyed by the header name, and the elements are
       * ArrayLists containing the associated values that have been set.
       */
      protected HashMap headers = new HashMap();
  
  
      /**
       * Descriptive information about this HttpResponse implementation.
       */
      protected static final String info =
  	"org.apache.catalina.connector.HttpResponseBase/1.0";
  
  
      /**
       * The error message set by <code>sendError()</code>.
       */
      protected String message = getStatusMessage(HttpServletResponse.SC_OK);
  
  
      /**
       * The HTTP status code associated with this Response.
       */
      protected int status = HttpServletResponse.SC_OK;
  
  
      /**
       * The time zone with which to construct date headers.
       */
      protected static final TimeZone zone = TimeZone.getTimeZone("GMT");
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Return the value for the specified header, or <code>null</code> if this
       * header has not been set.  If more than one value was added for this
       * name, only the first is returned; use getHeaderValues() to retrieve all
       * of them.
       *
       * @param name Header name to look up
       */
      public String getHeader(String name) {
  
  	ArrayList values = null;
  	synchronized (headers) {
  	    values = (ArrayList) headers.get(name);
  	}
  	if (values != null)
  	    return ((String) values.get(0));
  	else
  	    return (null);
  
      }
  
  
      /**
       * Return an array of all the header names set for this response, or
       * a zero-length array if no headers have been set.
       */
      public String[] getHeaderNames() {
  
  	synchronized (headers) {
  	    String results[] = new String[headers.size()];
  	    return ((String[]) headers.keySet().toArray(results));
  	}
  
      }
  
  
      /**
       * Return an array of all the header values associated with the
       * specified header name, or an zero-length array if there are no such
       * header values.
       *
       * @param name Header name to look up
       */
      public String[] getHeaderValues(String name) {
  
  	ArrayList values = null;
  	synchronized (headers) {
  	    values = (ArrayList) headers.get(name);
  	}
  	if (values == null)
  	    return (new String[0]);
  	String results[] = new String[values.size()];
  	return ((String[]) values.toArray(results));
  
      }
  
  
      /**
       * Return the error message that was set with <code>sendError()</code>
       * for this Response.
       */
      public String getMessage() {
  
  	return (this.message);
  
      }
  
  
      /**
       * Return the HTTP status code associated with this Response.
       */
      public int getStatus() {
  
  	return (this.status);
  
      }
  
  
      /**
       * Release all object references, and initialize instance variables, in
       * preparation for reuse of this object.
       */
      public void recycle() {
  
  	super.recycle();
  	cookies.clear();
  	headers.clear();
  	message = getStatusMessage(HttpServletResponse.SC_OK);
  	status = HttpServletResponse.SC_OK;
  
      }
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * Returns a default status message for the specified HTTP status code.
       *
       * @param status The status code for which a message is desired
       */
      protected String getStatusMessage(int status) {
  
  	switch (status) {
  	case SC_OK:
  	    return ("OK");
  	case SC_ACCEPTED:
  	    return ("Accepted");
  	case SC_BAD_GATEWAY:
  	    return ("Bad Gateway");
  	case SC_BAD_REQUEST:
  	    return ("Bad Request");
  	case SC_CONFLICT:
  	    return ("Conflict");
  	case SC_CONTINUE:
  	    return ("Continue");
  	case SC_CREATED:
  	    return ("Created");
  	case SC_EXPECTATION_FAILED:
  	    return ("Expectation Failed");
  	case SC_FORBIDDEN:
  	    return ("Forbidden");
  	case SC_GATEWAY_TIMEOUT:
  	    return ("Gateway Timeout");
  	case SC_GONE:
  	    return ("Gone");
  	case SC_HTTP_VERSION_NOT_SUPPORTED:
  	    return ("HTTP Version Not Supported");
  	case SC_INTERNAL_SERVER_ERROR:
  	    return ("Internal Server Error");
  	case SC_LENGTH_REQUIRED:
  	    return ("Length Required");
  	case SC_METHOD_NOT_ALLOWED:
  	    return ("Method Not Allowed");
  	case SC_MOVED_PERMANENTLY:
  	    return ("Moved Permanently");
  	case SC_MOVED_TEMPORARILY:
  	    return ("Moved Temporarily");
  	case SC_MULTIPLE_CHOICES:
  	    return ("Multiple Choices");
  	case SC_NO_CONTENT:
  	    return ("No Content");
  	case SC_NON_AUTHORITATIVE_INFORMATION:
  	    return ("Non-Authoritative Information");
  	case SC_NOT_ACCEPTABLE:
  	    return ("Not Acceptable");
  	case SC_NOT_FOUND:
  	    return ("Not Found");
  	case SC_NOT_IMPLEMENTED:
  	    return ("Not Implemented");
  	case SC_NOT_MODIFIED:
  	    return ("Not Modified");
  	case SC_PARTIAL_CONTENT:
  	    return ("Partial Content");
  	case SC_PAYMENT_REQUIRED:
  	    return ("Payment Required");
  	case SC_PRECONDITION_FAILED:
  	    return ("Precondition Failed");
  	case SC_PROXY_AUTHENTICATION_REQUIRED:
  	    return ("Proxy Authentication Required");
  	case SC_REQUEST_ENTITY_TOO_LARGE:
  	    return ("Request Entity Too Large");
  	case SC_REQUEST_TIMEOUT:
  	    return ("Request Timeout");
  	case SC_REQUEST_URI_TOO_LONG:
  	    return ("Request URI Too Long");
  	case SC_REQUESTED_RANGE_NOT_SATISFIABLE:
  	    return ("Requested Range Not Satisfiable");
  	case SC_RESET_CONTENT:
  	    return ("Reset Content");
  	case SC_SEE_OTHER:
  	    return ("See Other");
  	case SC_SERVICE_UNAVAILABLE:
  	    return ("Service Unavailable");
  	case SC_SWITCHING_PROTOCOLS:
  	    return ("Switching Protocols");
  	case SC_UNAUTHORIZED:
  	    return ("Unauthorized");
  	case SC_UNSUPPORTED_MEDIA_TYPE:
  	    return ("Unsupported Media Type");
  	case SC_USE_PROXY:
  	    return ("Use Proxy");
  	default:
  	    return ("HTTP Response Status " + status);
  	}
  
      }
  
  
      /**
       * Return <code>true</code> if the specified URL should be encoded with
       * a session identifier.  This will be true if all of the following
       * conditions are met:
       * <ul>
       * <li>The request we are responding to asked for a valid session
       * <li>The requested session ID was not received via a cookie
       * <li>The specified URL points back to somewhere within the web
       *     application that is responding to this request
       * </ul>
       *
       * @param location Absolute URL to be validated
       **/
      private boolean isEncodeable(String location) {
  
  	// Are we in a valid session that is not using cookies?
  	HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
  	HttpSession session = hreq.getSession(false);
  	if (session == null)
  	    return (false);
  	if (hreq.isRequestedSessionIdFromCookie())
  	    return (false);
  
  	// Is this a valid absolute URL?
  	URL url = null;
  	try {
  	    url = new URL(location);
  	} catch (MalformedURLException e) {
  	    return (false);
  	}
  
  	// Does this URL match down to (and including) the context path?
  	if (!hreq.getScheme().equalsIgnoreCase(url.getProtocol()))
  	    return (false);
  	if (!hreq.getServerName().equalsIgnoreCase(url.getHost()))
  	    return (false);
  	int serverPort = hreq.getServerPort();
  	if (serverPort == -1)	// Work around bug in java.net.URL.getHost()
  	    serverPort = 80;
  	int urlPort = url.getPort();
  	if (urlPort == -1)
  	    urlPort = 80;
  	if (serverPort != urlPort)
  	    return (false);
  
  	String contextPath = getContext().getPath();
  	if ((contextPath != null) && (contextPath.length() > 0)) {
  	    String file = url.getFile();
  	    if ((file == null) || !file.startsWith(contextPath))
  		return (false);
  	}
  
  	// This URL belongs to our web application, so it is encodeable
  	return (true);
  
      }
  
  
      private void log(String message) {
  	Logger logger = context.getLogger();
  	logger.log(message);
      }
  
  
      private void log(String message, Throwable throwable) {
  	Logger logger = context.getLogger();
  	logger.log(message, throwable);
      }
  
  
      /**
       * Send the HTTP response headers, if this has not already occurred.
       */
      protected void sendHeaders() throws IOException {
  
  	if (isCommitted())
  	    return;
  
  	// Prepare a suitable output writer
  	OutputStreamWriter osr =
  	    new OutputStreamWriter(getStream(), getCharacterEncoding());
  	PrintWriter outputWriter = new PrintWriter(osr);
  
  	// Send the "Status:" header
  	outputWriter.print(request.getRequest().getProtocol());
  	outputWriter.print(" ");
  	outputWriter.print(status);
  	if (message != null) {
  	    outputWriter.print(" ");
  	    outputWriter.print(message);
  	}
  	outputWriter.print("\r\n");
  //	log(request.getRequest().getProtocol() + " " + status + " " + message);
  
  	// Send the content-length and content-type headers (if any)
  	if (getContentType() != null) {
  	    outputWriter.print("Content-Type: " + getContentType() + "\r\n");
  //	    log(" Content-Type: " + getContentType());
  	}
  	if (getContentLength() >= 0) {
  	    outputWriter.print("Content-Length: " + getContentLength() +
  			       "\r\n");
  //	    log("  Content-Length: " + getContentLength());
  	}
  
  	// Send all specified headers (if any)
  	synchronized (headers) {
  	    Iterator names = headers.keySet().iterator();
  	    while (names.hasNext()) {
  		String name = (String) names.next();
  		ArrayList values = (ArrayList) headers.get(name);
  		Iterator items = values.iterator();
  		while (items.hasNext()) {
  		    String value = (String) items.next();
  		    outputWriter.print(name);
  		    outputWriter.print(": ");
  		    outputWriter.print(value);
  		    outputWriter.print("\r\n");
  		}
  	    }
  	}
  
  	// Add the session ID cookie if necessary
  	HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
  	HttpSession session = hreq.getSession(false);
  
  	if ((session != null) && session.isNew() &&
  	    getContext().getCookies()) {
  	    Cookie cookie = new Cookie(Constants.SessionCookie,
  				       session.getId());
  	    cookie.setMaxAge(-1);
  	    String contextPath = context.getPath();
  	    if ((contextPath != null) && (contextPath.length() > 0))
  		cookie.setPath(contextPath);
  	    if (hreq.isSecure())
  		cookie.setSecure(true);
  	    addCookie(cookie);
  	}
  
  	// Send all specified cookies (if any)
  	synchronized (cookies) {
  	    Iterator items = cookies.iterator();
  	    while (items.hasNext()) {
  		Cookie cookie = (Cookie) items.next();
  		outputWriter.print(CookieTools.getCookieHeaderName(cookie));
  		outputWriter.print(": ");
  		outputWriter.print(CookieTools.getCookieHeaderValue(cookie));
  		outputWriter.print("\r\n");
  	    }
  	}
  
  	// Send a terminating blank line to mark the end of the headers
  	outputWriter.print("\r\n");
  	outputWriter.flush();
  
      }
  
  
      /**
       * Convert (if necessary) and return the absolute URL that represents the
       * resource referenced by this possibly relative URL.  If this URL is
       * already absolute, return it unchanged.
       *
       * @param location URL to be (possibly) converted and then returned
       *
       * @exception IllegalArgumentException if a MalformedURLException is
       *  thrown when converting the relative URL to an absolute one
       */
      private String toAbsolute(String location) {
  
  	if (location == null)
  	    return (location);
  
  	// Construct a new absolute URL if possible (cribbed from
  	// the DefaultErrorPage servlet)
  	URL url = null;
  	try {
  	    url = new URL(location);
  	} catch (MalformedURLException e1) {
  	    HttpServletRequest hreq =
  		(HttpServletRequest) request.getRequest();
  	    String requrl = HttpUtils.getRequestURL(hreq).toString();
  	    try {
  		url = new URL(new URL(requrl), location);
  	    } catch (MalformedURLException e2) {
  		throw new IllegalArgumentException(location);
  	    }
  	}
  	return (url.toString());
  
      }
  
  
      /**
       * Return the specified URL with the specified session identifier
       * suitably encoded.
       *
       * @param url URL to be encoded with the session id
       * @param sessionId Session id to be included in the encoded URL
       */
      private String toEncoded(String url, String sessionId) {
  
  	if ((url == null) || (sessionId == null))
  	    return (url);
  
  	String path = url;
  	String query = "";
  	int question = url.indexOf("?");
  	if (question >= 0) {
  	    path = url.substring(0, question);
  	    query = url.substring(question);
  	}
  	StringBuffer sb = new StringBuffer(path);
  	sb.append(";jsessionid=");
  	sb.append(sessionId);
  	sb.append(query);
  	return (sb.toString());
  
      }
  
  
      // ------------------------------------------------ ServletResponse Methods
  
  
      /**
       * Flush the buffer and commit this response.  If this is the first output,
       * send the HTTP headers prior to the user data.
       *
       * @exception IOException if an input/output error occurs
       */
      public void flushBuffer() throws IOException {
  
  	if (!isCommitted())
  	    sendHeaders();
  
  	super.flushBuffer();
  
      }
  
  
      /**
       * Clear any content written to the buffer.  In addition, all cookies
       * and headers are cleared, and the status is reset.
       *
       * @exception IllegalStateException if this response has already
       *  been committed
       */
      public void reset() {
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	super.reset();
  	cookies.clear();
  	headers.clear();
  	message = null;
  	status = HttpServletResponse.SC_OK;
  
      }
  
  
      /**
       * Set the content length (in bytes) for this Response.
       *
       * @param length The new content length
       */
      public void setContentLength(int length) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	super.setContentLength(length);
  
      }
  
  
  
      /**
       * Set the content type for this Response.
       *
       * @param type The new content type
       */
      public void setContentType(String type) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	super.setContentType(type);
  
      }
  
  
      /**
       * Set the Locale that is appropriate for this response, including
       * setting the appropriate character encoding.
       *
       * @param locale The new locale
       */
      public void setLocale(Locale locale) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	super.setLocale(locale);
  	String language = locale.getLanguage();
  	if ((language != null) && (language.length() > 0))
  	    setHeader("Content-Language", language);
  	;	// FIXME -- set encoding based on the locale?
  
  
      }
  
  
      // -------------------------------------------- HttpServletResponse Methods
  
  
      /**
       * Add the specified Cookie to those that will be included with
       * this Response.
       *
       * @param cookie Cookie to be added
       */
      public void addCookie(Cookie cookie) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	synchronized (cookies) {
  	    cookies.add(cookie);
  	}
  
      }
  
  
      /**
       * Add the specified date header to the specified value.
       *
       * @param name Name of the header to set
       * @param value Date value to be set
       */
      public void addDateHeader(String name, long value) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	addHeader(name, format.format(new Date(value)));
  
      }
  
  
      /**
       * Add the specified header to the specified value.
       *
       * @param name Name of the header to set
       * @param value Value to be set
       */
      public void addHeader(String name, String value) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	synchronized (headers) {
  	    ArrayList values = (ArrayList) headers.get(name);
  	    if (values == null) {
  		values = new ArrayList();
  		headers.put(name, values);
  	    }
  	    values.add(value);
  	}
  
      }
  
  
      /**
       * Add the specified integer header to the specified value.
       *
       * @param name Name of the header to set
       * @param value Integer value to be set
       */
      public void addIntHeader(String name, int value) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	addHeader(name, "" + value);
  
      }
  
  
      /**
       * Has the specified header been set already in this response?
       *
       * @param name Name of the header to check
       */
      public boolean containsHeader(String name) {
  
  	synchronized (headers) {
  	    return (headers.get(name) != null);
  	}
  
      }
  
  
      /**
       * Encode the session identifier associated with this response
       * into the specified redirect URL, if necessary.
       *
       * @param url URL to be encoded
       */
      public String encodeRedirectURL(String url) {
  
  	if (isEncodeable(toAbsolute(url))) {
  	    HttpServletRequest hreq =
  	      (HttpServletRequest) request.getRequest();
  	    return (toEncoded(url, hreq.getSession().getId()));
  	} else
  	    return (url);
  
      }
  
  
      /**
       * Encode the session identifier associated with this response
       * into the specified redirect URL, if necessary.
       *
       * @param url URL to be encoded
       *
       * @deprecated As of Version 2.1 of the Java Servlet API, use
       *  <code>encodeRedirectURL()</code> instead.
       */
      public String encodeRedirectUrl(String url) {
  
  	return (encodeRedirectURL(url));
  
      }
  
  
      /**
       * Encode the session identifier associated with this response
       * into the specified URL, if necessary.
       *
       * @param url URL to be encoded
       */
      public String encodeURL(String url) {
  
  	if (isEncodeable(toAbsolute(url))) {
  	    HttpServletRequest hreq =
  	      (HttpServletRequest) request.getRequest();
  	    return (toEncoded(url, hreq.getSession().getId()));
  	} else
  	    return (url);
  
      }
  
  
      /**
       * Encode the session identifier associated with this response
       * into the specified URL, if necessary.
       *
       * @param url URL to be encoded
       *
       * @deprecated As of Version 2.1 of the Java Servlet API, use
       *  <code>encodeURL()</code> instead.
       */
      public String encodeUrl(String url) {
  
  	return (encodeURL(url));
  
      }
  
  
      /**
       * Send an error response with the specified status and a
       * default message.
       *
       * @param status HTTP status code to send
       *
       * @exception IllegalStateException if this response has
       *  already been committed
       * @exception IOException if an input/output error occurs
       */
      public void sendError(int status) throws IOException {
  
  	sendError(status, getStatusMessage(status));
  
      }
  
  
      /**
       * Send an error response with the specified status and message.
       *
       * @param status HTTP status code to send
       * @param message Corresponding message to send
       *
       * @exception IllegalStateException if this response has
       *  already been committed
       * @exception IOException if an input/output error occurs
       */
      public void sendError(int status, String message) throws IOException {
  
  	if (isCommitted())
  	    throw new IllegalStateException
  		(sm.getString("httpResponseBase.sendError.ise"));
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	// Record the status code and message.
  	this.status = status;
  	this.message = message;
  
  	// Cause the response to be committed but generate no output
  	try {
  	    resetBuffer();
  	} catch (Throwable t) {
  	    ;
  	}
  	String contentType = getContentType();
  	if ((contentType == null) || "text/plain".equals(contentType))
  	    setContentType("text/html");
  	try {
  	    flushBuffer();
  	} catch (IOException e) {
  	    ;
  	}
  
      }
  
  
      /**
       * Send a temporary redirect to the specified redirect location URL.
       *
       * @param location Location URL to redirect to
       *
       * @exception IllegalStateException if this response has
       *  already been committed
       * @exception IOException if an input/output error occurs
       */
      public void sendRedirect(String location) throws IOException {
  
  	if (isCommitted())
  	    throw new IllegalStateException
  		(sm.getString("httpResponseBase.sendRedirect.ise"));
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	reset();
  	String absolute = toAbsolute(location);
  	setStatus(SC_MOVED_TEMPORARILY, absolute);
  	setHeader("Location", absolute);
  	flushBuffer();
  
      }
  
  
      /**
       * Set the specified date header to the specified value.
       *
       * @param name Name of the header to set
       * @param value Date value to be set
       */
      public void setDateHeader(String name, long value) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	setHeader(name, format.format(new Date(value)));
  
      }
  
  
      /**
       * Set the specified header to the specified value.
       *
       * @param name Name of the header to set
       * @param value Value to be set
       */
      public void setHeader(String name, String value) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	ArrayList values = new ArrayList();
  	values.add(value);
  	synchronized (headers) {
  	    headers.put(name, values);
  	}
  
  	String match = name.toLowerCase();
  	if (match.equals("content-length")) {
  	    int contentLength = -1;
  	    try {
  		contentLength = Integer.parseInt(value);
  	    } catch (NumberFormatException e) {
  		;
  	    }
  	    if (contentLength >= 0)
  		setContentLength(contentLength);
  	} else if (match.equals("content-type")) {
  	    setContentType(value);
  	}
  
      }
  
  
      /**
       * Set the specified integer header to the specified value.
       *
       * @param name Name of the header to set
       * @param value Integer value to be set
       */
      public void setIntHeader(String name, int value) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	setHeader(name, "" + value);
  
      }
  
  
      /**
       * Set the HTTP status to be returned with this response.
       *
       * @param status The new HTTP status
       */
      public void setStatus(int status) {
  
  	setStatus(status, getStatusMessage(status));
  
      }
  
  
      /**
       * Set the HTTP status and message to be returned with this response.
       *
       * @param status The new HTTP status
       * @param message The associated text message
       *
       * @deprecated As of Version 2.1 of the Java Servlet API, this method
       *  has been deprecated due to the ambiguous meaning of the message
       *  parameter.
       */
      public void setStatus(int status, String message) {
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	this.status = status;
  	this.message = message;
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpResponseWrapper.java
  
  Index: HttpResponseWrapper.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/HttpResponseWrapper.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.catalina.connector;
  
  
  import org.apache.catalina.HttpResponse;
  
  
  /**
   * Abstract convenience class that wraps a Catalina-internal <b>HttpResponse</b>
   * object.  By default, all methods are delegated to the wrapped response,
   * but subclasses can override individual methods as required to provide the
   * functionality that they require.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public abstract class HttpResponseWrapper
      extends ResponseWrapper
      implements HttpResponse {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a wrapper for the specified response.
       *
       * @param response The response to be wrapped
       */
      public HttpResponseWrapper(HttpResponse response) {
  
  	super(response);
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Return the value for the specified header, or <code>null</code> if this
       * header has not been set.  If more than one value was added for this
       * name, only the first is returned; use getHeaderValues() to retrieve all
       * of them.
       *
       * @param name Header name to look up
       */
      public String getHeader(String name) {
  
  	return (((HttpResponse) response).getHeader(name));
  
      }
  
  
      /**
       * Return an array of all the header names set for this response, or
       * a zero-length array if no headers have been set.
       */
      public String[] getHeaderNames() {
  
  	return (((HttpResponse) response).getHeaderNames());
  
      }
  
  
      /**
       * Return an array of all the header values associated with the
       * specified header name, or an zero-length array if there are no such
       * header values.
       *
       * @param name Header name to look up
       */
      public String[] getHeaderValues(String name) {
  
  	return (((HttpResponse) response).getHeaderValues(name));
  
      }
  
  
      /**
       * Return the error message that was set with <code>sendError()</code>
       * for this response.
       */
      public String getMessage() {
  
  	return (((HttpResponse) response).getMessage());
  
      }
  
  
      /**
       * Return the HTTP status code associated with this Response.
       */
      public int getStatus() {
  
  	return (((HttpResponse) response).getStatus());
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  httpResponseBase.sendError.ise=Cannot call sendError() after the response has been committed
  httpResponseBase.sendRedirect.ise=Cannot call sendRedirect() after the response has been committed
  requestBase.getInputStream.ise=getReader() has already been called for this request
  requestBase.getReader.ise=getInputStream() has already been called for this request
  requestStream.close.closed=Cannot close the same input stream twice
  requestStream.read.closed=This input stream has been closed
  responseBase.getOutputStream.ise=getWriter() has already been called for this response
  responseBase.getWriter.ise=getOutputStream() has already been called for this response
  responseBase.reset.ise=Cannot reset after response has been committed
  responseBase.setBufferSize.ise=Buffer size cannot be changed after data has been written
  responseStream.close.closed=This output stream has already been closed
  responseStream.flush.closed=Cannot flush a closed output stream
  responseStream.write.closed=Cannot write to a closed output stream
  responseStream.write.count=Cannot write more bytes than content length to this output stream
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/RequestBase.java
  
  Index: RequestBase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/RequestBase.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.connector;
  
  
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.IOException;
  import java.security.Principal;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.Locale;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.ServletInputStream;
  import javax.servlet.ServletRequest;
  import org.apache.catalina.Connector;
  import org.apache.catalina.Context;
  import org.apache.catalina.Request;
  import org.apache.catalina.Response;
  import org.apache.catalina.Wrapper;
  import org.apache.catalina.util.Enumerator;
  import org.apache.catalina.util.RequestUtil;
  import org.apache.catalina.util.StringManager;
  
  
  /**
   * Convenience base implementation of the <b>Request</b> interface, which can
   * be used for the Request implementation required by most Connectors.  Only
   * the connector-specific methods need to be implemented.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public abstract class RequestBase
      implements ServletRequest, Request {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The attributes associated with this Request, keyed by attribute name.
       */
      protected HashMap attributes = new HashMap();
  
  
      /**
       * The authorization credentials sent with this Request.
       */
      protected String authorization = null;
  
  
      /**
       * The Connector through which this Request was received.
       */
      protected Connector connector = null;
  
  
      /**
       * The content length associated with this request.
       */
      protected int contentLength = -1;
  
  
      /**
       * The content type associated with this request.
       */
      protected String contentType = null;
  
  
      /**
       * The Context within which this Request is being processed.
       */
      protected Context context = null;
  
  
      /**
       * The default Locale if none are specified.
       */
      protected static Locale defaultLocale = Locale.getDefault();
  
  
      /**
       * Descriptive information about this Request implementation.
       */
      protected static final String info =
  	"org.apache.catalina.connector.RequestBase/1.0";
  
  
      /**
       * The input stream associated with this Request.
       */
      protected InputStream input = null;
  
  
      /**
       * The preferred Locales assocaited with this Request.
       */
      protected ArrayList locales = new ArrayList();
  
  
      /**
       * The protocol name and version associated with this Request.
       */
      protected String protocol = null;
  
  
      /**
       * The reader that has been returned by <code>getReader</code>, if any.
       */
      protected BufferedReader reader = null;
  
  
      /**
       * The remote address associated with this request.
       */
      protected String remoteAddr = null;
  
  
      /**
       * The fully qualified name of the remote host.
       */
      protected String remoteHost = null;
  
  
      /**
       * The response with which this request is associated.
       */
      protected Response response = null;
  
  
      /**
       * The scheme associated with this Request.
       */
      protected String scheme = null;
  
  
      /**
       * Was this request received on a secure connection?
       */
      protected boolean secure = false;
  
  
      /**
       * The server name associated with this Request.
       */
      protected String serverName = null;
  
  
      /**
       * The server port associated with this Request.
       */
      protected int serverPort = -1;
  
  
      /**
       * The string manager for this package.
       */
      protected static StringManager sm =
  	StringManager.getManager(Constants.Package);
  
  
      /**
       * The ServletInputStream that has been returned by
       * <code>getInputStream()</code>, if any.
       */
      protected ServletInputStream stream = null;
  
  
      /**
       * The Wrapper within which this Request is being processed.
       */
      protected Wrapper wrapper = null;
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * Return the authorization credentials sent with this request.
       */
      public String getAuthorization() {
  
  	return (this.authorization);
  
      }
  
  
      /**
       * Set the authorization credentials sent with this request.
       *
       * @param authorization The new authorization credentials
       */
      public void setAuthorization(String authorization) {
  
  	this.authorization = authorization;
  
      }
  
  
      /**
       * Return the Connector through which this Request was received.
       */
      public Connector getConnector() {
  
  	return (this.connector);
  
      }
  
  
      /**
       * Set the Connector through which this Request was received.
       *
       * @param connector The new connector
       */
      public void setConnector(Connector connector) {
  
  	this.connector = connector;
  
      }
  
  
      /**
       * Return the Context within which this Request is being processed.
       */
      public Context getContext() {
  
  	return (this.context);
  
      }
  
  
      /**
       * Set the Context within which this Request is being processed.  This
       * must be called as soon as the appropriate Context is identified, because
       * it identifies the value to be returned by <code>getContextPath()</code>,
       * and thus enables parsing of the request URI.
       *
       * @param context The newly associated Context
       */
      public void setContext(Context context) {
  
  	this.context = context;
  
      }
  
  
      /**
       * Return descriptive information about this Request implementation and
       * the corresponding version number, in the format
       * <code>&lt;description&gt;/&lt;version&gt;</code>.
       */
      public String getInfo() {
  
  	return (info);
  
      }
  
  
      /**
       * Return the <code>ServletRequest</code> for which this object
       * is the facade.  This method must be implemented by a subclass.
       */
      public ServletRequest getRequest() {
  
  	return ((ServletRequest) this);
  
      }
  
  
      /**
       * Return the Response with which this Request is associated.
       */
      public Response getResponse() {
  
  	return (this.response);
  
      }
  
  
      /**
       * Set the Response with which this Request is associated.
       *
       * @param response The new associated response
       */
      public void setResponse(Response response) {
  
  	this.response = response;
  
      }
  
  
      /**
       * Return the input stream associated with this Request.
       */
      public InputStream getStream() {
  
  	return (this.input);
  
      }
  
  
      /**
       * Set the input stream associated with this Request.
       *
       * @param input The new input stream
       */
      public void setStream(InputStream input) {
  
  	this.input = input;
  
      }
  
  
      /**
       * Return the Wrapper within which this Request is being processed.
       */
      public Wrapper getWrapper() {
  
  	return (this.wrapper);
  
      }
  
  
      /**
       * Set the Wrapper within which this Request is being processed.  This
       * must be called as soon as the appropriate Wrapper is identified, and
       * before the Request is ultimately passed to an application servlet.
       *
       * @param wrapper The newly associated Wrapper
       */
      public void setWrapper(Wrapper wrapper) {
  
  	this.wrapper = wrapper;
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Add a Locale to the set of preferred Locales for this Request.  The
       * first added Locale will be the first one returned by getLocales().
       *
       * @param locale The new preferred Locale
       */
      public void addLocale(Locale locale) {
  
  	synchronized (locales) {
  	    locales.add(locale);
  	}
  
      }
  
  
      /**
       * Create and return a ServletInputStream to read the content
       * associated with this Request.  The default implementation creates an
       * instance of RequestStream associated with this request, but this can
       * be overridden if necessary.
       *
       * @exception IOException if an input/output error occurs
       */
      public ServletInputStream createInputStream() throws IOException {
  
  	return (new RequestStream(this));
  
      }
  
  
      /**
       * Perform whatever actions are required to flush and close the input
       * stream or reader, in a single operation.
       *
       * @exception IOException if an input/output error occurs
       */
      public void finishRequest() throws IOException {
  
  	// If a Reader has been acquired, close it
  	if (reader != null) {
  	    try {
  		reader.close();
  	    } catch (IOException e) {
  		;
  	    }
  	}
  
  	// If a ServletInputStream has been acquired, close it
  	if (stream != null) {
  	    try {
  		stream.close();
  	    } catch (IOException e) {
  		;
  	    }
  	}
  
  	// The underlying input stream (perhaps from a socket)
  	// is not our responsibility
  
      }
  
  
      /**
       * Release all object references, and initialize instance variables, in
       * preparation for reuse of this object.
       */
      public void recycle() {
  
  	attributes.clear();
  	authorization = null;
  	// connector is NOT reset when recycling
  	contentLength = -1;
  	contentType = null;
  	context = null;
  	input = null;
  	locales.clear();
  	protocol = null;
  	reader = null;
  	remoteAddr = null;
  	remoteHost = null;
  	response = null;
  	scheme = null;
  	secure = false;
  	serverName = null;
  	serverPort = -1;
  	stream = null;
  	wrapper = null;
  
      }
  
  
      /**
       * Set the content length associated with this Request.
       *
       * @param length The new content length
       */
      public void setContentLength(int length) {
  
  	this.contentLength = length;
  
      }
  
  
      /**
       * Set the content type (and optionally the character encoding)
       * associated with this Request.  For example,
       * <code>text/html; charset=ISO-8859-4</code>.
       *
       * @param type The new content type
       */
      public void setContentType(String type) {
  
  	this.contentType = type;
  
      }
  
  
      /**
       * Set the protocol name and version associated with this Request.
       *
       * @param protocol Protocol name and version
       */
      public void setProtocol(String protocol) {
  
  	this.protocol = protocol;
  
      }
  
  
      /**
       * Set the IP address of the remote client associated with this Request.
       *
       * @param remoteAddr The remote IP address
       */
      public void setRemoteAddr(String remoteAddr) {
  
  	this.remoteAddr = remoteAddr;
  
      }
  
  
      /**
       * Set the fully qualified name of the remote client associated with this
       * Request.
       *
       * @param remoteHost The remote host name
       */
      public void setRemoteHost(String remoteHost) {
  
  	this.remoteHost = remoteHost;
  
      }
  
  
      /**
       * Set the name of the scheme associated with this request.  Typical values
       * are <code>http</code>, <code>https</code>, and <code>ftp</code>.
       *
       * @param scheme The scheme
       */
      public void setScheme(String scheme) {
  
  	this.scheme = scheme;
  
      }
  
  
      /**
       * Set the value to be returned by <code>isSecure()</code>
       * for this Request.
       *
       * @param secure The new isSecure value
       */
      public void setSecure(boolean secure) {
  
  	this.secure = secure;
  
      }
  
  
      /**
       * Set the name of the server (virtual host) to process this request.
       *
       * @param name The server name
       */
      public void setServerName(String name) {
  
  	this.serverName = name;
  
      }
  
  
      /**
       * Set the port number of the server to process this request.
       *
       * @param port The server port
       */
      public void setServerPort(int port) {
  
  	this.serverPort = port;
  
      }
  
  
      // ------------------------------------------------- ServletRequest Methods
  
  
      /**
       * Return the specified request attribute if it exists; otherwise, return
       * <code>null</code>.
       *
       * @param name Name of the request attribute to return
       */
      public Object getAttribute(String name) {
  
  	synchronized (attributes) {
  	    return (attributes.get(name));
  	}
  
      }
  
  
      /**
       * Return the names of all request attributes for this Request, or an
       * empty <code>Enumeration</code> if there are none.
       */
      public Enumeration getAttributeNames() {
  
  	synchronized (attributes) {
  	    return (new Enumerator(attributes.keySet()));
  	}
  
      }
  
  
      /**
       * Return the character encoding for this Request.
       */
      public String getCharacterEncoding() {
  
  	return (RequestUtil.parseCharacterEncoding(getContentType()));
  
      }
  
  
      /**
       * Return the content length for this Request.
       */
      public int getContentLength() {
  
  	return (this.contentLength);
  
      }
  
  
      /**
       * Return the content type for this Request.
       */
      public String getContentType() {
  
  	return (contentType);
  
      }
  
  
      /**
       * Return the servlet input stream for this Request.  The default
       * implementation returns a servlet input stream created by
       * <code>createInputStream()</code>.
       *
       * @exception IllegalStateException if <code>getReader()</code> has
       *  already been called for this request
       * @exception IOException if an input/output error occurs
       */
      public ServletInputStream getInputStream() throws IOException {
  
  	if (reader != null)
  	    throw new IllegalStateException
  		(sm.getString("requestBase.getInputStream.ise"));
  
  	if (stream == null)
  	    stream = createInputStream();
  	return (stream);
  
      }
  
  
      /**
       * Return the preferred Locale that the client will accept content in,
       * based on the value for the first <code>Accept-Language</code> header
       * that was encountered.  If the request did not specify a preferred
       * language, the server's default Locale is returned.
       */
      public Locale getLocale() {
  
  	synchronized (locales) {
  	    if (locales.size() > 0)
  		return ((Locale) locales.get(0));
  	    else
  		return (defaultLocale);
  	}
  
      }
  
  
      /**
       * Return the set of preferred Locales that the client will accept
       * content in, based on the values for any <code>Accept-Language</code>
       * headers that were encountered.  If the request did not specify a
       * preferred language, the server's default Locale is returned.
       */
      public Enumeration getLocales() {
  
  	synchronized (locales) {
  	    if (locales.size() > 0)
  		return (new Enumerator(locales));
  	}
  	ArrayList results = new ArrayList();
  	results.add(defaultLocale);
  	return (new Enumerator(results));
  
      }
  
  
      /**
       * Return the value of the specified request parameter, if any; otherwise,
       * return <code>null</code>.  If there is more than one value defined,
       * return only the first one.
       *
       * @param name Name of the desired request parameter
       */
      public abstract String getParameter(String name);
  
  
      /**
       * Return the names of all defined request parameters for this request.
       */
      public abstract Enumeration getParameterNames();
  
  
      /**
       * Return the defined values for the specified request parameter, if any;
       * otherwise, return <code>null</code>.
       *
       * @param name Name of the desired request parameter
       */
      public abstract String[] getParameterValues(String name);
  
  
      /**
       * Return the protocol and version used to make this Request.
       */
      public String getProtocol() {
  
  	return (this.protocol);
  
      }
  
  
      /**
       * Read the Reader wrapping the input stream for this Request.  The
       * default implementation wraps a <code>BufferedReader</code> around the
       * servlet input stream returned by <code>createInputStream()</code>.
       *
       * @exception IllegalStateException if <code>getInputStream()</code>
       *  has already been called for this request
       * @exception IOException if an input/output error occurs
       */
      public BufferedReader getReader() throws IOException {
  
  	if (stream != null)
  	    throw new IllegalStateException
  		(sm.getString("requestBase.getReader.ise"));
  
  	if (reader == null) {
  	    String encoding = getCharacterEncoding();
  	    InputStreamReader isr = null;
  	    if (encoding != null)
  		isr = new InputStreamReader(createInputStream(), encoding);
  	    else
  		isr = new InputStreamReader(createInputStream());
  	    reader = new BufferedReader(isr);
  	}
  	return (reader);
  
      }
  
  
      /**
       * Return the real path of the specified virtual path.
       *
       * @param path Path to be translated
       *
       * @deprecated As of version 2.1 of the Java Servlet API, use
       *  <code>ServletContext.getRealPath()</code>.
       */
      public String getRealPath(String path) {
  
  	if (context == null)
  	    return (null);
  	ServletContext servletContext = context.getServletContext();
  	if (servletContext == null)
  	    return (null);
  	else {
  	    try {
  		return (servletContext.getRealPath(path));
  	    } catch (IllegalArgumentException e) {
  		return (null);
  	    }
  	}
  
      }
  
  
      /**
       * Return the remote IP address making this Request.
       */
      public String getRemoteAddr() {
  
  	return (this.remoteAddr);
  
      }
  
  
      /**
       * Return the remote host name making this Request.
       */
      public String getRemoteHost() {
  
  	return (this.remoteHost);
  
      }
  
  
      /**
       * Return a RequestDispatcher that wraps the resource at the specified
       * path, which may be interpreted as relative to the current request path.
       *
       * @param path Path of the resource to be wrapped
       */
      public abstract RequestDispatcher getRequestDispatcher(String path);
  
  
      /**
       * Return the scheme used to make this Request.
       */
      public String getScheme() {
  
  	return (this.scheme);
  
      }
  
  
      /**
       * Return the server name responding to this Request.
       */
      public String getServerName() {
  
  	return (this.serverName);
  
      }
  
  
      /**
       * Return the server port responding to this Request.
       */
      public int getServerPort() {
  
  	return (this.serverPort);
  
      }
  
  
      /**
       * Was this request received on a secure connection?
       */
      public boolean isSecure() {
  
  	return (this.secure);
  
      }
  
  
      /**
       * Remove the specified request attribute if it exists.
       *
       * @param name Name of the request attribute to remove
       */
      public void removeAttribute(String name) {
  
  	synchronized (attributes) {
  	    attributes.remove(name);
  	}
  
      }
  
  
      /**
       * Set the specified request attribute to the specified value.
       *
       * @param name Name of the request attribute to set
       * @param value The associated value
       */
      public void setAttribute(String name, Object value) {
  
  	synchronized (attributes) {
  	    attributes.put(name, value);
  	}
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/RequestStream.java
  
  Index: RequestStream.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/RequestStream.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.catalina.connector;
  
  
  import java.io.InputStream;
  import java.io.IOException;
  import javax.servlet.ServletInputStream;
  import org.apache.catalina.Request;
  import org.apache.catalina.util.StringManager;
  
  
  /**
   * Convenience implementation of <b>ServletInputStream</b> that works with
   * the standard implementations of <b>Request</b>.  If the content length has
   * been set on our associated Request, this implementation will enforce
   * not reading more than that many bytes on the underlying stream.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public class RequestStream
      extends ServletInputStream {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a servlet input stream associated with the specified Request.
       *
       * @param request The associated request
       */
      public RequestStream(Request request) {
  
  	super();
  	closed = false;
  	count = 0;
  	length = request.getRequest().getContentLength();
  	stream = request.getStream();
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * Has this stream been closed?
       */
      protected boolean closed = false;
  
  
      /**
       * The number of bytes which have already been returned by this stream.
       */
      protected int count = 0;
  
  
      /**
       * The content length past which we will not read, or -1 if there is
       * no defined content length.
       */
      protected int length = -1;
  
  
      /**
       * The localized strings for this package.
       */
      protected static StringManager sm =
  	StringManager.getManager(Constants.Package);
  
  
      /**
       * The underlying input stream from which we should read data.
       */
      protected InputStream stream = null;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Close this input stream.  No physical level I-O is performed, but
       * any further attempt to read from this stream will throw an IOException.
       * If a content length has been set but not all of the bytes have yet been
       * consumed, the remaining bytes will be swallowed.
       */
      public void close() throws IOException {
  
  	if (closed)
  	    throw new IOException(sm.getString("requestStream.close.closed"));
  
  	if (length > 0) {
  	    while (count < length) {
  		int b = read();
  		if (b < 0)
  		    break;
  	    }
  	}
  
  	closed = true;
  
      }
  
  
  
      /**
       * Read and return a single byte from this input stream, or -1 if end of
       * file has been encountered.
       *
       * @exception IOException if an input/output error occurs
       */
      public int read() throws IOException {
  
  	// Has this stream been closed?
  	if (closed)
  	    throw new IOException(sm.getString("requestStream.read.closed"));
  
  	// Have we read the specified content length already?
  	if ((length >= 0) && (count >= length))
  	    return (-1);	// End of file indicator
  
  	// Read and count the next byte, then return it
  	int b = stream.read();
  	if (b >= 0)
  	    count++;
  	return (b);
  
      }
  
  
      /**
       * Read some number of bytes from the input stream, and store them
       * into the buffer array b.  The number of bytes actually read is
       * returned as an integer.  This method blocks until input data is
       * available, end of file is detected, or an exception is thrown.
       *
       * @param b The buffer into which the data is read
       *
       * @exception IOException if an input/output error occurs
       */
      public int read(byte b[]) throws IOException {
  
  	return (read(b, 0, b.length));
  
      }
  
  
      /**
       * Read up to <code>len</code> bytes of data from the input stream
       * into an array of bytes.  An attempt is made to read as many as
       * <code>len</code> bytes, but a smaller number may be read,
       * possibly zero.  The number of bytes actually read is returned as
       * an integer.  This method blocks until input data is available,
       * end of file is detected, or an exception is thrown.
       *
       * @param b The buffer into which the data is read
       * @param off The start offset into array <code>b</code> at which
       *  the data is written
       * @param len The maximum number of bytes to read
       *
       * @exception IOException if an input/output error occurs
       */
      public int read(byte b[], int off, int len) throws IOException {
  
  	int toRead = len;
  	if (length > 0) {
  	    if (count >= length)
  		return (-1);
  	    if ((count + len) > length)
  		toRead = length - count;
  	}
  	int actuallyRead = super.read(b, off, toRead);
  	return (actuallyRead);
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/RequestWrapper.java
  
  Index: RequestWrapper.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/RequestWrapper.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  
  
  package org.apache.catalina.connector;
  
  
  import java.io.InputStream;
  import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.ServletInputStream;
  import javax.servlet.ServletRequest;
  import org.apache.catalina.Connector;
  import org.apache.catalina.Context;
  import org.apache.catalina.Request;
  import org.apache.catalina.Response;
  import org.apache.catalina.Wrapper;
  
  
  /**
   * Abstract convenience class that wraps a Catalina-internal <b>Request</b>
   * object.  By default, all methods are delegated to the wrapped request,
   * but subclasses can override individual methods as required to provide the
   * functionality that they require.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public abstract class RequestWrapper implements Request {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a wrapper for the specified request.
       *
       * @param request The request to be wrapped
       */
      public RequestWrapper(Request request) {
  
  	super();
  	this.request = request;
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The wrapped request.
       */
      protected Request request = null;
  
  
      /**
       * Return the wrapped request.
       */
      public Request getWrappedRequest() {
  
  	return (this.request);
  
      }
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * Return the authorization credentials sent with this request.
       */
      public String getAuthorization() {
  
  	return (request.getAuthorization());
  
      }
  
  
      /**
       * Set the authorization credentials sent with this request.
       *
       * @param authorization The new authorization credentials
       */
      public void setAuthorization(String authorization) {
  
  	request.setAuthorization(authorization);
  
      }
  
  
      /**
       * Return the Connector through which this Request was received.
       */
      public Connector getConnector() {
  
  	return (request.getConnector());
  
      }
  
  
      /**
       * Set the Connector through which this Request was received.
       *
       * @param connector The new connector
       */
      public void setConnector(Connector connector) {
  
  	request.setConnector(connector);
  
      }
  
  
      /**
       * Return the Context within which this Request is being processed.
       */
      public Context getContext() {
  
  	return (request.getContext());
  
      }
  
  
      /**
       * Set the Context within which this Request is being processed.  This
       * must be called as soon as the appropriate Context is identified, because
       * it identifies the value to be returned by <code>getContextPath()</code>,
       * and thus enables parsing of the request URI.
       *
       * @param context The newly associated Context
       */
      public void setContext(Context context) {
  
  	request.setContext(context);
  
      }
  
  
      /**
       * Return descriptive information about this Request implementation and
       * the corresponding version number, in the format
       * <code>&lt;description&gt;/&lt;version&gt;</code>.
       */
      public String getInfo() {
  
  	return (request.getInfo());
  
      }
  
  
      /**
       * Return the <code>ServletRequest</code> for which this object
       * is the facade.
       */
      public ServletRequest getRequest() {
  
  	return (request.getRequest());
  
      }
  
  
      /**
       * Return the Response with which this Request is associated.
       */
      public Response getResponse() {
  
  	return (request.getResponse());
  
      }
  
  
      /**
       * Set the Response with which this Request is associated.
       *
       * @param response The new associated response
       */
      public void setResponse(Response response) {
  
  	request.setResponse(response);
  
      }
  
  
      /**
       * Return the input stream associated with this Request.
       */
      public InputStream getStream() {
  
  	return (request.getStream());
  
      }
  
  
      /**
       * Set the input stream associated with this Request.
       *
       * @param stream The new input stream
       */
      public void setStream(InputStream stream) {
  
  	request.setStream(stream);
  
      }
  
  
      /**
       * Return the Wrapper within which this Request is being processed.
       */
      public Wrapper getWrapper() {
  
  	return (request.getWrapper());
  
      }
  
  
      /**
       * Set the Wrapper within which this Request is being processed.  This
       * must be called as soon as the appropriate Wrapper is identified, and
       * before the Request is ultimately passed to an application servlet.
       *
       * @param wrapper The newly associated Wrapper
       */
      public void setWrapper(Wrapper wrapper) {
  
  	request.setWrapper(wrapper);
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Create and return a ServletInputStream to read the content
       * associated with this Request.
       *
       * @exception IOException if an input/output error occurs
       */
      public ServletInputStream createInputStream() throws IOException {
  
  	return (request.createInputStream());
  
      }
  
  
      /**
       * Perform whatever actions are required to flush and close the input
       * stream or reader, in a single operation.
       *
       * @exception IOException if an input/output error occurs
       */
      public void finishRequest() throws IOException {
  
  	request.finishRequest();
  
      }
  
  
      /**
       * Release all object references, and initialize instance variables, in
       * preparation for reuse of this object.
       */
      public void recycle() {
  
  	request.recycle();
  
      }
  
  
      /**
       * Set the content length associated with this Request.
       *
       * @param length The new content length
       */
      public void setContentLength(int length) {
  
  	request.setContentLength(length);
  
      }
  
  
      /**
       * Set the content type (and optionally the character encoding)
       * associated with this Request.  For example,
       * <code>text/html; charset=ISO-8859-4</code>.
       *
       * @param type The new content type
       */
      public void setContentType(String type) {
  
  	request.setContentType(type);
  
      }
  
  
      /**
       * Set the protocol name and version associated with this Request.
       *
       * @param protocol Protocol name and version
       */
      public void setProtocol(String protocol) {
  
  	request.setProtocol(protocol);
  
      }
  
  
      /**
       * Set the remote IP address associated with this Request.  NOTE:  This
       * value will be used to resolve the value for <code>getRemoteHost()</code>
       * if that method is called.
       *
       * @param remote The remote IP address
       */
      public void setRemoteAddr(String remote) {
  
  	request.setRemoteAddr(remote);
  
      }
  
  
      /**
       * Set the name of the scheme associated with this request.  Typical values
       * are <code>http</code>, <code>https</code>, and <code>ftp</code>.
       *
       * @param scheme The scheme
       */
      public void setScheme(String scheme) {
  
  	request.setScheme(scheme);
  
      }
  
  
      /**
       * Set the value to be returned by <code>isSecure()</code>
       * for this Request.
       *
       * @param secure The new isSecure value
       */
      public void setSecure(boolean secure) {
  
  	request.setSecure(secure);
  
      }
  
  
      /**
       * Set the name of the server (virtual host) to process this request.
       *
       * @param name The server name
       */
      public void setServerName(String name) {
  
  	request.setServerName(name);
  
      }
  
  
      /**
       * Set the port number of the server to process this request.
       *
       * @param port The server port
       */
      public void setServerPort(int port) {
  
  	request.setServerPort(port);
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseBase.java
  
  Index: ResponseBase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseBase.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.connector;
  
  
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.util.Locale;
  import javax.servlet.ServletException;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.ServletResponse;
  import org.apache.catalina.Connector;
  import org.apache.catalina.Context;
  import org.apache.catalina.Request;
  import org.apache.catalina.Response;
  import org.apache.catalina.util.RequestUtil;
  import org.apache.catalina.util.StringManager;
  
  
  /**
   * Convenience base implementation of the <b>Response</b> interface, which can
   * be used for the Response implementation required by most Connectors.  Only
   * the connector-specific methods need to be implemented.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public abstract class ResponseBase
      implements Response, ServletResponse {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The buffer through which all of our output bytes are passed.
       */
      protected byte[] buffer = new byte[1024];
  
  
      /**
       * The number of data bytes currently in the buffer.
       */
      protected int bufferCount = 0;
  
  
      /**
       * Has this response been committed yet?
       */
      protected boolean committed = false;
  
  
      /**
       * The Connector through which this Response is returned.
       */
      protected Connector connector = null;
  
  
      /**
       * The actual number of bytes written to this Response.
       */
      protected int contentCount = 0;
  
  
      /**
       * The content length associated with this Response.
       */
      protected int contentLength = -1;
  
  
      /**
       * The content type associated with this Response.
       */
      protected String contentType = "text/plain";
  
  
      /**
       * The Context within which this Response is being produced.
       */
      protected Context context = null;
  
  
      /**
       * The character encoding associated with this Response.
       */
      protected String encoding = "ISO-8859-1";
  
  
      /**
       * Are we currently processing inside a RequestDispatcher.include()?
       */
      protected boolean included = false;
  
  
      /**
       * Descriptive information about this Response implementation.
       */
      protected static final String info =
  	"org.apache.catalina.connector.ResponseBase/1.0";
  
  
      /**
       * The Locale associated with this Response.
       */
      protected Locale locale = Locale.getDefault();
  
  
      /**
       * The output stream associated with this Response.
       */
      protected OutputStream output = null;
  
  
      /**
       * The Request with which this Response is associated.
       */
      protected Request request = null;
  
  
      /**
       * The string manager for this package.
       */
      protected static StringManager sm =
  	StringManager.getManager(Constants.Package);
  
  
      /**
       * The ServletOutputStream that has been returned by
       * <code>getOutputStream()</code>, if any.
       */
      protected ServletOutputStream stream = null;
  
  
      /**
       * The PrintWriter that has been returned by
       * <code>getWriter()</code>, if any.
       */
      protected PrintWriter writer = null;
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * Return the Connector through which this Response will be transmitted.
       */
      public Connector getConnector() {
  
  	return (this.connector);
  
      }
  
  
      /**
       * Set the Connector through which this Response will be transmitted.
       *
       * @param connector The new connector
       */
      public void setConnector(Connector connector) {
  
  	this.connector = connector;
  
      }
  
  
      /**
       * Return the number of bytes actually written to the output stream.
       */
      public int getContentCount() {
  
  	return (this.contentCount);
  
      }
  
  
      /**
       * Return the Context with which this Response is associated.
       */
      public Context getContext() {
  
  	return (this.context);
  
      }
  
  
      /**
       * Set the Context with which this Response is associated.  This should
       * be called as soon as the appropriate Context is identified.
       *
       * @param context The associated Context
       */
      public void setContext(Context context) {
  
  	this.context = context;
  
      }
  
  
      /**
       * Return the "processing inside an include" flag.
       */
      public boolean getIncluded() {
  
  	return (this.included);
  
      }
  
  
      /**
       * Set the "processing inside an include" flag.
       *
       * @param included <code>true</code> if we are currently inside a
       *  RequestDispatcher.include(), else <code>false</code>
       */
      public void setIncluded(boolean included) {
  
  	this.included = included;
  
      }
  
  
      /**
       * Return descriptive information about this Response implementation and
       * the corresponding version number, in the format
       * <code>&lt;description&gt;/&lt;version&gt;</code>.
       */
      public String getInfo() {
  
  	return (this.info);
  
      }
  
  
      /**
       * Return the Request with which this Response is associated.
       */
      public Request getRequest() {
  
  	return (this.request);
  
      }
  
  
      /**
       * Set the Request with which this Response is associated.
       *
       * @param request The new associated request
       */
      public void setRequest(Request request) {
  
  	this.request = request;
  
      }
  
  
      /**
       * Return the <code>ServletResponse</code> for which this object
       * is the facade.
       */
      public ServletResponse getResponse() {
  
  	return ((ServletResponse) this);
  
      }
  
  
      /**
       * Return the output stream associated with this Response.
       */
      public OutputStream getStream() {
  
  	return (this.output);
  
      }
  
  
      /**
       * Set the output stream associated with this Response.
       *
       * @param stream The new output stream
       */
      public void setStream(OutputStream stream) {
  
  	this.output = stream;
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Create and return a ServletOutputStream to write the content
       * associated with this Response.
       *
       * @exception IOException if an input/output error occurs
       */
      public ServletOutputStream createOutputStream() throws IOException {
  
  	return (new ResponseStream(this));
  
      }
  
  
      /**
       * Perform whatever actions are required to flush and close the output
       * stream or writer, in a single operation.
       *
       * @exception IOException if an input/output error occurs
       */
      public void finishResponse() throws IOException {
  
  	// If no stream has been requested yet, get one so we can
  	// flush the necessary headers
  	if (this.stream == null) {
  	    ServletOutputStream sos = getOutputStream();
  	    sos.close();
  	    return;
  	}
  
  	// If our stream is closed, no action is necessary
  	if ( ((ResponseStream) stream).closed() )
  	    return;
  
  	// Flush and close the appropriate output mechanism
  	if (writer != null) {
  	    writer.flush();
  	    writer.close();
  	} else {
  	    stream.flush();
  	    stream.close();
  	}
  
  	// The underlying output stream (perhaps from a socket)
  	// is not our responsibility
  
      }
  
  
      /**
       * Return the content length that was set or calculated for this Response.
       */
      public int getContentLength() {
  
  	return (this.contentLength);
  
      }
  
  
      /**
       * Return the content type that was set or calculated for this response,
       * or <code>null</code> if no content type was set.
       */
      public String getContentType() {
  
  	return (this.contentType);
  
      }
  
  
      /**
       * Return a PrintWriter that can be used to render error messages,
       * regardless of whether a stream or writer has already been acquired.
       */
      public PrintWriter getReporter() {
  
  	return (new PrintWriter(output));
  
      }
  
  
      /**
       * Release all object references, and initialize instance variables, in
       * preparation for reuse of this object.
       */
      public void recycle() {
  
  	// buffer is NOT reset when recycling
  	bufferCount = 0;
  	committed = false;
  	// connector is NOT reset when recycling
  	contentCount = 0;
  	contentLength = -1;
  	contentType = "text/plain";
  	context = null;
  	encoding = "ISO-8859-1";
  	included = false;
  	locale = Locale.getDefault();
  	output = null;
  	request = null;
  	stream = null;
  	writer = null;
  
      }
  
  
      /**
       * Reset the data buffer but not any status or header information.
       */
      public void resetBuffer() {
  
  	bufferCount = 0;
  
      }
  
  
      // -------------------------------------------------------- Package Methods
  
  
      /**
       * Write the specified byte to our output stream, flushing if necessary.
       *
       * @param b The byte to be written
       *
       * @exception IOException if an input/output error occurs
       */
      public void write(int b) throws IOException {
  
  	if (bufferCount >= buffer.length)
  	    flushBuffer();
  	buffer[bufferCount++] = (byte) b;
  	contentCount++;
  
      }
  
  
      /**
       * Write <code>b.length</code> bytes from the specified byte array
       * to our output stream.  Flush the output stream as necessary.
       *
       * @param b The byte array to be written
       *
       * @exception IOException if an input/output error occurs
       */
      public void write(byte b[]) throws IOException {
  
  	write(b, 0, b.length);
  
      }
  
  
      /**
       * Write <code>len</code> bytes from the specified byte array, starting
       * at the specified offset, to our output stream.  Flush the output
       * stream as necessary.
       *
       * @param b The byte array containing the bytes to be written
       * @param off Zero-relative starting offset of the bytes to be written
       * @param len The number of bytes to be written
       *
       * @exception IOException if an input/output error occurs
       */
      public void write(byte b[], int off, int len) throws IOException {
  
  	// If the whole thing fits in the buffer, just put it there
  	if (len == 0)
  	    return;
  	if (len <= (buffer.length - bufferCount)) {
  	    System.arraycopy(b, off, buffer, bufferCount, len);
  	    bufferCount += len;
  	    contentCount += len;
  	    return;
  	}
  
  	// Flush the buffer and start writing full-buffer-size chunks
  	flushBuffer();
  	int iterations = len / buffer.length;
  	int leftoverStart = iterations * buffer.length;
  	int leftoverLen = len - leftoverStart;
  	for (int i = 0; i < iterations; i++)
  	    write(b, off + (i * buffer.length), buffer.length);
  
  	// Write the remainder (guaranteed to fit in the buffer)
  	if (leftoverLen > 0)
  	    write(b, off + leftoverStart, leftoverLen);
  
      }
  
  
      // ------------------------------------------------ ServletResponse Methods
  
  
      /**
       * Flush the buffer and commit this response.
       *
       * @exception IOException if an input/output error occurs
       */
      public void flushBuffer() throws IOException {
  
  	committed = true;
  	if (bufferCount > 0) {
  	    output.write(buffer, 0, bufferCount);
  	    bufferCount = 0;
  	}
  
      }
  
  
      /**
       * Return the actual buffer size used for this Response.
       */
      public int getBufferSize() {
  
  	return (buffer.length);
  
      }
  
  
      /**
       * Return the character encoding used for this Response.
       */
      public String getCharacterEncoding() {
  
  	return (encoding);
  
      }
  
  
      /**
       * Return the servlet output stream associated with this Response.
       *
       * @exception IllegalStateException if <code>getWriter</code> has
       *  already been called for this response
       * @exception IOException if an input/output error occurs
       */
      public ServletOutputStream getOutputStream() throws IOException {
  
  	if (writer != null)
  	    throw new IllegalStateException
  		(sm.getString("responseBase.getOutputStream.ise"));
  
  	if (stream == null)
  	    stream = createOutputStream();
  	return (stream);
  
      }
  
  
      /**
       * Return the Locale assigned to this response.
       */
      public Locale getLocale() {
  
  	return (locale);
  
      }
  
  
      /**
       * Return the writer associated with this Response.
       *
       * @exception IllegalStateException if <code>getOutputStream</code> has
       *  already been called for this response
       * @exception IOException if an input/output error occurs
       */
      public PrintWriter getWriter() throws IOException {
  
  	if (writer != null)
  	    return (writer);
  
  	if (stream != null)
  	    throw new IllegalStateException
  		(sm.getString("responseBase.getWriter.ise"));
  
  	stream = createOutputStream();
  	OutputStreamWriter osr =
  	  new OutputStreamWriter(stream, getCharacterEncoding());
  	writer = new PrintWriter(osr);
  	return (writer);
  
      }
  
  
      /**
       * Has the output of this response already been committed?
       */
      public boolean isCommitted() {
  
  	return (committed);
  
      }
  
  
      /**
       * Clear any content written to the buffer.
       *
       * @exception IllegalStateException if this response has already
       *  been committed
       */
      public void reset() {
  
  	if (committed)
  	    throw new IllegalStateException
  		(sm.getString("responseBase.reset.ise"));
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	if (stream != null)
  	    ((ResponseStream) stream).reset();
  	bufferCount = 0;
  	contentLength = -1;
  	contentType = "text/plain";
  
      }
  
  
      /**
       * Set the buffer size to be used for this Response.
       *
       * @param size The new buffer size
       *
       * @exception IllegalStateException if this method is called after
       *  output has been committed for this response
       */
      public void setBufferSize(int size) {
  
  	if (bufferCount > 0)
  	    throw new IllegalStateException
  		(sm.getString("responseBase.setBufferSize.ise"));
  
  	if (buffer.length >= size)
  	    return;
  	buffer = new byte[size];
  
      }
  
  
      /**
       * Set the content length (in bytes) for this Response.
       *
       * @param length The new content length
       */
      public void setContentLength(int length) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	this.contentLength = length;
  
      }
  
  
      /**
       * Set the content type for this Response.
       *
       * @param type The new content type
       */
      public void setContentType(String type) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	this.contentType = type;
  	if (type.indexOf(";") >= 0) {
  	    encoding = RequestUtil.parseCharacterEncoding(type);
  	    if (encoding == null)
  	        encoding = "ISO-8859-1";
  	}
  
      }
  
  
      /**
       * Set the Locale that is appropriate for this response, including
       * setting the appropriate character encoding.
       *
       * @param locale The new locale
       */
      public void setLocale(Locale locale) {
  
  	if (isCommitted())
  	    return;
  
  	if (included)
  	    return;	// Ignore any call from an included servlet
  
  	this.locale = locale;
  	;	// FIXME -- set encoding based on the locale!
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseStream.java
  
  Index: ResponseStream.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseStream.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.connector;
  
  
  import java.io.IOException;
  import java.io.OutputStream;
  import javax.servlet.ServletOutputStream;
  import org.apache.catalina.Response;
  import org.apache.catalina.util.StringManager;
  
  
  /**
   * Convenience implementation of <b>ServletOutputStream</b> that works with
   * the standard ResponseBase implementation of <b>Response</b>.  If the content
   * length has been set on our associated Response, this implementation will
   * enforce not writing more than that many bytes on the underlying stream.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public class ResponseStream
      extends ServletOutputStream {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a servlet output stream associated with the specified Request.
       *
       * @param response The associated response
       */
      public ResponseStream(Response response) {
  
  	super();
  	closed = false;
  	count = 0;
  	this.response = response;
  	this.stream = response.getStream();
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * Has this stream been closed?
       */
      protected boolean closed = false;
  
  
      /**
       * The number of bytes which have already been written to this stream.
       */
      protected int count = 0;
  
  
      /**
       * The content length past which we will not write, or -1 if there is
       * no defined content length.
       */
      protected int length = -1;
  
  
      /**
       * The Response with which this input stream is associated.
       */
      protected Response response = null;
  
  
      /**
       * The localized strings for this package.
       */
      protected static StringManager sm =
  	StringManager.getManager(Constants.Package);
  
  
      /**
       * The underlying output stream to which we should write data.
       */
      protected OutputStream stream = null;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Close this output stream, causing any buffered data to be flushed and
       * any further output data to throw an IOException.
       */
      public void close() throws IOException {
  
  	if (closed)
  	    throw new IOException(sm.getString("responseStream.close.closed"));
  
  	response.getResponse().flushBuffer();
  	closed = true;
  
      }
  
  
      /**
       * Flush any buffered data for this output stream, which also causes the
       * response to be committed.
       */
      public void flush() throws IOException {
  
  	if (closed)
  	    throw new IOException(sm.getString("responseStream.flush.closed"));
  
  	response.getResponse().flushBuffer();
  
      }
  
  
      /**
       * Write the specified byte to our output stream.
       *
       * @param b The byte to be written
       *
       * @exception IOException if an input/output error occurs
       */
      public void write(int b) throws IOException {
  
  	if (closed)
  	    throw new IOException(sm.getString("responseStream.write.closed"));
  
  	if ((length > 0) && (count >= length))
  	    throw new IOException(sm.getString("responseStream.write.count"));
  
  	((ResponseBase) response).write(b);
  	count++;
  
      }
  
  
      /**
       * Write <code>b.length</code> bytes from the specified byte array
       * to our output stream.
       *
       * @param b The byte array to be written
       *
       * @exception IOException if an input/output error occurs
       */
      public void write(byte b[]) throws IOException {
  
  	write(b, 0, b.length);
  
      }
  
  
      /**
       * Write <code>len</code> bytes from the specified byte array, starting
       * at the specified offset, to our output stream.
       *
       * @param b The byte array containing the bytes to be written
       * @param off Zero-relative starting offset of the bytes to be written
       * @param len The number of bytes to be written
       *
       * @exception IOException if an input/output error occurs
       */
      public void write(byte b[], int off, int len) throws IOException {
  
  	if (closed)
  	    throw new IOException(sm.getString("responseStream.write.closed"));
  
  	int actual = len;
  	if ((length > 0) && ((count + len) >= length))
  	    actual = length - count;
  	((ResponseBase) response).write(b, 0, actual);
  	count += actual;
  	if (actual < len)
  	    throw new IOException(sm.getString("responseStream.write.count"));
  
      }
  
  
      // -------------------------------------------------------- Package Methods
  
  
      /**
       * Has this response stream been closed?
       */
      boolean closed() {
  
  	return (this.closed);
  
      }
  
  
      /**
       * Reset the count of bytes written to this stream to zero.
       */
      void reset() {
  
  	count = 0;
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseWrapper.java
  
  Index: ResponseWrapper.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/ResponseWrapper.java,v 1.1 2000/08/11 22:40:19 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2000/08/11 22:40:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.connector;
  
  
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import javax.servlet.ServletException;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.ServletResponse;
  import org.apache.catalina.Connector;
  import org.apache.catalina.Context;
  import org.apache.catalina.Request;
  import org.apache.catalina.Response;
  import org.apache.catalina.Wrapper;
  
  
  /**
   * Abstract convenience class that wraps a Catalina-internal <b>Response</b>
   * object.  By default, all methods are delegated to the wrapped response,
   * but subclasses can override individual methods as required to provide the
   * functionality that they require.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2000/08/11 22:40:19 $
   */
  
  public abstract class ResponseWrapper implements Response {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct a wrapper for the specified response.
       *
       * @param response The response to be wrapped
       */
      public ResponseWrapper(Response response) {
  
  	super();
  	this.response = response;
  
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The wrapped response.
       */
      protected Response response = null;
  
  
      /**
       * Return the wrapped response.
       */
      public Response getWrappedResponse() {
  
  	return (this.response);
  
      }
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * Return the Connector through which this Response is returned.
       */
      public Connector getConnector() {
  
  	return (response.getConnector());
  
      }
  
  
      /**
       * Set the Connector through which this Response is returned.
       *
       * @param connector The new connector
       */
      public void setConnector(Connector connector) {
  
  	response.setConnector(connector);
  
      }
  
  
      /**
       * Return the number of bytes actually written to the output stream.
       */
      public int getContentCount() {
  
  	return (response.getContentCount());
  
      }
  
  
      /**
       * Return the Context with which this Response is associated.
       */
      public Context getContext() {
  
  	return (response.getContext());
  
      }
  
  
      /**
       * Set the Context with which this Response is associated.  This should
       * be called as soon as the appropriate Context is identified.
       *
       * @param context The associated Context
       */
      public void setContext(Context context) {
  
  	response.setContext(context);
  
      }
  
  
      /**
       * Return the "processing inside an include" flag.
       */
      public boolean getIncluded() {
  
  	return (response.getIncluded());
  
      }
  
  
      /**
       * Set the "processing inside an include" flag.
       *
       * @param included <code>true</code> if we are currently inside a
       *  RequestDispatcher.include(), else <code>false</code>
       */
      public void setIncluded(boolean included) {
  
  	response.setIncluded(included);
  
      }
  
  
      /**
       * Return descriptive information about this Response implementation and
       * the corresponding version number, in the format
       * <code>&lt;description&gt;/&lt;version&gt;</code>.
       */
      public String getInfo() {
  
  	return (response.getInfo());
  
      }
  
  
      /**
       * Return the Request with which this Response is associated.
       */
      public Request getRequest() {
  
  	return (response.getRequest());
  
      }
  
  
      /**
       * Set the Request with which this Response is associated.
       *
       * @param request The new associated request
       */
      public void setRequest(Request request) {
  
  	response.setRequest(request);
  
      }
  
  
      /**
       * Return the <code>ServletResponse</code> for which this object
       * is the facade.
       */
      public ServletResponse getResponse() {
  
  	return (response.getResponse());
  
      }
  
  
      /**
       * Return the output stream associated with this Response.
       */
      public OutputStream getStream() {
  
  	return (response.getStream());
  
      }
  
  
      /**
       * Set the output stream associated with this Response.
       *
       * @param stream The new output stream
       */
      public void setStream(OutputStream stream) {
  
  	response.setStream(stream);
  
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Create and return a ServletOutputStream to write the content
       * associated with this Response.
       *
       * @exception IOException if an input/output error occurs
       */
      public ServletOutputStream createOutputStream() throws IOException {
  
  	return (response.createOutputStream());
  
      }
  
  
      /**
       * Perform whatever actions are required to flush and close the output
       * stream or writer, in a single operation.
       *
       * @exception IOException if an input/output error occurs
       */
      public void finishResponse() throws IOException {
  
  	response.finishResponse();
  
      }
  
  
      /**
       * Return the content length that was set or calculated for this Response.
       */
      public int getContentLength() {
  
  	return (response.getContentLength());
  
      }
  
  
      /**
       * Return the content type that was set or calculated for this response,
       * or <code>null</code> if no content type was set.
       */
      public String getContentType() {
  
  	return (response.getContentType());
  
      }
  
  
      /**
       * Return a PrintWriter that can be used to render error messages,
       * regardless of whether a stream or writer has already been acquired.
       */
      public PrintWriter getReporter() {
  
  	return (response.getReporter());
  
      }
  
  
      /**
       * Release all object references, and initialize instance variables, in
       * preparation for reuse of this object.
       */
      public void recycle() {
  
  	response.recycle();
  
      }
  
  
      /**
       * Reset the data buffer but not any status or header information.
       */
      public void resetBuffer() {
  
  	response.resetBuffer();
  
      }
  
  
  }