📄 pi3httpservletresponse.java
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
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 name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
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 AUTHORS 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.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/org/pi3/servlet/core/Pi3HttpServletResponse.java,v $
* $Date: 2003/05/13 18:42:21 $
*
Description:
Implementation of both servlet API ServletResponse, HttpServletResponse interfaces.
\*____________________________________________________________________________*/
package org.pi3.servlet.core;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedOutputStream;
import java.lang.reflect.Array;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.StringTokenizer;
import javax.servlet.ServletResponse;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUtils;
import org.pi3.servlet.util.Pi3ServletUtils;
import org.pi3.servlet.util.Pi3BufferedOutputStream;
/**
*
* Implements both the <code>ServletResponse</code> and
* <code>javax.servlet.http.HttpServletResponse</code>
* interface to provide HTTP-specific functionality in
* sending a response back to the client.
*
* Defines an HTTP servlet response that a servlet running on
* Pi3Web sends to a client. This interface allows the servlet's
* <code>service</code> method to access HTTP headers and return
* data to its client.
*
*
* @author Various
* @version $Version$
*
*
*/
final public class Pi3HttpServletResponse implements ServletResponse, HttpServletResponse {
private int hobj;
private int hsrv;
private int sresult = 0;
private boolean usedPW = false;
private boolean usedOS = false;
private Pi3ServletOutputStream sout;
private PrintWriter pw;
private Locale locale;
Pi3ServletUtils su;
HttpServletRequest req;
/** This constructor is used by the native side */
protected Pi3HttpServletResponse(int pihttp, int hservlet,
Pi3ServletUtils sutils, HttpServletRequest request) {
hobj = pihttp;
hsrv = hservlet;
req = request;
sout = new Pi3ServletOutputStream(hobj, hsrv);
pw = new PrintWriter(new Pi3BufferedOutputStream(sout));
su = sutils;
}
/** The method to get the PiHttp object from the native side */
protected int getPiHttp() {
return hobj;
}
/** The method to get the Servlet handler object from the native side */
protected int getServletHandler() {
return hsrv;
}
/** The method to get the result of calling native methods from the native side */
protected int getServerResult() {
return sresult;
}
/** The method to get the availability to use the output ressources from the native side */
protected boolean isCommitted() {
return sout.isCommitted();
}
private String toAbsoluteURL(String location) throws MalformedURLException {
if (location == null) return null;
URL url;
try {
url = new URL(location);
} catch (MalformedURLException e1) {
String requrl = HttpUtils.getRequestURL(req).toString();
try {
url = new URL(new URL(requrl), location);
} catch (MalformedURLException e2) {
return (location);
}
}
return (url.toString());
}
private boolean useURLRewriting(String location) {
if (!req.isRequestedSessionIdValid()) return (false);
if (req.isRequestedSessionIdFromCookie()) return (false);
URL url;
try {
url = new URL(toAbsoluteURL(location));
} catch (MalformedURLException e) {
return (false);
}
if (!req.getScheme().equalsIgnoreCase(url.getProtocol())) return (false);
if (!req.getServerName().equalsIgnoreCase(url.getHost())) return (false);
if ((url.getPort() > 0) && (req.getServerPort() != url.getPort())) return (false);
/* String contextPath = req.getContext().getPath();
if ((contextPath != null) && (contextPath.length() > 0)) {
String file = url.getFile();
if ((file == null) || !file.startsWith(contextPath)) return (false);
}
*/
return (true);
}
private String rewriteURL(String location) {
if (location == null) return null;
Pi3HttpSession ses = (Pi3HttpSession)req.getSession(false);
if ( ses == null) return null;
String path = null;
String query = null;
int question = location.indexOf("?");
if (question < 0) {
path = location;
} else {
path = location.substring(0, question);
query = location.substring(question);
}
StringBuffer sb = new StringBuffer(path);
sb.append(";");
sb.append(Constants.SESSION_PARAMETER_NAME);
sb.append("=");
sb.append(ses.getSID());
if (query != null) sb.append(query);
return (sb.toString());
}
/**
* Returns the name of the charset used for
* the MIME body sent in this response.
*
* <p>If no charset has been assigned, it is implicitly
* set to <code>ISO-8859-1</code> (<code>Latin-1</code>).
*
* <p>See RFC 2047 (http://ds.internic.net/rfc/rfc2045.txt)
* for more information about character encoding and MIME.
*
* @return a <code>String</code> specifying the
* name of the charset, for
* example, <code>ISO-8859-1</code>
*
*/
public String getCharacterEncoding() {
if (containsHeader("Content-Type")) {
String ct = su.getResponseVariable("Content-Type");
if (ct != null) {
StringTokenizer st = new StringTokenizer(ct,";");
String to;
while (st.hasMoreTokens()) {
to = st.nextToken();
if (to.trim().toUpperCase().startsWith("CHARSET=")) {
return to.substring(to.indexOf("=") + 1);
}
}
}
}
return su.getDefaultCharset();
}
/**
* Returns a {@link ServletOutputStream} suitable for writing binary
* data in the response. The servlet container does not encode the
* binary data. Either this method or {@link #getWriter} may
* be called to write the body, not both.
*
* @return a {@link ServletOutputStream} for writing binary data
*
* @exception IllegalStateException if the <code>getWriter</code> method
* has been called on this response
*
* @exception IOException if an input or output exception occurred
*
* @see #getWriter
*
*/
public ServletOutputStream getOutputStream() throws IOException {
if (usedPW) {
IllegalStateException e = new IllegalStateException();
throw e;
}
usedOS = true;
return sout;
}
/**
* Returns a <code>PrintWriter</code> object that
* can send character text to the client.
* The character encoding used is the one specified
* in the <code>charset=</code> property of the
* {@link #setContentType} method, which must be called
* <i>before</i> calling this method for the charset to take effect.
*
* <p>If necessary, the MIME type of the response is
* modified to reflect the character encoding used.
*
* <p>Either this method or {@link #getOutputStream} may be called
* to write the body, not both.
*
*
* @return a <code>PrintWriter</code> object that
* can return character data to the client
*
* @exception UnsupportedEncodingException if the charset specified in
* <code>setContentType</code> cannot be
* used
*
* @exception IllegalStateException if the <code>getOutputStream</code>
* method has already been called for this
* response object
*
* @exception IOException if an input or output exception occurred
*
* @see #getOutputStream
* @see #setContentType
*
*/
public PrintWriter getWriter() throws IOException {
if (usedOS) {
IllegalStateException e = new IllegalStateException();
throw e;
}
usedPW = true;
return pw;
}
/**
* Sets the length of the content body in the response
* In HTTP servlets, this method sets the HTTP Content-Length header.
*
*
* @param len an integer specifying the length of the
* content being returned to the client; sets
* the Content-Length header
*
*/
public native void setContentLength(int len);
/**
* Sets the content type of the response being sent to
* the client. The content type may include the type of character
* encoding used, for example, <code>text/html; charset=ISO-8859-4</code>.
*
* <p>If obtaining a <code>PrintWriter</code>, this method should be
* called first.
*
*
* @param type a <code>String</code> specifying the MIME
* type of the content
*
* @see #getOutputStream
* @see #getWriter
*
*/
public native void setContentType(String type);
/***********************
* HttpServletResponse *
***********************/
/**
* Adds the specified cookie to the response. This method can be called
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -