📄 coyoteresponse.java
字号:
/*
* $Header: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/coyote/tomcat5/CoyoteResponse.java,v 1.11 2004/01/20 19:39:00 luehe Exp $
* $Revision: 1.11 $
* $Date: 2004/01/20 19:39:00 $
*
* ====================================================================
*
* 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.coyote.tomcat5;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Vector;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Connector;
import org.apache.catalina.Context;
import org.apache.catalina.HttpResponse;
import org.apache.catalina.Wrapper;
import org.apache.catalina.util.CharsetMapper;
import org.apache.catalina.util.DateTool;
import org.apache.catalina.util.StringManager;
import org.apache.coyote.Response;
import org.apache.tomcat.util.buf.CharChunk;
import org.apache.tomcat.util.buf.UEncoder;
import org.apache.tomcat.util.http.FastHttpDateFormat;
import org.apache.tomcat.util.http.MimeHeaders;
import org.apache.tomcat.util.http.ServerCookie;
import org.apache.tomcat.util.net.URL;
/**
* Wrapper object for the Coyote response.
*
* @author Remy Maucherat
* @author Craig R. McClanahan
* @version $Revision: 1.11 $ $Date: 2004/01/20 19:39:00 $
*/
public class CoyoteResponse
implements HttpResponse, HttpServletResponse {
// ----------------------------------------------------------- Constructors
public CoyoteResponse() {
urlEncoder.addSafeCharacter('/');
}
// ----------------------------------------------------- Instance Variables
/**
* The date format we will use for creating date headers.
*/
protected SimpleDateFormat format = null;
/**
* Descriptive information about this Response implementation.
*/
protected static final String info =
"org.apache.coyote.tomcat5.CoyoteResponse/1.0";
/**
* The string manager for this package.
*/
protected static StringManager sm =
StringManager.getManager(Constants.Package);
// ------------------------------------------------------------- Properties
/**
* Associated Catalina connector.
*/
protected Connector connector;
/**
* 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;
}
/**
* Coyote response.
*/
protected Response coyoteResponse;
/**
* Set the Coyote response.
*
* @param response The Coyote response
*/
public void setCoyoteResponse(Response coyoteResponse) {
this.coyoteResponse = coyoteResponse;
outputBuffer.setResponse(coyoteResponse);
}
/**
* Get the Coyote response.
*/
public Response getCoyoteResponse() {
return (coyoteResponse);
}
/**
* 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);
}
/**
* The associated output buffer.
*/
protected OutputBuffer outputBuffer = new OutputBuffer();
/**
* The associated output stream.
*/
protected CoyoteOutputStream outputStream =
new CoyoteOutputStream(outputBuffer);
/**
* The associated writer.
*/
protected CoyoteWriter writer = new CoyoteWriter(outputBuffer);
/**
* The application commit flag.
*/
protected boolean appCommitted = false;
/**
* The included flag.
*/
protected boolean included = false;
/**
* The characterEncoding flag
*/
private boolean isCharacterEncodingSet = false;
/**
* The contextType flag
*/
private boolean isContentTypeSet = false;
/**
* The error flag.
*/
protected boolean error = false;
/**
* The set of Cookies associated with this Response.
*/
protected ArrayList cookies = new ArrayList();
/**
* Using output stream flag.
*/
protected boolean usingOutputStream = false;
/**
* Using writer flag.
*/
protected boolean usingWriter = false;
/**
* URL encoder.
*/
protected UEncoder urlEncoder = new UEncoder();
/**
* Recyclable buffer to hold the redirect URL.
*/
protected CharChunk redirectURLCC = new CharChunk();
// --------------------------------------------------------- Public Methods
/**
* Release all object references, and initialize instance variables, in
* preparation for reuse of this object.
*/
public void recycle() {
outputBuffer.recycle();
usingOutputStream = false;
usingWriter = false;
appCommitted = false;
included = false;
error = false;
isContentTypeSet = false;
isCharacterEncodingSet = false;
cookies.clear();
if ((Constants.SECURITY) && (facade != null)) {
facade.clear();
facade = null;
}
writer.recycle();
}
// ------------------------------------------------------- Response Methods
/**
* Return the number of bytes actually written to the output stream.
*/
public int getContentCount() {
return outputBuffer.getContentWritten();
}
/**
* Set the application commit flag.
*
* @param appCommitted The new application committed flag value
*/
public void setAppCommitted(boolean appCommitted) {
this.appCommitted = appCommitted;
}
/**
* Application commit flag accessor.
*/
public boolean isAppCommitted() {
return (this.appCommitted || isCommitted() || isSuspended()
|| ((getContentLength() != -1)
&& (getContentCount() >= getContentLength())));
}
/**
* Return the "processing inside an include" flag.
*/
public boolean getIncluded() {
return 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><description>/<version></code>.
*/
public String getInfo() {
return (info);
}
/**
* The request with which this response is associated.
*/
protected CoyoteRequest request = null;
/**
* Return the Request with which this Response is associated.
*/
public org.apache.catalina.Request getRequest() {
return (this.request);
}
/**
* Set the Request with which this Response is associated.
*
* @param request The new associated request
*/
public void setRequest(org.apache.catalina.Request request) {
this.request = (CoyoteRequest) request;
}
/**
* The facade associated with this response.
*/
protected CoyoteResponseFacade facade = null;
/**
* Return the <code>ServletResponse</code> for which this object
* is the facade.
*/
public ServletResponse getResponse() {
if (facade == null) {
facade = new CoyoteResponseFacade(this);
}
return (facade);
}
/**
* Return the output stream associated with this Response.
*/
public OutputStream getStream() {
return outputStream;
}
/**
* Set the output stream associated with this Response.
*
* @param stream The new output stream
*/
public void setStream(OutputStream stream) {
// This method is evil
}
/**
* Set the suspended flag.
*
* @param suspended The new suspended flag value
*/
public void setSuspended(boolean suspended) {
outputBuffer.setSuspended(suspended);
}
/**
* Suspended flag accessor.
*/
public boolean isSuspended() {
return outputBuffer.isSuspended();
}
/**
* Set the error flag.
*/
public void setError() {
error = true;
}
/**
* Error flag accessor.
*/
public boolean isError() {
return error;
}
/**
* 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 {
// Probably useless
return outputStream;
}
/**
* 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 {
// Writing leftover bytes
try {
outputBuffer.close();
} catch(IOException e) {
;
} catch(Throwable t) {
t.printStackTrace();
}
}
/**
* Return the content length that was set or calculated for this Response.
*/
public int getContentLength() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -