📄 responseimpl.java
字号:
/*
* $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Attic/ResponseImpl.java,v 1.33.2.5 2001/01/12 04:39:04 larryi Exp $
* $Revision: 1.33.2.5 $
* $Date: 2001/01/12 04:39:04 $
*
* ====================================================================
*
* 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.tomcat.core;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.tomcat.util.*;
import org.apache.tomcat.facade.*;
/**
*
* @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>
*/
public class ResponseImpl implements Response {
protected static StringManager sm =
StringManager.getManager("org.apache.tomcat.core");
static final Locale DEFAULT_LOCALE=new Locale(Constants.LOCALE_DEFAULT, "");
protected Request request;
protected HttpServletResponse responseFacade;
protected Vector userCookies = new Vector();
protected String contentType = Constants.DEFAULT_CONTENT_TYPE;
protected String contentLanguage = null;
protected String characterEncoding = Constants.DEFAULT_CHAR_ENCODING;
protected String sessionId;
protected int contentLength = -1;
protected int status = 200;
private Locale locale = DEFAULT_LOCALE;
protected MimeHeaders headers = new MimeHeaders();
protected BufferedServletOutputStream out;
protected PrintWriter writer;
protected ByteBuffer bBuffer;
protected boolean usingStream = false;
protected boolean usingWriter = false;
protected boolean started = false;
protected boolean commited = false;
boolean notIncluded=true;
Exception errorException=null;
String errorURI=null;
// default implementation will just append everything here
StringBuffer body=null;
public ResponseImpl() {
out=new BufferedServletOutputStream();
out.setResponse(this);
}
public HttpServletResponse getFacade() {
if( responseFacade==null ) {
Context ctx= request.getContext();
if( ctx == null ) {
ctx=request.getContextManager().getContext("");
}
responseFacade = ctx.getFacadeManager().createHttpServletResponseFacade(this);
}
return responseFacade;
}
public void setRequest(Request request) {
this.request = request;
}
public Request getRequest() {
return request;
}
/* -------------------- */
// Included response behavior
public boolean isIncluded() {
return ! notIncluded;
}
public void setIncluded( boolean incl ) {
notIncluded= ! incl;
if( incl ) {
// included behavior, no header output,
// no status change on errors.
// XXX we can optimize a bit - replace headers with
// an new Hashtable we can throw away.
} else {
// move back to normal behavior.
}
}
public void setErrorException(Exception ex) {
errorException = ex;
}
public Exception getErrorException() {
return errorException;
}
public void setErrorURI(String uri) {
errorURI = uri;
}
public String getErrorURI() {
return errorURI;
}
public boolean isStarted() {
return started;
}
public void recycle() {
userCookies.removeAllElements(); // XXX reuse !!!
contentType = Constants.DEFAULT_CONTENT_TYPE;
contentLanguage = null;
locale = DEFAULT_LOCALE;
characterEncoding = Constants.DEFAULT_CHAR_ENCODING;
contentLength = -1;
status = 200;
usingWriter = false;
usingStream = false;
sessionId=null;
writer=null;
started = false;
commited = false;
notIncluded=true;
errorURI=null;
// adapter
body=null;
if( out != null ) out.recycle();
if( bBuffer != null ) bBuffer.recycle();
headers.clear();
}
public void finish() throws IOException {
if (usingWriter && (writer != null)) {
writer.flush();
writer.close();
}
if( bBuffer != null) {
bBuffer.flush();
request.getContextManager().doAfterBody(request, this);
return;
}
out.flush();
out.reallyFlush();
request.getContextManager().doAfterBody(request, this);
out.close();
}
public boolean containsHeader(String name) {
return headers.containsHeader(name);
}
// XXX
// mark whether or not we are being used as a stream our writer
public boolean isUsingStream() {
return usingStream;
}
public void setUsingStream( boolean stream ) {
usingStream=stream;
}
public boolean isUsingWriter() {
return usingWriter;
}
public void setUsingWriter( boolean writer ) {
usingWriter=writer;
out.setUsingWriter (true);
}
public PrintWriter getWriter() throws IOException {
return getWriter( out );
}
public PrintWriter getWriter(ServletOutputStream outs) throws IOException {
if(writer!=null) return writer;
// it already did all the checkings
started = true;
usingWriter = true;
writer = new ServletWriterFacade( getConverter(outs), this);
return writer;
}
public Writer getConverter( ServletOutputStream outs ) throws IOException {
String encoding = getCharacterEncoding();
if (encoding == null) {
// use default platform encoding - is this correct ?
return new OutputStreamWriter(outs);
} else {
try {
return new OutputStreamWriter(outs, encoding);
} catch (java.io.UnsupportedEncodingException ex) {
// XXX log it
System.out.println("Unsuported encoding: " + encoding );
return new OutputStreamWriter(outs);
}
}
}
public ByteBuffer getOutputBuffer() {
return bBuffer;
}
public void setOutputBuffer(ByteBuffer buf) {
bBuffer=buf;
if( buf!= null) buf.setParent( this );
}
/** Either implement ServletOutputStream or return BufferedServletOutputStream(this)
and implement doWrite();
@deprecated
*/
public ServletOutputStream getOutputStream() {
started = true;
return out; // out.getServletOutputStreamFacade();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -