org.apache.wicket.protocol.http
Class WebResponse

java.lang.Object
  extended by org.apache.wicket.Response
      extended by org.apache.wicket.protocol.http.WebResponse
Direct Known Subclasses:
BufferedWebResponse

public class WebResponse
extends Response

Implements responses over the HTTP protocol by holding an underlying HttpServletResponse object and providing convenience methods for using that object. Convenience methods include methods which: add a cookie, close the stream, encode a URL, redirect a request to another resource, determine if a redirect has been issued, set the content type, set the locale and, most importantly, write a String to the response output.

Author:
Jonathan Locke

Field Summary
protected  boolean redirect
          True if response is a redirect.
 
Constructor Summary
WebResponse()
          Constructor for testing harness.
WebResponse(javax.servlet.http.HttpServletResponse httpServletResponse)
          Package private constructor.
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
          Add a cookie to the web response
 void clearCookie(javax.servlet.http.Cookie cookie)
          Convenience method for clearing a cookie.
 void close()
          Closes response output.
protected  void configureAjaxRedirect()
          Additional header configs for ajax redirects
 void disableCaching()
          Make this response non-cacheable
 CharSequence encodeURL(CharSequence url)
          Returns the given url encoded.
 javax.servlet.http.HttpServletResponse getHttpServletResponse()
          Gets the wrapped http servlet response object.
 OutputStream getOutputStream()
           
 boolean isAjax()
          Is the request, which matches this response an ajax request.
 boolean isRedirect()
          Whether this response is going to redirect the user agent.
 void redirect(String url)
          CLIENTS SHOULD NEVER CALL THIS METHOD FOR DAY TO DAY USE!
protected  void sendRedirect(String url)
          Called when Wicket wants to send a redirect to the servlet response.
 void setAjax(boolean ajax)
          Set that the request which matches this response is an ajax request.
 void setAttachmentHeader(String filename)
          Convenience method for setting the content-disposition:attachment header.
 void setContentLength(long length)
          Set the content length on the response, if appropriate in the subclass.
 void setContentType(String mimeType)
          Set the content type on the response.
 void setDateHeader(String header, long date)
          Set a header to the date value in the servlet response stream.
 void setHeader(String header, String value)
          Set a header to the string value in the servlet response stream.
 void setLastModifiedTime(Time time)
          Set the contents last modified time, if appropriate in the subclass.
 void setLocale(Locale locale)
          Output stream encoding.
 void write(AppendingStringBuffer asb)
          Writes AppendingStringBuffer to response output.
 void write(CharSequence string)
          Writes string to response output.
 
Methods inherited from class org.apache.wicket.Response
detectContentType, filter, getCharacterEncoding, println, reset, setCharacterEncoding, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

redirect

protected boolean redirect
True if response is a redirect.

Constructor Detail

WebResponse

public WebResponse()
Constructor for testing harness.


WebResponse

public WebResponse(javax.servlet.http.HttpServletResponse httpServletResponse)
Package private constructor.

Parameters:
httpServletResponse - The servlet response object
Method Detail

addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Add a cookie to the web response

Parameters:
cookie -

clearCookie

public void clearCookie(javax.servlet.http.Cookie cookie)
Convenience method for clearing a cookie.

Parameters:
cookie - The cookie to set
See Also:
addCookie(Cookie)

close

public void close()
Closes response output.

Overrides:
close in class Response

encodeURL

public CharSequence encodeURL(CharSequence url)
Returns the given url encoded.

Overrides:
encodeURL in class Response
Parameters:
url - The URL to encode
Returns:
The encoded url

getHttpServletResponse

public final javax.servlet.http.HttpServletResponse getHttpServletResponse()
Gets the wrapped http servlet response object.

Returns:
The wrapped http servlet response object

getOutputStream

public OutputStream getOutputStream()
Specified by:
getOutputStream in class Response
Returns:
The output stream for this response
See Also:
Response.getOutputStream()

isRedirect

