ResponseContext.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 11k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms,
  6.  * with or without modification, are permitted provided
  7.  * that the following conditions are met:
  8.  *
  9.  * 1) Redistributions of source code must retain the above
  10.  * copyright notice, this list of conditions and the
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the
  13.  * above copyright notice, this list of conditions and
  14.  * the following disclaimer in the documentation and/or
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor
  17.  * the names of its contributors may be used to endorse
  18.  * or promote products derived from this software without
  19.  * specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  *
  39.  * The JForum Project
  40.  * http://www.jforum.net
  41.  */
  42. package net.jforum.context;
  43. import javax.servlet.ServletOutputStream;
  44. import javax.servlet.http.Cookie;
  45. import java.io.IOException;
  46. import java.io.PrintWriter;
  47. /**
  48.  * User: SergeMaslyukov Date: 20.08.2006 Time: 18:48:56 <p/> $Id: WebContextResponse.java,v 1.1
  49.  * 2006/08/20 15:30:30 sergemaslyukov Exp $
  50.  */
  51. public interface ResponseContext
  52. {
  53. /**
  54.  * Sets the length of the content body in the response In HTTP servlets, this method sets the
  55.  * HTTP Content-Length header.
  56.  * 
  57.  * @param len an integer specifying the length of the content being returned to the client; 
  58.  * sets the Content-Length header
  59.  */
  60. public void setContentLength(int len);
  61. /**
  62.  * Returns a boolean indicating whether the named response header has already been set.
  63.  * 
  64.  * @param name the header name
  65.  * @return <code>true</code> if the named response header has already been set;
  66.  * <code>false</code> otherwise
  67.  */
  68. public boolean containsHeader(String name);
  69. /**
  70.  * Sets a response header with the given name and value. If the header had already been set, the
  71.  * new value overwrites the previous one. The <code>containsHeader</code> method can be used
  72.  * to test for the presence of a header before setting its value.
  73.  * 
  74.  * @param name the name of the header
  75.  * @param value the header value If it contains octet string, it should be encoded 
  76.  * according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)
  77.  * 
  78.  * @see #containsHeader
  79.  * @see #addHeader
  80.  */
  81. public void setHeader(String name, String value);
  82. /**
  83.  * Adds the specified cookie to the response. This method can be called multiple times to set
  84.  * more than one cookie.
  85.  * 
  86.  * @param cookie the Cookie to return to the client
  87.  */
  88. public void addCookie(Cookie cookie);
  89. /**
  90.  * Encodes the specified URL for use in the <code>sendRedirect</code> method or, if encoding
  91.  * is not needed, returns the URL unchanged. The implementation of this method includes the
  92.  * logic to determine whether the session ID needs to be encoded in the URL. Because the rules
  93.  * for making this determination can differ from those used to decide whether to encode a normal
  94.  * link, this method is separated from the <code>encodeURL</code> method.
  95.  * 
  96.  * <p>
  97.  * All URLs sent to the <code>HttpServletResponse.sendRedirect</code> method should be run
  98.  * through this method. Otherwise, URL rewriting cannot be used with browsers which do not
  99.  * support cookies.
  100.  * 
  101.  * @param url the url to be encoded.
  102.  * @return the encoded URL if encoding is needed; the unchanged URL otherwise.
  103.  * 
  104.  * @see #sendRedirect
  105.  * @see #encodeUrl
  106.  */
  107. public String encodeRedirectURL(String url);
  108. /**
  109.  * Returns the name of the character encoding (MIME charset) used for the body sent in this
  110.  * response. The character encoding may have been specified explicitly using the
  111.  * {@link #setCharacterEncoding} or {@link #setContentType} methods, or implicitly using the
  112.  * {@link #setLocale} method. Explicit specifications take precedence over implicit
  113.  * specifications. Calls made to these methods after <code>getWriter</code> has been called or
  114.  * after the response has been committed have no effect on the character encoding. If no
  115.  * character encoding has been specified, <code>ISO-8859-1</code> is returned.
  116.  * <p>
  117.  * See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) for more information about character
  118.  * encoding and MIME.
  119.  * 
  120.  * @return a <code>String</code> specifying the name of the character encoding, for example,
  121.  * <code>UTF-8</code>
  122.  * 
  123.  */
  124. public String getCharacterEncoding();
  125. /**
  126.  * Sends a temporary redirect response to the client using the specified redirect location URL.
  127.  * This method can accept relative URLs; the servlet container must convert the relative URL to
  128.  * an absolute URL before sending the response to the client. If the location is relative
  129.  * without a leading '/' the container interprets it as relative to the current request URI. If
  130.  * the location is relative with a leading '/' the container interprets it as relative to the
  131.  * servlet container root.
  132.  * 
  133.  * <p>
  134.  * If the response has already been committed, this method throws an IllegalStateException.
  135.  * After using this method, the response should be considered to be committed and should not be
  136.  * written to.
  137.  * 
  138.  * @param location the redirect location URL
  139.  * @exception IOException If an input or output exception occurs
  140.  * @exception IllegalStateException If the response was committed or if a partial 
  141.  * URL is given and cannot be converted into a valid URL
  142.  */
  143. public void sendRedirect(String location) throws IOException;
  144. /**
  145.  * Returns a {@link javax.servlet.ServletOutputStream} suitable for writing binary data in the
  146.  * response. The servlet container does not encode the binary data.
  147.  * 
  148.  * <p>
  149.  * Calling flush() on the ServletOutputStream commits the response.
  150.  * 
  151.  * Either this method or {@link #getWriter} may be called to write the body, not both.
  152.  * 
  153.  * @return a {@link javax.servlet.ServletOutputStream} for writing binary data
  154.  * @exception IllegalStateException if the <code>getWriter</code> method has 
  155.  * been called on this response
  156.  * @exception IOException if an input or output exception occurred
  157.  * @see #getWriter
  158.  */
  159. public ServletOutputStream getOutputStream() throws IOException;
  160. /**
  161.  * Returns a <code>PrintWriter</code> object that can send character text to the client. The
  162.  * <code>PrintWriter</code> uses the character encoding returned by
  163.  * {@link #getCharacterEncoding}. If the response's character encoding has not been specified
  164.  * as described in <code>getCharacterEncoding</code> (i.e., the method just returns the
  165.  * default value <code>ISO-8859-1</code>), <code>getWriter</code> updates it to
  166.  * <code>ISO-8859-1</code>.
  167.  * <p>
  168.  * Calling flush() on the <code>PrintWriter</code> commits the response.
  169.  * <p>
  170.  * Either this method or {@link #getOutputStream} may be called to write the body, not both.
  171.  * 
  172.  * @return a <code>PrintWriter</code> object that can return character data to the client
  173.  * 
  174.  * @exception java.io.UnsupportedEncodingException if the character encoding 
  175.  * returned by <code>getCharacterEncoding</code> cannot be used
  176.  * @exception IllegalStateException if the <code>getOutputStream</code> method has 
  177.  * already been called for this response object
  178.  * @exception IOException if an input or output exception occurred
  179.  * 
  180.  * @see #getOutputStream
  181.  * @see #setCharacterEncoding
  182.  * 
  183.  */
  184. public PrintWriter getWriter() throws IOException;
  185. /**
  186.  * Sets the content type of the response being sent to the client, if the response has not been
  187.  * committed yet. The given content type may include a character encoding specification, for
  188.  * example, <code>text/html;charset=UTF-8</code>. The response's character encoding is only
  189.  * set from the given content type if this method is called before <code>getWriter</code> is
  190.  * called.
  191.  * <p>
  192.  * This method may be called repeatedly to change content type and character encoding. This
  193.  * method has no effect if called after the response has been committed. It does not set the
  194.  * response's character encoding if it is called after <code>getWriter</code> has been called
  195.  * or after the response has been committed.
  196.  * <p>
  197.  * Containers must communicate the content type and the character encoding used for the servlet
  198.  * response's writer to the client if the protocol provides a way for doing so. In the case of
  199.  * HTTP, the <code>Content-Type</code> header is used.
  200.  * 
  201.  * @param type a <code>String</code> specifying the MIME type of the content
  202.  * 
  203.  * @see #setLocale
  204.  * @see #setCharacterEncoding
  205.  * @see #getOutputStream
  206.  * @see #getWriter
  207.  */
  208. public void setContentType(String type);
  209. /**
  210.  * Encodes the specified URL by including the session ID in it, or, if encoding is not needed,
  211.  * returns the URL unchanged. The implementation of this method includes the logic to determine
  212.  * whether the session ID needs to be encoded in the URL. For example, if the browser supports
  213.  * cookies, or session tracking is turned off, URL encoding is unnecessary.
  214.  * 
  215.  * <p>
  216.  * For robust session tracking, all URLs emitted by a servlet should be run through this method.
  217.  * Otherwise, URL rewriting cannot be used with browsers which do not support cookies.
  218.  * 
  219.  * @param url the url to be encoded.
  220.  * @return the encoded URL if encoding is needed; the unchanged URL otherwise.
  221.  */
  222. public String encodeURL(String url);
  223. /**
  224.  * Adds a response header with the given name and value. This method allows response headers to
  225.  * have multiple values.
  226.  * 
  227.  * @param name the name of the header
  228.  * @param value the additional header value If it contains octet string, 
  229.  * it should be encoded according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)
  230.  */
  231. public void addHeader(String name, String value);
  232. /**
  233.  * Sends an error response to the client using the specified status code and clearing the
  234.  * buffer.
  235.  * <p>
  236.  * If the response has already been committed, this method throws an IllegalStateException.
  237.  * After using this method, the response should be considered to be committed and should not be
  238.  * written to.
  239.  * 
  240.  * @param sc the error status code
  241.  * @exception java.io.IOException If an input or output exception occurs
  242.  * @exception IllegalStateException If the response was committed before this method call
  243.  */
  244. public void sendError(int sc) throws IOException;
  245. }