001: //========================================================================
002: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: //Licensed under the Apache License, Version 2.0 (the "License");
005: //you may not use this file except in compliance with the License.
006: //You may obtain a copy of the License at
007: //http://www.apache.org/licenses/LICENSE-2.0
008: //Unless required by applicable law or agreed to in writing, software
009: //distributed under the License is distributed on an "AS IS" BASIS,
010: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: //See the License for the specific language governing permissions and
012: //limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty.handler;
016:
017: import java.io.IOException;
018:
019: import javax.servlet.ServletException;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.mortbay.jetty.HandlerContainer;
024: import org.mortbay.jetty.HttpConnection;
025: import org.mortbay.jetty.Request;
026: import org.mortbay.util.URIUtil;
027:
028: /* ------------------------------------------------------------ */
029: /** Moved ContextHandler.
030: * This context can be used to replace a context that has changed
031: * location. Requests are redirected (either to a fixed URL or to a
032: * new context base).
033: */
034: public class MovedContextHandler extends ContextHandler {
035: String _newContextURL;
036: boolean _discardPathInfo;
037: boolean _discardQuery;
038: boolean _permanent;
039: Redirector _redirector;
040:
041: public MovedContextHandler() {
042: _redirector = new Redirector();
043: addHandler(_redirector);
044: }
045:
046: public MovedContextHandler(HandlerContainer parent,
047: String contextPath, String newContextURL) {
048: super (parent, contextPath);
049: _newContextURL = newContextURL;
050: _redirector = new Redirector();
051: addHandler(_redirector);
052: }
053:
054: public boolean isDiscardPathInfo() {
055: return _discardPathInfo;
056: }
057:
058: public void setDiscardPathInfo(boolean discardPathInfo) {
059: _discardPathInfo = discardPathInfo;
060: }
061:
062: public String getNewContextURL() {
063: return _newContextURL;
064: }
065:
066: public void setNewContextURL(String newContextURL) {
067: _newContextURL = newContextURL;
068: }
069:
070: public boolean isPermanent() {
071: return _permanent;
072: }
073:
074: public void setPermanent(boolean permanent) {
075: _permanent = permanent;
076: }
077:
078: public boolean isDiscardQuery() {
079: return _discardQuery;
080: }
081:
082: public void setDiscardQuery(boolean discardQuery) {
083: _discardQuery = discardQuery;
084: }
085:
086: private class Redirector extends AbstractHandler {
087: public void handle(String target, HttpServletRequest request,
088: HttpServletResponse response, int dispatch)
089: throws IOException, ServletException {
090: if (_newContextURL == null)
091: return;
092:
093: Request base_request = (request instanceof Request) ? (Request) request
094: : HttpConnection.getCurrentConnection()
095: .getRequest();
096:
097: String url = _newContextURL;
098: if (!_discardPathInfo && request.getPathInfo() != null)
099: url = URIUtil.addPaths(url, request.getPathInfo());
100: if (!_discardQuery && request.getQueryString() != null)
101: url += "?" + request.getQueryString();
102:
103: response.sendRedirect(url);
104: if (_permanent)
105: response
106: .setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
107:
108: base_request.setHandled(true);
109: }
110:
111: }
112:
113: }
|