/*
* $Id: AnyResponse.java,v 1.22 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.net;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.script.Context;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
///
/// @class Response
/// The HTTP response to the client terminal/browser.
///
/**
* class AnyResponse
*
* @author: Jani Lehtim�ki
*/
public class AnyResponse extends AnyAbstractClass
{
private HttpServletResponse _response;
public AnyResponse(HttpServletResponse response)
{
_response = response;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public Object toObject()
{
return _response;
}
/// @method encode
/// Encodes and returns HttpSession parameters to given url.
/// @synopsis string encode(string url)
/// @param url
/// @return encoded url
public static final Object[] p_encode = { "url" };
public Any m_encode(String url)
{
return Any.create(_response.encodeUrl(url));
}
/// @method setContentType
/// Sets the content type.
/// @synopsis void setContentType(string contentType)
/// @param contentType Mime type of content
public static final Object[] p_setContentType = { "contentType" };
public Any m_setContentType(String contentType)
{
_response.setContentType(contentType);
return this;
}
/// @method setContentLength
/// Sets the content length.
/// @synopsis void setContentLength(int contentLength)
/// @param contentLength Length of content
public static final Object[] p_setContentLength = { "contentLength" };
public Any m_setContentLength(int contentLength)
{
_response.setContentLength(contentLength);
return this;
}
/// @method setHeader
/// Sets the value of given response header.
/// @synopsis void setHeader(string name, string stringvalue)
/// @synopsis void setHeader(string name, int intvalue)
/// @param name name
/// @param stringvalue string value
/// @param intvalue int value value
public static final Object[] p_setHeader = { "name", "value" };
public Any m_setHeader(String name, Any value)
{
if (value.isInt()) {
_response.setIntHeader(name, value.toInt());
} else {
_response.setHeader(name, value.toString());
}
return this;
}
/// @method sendError
/// Sends an errors response to client using specified status code
/// and message. After this method is called response is considered committed
/// and should be written to.
/// @synopsis void sendError(int code)
/// @synopsis void sendError(int code, string message)
/// @param code HTTP error response code
/// @param message Optional message, if not given, default
/// messages are used.
/// @throws IOError If an IO error occurs
public static final Object[] p_sendError = { null, "code", "*message", null };
public Any m_sendError(Context context, int code, String msg)
{
if (msg == null) {
msg = anvil.server.MimeTypes.getErrorText(code);
}
try {
_response.sendError(code, msg);
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method setStatus
/// Sets the status of response when there is not error.
/// @synopsis void setStatus(int code)
/// @param code HTTP response code
public static final Object[] p_setStatus = { "code" };
public Any m_setStatus(int code)
{
_response.setStatus(code);
return this;
}
/// @method addCookie
/// ADds the specified cookie to response.
/// It can be calle multiple times to set more than
/// one cookie.
/// @synopsis void addCookie(Cookie cookie)
public static final Object[] p_addCookie = { null, "cookie" };
public Any m_addCookie(Context context, Any cookie_)
{
if (cookie_ instanceof AnyCookie) {
Cookie cookie = (Cookie)cookie_.toObject();
_response.addCookie(cookie);
return this;
}
throw context.BadParameter("Expected Cookie");
}
/// @method sendRedirect
/// Creates temporary redirection (302) to given url.
/// @synopsis void sendRedirect(string url)
/// @throws IOError If an IO error occurs
public static final Object[] p_sendRedirect = { null, "url" };
public Any m_sendRedirect(Context context, String url)
{
try {
_response.sendRedirect(url);
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method flush
/// Flushes any pending data to client, including status code and headers
/// @synopsis void flush()
/// @throws IOError If an IO error occurs
public Any m_flush(Context context)
{
try {
_response.flushBuffer();
return this;
} catch (IOException e) {
throw context.exception(e);
}
}
/// @method getOutputStream
/// Returns the output stream suitable for writing data to the response.
/// @synopsis OutputStream getOutputStream()
/// @throws IOError If an IO error occurs
public Any m_getOutputStream(Context context)
{
try {
return new anvil.core.io.AnyOutputStream(_response.getOutputStream());
} catch (IOException e) {
throw context.exception(e);
}
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Response", AnyResponse.class,
//DOC{{
""+
"\n" +
" @class Response\n" +
" The HTTP response to the client terminal/browser.\n" +
"\n" +
" @method encode\n" +
" Encodes and returns HttpSession parameters to given url.\n" +
" @synopsis string encode(string url) \n" +
" @param url\n" +
" @return encoded url\n" +
" @method setContentType\n" +
" Sets the content type.\n" +
" @synopsis void setContentType(string contentType)\n" +
" @param contentType Mime type of content\n" +
" @method setContentLength\n" +
" Sets the content length.\n" +
" @synopsis void setContentLength(int contentLength)\n" +
" @param contentLength Length of content\n" +
" @method setHeader\n" +
" Sets the value of given response header.\n" +
" @synopsis void setHeader(string name, string stringvalue)\n" +
" @synopsis void setHeader(string name, int intvalue)\n" +
" @param name name\n" +
" @param stringvalue string value\n" +
" @param intvalue int value value\n" +
" @method sendError\n" +
" Sends an errors response to client using specified status code\n" +
" and message. After this method is called response is considered committed\n" +
" and should be written to.\n" +
" @synopsis void sendError(int code)\n" +
" @synopsis void sendError(int code, string message)\n" +
" @param code HTTP error response code\n" +
" @param message Optional message, if not given, default\n" +
" messages are used.\n" +
" @throws IOError If an IO error occurs\n" +
" @method setStatus\n" +
" Sets the status of response when there is not error.\n" +
" @synopsis void setStatus(int code)\n" +
" @param code HTTP response code\n" +
" @method addCookie\n" +
" ADds the specified cookie to response.\n" +
" It can be calle multiple times to set more than\n" +
" one cookie.\n" +
" @synopsis void addCookie(Cookie cookie)\n" +
" @method sendRedirect\n" +
" Creates temporary redirection (302) to given url.\n" +
" @synopsis void sendRedirect(string url)\n" +
" @throws IOError If an IO error occurs\n" +
" @method flush\n" +
" Flushes any pending data to client, including status code and headers\n" +
" @synopsis void flush()\n" +
" @throws IOError If an IO error occurs\n" +
" @method getOutputStream\n" +
" Returns the output stream suitable for writing data to the response.\n" +
" @synopsis OutputStream getOutputStream()\n" +
" @throws IOError If an IO error occurs\n"
//}}DOC
);
static {
NetModule.class.getName();
}
}