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 path=_newContextURL;
105             if (!_discardPathInfo && request.getPathInfo()!=null)
106                 path=URIUtil.addPaths(path, request.getPathInfo());
107             
108             StringBuilder location = URIUtil.hasScheme(path)?new StringBuilder():baseRequest.getRootURL();
109 
110             location.append(path);
111             if (!_discardQuery && request.getQueryString()!=null)
112             {
113                 location.append('?');
114                 String q=request.getQueryString();
115                 q=q.replaceAll("\r\n?&=","!");
116                 location.append(q);
117             }
118             
119             response.setHeader(HttpHeaders.LOCATION,location.toString());
120 
121             if (_expires!=null)
122                 response.setHeader(HttpHeaders.EXPIRES,_expires);
123             
124             response.setStatus(_permanent?HttpServletResponse.SC_MOVED_PERMANENTLY:HttpServletResponse.SC_FOUND);
125             response.setContentLength(0);
126             baseRequest.setHandled(true);
127         }
128         
129     }
130 
131     /* ------------------------------------------------------------ */
132     /**
133      * @return the expires header value or null if no expires header
134      */
135     public String getExpires()
136     {
137         return _expires;
138     }
139 
140     /* ------------------------------------------------------------ */
141     /**
142      * @param expires the expires header value or null if no expires header
143      */
144     public void setExpires(String expires)
145     {
146         _expires = expires;
147     }
148 
149 }