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/04/18 05:45:19 UTC

cvs commit: jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core ApplicationContext.java

craigmcc    00/04/17 20:45:18

  Modified:    proposals/catalina/src/share/org/apache/tomcat/connector
                        HttpResponseBase.java LocalStrings.properties
                        ResponseBase.java ResponseStream.java
               proposals/catalina/src/share/org/apache/tomcat/connector/http
                        HttpProcessor.java
               proposals/catalina/src/share/org/apache/tomcat/core
                        ApplicationContext.java
  Log:
  Link the ResponseStream (implementation of ServletOutputStream) to the
  buffering provided by the underlying ResponseBase object.
  
  Clean up spec conformance and internationalization details (although more
  will undoubtedly be discovered).
  
  Revision  Changes    Path
  1.3       +51 -13    jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/HttpResponseBase.java
  
  Index: HttpResponseBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/HttpResponseBase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- HttpResponseBase.java	2000/04/17 18:39:48	1.2
  +++ HttpResponseBase.java	2000/04/18 03:45:14	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/HttpResponseBase.java,v 1.2 2000/04/17 18:39:48 craigmcc Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/04/17 18:39:48 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/HttpResponseBase.java,v 1.3 2000/04/18 03:45:14 craigmcc Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/04/18 03:45:14 $
    *
    * ====================================================================
    *
  @@ -74,6 +74,7 @@
   import java.util.Date;
   import java.util.Enumeration;
   import java.util.Hashtable;
  +import java.util.Locale;
   import java.util.TimeZone;
   import java.util.Vector;
   import javax.servlet.http.Cookie;
  @@ -92,7 +93,7 @@
    * methods need to be implemented.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.2 $ $Date: 2000/04/17 18:39:48 $
  + * @version $Revision: 1.3 $ $Date: 2000/04/18 03:45:14 $
    */
   
   public class HttpResponseBase
  @@ -429,6 +430,13 @@
   	    outputWriter.print("\r\n");
   	}
   
  +	// Send the content-length and content-type headers (if any)
  +	if (getContentType() != null)
  +	    outputWriter.print("Content-Type: " + getContentType() + "\r\n");
  +	if (getContentLength() >= 0)
  +	    outputWriter.print("Content-Length: " + getContentLength() +
  +			       "\r\n");
  +
   	// Send all specified headers (if any)
   	Enumeration names = headers.keys();
   	while (names.hasMoreElements()) {
  @@ -557,7 +565,8 @@
   
   
       /**
  -     * Clear any content written to the buffer.
  +     * 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
  @@ -583,7 +592,6 @@
   	if (isCommitted())
   	    return;
   	super.setContentLength(length);
  -	setIntHeader("Content-Length", length);
   
       }
   
  @@ -599,11 +607,30 @@
   	if (isCommitted())
   	    return;
   	super.setContentType(type);
  -	setHeader("Content-Type", 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;
  +	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
   
   
  @@ -650,12 +677,9 @@
   	    return;
   	Vector values = (Vector) headers.get(name);
   	if (values != null)
  -	    values.addElement(value);
  -	else {
  -	    values = new Vector();
   	    values.addElement(value);
  -	    headers.put(name, values);
  -	}
  +	else
  +	    setHeader(name, value);
   
       }
   
  @@ -786,6 +810,7 @@
   
   
   	// Format a default output message for this error
  +	// FIXME - need to handle web.xml declared error pages
   	reset();
   	setContentType("text/html");
   	this.status = status;
  @@ -856,7 +881,20 @@
   	    return;
   	Vector values = new Vector();
   	values.addElement(value);
  -	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);
  +	} else
  +	    headers.put(name, values);
   
       }
   
  
  
  
  1.3       +4 -0      jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/LocalStrings.properties,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LocalStrings.properties	2000/04/17 18:39:48	1.2
  +++ LocalStrings.properties	2000/04/18 03:45:15	1.3
  @@ -8,3 +8,7 @@
   response.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.5       +38 -9     jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/ResponseBase.java
  
  Index: ResponseBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/ResponseBase.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ResponseBase.java	2000/04/17 18:39:48	1.4
  +++ ResponseBase.java	2000/04/18 03:45:15	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/ResponseBase.java,v 1.4 2000/04/17 18:39:48 craigmcc Exp $
  - * $Revision: 1.4 $
  - * $Date: 2000/04/17 18:39:48 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/ResponseBase.java,v 1.5 2000/04/18 03:45:15 craigmcc Exp $
  + * $Revision: 1.5 $
  + * $Date: 2000/04/18 03:45:15 $
    *
    * ====================================================================
    *
  @@ -87,7 +87,7 @@
    * the connector-specific methods need to be implemented.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.4 $ $Date: 2000/04/17 18:39:48 $
  + * @version $Revision: 1.5 $ $Date: 2000/04/18 03:45:15 $
    */
   
   public abstract class ResponseBase
  @@ -140,6 +140,12 @@
   
   
       /**
  +     * The character encoding associated with this Response.
  +     */
  +    protected String encoding = RequestUtil.parseCharacterEncoding(null);
  +
  +
  +    /**
        * Descriptive information about this Response implementation.
        */
       protected static final String info =
  @@ -149,7 +155,7 @@
       /**
        * The Locale associated with this Response.
        */
  -    protected Locale locale = null;
  +    protected Locale locale = Locale.getDefault();
   
   
       /**
  @@ -350,7 +356,8 @@
   	contentLength = -1;
   	contentType = "text/plain";
   	context = null;
  -	locale = null;
  +	encoding = RequestUtil.parseCharacterEncoding(null);
  +	locale = Locale.getDefault();
   	output = null;
   	request = null;
   	stream = null;
  @@ -359,6 +366,25 @@
       }
   
   
  +    // -------------------------------------------------------- Package Methods
  +
  +
  +    /**
  +     * Write the specified byte to our output stream, flushing if necessary.
  +     *
  +     * @param b The byte to be written
  +     *
  +     * @exception IOExceptino if an input/output error occurs
  +     */
  +    public void write(int b) throws IOException {
  +
  +	if (bufferCount >= buffer.length)
  +	    flushBuffer();
  +	buffer[bufferCount++] = (byte) b;
  +
  +    }
  +
  +
       // ------------------------------------------------ ServletResponse Methods
   
   
  @@ -393,7 +419,7 @@
        */
       public String getCharacterEncoding() {
   
  -	return (RequestUtil.parseCharacterEncoding(getContentType()));
  +	return (encoding);
   
       }
   
  @@ -474,11 +500,11 @@
   	    throw new IllegalStateException
   		(sm.getString("responseBase.reset.ise"));
   
  +	if (stream != null)
  +	    ((ResponseStream) stream).reset();
   	bufferCount = 0;
   	contentLength = -1;
   	contentType = "text/plain";
  -	stream = null;
  -	writer = null;
   
       }
   
  @@ -528,6 +554,8 @@
   	if (isCommitted())
   	    return;
   	this.contentType = type;
  +	if (type.indexOf(";") >= 0)
  +	    encoding = RequestUtil.parseCharacterEncoding(type);
   
       }
   
  @@ -543,6 +571,7 @@
   	if (isCommitted())
   	    return;
   	this.locale = locale;
  +	;	// FIXME -- set encoding based on the locale!
   
       }
   
  
  
  
  1.2       +85 -7     jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/ResponseStream.java
  
  Index: ResponseStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/ResponseStream.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResponseStream.java	2000/02/20 02:55:06	1.1
  +++ ResponseStream.java	2000/04/18 03:45:15	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/ResponseStream.java,v 1.1 2000/02/20 02:55:06 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/02/20 02:55:06 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/ResponseStream.java,v 1.2 2000/04/18 03:45:15 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/04/18 03:45:15 $
    *
    * ====================================================================
    *
  @@ -69,16 +69,17 @@
   import java.io.OutputStream;
   import javax.servlet.ServletOutputStream;
   import org.apache.tomcat.Response;
  +import org.apache.tomcat.util.StringManager;
   
   
   /**
    * Convenience implementation of <b>ServletOutputStream</b> that works with
  - * the standard ResponseBase implementation of <b>Response</b>.
  + * 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.
    *
  - * <b>FIXME</b>:  Enforce writing up to content length if specified
  - *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2000/02/20 02:55:06 $
  + * @version $Revision: 1.2 $ $Date: 2000/04/18 03:45:15 $
    */
   
   final class ResponseStream
  @@ -96,6 +97,8 @@
       public ResponseStream(Response response) {
   
   	super();
  +	closed = false;
  +	count = 0;
   	this.response = response;
   	this.stream = response.getStream();
   
  @@ -106,12 +109,38 @@
   
   
       /**
  +     * Has this stream been closed?
  +     */
  +    private boolean closed = false;
  +
  +
  +    /**
  +     * The number of bytes which have already been written to this stream.
  +     */
  +    private int count = 0;
  +
  +
  +    /**
  +     * The content length past which we will not write, or -1 if there is
  +     * no defined content length.
  +     */
  +    private int length = -1;
  +
  +
  +    /**
        * The Response with which this input stream is associated.
        */
       private Response response = null;
   
   
       /**
  +     * The localized strings for this package.
  +     */
  +    private static StringManager sm =
  +	StringManager.getManager(Constants.Package);
  +
  +
  +    /**
        * The underlying output stream to which we should write data.
        */
       private OutputStream stream = null;
  @@ -121,6 +150,35 @@
   
   
       /**
  +     * 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("responseBase.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("responseBase.flush.closed"));
  +
  +	response.getResponse().flushBuffer();
  +
  +    }
  +
  +
  +    /**
        * Write the specified byte to our output stream.
        *
        * @param b The byte to be written
  @@ -128,8 +186,28 @@
        * @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++;
  +
  +    }
  +
  +
  +    // -------------------------------------------------------- Package Methods
  +
  +
  +    /**
  +     * Reset the count of bytes written to this stream to zero.
  +     */
  +    void reset() {
   
  -	stream.write(b);
  +	count = 0;
   
       }
   
  
  
  
  1.8       +8 -5      jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/http/HttpProcessor.java
  
  Index: HttpProcessor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/http/HttpProcessor.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- HttpProcessor.java	2000/04/17 18:39:49	1.7
  +++ HttpProcessor.java	2000/04/18 03:45:17	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/http/HttpProcessor.java,v 1.7 2000/04/17 18:39:49 craigmcc Exp $
  - * $Revision: 1.7 $
  - * $Date: 2000/04/17 18:39:49 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/connector/http/HttpProcessor.java,v 1.8 2000/04/18 03:45:17 craigmcc Exp $
  + * $Revision: 1.8 $
  + * $Date: 2000/04/18 03:45:17 $
    *
    * ====================================================================
    *
  @@ -97,7 +97,7 @@
    * the request.  When the processor is completed, it will recycle itself.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.7 $ $Date: 2000/04/17 18:39:49 $
  + * @version $Revision: 1.8 $ $Date: 2000/04/18 03:45:17 $
    */
   
   final class HttpProcessor
  @@ -312,8 +312,11 @@
   
   	    // Set the corresponding request headers
   	    if (match.equals("authorization")) {
  -		// Do not really want to include this but it needs visibility
  +		// FIXME - Don't want to include this but it needs visibility
   		request.addHeader(name, value);
  +	    } else if (match.equals("accept-language")) {
  +		request.addHeader(name, value);
  +		// FIXME - Also parse and add locales to the request
   	    } else if (match.equals("cookie")) {
   		Cookie cookies[] = RequestUtil.parseCookieHeader(value);
   		for (int i = 0; i < cookies.length; i++) {
  
  
  
  1.3       +4 -5      jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ApplicationContext.java
  
  Index: ApplicationContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ApplicationContext.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ApplicationContext.java	2000/02/13 01:43:44	1.2
  +++ ApplicationContext.java	2000/04/18 03:45:17	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ApplicationContext.java,v 1.2 2000/02/13 01:43:44 craigmcc Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/02/13 01:43:44 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ApplicationContext.java,v 1.3 2000/04/18 03:45:17 craigmcc Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/04/18 03:45:17 $
    *
    * ====================================================================
    *
  @@ -80,7 +80,6 @@
   import org.apache.tomcat.Logger;
   import org.apache.tomcat.Resources;
   import org.apache.tomcat.Wrapper;
  -import org.apache.tomcat.deployment.ContextParameter;
   
   
   /**
  @@ -89,7 +88,7 @@
    * associated with each instance of <code>StandardContext</code>.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.2 $ $Date: 2000/02/13 01:43:44 $
  + * @version $Revision: 1.3 $ $Date: 2000/04/18 03:45:17 $
    */
   
   final class ApplicationContext