001package votorola.g.web.wic; // Copyright 2010, 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 org.apache.wicket.request.*;
004import org.apache.wicket.request.RequestHandlerStack.ReplaceHandlerException;
005import org.apache.wicket.request.http.WebResponse;
006
007
008/** An alternative redirect exception that supports not only HTTP 301 (permanent) and 302
009  * (temporary) redirects, but also 303 (see other).
010  *
011  *     @see org.apache.wicket.request.flow.RedirectToUrlException
012  *     @see <a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html'
013  *       >Status Code Definitions</a>
014  */
015public final class RedirectException extends ReplaceHandlerException
016{
017
018
019    /** Constructs a RedirectException.
020      *
021      *     @param _location the redirection target, to be used as the HTTP Location field
022      *       in the response.
023      *     @param _statusCode the HTTP status code.  Only 301 and 303 are tested.
024      */
025    public RedirectException( String _location, int _statusCode )
026    {
027                super( new RequestHandler(_location,_statusCode), true );
028    }
029
030
031
032//// P r i v a t e ///////////////////////////////////////////////////////////////////////
033
034
035    private static final class RequestHandler implements IRequestHandler
036    {
037        // cf. org.apache.wicket.request.http.handler.RedirectRequestHandler
038
039        private RequestHandler( String _location, int _statusCode )
040        {
041            location = _location;
042            statusCode = _statusCode;
043        }
044
045
046        private final String location;
047
048        private final int statusCode;
049
050
051       // - I - R e q u e s t - H a n d l e r --------------------------------------------
052
053
054        public void detach( IRequestCycle requestCycle ) {}
055
056
057        public void respond( final IRequestCycle requestCycle )
058        {
059            final WebResponse response = (WebResponse)requestCycle.getResponse();
060            if( statusCode == 302 ) response.sendRedirect( location );
061            else
062            {
063                        response.setStatus( statusCode );
064                response.setHeader( "Location", location );
065            }
066        }
067
068
069    }
070
071
072}