DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14766>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14766

Redirect Vavle

           Summary: Redirect Vavle
           Product: Tomcat 4
           Version: Unknown
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Catalina:Modules
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


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

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to