001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.dispatch;
030:
031: import javax.servlet.FilterChain;
032: import javax.servlet.ServletException;
033: import javax.servlet.ServletRequest;
034: import javax.servlet.ServletResponse;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037: import java.io.IOException;
038: import java.io.PrintWriter;
039:
040: /**
041: * Represents the final servlet in a filter chain.
042: */
043: public class MovedFilterChain implements FilterChain {
044: private int _code;
045: private String _url;
046: private String _queryString;
047:
048: /**
049: * Create the redirect filter chain servlet.
050: *
051: * @param url the URL to redirect to.
052: */
053: public MovedFilterChain(int code, String url) {
054: _code = code;
055: _url = url;
056: }
057:
058: /**
059: * Create the redirect filter chain servlet.
060: *
061: * @param url the URL to redirect to.
062: */
063: public MovedFilterChain(int code, String url, String queryString) {
064: _code = code;
065: _url = url;
066: _queryString = queryString;
067: }
068:
069: /**
070: * Invokes the final servlet at the end of the chain.
071: *
072: * @param request the servlet request
073: * @param response the servlet response
074: *
075: * @since Servlet 2.3
076: */
077: public void doFilter(ServletRequest request,
078: ServletResponse response) throws ServletException,
079: IOException {
080: HttpServletRequest req = (HttpServletRequest) request;
081: HttpServletResponse res = (HttpServletResponse) response;
082:
083: String queryString = _queryString;
084:
085: if (queryString == null)
086: queryString = req.getQueryString();
087:
088: String url;
089:
090: if (queryString != null && _url.indexOf('?') < 0)
091: url = res.encodeURL(_url + '?' + queryString);
092: else
093: url = res.encodeURL(_url);
094:
095: res.setHeader("Location", url);
096:
097: res.setStatus(_code);
098:
099: PrintWriter out = response.getWriter();
100:
101: out.println("The URL has moved to <a href=\"" + url + "\">"
102: + url + "</a>");
103: }
104: }
|