|
Tomcat example source code file (Response.java)
The Tomcat Response.java source code/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote;
import java.io.IOException;
import java.util.Locale;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.http.MimeHeaders;
/**
* 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 <hans@gefionsoftware.com>
* @author Remy Maucherat
*/
public final class Response {
// ----------------------------------------------------------- Constructors
public Response() {
}
// ----------------------------------------------------- Class Variables
/**
* Default locale as mandated by the spec.
*/
private static Locale DEFAULT_LOCALE = Locale.getDefault();
// ----------------------------------------------------- Instance Variables
/**
* Status code.
*/
protected int status = 200;
/**
* Status message.
*/
protected String message = null;
/**
* Response headers.
*/
protected MimeHeaders headers = new MimeHeaders();
/**
* Associated output buffer.
*/
protected OutputBuffer outputBuffer;
/**
* Notes.
*/
protected Object notes[] = new Object[Constants.MAX_NOTES];
/**
* Committed flag.
*/
protected boolean commited = false;
/**
* Action hook.
*/
public ActionHook hook;
/**
* HTTP specific fields.
*/
protected String contentType = null;
protected String contentLanguage = null;
protected String characterEncoding = Constants.DEFAULT_CHARACTER_ENCODING;
protected long contentLength = -1;
private Locale locale = DEFAULT_LOCALE;
// General informations
private long bytesWritten=0;
/**
* Holds request error exception.
*/
protected Exception errorException = null;
/**
* Has the charset been explicitly set.
*/
protected boolean charsetSet = false;
/**
* Request error URI.
*/
protected String errorURI = null;
protected Request req;
// ------------------------------------------------------------- Properties
public Request getRequest() {
return req;
}
public void setRequest( Request req ) {
this.req=req;
}
public OutputBuffer getOutputBuffer() {
return outputBuffer;
}
public void setOutputBuffer(OutputBuffer outputBuffer) {
this.outputBuffer = outputBuffer;
}
public MimeHeaders getMimeHeaders() {
return headers;
}
public ActionHook getHook() {
return hook;
}
public void setHook(ActionHook hook) {
this.hook = hook;
}
// -------------------- Per-Response "notes" --------------------
public final void setNote(int pos, Object value) {
notes[pos] = value;
}
public final Object getNote(int pos) {
return notes[pos];
}
// -------------------- Actions --------------------
public void action(ActionCode actionCode, Object param) {
if (hook != null) {
if( param==null )
hook.action(actionCode, this);
else
hook.action(actionCode, param);
}
}
// -------------------- State --------------------
public int getStatus() {
return status;
}
/**
* Set the response status
*/
public void setStatus( int status ) {
this.status = status;
}
/**
* Get the status message.
*/
public String getMessage() {
return message;
}
/**
* Set the status message.
*/
public void setMessage(String message) {
this.message = message;
}
public boolean isCommitted() {
return commited;
}
public void setCommitted(boolean v) {
this.commited = v;
}
// -----------------Error State --------------------
/**
* Set the error Exception that occurred during
* request processing.
*/
public void setErrorException(Exception ex) {
errorException = ex;
}
/**
* Get the Exception that occurred during request
* processing.
*/
public Exception getErrorException() {
return errorException;
}
public boolean isExceptionPresent() {
return ( errorException != null );
}
/**
* Set request URI that caused an error during
* request processing.
*/
public void setErrorURI(String uri) {
errorURI = uri;
}
/** Get the request URI that caused the original error.
*/
public String getErrorURI() {
return errorURI;
}
// -------------------- Methods --------------------
public void reset()
throws IllegalStateException {
// 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 void finish() throws IOException {
action(ActionCode.ACTION_CLOSE, this);
}
public void acknowledge() throws IOException {
action(ActionCode.ACTION_ACK, this);
}
// -------------------- Headers --------------------
/**
* Warning: This method always returns <code>false
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.