FileDocCategorySizeDatePackage
Response.javaAPI DocApache Tomcat 6.0.1415246Fri Jul 20 04:20:36 BST 2007org.apache.coyote

Response

public final class Response extends Object
Response object.
author
James Duncan Davidson [duncan@eng.sun.com]
author
Jason Hunter [jch@eng.sun.com]
author
James Todd [gonzo@eng.sun.com]
author
Harish Prabandham
author
Hans Bergsten
author
Remy Maucherat

Fields Summary
private static Locale
DEFAULT_LOCALE
Default locale as mandated by the spec.
protected int
status
Status code.
protected String
message
Status message.
protected org.apache.tomcat.util.http.MimeHeaders
headers
Response headers.
protected OutputBuffer
outputBuffer
Associated output buffer.
protected Object[]
notes
Notes.
protected boolean
commited
Committed flag.
public ActionHook
hook
Action hook.
protected String
contentType
HTTP specific fields.
protected String
contentLanguage
protected String
characterEncoding
protected long
contentLength
private Locale
locale
private long
bytesWritten
protected Exception
errorException
Holds request error exception.
protected boolean
charsetSet
Has the charset been explicitly set.
protected String
errorURI
Request error URI.
protected Request
req
Constructors Summary
public Response()

    
Methods Summary
public voidacknowledge()

        action(ActionCode.ACTION_ACK, this);
    
public voidaction(ActionCode actionCode, java.lang.Object param)

        if (hook != null) {
            if( param==null ) 
                hook.action(actionCode, this);
            else
                hook.action(actionCode, param);
        }
    
public voidaddHeader(java.lang.String name, java.lang.String value)

        char cc=name.charAt(0);
        if( cc=='C" || cc=='c" ) {
            if( checkSpecialHeader(name, value) )
            return;
        }
        headers.addValue(name).setString( value );
    
private booleancheckSpecialHeader(java.lang.String name, java.lang.String value)
Set internal fields for special header names. Called from set/addHeader. Return true if the header is special, no need to set the header.

        // XXX Eliminate redundant fields !!!
        // ( both header and in special fields )
        if( name.equalsIgnoreCase( "Content-Type" ) ) {
            setContentType( value );
            return true;
        }
        if( name.equalsIgnoreCase( "Content-Length" ) ) {
            try {
                long cL=Long.parseLong( value );
                setContentLength( cL );
                return true;
            } catch( NumberFormatException ex ) {
                // Do nothing - the spec doesn't have any "throws" 
                // and the user might know what he's doing
                return false;
            }
        }
        if( name.equalsIgnoreCase( "Content-Language" ) ) {
            // XXX XXX Need to construct Locale or something else
        }
        return false;
    
public booleancontainsHeader(java.lang.String name)
Warning: This method always returns false for Content-Type and Content-Length.

        return headers.getHeader(name) != null;
    
public voiddoWrite(org.apache.tomcat.util.buf.ByteChunk chunk)
Write a chunk of bytes.

        outputBuffer.doWrite(chunk, this);
        bytesWritten+=chunk.getLength();
    
public voidfinish()

        action(ActionCode.ACTION_CLOSE, this);
    
public longgetBytesWritten()

        return bytesWritten;
    
public java.lang.StringgetCharacterEncoding()

        return characterEncoding;
    
public java.lang.StringgetContentLanguage()
Return the content language.

        return contentLanguage;
    
public intgetContentLength()

        long length = getContentLengthLong();
        
        if (length < Integer.MAX_VALUE) {
            return (int) length;
        }
        return -1;
    
public longgetContentLengthLong()

        return contentLength;
    
public java.lang.StringgetContentType()


        String ret = contentType;

        if (ret != null 
            && characterEncoding != null
            && charsetSet) {
            ret = ret + ";charset=" + characterEncoding;
        }

        return ret;
    
public java.lang.ExceptiongetErrorException()
Get the Exception that occurred during request processing.

        return errorException;
    
public java.lang.StringgetErrorURI()
Get the request URI that caused the original error.

        return errorURI;
    
public ActionHookgetHook()

        return hook;
    
public java.util.LocalegetLocale()

        return locale;
    
public java.lang.StringgetMessage()
Get the status message.

        return message;
    
public org.apache.tomcat.util.http.MimeHeadersgetMimeHeaders()

        return headers;
    
public final java.lang.ObjectgetNote(int pos)

        return notes[pos];
    
public OutputBuffergetOutputBuffer()

        return outputBuffer;
    
public RequestgetRequest()


    // ------------------------------------------------------------- Properties

       
        return req;
    
public intgetStatus()

        return status;
    
public booleanisCommitted()

        return commited;
    
public booleanisExceptionPresent()

        return ( errorException != null );
    
public voidrecycle()

        
        contentType = null;
        contentLanguage = null;
        locale = DEFAULT_LOCALE;
        characterEncoding = Constants.DEFAULT_CHARACTER_ENCODING;
        charsetSet = false;
        contentLength = -1;
        status = 200;
        message = null;
        commited = false;
        errorException = null;
        errorURI = null;
        headers.clear();

        // update counters
        bytesWritten=0;
    
