FileDocCategorySizeDatePackage
Response.javaAPI DocGlassfish v2 API16782Fri May 04 22:32:42 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.
private NotesManagerImpl
notesManager
Notes.
protected boolean
commited
Committed flag.
public ActionHook
hook
Action hook.
protected String
contentType
HTTP specific fields.
protected String
contentLanguage
protected String
characterEncoding
private String
quotedCharsetValue
protected int
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 {
                int cL=Integer.parseInt( 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)

        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()

        return contentLength;
    
public java.lang.StringgetContentType()


        String ret = contentType;

        if (ret != null 
                /* SJSAS 6316254
                && characterEncoding != null
                */
                // START SJSAS 6316254
                && quotedCharsetValue != null
                // END SJSAS 6316254
                && charsetSet) {
            /* SJSAS 6316254
            ret = ret + ";charset=" + characterEncoding;   
            */
            // START SJSAS 6316254
            ret = ret + ";charset=" + quotedCharsetValue;
            // END SJSAS 6316254
        }

        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 notesManager.getNote(pos);
    
public NotesManagerImplgetNotesManager()

        return notesManager;
    
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;
        // START SJSAS 6316254
        quotedCharsetValue = characterEncoding;
        // END SJSAS 6316254
        charsetSet = false;
        contentLength = -1;
        status = 200;
        message = null;
        commited = false;
        errorException = null;
        errorURI = null;
        headers.clear();

        // update counters
        bytesWritten=0;
    
public voidremoveSessionCookies()
Removes any Set-Cookie response headers whose value contains the string JSESSIONID

        headers.removeHeader("Set-Cookie", Constants.SESSION_COOKIE_NAME);
    
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;
        // START SJSAS 6316254
        quotedCharsetValue = characterEncoding;
        // END SJSAS 6316254
        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;
        // START SJSAS 6316254
        quotedCharsetValue = charset;
        // END SJSAS 6316254
        charsetSet=true;
    
public voidsetCommitted(boolean v)

        this.commited = v;
    
public voidsetContentLength(int 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;
            // START SJSAS 6316254
            this.quotedCharsetValue = charsetValue;
            // END SJSAS 6316254
            this.characterEncoding = charsetValue.replace('"", ' ").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)

	notesManager.setNote(pos,value);
    
public voidsetNotesManager(NotesManagerImpl notesManager)

        this.notesManager = notesManager;
    
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;