public final boolean isRedirect()
Whether this response is going to redirect the user agent.

Overrides:
isRedirect in class Response
Returns:
True if this response is going to redirect the user agent

redirect

public void redirect(String url)
CLIENTS SHOULD NEVER CALL THIS METHOD FOR DAY TO DAY USE!

Redirects to the given url. Implementations should encode the URL to make sure cookie-less operation is supported in case clients forgot.

Overrides:
redirect in class Response
Parameters:
url - The URL to redirect to

sendRedirect

protected void sendRedirect(String url)
                     throws IOException
Called when Wicket wants to send a redirect to the servlet response. By default, WebResponse just calls httpServletResponse.sendRedirect(url). However, certain servlet containers do not treat relative URL redirects correctly (i.e. WebSphere). If you are using one of these containers, you can override this method and convert the relative URL to an absolute URL before sending the redirect to the servlet container. Example of how to fix this for your buggy container (in your application):
 @Override
 protected WebResponse newWebResponse(HttpServletResponse servletResponse)
 {
        return new WebResponse(servletResponse)
        {
                @Override
                public void sendRedirect(String url) throws IOException
                {
                        String reqUrl = ((WebRequest)RequestCycle.get().getRequest()).getHttpServletRequest()
                                .getRequestURI();
                        String absUrl = RequestUtils.toAbsolutePath(reqUrl, url);
                        getHttpServletResponse().sendRedirect(absUrl);
                }
        };
 }
 

Parameters:
url - the URL to redirect to
Throws:
IOException

configureAjaxRedirect

protected void configureAjaxRedirect()
Additional header configs for ajax redirects


setContentType

public final void setContentType(String mimeType)
Set the content type on the response.

Overrides:
setContentType in class Response
Parameters:
mimeType - The mime type

setContentLength

public void setContentLength(long length)
Description copied from class: Response
Set the content length on the response, if appropriate in the subclass. This default implementation does nothing.

Overrides:
setContentLength in class Response
Parameters:
length - The length of the content
See Also:
Response.setContentLength(long)

setLastModifiedTime

public void setLastModifiedTime(Time time)
Description copied from class: Response
Set the contents last modified time, if appropriate in the subclass. This default implementation does nothing.

Overrides:
setLastModifiedTime in class Response
Parameters:
time - The time object
See Also:
Response.setLastModifiedTime(org.apache.wicket.util.time.Time)

setLocale

public final void setLocale(Locale locale)
Output stream encoding. If the deployment descriptor contains a locale-encoding-mapping-list element, and that element provides a mapping for the given locale, that mapping is used. Otherwise, the mapping from locale to character encoding is container dependent. Default is ISO-8859-1.

Overrides:
setLocale in class Response
Parameters:
locale - The locale use for mapping the character encoding
See Also:
ServletResponse.setLocale(java.util.Locale)

write

public void write(CharSequence string)
Writes string to response output.

Specified by:
write in class Response
Parameters:
string - The string to write

write

public void write(AppendingStringBuffer asb)
Writes AppendingStringBuffer to response output.

Parameters:
asb - The AppendingStringBuffer to write to the stream

setDateHeader

public void setDateHeader(String header,
                          long date)
Set a header to the date value in the servlet response stream.

Parameters:
header -
date -

setHeader

public void setHeader(String header,
                      String value)
Set a header to the string value in the servlet response stream.

Parameters:
header -
value -

setAttachmentHeader

public void setAttachmentHeader(String filename)
Convenience method for setting the content-disposition:attachment header. This header is used if the response should prompt the user to download it as a file instead of opening in a browser.

Parameters:
filename - file name of the attachment

isAjax

public boolean isAjax()
Is the request, which matches this response an ajax request.

Returns:
True if the request is an ajax request.

setAjax

public void setAjax(boolean ajax)
Set that the request which matches this response is an ajax request.

Parameters:
ajax - True if the request is an ajax request.

disableCaching

public void disableCaching()
Make this response non-cacheable



Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.