View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.handler;
15  
16  import java.io.IOException;
17  
18  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.eclipse.jetty.http.HttpHeaders;
23  import org.eclipse.jetty.server.HandlerContainer;
24  import org.eclipse.jetty.server.HttpConnection;
25  import org.eclipse.jetty.server.Request;
26  import org.eclipse.jetty.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      String _expires;
42  
43      public MovedContextHandler()
44      {
45          _redirector=new Redirector();
46          setHandler(_redirector);
47          setAllowNullPathInfo(true);
48      }
49      
50      public MovedContextHandler(HandlerContainer parent, String contextPath, String newContextURL)
51      {
52          super(parent,contextPath);
53          _newContextURL=newContextURL;
54          _redirector=new Redirector();
55          setHandler(_redirector);
56      }
57  
58      public boolean isDiscardPathInfo()
59      {
60          return _discardPathInfo;
61      }
62  
63      public void setDiscardPathInfo(boolean discardPathInfo)
64      {
65          _discardPathInfo = discardPathInfo;
66      }
67  
68      public String getNewContextURL()
69      {
70          return _newContextURL;
71      }
72  
73      public void setNewContextURL(String newContextURL)
74      {
75          _newContextURL = newContextURL;
76      }
77  
78      public boolean isPermanent()
79      {
80          return _permanent;
81      }
82  
83      public void setPermanent(boolean permanent)
84      {
85          _permanent = permanent;
86      }
87  
88      public boolean isDiscardQuery()
89      {
90          return _discardQuery;
91      }
92  
93      public void setDiscardQuery(boolean discardQuery)
94      {
95          _discardQuery = discardQuery;
96      }
97      
98      private class Redirector extends AbstractHandler
99      {
100         public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
101         {
102             if (_newContextURL==null)
103                 return;
104             
105             Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest();
106             
107             String url = _newContextURL;
108             if (!_discardPathInfo && request.getPathInfo()!=null)
109                 url=URIUtil.addPaths(url, request.getPathInfo());
110             if (!_discardQuery && request.getQueryString()!=null)
111                 url+="?"+request.getQueryString();
112             
113             response.sendRedirect(url);
114 
115             String path=_newContextURL;
116             if (!_discardPathInfo && request.getPathInfo()!=null)
117                 path=URIUtil.addPaths(path, request.getPathInfo());
118             
119             StringBuilder location = URIUtil.hasScheme(path)?new StringBuilder():base_request.getRootURL();
120 
121             location.append(path);
122             if (!_discardQuery && request.getQueryString()!=null)
123             {
124                 location.append('?');
125                 location.append(request.getQueryString());
126             }
127             
128             response.setHeader(HttpHeaders.LOCATION,location.toString());
129 
130             if (_expires!=null)
131                 response.setHeader(HttpHeaders.EXPIRES,_expires);
132             
133             response.setStatus(_permanent?HttpServletResponse.SC_MOVED_PERMANENTLY:HttpServletResponse.SC_FOUND);
134             response.setContentLength(0);
135             base_request.setHandled(true);
136         }
137         
138     }
139 
140     /* ------------------------------------------------------------ */
141     /**
142      * @return the expires header value or null if no expires header
143      */
144     public String getExpires()
145     {
146         return _expires;
147     }
148 
149     /* ------------------------------------------------------------ */
150     /**
151      * @param expires the expires header value or null if no expires header
152      */
153     public void setExpires(String expires)
154     {
155         _expires = expires;
156     }
157 
158 }