001package votorola.g.web.wic; // Copyright 2009, 2012, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import javax.servlet.http.*;
004import org.apache.wicket.request.http.*;
005import votorola.g.lang.*;
006
007
008/** Web response utilities.
009  *
010  * <p>Note: WebResponse#{@linkplain WebResponse#enableCaching enableCaching} is
011  * documented to enable caching in 1.5.4, but it does not.  You must also override
012  * WebPage.{@linkplain org.apache.wicket.markup.html.WebPage#setHeaders setHeaders} or it
013  * clobbers the headers by calling disableCaching(). See <a
014  * href='https://issues.apache.org/jira/browse/WICKET-4357'>bug 4357</a>.</p>
015  */
016public @ThreadSafe final class WebResponseX
017{
018
019
020    private WebResponseX() {}
021
022
023
024    /** Adds a cookie to the response, working around a known bug in WebResponse subclass
025      * BufferedWebResponse.
026      *
027      *     @see org.apache.wicket.protocol.http.BufferedWebResponse#addCookie(Cookie)
028      */
029    public static void addCookie( final WebResponse response, final Cookie cookie )
030    {
031     // response.addCookie( cookie ); // uncomment here and comment below to TEST
032     /// If that's a BufferedWebResponse, then it fails under certain conditions: (1) when
033     /// called on the last of three 302 redirects during OpenID login; and (2) on single
034     /// redirect immediately after container startup, though it later recovers.  See:
035     /// https://issues.apache.org/jira/browse/WICKET-4358
036     /// So bypass the buffered response:
037        ((HttpServletResponse)response.getContainerResponse()).addCookie( cookie );
038    }
039
040
041
042    /** Clears a cookie in the response, working around a known bug in WebResponse
043      * subclass BufferedWebResponse.  A cookie instance obtained from the HTTP request
044      * will have values for domain, path and so forth that are initially null.  Ensure
045      * they are corrected to match the values under which the cookie was originally
046      * stored before calling this method, or it will probably fail.
047      *
048      *     @param cookie the cookie to clear, or null to clear nothing.
049      *
050      *     @see org.apache.wicket.protocol.http.BufferedWebResponse#clearCookie(Cookie)
051      */
052    public static void clearCookie( final WebResponse response, final Cookie cookie )
053    {
054        if( cookie == null ) return;
055
056                cookie.setMaxAge( 0/*delete*/ );
057                cookie.setValue( null );
058                addCookie( response, cookie );
059    }
060
061
062
063}