|
What this is
Other links
The source code
/*
* $Header: /home/cvs/jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletResponseFacade.java,v 1.33 2004/02/23 06:06:13 billbarker Exp $
* $Revision: 1.33 $
* $Date: 2004/02/23 06:06:13 $
*
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed 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.tomcat.facade;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.Locale;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.core.Context;
import org.apache.tomcat.core.ContextManager;
import org.apache.tomcat.core.OutputBuffer;
import org.apache.tomcat.core.Request;
import org.apache.tomcat.core.Response;
import org.apache.tomcat.core.ServerSession;
import org.apache.tomcat.util.http.MimeHeaders;
import org.apache.tomcat.util.http.ServerCookie;
import org.apache.tomcat.util.net.URL;
import org.apache.tomcat.util.res.StringManager;
/**
*
* @author James Duncan Davidson [duncan@eng.sun.com]
* @author Jason Hunter [jch@eng.sun.com]
* @author James Todd [gonzo@eng.sun.com]
* @author Costin Manolache
* @author Hans Bergsten [hans@gefionsoftware.com]
*/
public final class HttpServletResponseFacade implements HttpServletResponse
{
// Use the strings from core
private static StringManager sm = StringManager.getManager("org.apache.tomcat.resources");
private Response response;
private boolean usingStream = false;
private boolean usingWriter = false;
ServletOutputStreamFacade osFacade=null;
ServletWriterFacade writer;
/** Package
*/
HttpServletResponseFacade(Response response) {
this.response = response;
OutputBuffer oBuffer= response.getBuffer();
writer = new ServletWriterFacade( oBuffer, response);
}
void recycle() {
usingStream = false;
usingWriter= false;
if( writer.checkError() ) {
OutputBuffer oBuffer= response.getBuffer();
writer = new ServletWriterFacade( oBuffer, response);
}
if( osFacade != null ) osFacade.recycle();
}
// -------------------- Public methods --------------------
public void addCookie(Cookie cookie) {
if( response.isIncluded() ) return;
// layer costs - this can be avoided, but it's not a
// frequent operation ( for example sc can be reused )
// XXX reuse
StringBuffer sb=new StringBuffer();
ServerCookie.appendCookieValue( sb, cookie.getVersion(),
cookie.getName(), cookie.getValue(),
cookie.getPath(), cookie.getDomain(),
cookie.getComment(), cookie.getMaxAge(),
cookie.getSecure());
// the header name is Set-Cookie for both "old" and v.1 ( RFC2109 )
// RFC2965 is not supported by browsers and the Servlet spec
// asks for 2109.
addHeader( "Set-Cookie",
sb.toString());
}
public boolean containsHeader(String name) {
return response.containsHeader(name);
}
/** Delegate to various components of tomcat. This is not
* part of response, but session code.
*/
public String encodeRedirectURL(String location) {
String absolute = toAbsolute(location);
if (isEncodeable(absolute)) {
if( "".equals(location) ) {
location = absolute;
}
return (toEncoded(location, response.getRequest().getSession(false)));
} else {
return (location);
}
}
/**
* @deprecated
*/
public String encodeRedirectUrl(String location) {
return encodeRedirectURL(location);
}
public String encodeURL(String url) {
String absolute = toAbsolute(url);
if (isEncodeable(absolute)) {
if( "".equals(url) ) {
url = absolute;
}
return (toEncoded(url, response.getRequest().getSession(false)));
} else {
return (url);
}
}
/**
* @deprecated
*/
public String encodeUrl(String url) {
return encodeURL(url);
}
public String getCharacterEncoding() {
return response.getCharacterEncoding();
}
public ServletOutputStream getOutputStream() throws IOException {
if ( usingWriter ) {
String msg = sm.getString("serverResponse.outputStream.ise");
throw new IllegalStateException(msg);
}
usingStream=true;
// response.setUsingStream( true );
if( osFacade!=null) return osFacade;
//if( response.getOutputBuffer() != null ) {
osFacade=new ServletOutputStreamFacade(response);
// response.setServletOutputStream( osFacade );
//}
return osFacade;
// // old mechanism
// return response.getOutputStream();
// // response.getBufferedOutputStream().getServletOutputStreamFacade();
}
public PrintWriter getWriter() throws IOException {
if (usingStream) {
String msg = sm.getString("serverResponse.writer.ise");
throw new IllegalStateException(msg);
}
usingWriter= true ;
return writer;
}
public void sendError(int sc) throws IOException {
sendError(sc, "No detailed message");
}
public void sendError(int sc, String msg) throws IOException {
if (isCommitted()) {
Context ctx=response.getRequest().getContext();
ctx.log( "Servlet API error: sendError with commited buffer ", new Throwable("Trace"));
throw new IllegalStateException(sm.
getString("hsrf.error.ise"));
}
// if (sc != HttpServletResponse.SC_UNAUTHORIZED) // CRM: FIXME
// response.resetBuffer();
// Keep headers and cookies that are set
setStatus( sc );
Request request=response.getRequest();
request.setAttribute("javax.servlet.error.message", msg);
ContextManager cm=request.getContextManager();
cm.handleStatus( request, response, sc );
}
public void sendRedirect(String location)
throws IOException, IllegalArgumentException
{
if (location == null) {
String msg = sm.getString("hsrf.redirect.iae");
throw new IllegalArgumentException(msg);
}
// Even though DefaultErrorServlet will convert this
// location to absolute (if required) we should do so
// here in case the app has a non-default handler
sendError(HttpServletResponse.SC_MOVED_TEMPORARILY,
toAbsolute(location));
}
public void setContentLength(int len) {
response.setContentLength(len);
}
public void setContentType(String type) {
response.setContentType(type);
}
public void setDateHeader(String name, long date) {
if( ! response.isIncluded() ) {
MimeHeaders headers=response.getMimeHeaders();
headers.setValue( name ).setTime( date );
}
}
public void addDateHeader(String name, long value) {
if ( ! response.isIncluded() ) {
MimeHeaders headers=response.getMimeHeaders();
headers.addValue( name ).setTime( value );
}
}
public void setHeader(String name, String value) {
response.setHeader(name, value);
}
public void addHeader(String name, String value) {
response.addHeader(name, value);
}
public void setIntHeader(String name, int value) {
response.setHeader(name, Integer.toString(value));
}
public void addIntHeader(String name, int value) {
response.addHeader(name, Integer.toString(value));
}
public void setStatus(int sc) {
response.setStatus(sc);
}
public void setBufferSize(int size) throws IllegalStateException {
response.setBufferSize(size);
}
public int getBufferSize() {
return response.getBufferSize();
}
public void reset() throws IllegalStateException {
response.reset();
}
public boolean isCommitted() {
return response.isBufferCommitted();
}
public void flushBuffer() throws IOException {
response.flushBuffer();
}
public void setLocale(Locale loc) {
response.setLocale(loc);
}
public Locale getLocale() {
return response.getLocale();
}
/**
*
* @deprecated
*/
public void setStatus(int sc, String msg) {
response.setStatus(sc);
}
// -------------------- Private methods --------------------
/**
* Return
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 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.