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      final Redirector _redirector;
37      String _newContextURL;
38      boolean _discardPathInfo;
39      boolean _discardQuery;
40      boolean _permanent;
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, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
101         {
102             if (_newContextURL==null)
103                 return;
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 
113             String path=_newContextURL;
114             if (!_discardPathInfo && request.getPathInfo()!=null)
115                 path=URIUtil.addPaths(path, request.getPathInfo());
116             
117             StringBuilder location = URIUtil.hasScheme(path)?new StringBuilder():baseRequest.getRootURL();
118 
119             location.append(path);
120             if (!_discardQuery && request.getQueryString()!=null)
121             {
122                 location.append('?');
123                 location.append(request.getQueryString());
124             }
125             
126             response.setHeader(HttpHeaders.LOCATION,location.toString());
127 
128             if (_expires!=null)
129                 response.setHeader(HttpHeaders.EXPIRES,_expires);
130             
131             response.setStatus(_permanent?HttpServletResponse.SC_MOVED_PERMANENTLY:HttpServletResponse.SC_FOUND);
132             response.setContentLength(0);
133             baseRequest.setHandled(true);
134         }
135         
136     }
137 
138     /* ------------------------------------------------------------ */
139     /**
140      * @return the expires header value or null if no expires header
141      */
142     public String getExpires()
143     {
144         return _expires;
145     }
146 
147     /* ------------------------------------------------------------ */
148     /**
149      * @param expires the expires header value or null if no expires header
150      */
151     public void setExpires(String expires)
152     {
153         _expires = expires;
154     }
155 
156 }