Bug 14766

Summary: Redirect Vavle
Product: Tomcat 4 Reporter: Jens Andersen <ja>
Component: Catalina:ModulesAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED WONTFIX    
Severity: enhancement CC: ja
Priority: P3    
Version: Unknown   
Target Milestone: ---   
Hardware: All   
OS: All   

Description Jens Andersen 2002-11-22 13:18:33 UTC
I think it would be nice to have a redirecct valve - like the one provided below
;-). 

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.HttpRequest;
import org.apache.catalina.HttpResponse;
import org.apache.catalina.Logger;
import org.apache.catalina.Request;
import org.apache.catalina.Response;
import org.apache.catalina.Valve;
import org.apache.catalina.ValveContext;
import org.apache.catalina.valves.ValveBase;
import org.apache.catalina.util.StringManager;

import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;

/**
 * Implementation of a Valve that performs redirection based on comparing the
 * appropriate request property (selected based on which subclass you choose
 * to configure into your Container's pipeline) against a set of regular
 * expressions configured for this Valve.
 * <p>
 * This valve is configured by setting the <code>pattern</code> and
 * <code>target</code> properties. The  <code>pattern</code> is a
 * expressions (in the syntax supported by the jakarta-regexp library) to
 * which the appropriate request property will be compared. This Valve may 
 * be attached to any Container.
 *
 * @author 	Jens Andersen (ja at it-practice.dk), Last edited by $Author$
 * @version	$Revision$
 */
public class RedirectValve extends ValveBase implements Valve {
	//protected static StringManager sm =
StringManager.getManager("dk.itp.catalina.valves");
	protected static StringManager sm =
StringManager.getManager("org.apache.catalina.valves");
	/**
	 * Info string containing information about the implementation.
	 */
	private static final String info = "dk.itp.catalina.valves.RedirectValve/1.0";

	/**
	 * The pattern compiled which makes it useable for the PatterMatcher
	 */
	private RE re;	


	/**
	 * The target represents the URI which the client is redirected to 
	 * if a match succed
	 */
	private String target;

	
	private boolean debug = true;
	
	/**
	 * Constructor for RedirectValve.
	 */
	public RedirectValve() {
		super();
	}

	/**
	 * Set the pattern which must be tested against
	 * @param pattern - the pattern conforming to the Perl 5.0 syntax
	 * @exception IllegalArgumentException if the pattern has invalid syntax
	 */
	public void setPattern(String pattern){
		try{
			re = new RE(pattern);						
		}catch(RESyntaxException rese){
			throw new IllegalArgumentException(sm.getString("redirectValve.syntax",
pattern));
		}
	}
	
	/**
	 * Set the target which the client request must be redirected to if the 
	 * incomming request and the pattern matches perfectly.
	 * @param target - the redirect URI
	 */
	public void setTarget(String target){
		this.target = target;					
	}
    /**
     * Extract the desired request property, and pass it (along with the
     * specified request and response objects) to the protected
     * <code>process()</code> method to perform the actual filtering.
     * This method must be implemented by a concrete subclass.
     *
     * @param request The servlet request to be processed
     * @param response The servlet response to be created
     * @param context The valve context used to invoke the next valve
     *  in the current processing pipeline
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
	public void invoke(Request request, Response response, ValveContext context)
throws IOException, ServletException {
        if(!(request instanceof HttpRequest) || !(response instanceof
HttpResponse) || re == null){
            context.invokeNext(request, response);
            return;
        }
        
        HttpRequest httprequest = (HttpRequest)request;
        HttpResponse httpresponse = (HttpResponse)response;
        
        HttpServletRequest hreq = (HttpServletRequest)httprequest.getRequest();
        HttpServletResponse hres = (HttpServletResponse)httpresponse.getResponse();
        
		if(re.match(target)){
			hres.sendRedirect(hres.encodeURL(target));
			return;
		}else{
			context.invokeNext(request, response);
            return;
		}
	}
	/**
	 * Method which returns information about the valve
	 * @see org.apache.catalina.Valve#getInfo()
	 */
	public String getInfo() {
        return info;
	}
   
   	public String toString(){
        StringBuffer sb = new StringBuffer("RedirectValve[");
        if(container != null)
            sb.append(container.getName());
        sb.append("]");
        return sb.toString();
    }
}//RedirectValve
Comment 1 Mark Thomas 2010-12-14 18:32:35 UTC
The patch is incomplete and redirection to single URI is too limiting. To be useful, the feature needs to look more like mod_rewrite.