2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.handler;
21 import java.io.IOException;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
27 import org.eclipse.jetty.http.HttpHeader;
28 import org.eclipse.jetty.server.HandlerContainer;
29 import org.eclipse.jetty.server.Request;
30 import org.eclipse.jetty.util.URIUtil;
32 /* ------------------------------------------------------------ */
33 /** Moved ContextHandler.
34 * This context can be used to replace a context that has changed
35 * location. Requests are redirected (either to a fixed URL or to a
38 public class MovedContextHandler extends ContextHandler
40 final Redirector _redirector;
41 String _newContextURL;
42 boolean _discardPathInfo;
43 boolean _discardQuery;
47 public MovedContextHandler()
49 _redirector=new Redirector();
50 setHandler(_redirector);
51 setAllowNullPathInfo(true);
54 public MovedContextHandler(HandlerContainer parent, String contextPath, String newContextURL)
56 super(parent,contextPath);
57 _newContextURL=newContextURL;
58 _redirector=new Redirector();
59 setHandler(_redirector);
62 public boolean isDiscardPathInfo()
64 return _discardPathInfo;
67 public void setDiscardPathInfo(boolean discardPathInfo)
69 _discardPathInfo = discardPathInfo;
72 public String getNewContextURL()
74 return _newContextURL;
77 public void setNewContextURL(String newContextURL)
79 _newContextURL = newContextURL;
82 public boolean isPermanent()
87 public void setPermanent(boolean permanent)
89 _permanent = permanent;
92 public boolean isDiscardQuery()
97 public void setDiscardQuery(boolean discardQuery)
99 _discardQuery = discardQuery;
102 private class Redirector extends AbstractHandler
104 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
106 if (_newContextURL==null)
109 String path=_newContextURL;
110 if (!_discardPathInfo && request.getPathInfo()!=null)
111 path=URIUtil.addPaths(path, request.getPathInfo());
113 StringBuilder location = URIUtil.hasScheme(path)?new StringBuilder():baseRequest.getRootURL();
115 location.append(path);
116 if (!_discardQuery && request.getQueryString()!=null)
118 location.append('?');
119 String q=request.getQueryString();
120 q=q.replaceAll("\r\n?&=","!");
124 response.setHeader(HttpHeader.LOCATION.asString(),location.toString());
127 response.setHeader(HttpHeader.EXPIRES.asString(),_expires);
129 response.setStatus(_permanent?HttpServletResponse.SC_MOVED_PERMANENTLY:HttpServletResponse.SC_FOUND);
130 response.setContentLength(0);
131 baseRequest.setHandled(true);
136 /* ------------------------------------------------------------ */
138 * @return the expires header value or null if no expires header
140 public String getExpires()
145 /* ------------------------------------------------------------ */
147 * @param expires the expires header value or null if no expires header
149 public void setExpires(String expires)