View Javadoc

    1   //========================================================================
    2   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.jetty.handler;
    16
    17  import java.io.IOException;
    18
    19  import javax.servlet.ServletException;
    20  import javax.servlet.http.HttpServletRequest;
    21  import javax.servlet.http.HttpServletResponse;
    22
    23  import org.mortbay.jetty.HandlerContainer;
    24  import org.mortbay.jetty.HttpConnection;
    25  import org.mortbay.jetty.Request;
    26  import org.mortbay.util.URIUtil;
    27
    28  /* ------------------------------------------------------------ */
    29  /** Moved ContextHandler.
    30   * This context can be used to replace a context that has changed
    31   * location.  Requests are redirected (either to a fixed URL or to a
    32   * new context base).
    33   */
    34  public class MovedContextHandler extends ContextHandler
    35  {
    36      String _newContextURL;
    37      boolean _discardPathInfo;
    38      boolean _discardQuery;
    39      boolean _permanent;
    40      Redirector _redirector;
    41
    42      public MovedContextHandler()
    43      {
    44          _redirector=new Redirector();
    45          addHandler(_redirector);
    46      }
    47
    48      public MovedContextHandler(HandlerContainer parent, String contextPath, String newContextURL)
    49      {
    50          super(parent,contextPath);
    51          _newContextURL=newContextURL;
    52          _redirector=new Redirector();
    53          addHandler(_redirector);
    54      }
    55
    56      public boolean isDiscardPathInfo()
    57      {
    58          return _discardPathInfo;
    59      }
    60
    61      public void setDiscardPathInfo(boolean discardPathInfo)
    62      {
    63          _discardPathInfo = discardPathInfo;
    64      }
    65
    66      public String getNewContextURL()
    67      {
    68          return _newContextURL;
    69      }
    70
    71      public void setNewContextURL(String newContextURL)
    72      {
    73          _newContextURL = newContextURL;
    74      }
    75
    76      public boolean isPermanent()
    77      {
    78          return _permanent;
    79      }
    80
    81      public void setPermanent(boolean permanent)
    82      {
    83          _permanent = permanent;
    84      }
    85
    86      public boolean isDiscardQuery()
    87      {
    88          return _discardQuery;
    89      }
    90
    91      public void setDiscardQuery(boolean discardQuery)
    92      {
    93          _discardQuery = discardQuery;
    94      }
    95
    96      private class Redirector extends AbstractHandler
    97      {
    98          public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
    99          {
    100             if (_newContextURL==null)
    101                 return;
    102
    103             Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest();
    104
    105             String url = _newContextURL;
    106             if (!_discardPathInfo && request.getPathInfo()!=null)
    107                 url=URIUtil.addPaths(url, request.getPathInfo());
    108             if (!_discardQuery && request.getQueryString()!=null)
    109                 url+="?"+request.getQueryString();
    110
    111             response.sendRedirect(url);
    112             if (_permanent)
    113                 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    114
    115             base_request.setHandled(true);
    116         }
    117
    118     }
    119
    120 }