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.Request;
25  import org.eclipse.jetty.util.URIUtil;
26  
27  /* ------------------------------------------------------------ */
28  /** Moved ContextHandler.
29   * This context can be used to replace a context that has changed
30   * location.  Requests are redirected (either to a fixed URL or to a
31   * new context base). 
32   */
33  public class MovedContextHandler extends ContextHandler
34  {
35      final Redirector _redirector;
36      String _newContextURL;
37      boolean _discardPathInfo;
38      boolean _discardQuery;
39      boolean _permanent;
40      String _expires;
41  
42      public MovedContextHandler()
43      {
44          _redirector=new Redirector();
45          setHandler(_redirector);
46          setAllowNullPathInfo(true);
47      }
48      
49      public MovedContextHandler(HandlerContainer parent, String contextPath, String newContextURL)
50      {
51          super(parent,contextPath);
52          _newContextURL=newContextURL;
53          _redirector=new Redirector();
54          setHandler(_redirector);
55      }
56  
57      public boolean isDiscardPathInfo()
58      {
59          return _discardPathInfo;
60      }
61  
62      public void setDiscardPathInfo(boolean discardPathInfo)
63      {
64          _discardPathInfo = discardPathInfo;
65      }
66  
67      public String getNewContextURL()
68      {
69          return _newContextURL;
70      }
71  
72      public void setNewContextURL(String newContextURL)
73      {
74          _newContextURL = newContextURL;
75      }
76  
77      public boolean isPermanent()
78      {
79          return _permanent;
80      }
81  
82      public void setPermanent(boolean permanent)
83      {
84          _permanent = permanent;
85      }
86  
87      public boolean isDiscardQuery()
88      {
89          return _discardQuery;
90      }
91  
92      public void setDiscardQuery(boolean discardQuery)
93      {
94          _discardQuery = discardQuery;
95      }
96      
97      private class Redirector extends AbstractHandler
98      {
99          public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
100         {
101             if (_newContextURL==null)
102                 return;
103                         
104             String url = _newContextURL;
105             if (!_discardPathInfo && request.getPathInfo()!=null)
106                 url=URIUtil.addPaths(url, request.getPathInfo());
107             if (!_discardQuery && request.getQueryString()!=null)
108                 url+="?"+request.getQueryString();
109 
110             String path=_newContextURL;
111             if (!_discardPathInfo && request.getPathInfo()!=null)
112                 path=URIUtil.addPaths(path, request.getPathInfo());
113             
114             StringBuilder location = URIUtil.hasScheme(path)?new StringBuilder():baseRequest.getRootURL();
115 
116             location.append(path);
117             if (!_discardQuery && request.getQueryString()!=null)
118             {
119                 location.append('?');
120                 location.append(request.getQueryString());
121             }
122             
123             response.setHeader(HttpHeaders.LOCATION,location.toString());
124 
125             if (_expires!=null)
126                 response.setHeader(HttpHeaders.EXPIRES,_expires);
127             
128             response.setStatus(_permanent?HttpServletResponse.SC_MOVED_PERMANENTLY:HttpServletResponse.SC_FOUND);
129             response.setContentLength(0);
130             baseRequest.setHandled(true);
131         }
132         
133     }
134 
135     /* ------------------------------------------------------------ */
136     /**
137      * @return the expires header value or null if no expires header
138      */
139     public String getExpires()
140     {
141         return _expires;
142     }
143 
144     /* ------------------------------------------------------------ */
145     /**
146      * @param expires the expires header value or null if no expires header
147      */
148     public void setExpires(String expires)
149     {
150         _expires = expires;
151     }
152 
153 }