public voidreset()

        
        // Reset the headers only if this is the main request,
        // not for included
        contentType = null;
        locale = DEFAULT_LOCALE;
        contentLanguage = null;
        characterEncoding = Constants.DEFAULT_CHARACTER_ENCODING;
        contentLength = -1;
        charsetSet = false;

        status = 200;
        message = null;
        headers.clear();
        
        // Force the PrintWriter to flush its data to the output
        // stream before resetting the output stream
        //
        // Reset the stream
        if (commited) {
            //String msg = sm.getString("servletOutputStreamImpl.reset.ise"); 
            throw new IllegalStateException();
        }
        
        action(ActionCode.ACTION_RESET, this);
    
public voidsendHeaders()
Signal that we're done with the headers, and body will follow. Any implementation needs to notify ContextManager, to allow interceptors to fix headers.

        action(ActionCode.ACTION_COMMIT, this);
        commited = true;
    
public voidsetBytesWritten(long bytesWritten)

        this.bytesWritten = bytesWritten;
    
public voidsetCharacterEncoding(java.lang.String charset)


        if (isCommitted())
            return;
        if (charset == null)
            return;

        characterEncoding = charset;
        charsetSet=true;
    
public voidsetCommitted(boolean v)

        this.commited = v;
    
public voidsetContentLength(int contentLength)

        this.contentLength = contentLength;
    
public voidsetContentLength(long contentLength)

        this.contentLength = contentLength;
    
public voidsetContentType(java.lang.String type)
Sets the content type. This method must preserve any response charset that may already have been set via a call to response.setContentType(), response.setLocale(), or response.setCharacterEncoding().

param
type the content type


        int semicolonIndex = -1;

        if (type == null) {
            this.contentType = null;
            return;
        }

        /*
         * Remove the charset param (if any) from the Content-Type, and use it
         * to set the response encoding.
         * The most recent response encoding setting will be appended to the
         * response's Content-Type (as its charset param) by getContentType();
         */
        boolean hasCharset = false;
        int len = type.length();
        int index = type.indexOf(';");
        while (index != -1) {
            semicolonIndex = index;
            index++;
            while (index < len && Character.isSpace(type.charAt(index))) {
                index++;
            }
            if (index+8 < len
                    && type.charAt(index) == 'c"
                    && type.charAt(index+1) == 'h"
                    && type.charAt(index+2) == 'a"
                    && type.charAt(index+3) == 'r"
                    && type.charAt(index+4) == 's"
                    && type.charAt(index+5) == 'e"
                    && type.charAt(index+6) == 't"
                    && type.charAt(index+7) == '=") {
                hasCharset = true;
                break;
            }
            index = type.indexOf(';", index);
        }

        if (!hasCharset) {
            this.contentType = type;
            return;
        }

        this.contentType = type.substring(0, semicolonIndex);
        String tail = type.substring(index+8);
        int nextParam = tail.indexOf(';");
        String charsetValue = null;
        if (nextParam != -1) {
            this.contentType += tail.substring(nextParam);
            charsetValue = tail.substring(0, nextParam);
        } else {
            charsetValue = tail;
        }

        // The charset value may be quoted, but must not contain any quotes.
        if (charsetValue != null && charsetValue.length() > 0) {
            charsetSet=true;
            charsetValue = charsetValue.replace('"", ' ");
            this.characterEncoding = charsetValue.trim();
        }
    
public voidsetErrorException(java.lang.Exception ex)
Set the error Exception that occurred during request processing.

    errorException = ex;
    
public voidsetErrorURI(java.lang.String uri)
Set request URI that caused an error during request processing.

        errorURI = uri;
    
public voidsetHeader(java.lang.String name, java.lang.String value)

        char cc=name.charAt(0);
        if( cc=='C" || cc=='c" ) {
            if( checkSpecialHeader(name, value) )
            return;
        }
        headers.setValue(name).setString( value);
    
public voidsetHook(ActionHook hook)

        this.hook = hook;
    
public voidsetLocale(java.util.Locale locale)
Called explicitely by user to set the Content-Language and the default encoding


        if (locale == null) {
            return;  // throw an exception?
        }

        // Save the locale for use by getLocale()
        this.locale = locale;

        // Set the contentLanguage for header output
        contentLanguage = locale.getLanguage();
        if ((contentLanguage != null) && (contentLanguage.length() > 0)) {
            String country = locale.getCountry();
            StringBuffer value = new StringBuffer(contentLanguage);
            if ((country != null) && (country.length() > 0)) {
                value.append('-");
                value.append(country);
            }
            contentLanguage = value.toString();
        }

    
public voidsetMessage(java.lang.String message)
Set the status message.

        this.message = message;
    
public final voidsetNote(int pos, java.lang.Object value)

        notes[pos] = value;
    
public voidsetOutputBuffer(OutputBuffer outputBuffer)

        this.outputBuffer = outputBuffer;
    
public voidsetRequest(Request req)

        this.req=req;
    
public voidsetStatus(int status)
Set the response status

        this.status = status;