ASF Bugzilla – Attachment 27246 Details for
Bug 51468
Redirect Valve
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
A proposed implementation for a Redirect valve
RedirectValve.java (text/plain), 7.73 KB, created by
Sergey Ushakov
on 2011-07-04 03:24:28 UTC
(
hide
)
Description:
A proposed implementation for a Redirect valve
Filename:
MIME Type:
Creator:
Sergey Ushakov
Created:
2011-07-04 03:24:28 UTC
Size:
7.73 KB
patch
obsolete
>package org.apache.catalina.valves; > >import java.io.IOException; > >import java.text.MessageFormat; > >import java.util.regex.Matcher; >import java.util.regex.Pattern; >import java.util.regex.PatternSyntaxException; > >import javax.servlet.ServletException; > >import org.apache.catalina.Valve; > >import org.apache.catalina.connector.Request; >import org.apache.catalina.connector.Response; > >// may be excessive when compiled aside with the other classes in the package >import org.apache.catalina.valves.ValveBase; > >import org.apache.juli.logging.Log; >import org.apache.juli.logging.LogFactory; > >import org.apache.tomcat.util.res.StringManager; > >/** > * <p>Implementation of a Valve for Tomcat/Catalina container that performs > * redirection based on matching the request URL against a pre-configured > * pattern.</p> > * > * <p>This valve is configured by setting the <code>from</code> and > * <code>to</code> properties. The <code>from</code> property is accepted as > * a Java regex expression to match against a request URL. The > * <code>to</code> property allows using capturing group references in the > * form of '{N}'.</p> > * > * <p>A typical entry for this valve in Tomcat "server.xml" configuration > * file, intended for redirecting requests from internal container-based URLs > * to a front-end server, might look like:</p> > * <pre> > * <Valve className="usn.apache.catalina.valves.RedirectValve" > * from="^http://([^:]+):8080/my-web-application/(.*)$" > * to="http://{1}/my-front-end-mapping/{2}" > * /> > * </pre> > * > * @author Jens Andersen, 2002, initial idea and skeleton, see <a > * href="https://issues.apache.org/bugzilla/show_bug.cgi?id=14766" > * >https://issues.apache.org/bugzilla/show_bug.cgi?id=14766</a> > * > * @author Sergey Ushakov, 2011, final implementation > */ >public class RedirectValve extends ValveBase implements Valve > { > /** > * Name of the request attribute for sharing the reconstructed request > * URL between successive valve instances. > */ > protected static final String ATTRIBUTE_URL = > "usn.apache.catalina.valves.RedirectValve.URL"; > > /** > * Apache Tomcat specific localized strings manager. > */ > protected static StringManager sm = > StringManager.getManager ("usn.apache.catalina.valves"); > > /** > * Info string containing information about the implementation. > */ > private static final String info = > "usn.apache.catalina.valves.RedirectValve"; > > /** > * A Tomcat/Catalina specific logger > */ > protected static Log log = LogFactory.getLog (RedirectValve.class); > > /** > * The compiled pattern to be matched against the request URL. > */ > protected Pattern pattern; > > /** > * The URL string which the client is redirected to if a match succeeds. > */ > protected String sTargetTemplate; > > > /** > * The constructor. > */ > /*=============*/ > public RedirectValve > /*=============*/ > () > { > super (); > } // RedirectValve > > > /** > * Set the pattern that request URLs will be tested against. > * > * @param sPattern the pattern conforming to the Java regex syntax > * > * @exception {@link java.lang.IllegalArgumentException > * IllegalArgumentException} if the pattern has invalid syntax > */ > /*=======*/ > public void setFrom > /*=======*/ > ( > String sPattern > ) > { > log.info (sm.getString ("redirectValve.patternInit", sPattern)); > try > { > this.pattern = Pattern.compile (sPattern); > } > catch (PatternSyntaxException e) > { > throw new IllegalArgumentException > (sm.getString ("redirectValve.badPattern", > sPattern)); > } > } // setFrom > > /** > * Set the URL template which the client request would be redirected to > * if the incoming request and the pattern match. References to capturing > * groups in the match pattern may used in the form of '{N}'. > * > * @param sTargetTemplate the redirect URI template > */ > /*=====*/ > public void setTo > /*=====*/ > ( > String sTargetTemplate > ) > { > this.sTargetTemplate = sTargetTemplate; > } // setTo > > > /** > * <p>The main method to be implemented by a {@link > * org.apache.catalina.Valve Valve}.</p> > * > * <p>Match the request URL against a pre-configured pattern and perform > * the redirection if necessary.</p> > * > * @param request the request to be processed > * @param response the response to be created > * > * @exception {@link java.io.IOException IOException} if an input/output > * error occurs > * @exception {@link javax.servlet.ServletException ServletException} if > * a servlet-level error occurs > */ > @Override > /*======*/ > public void invoke > /*======*/ > ( > Request request, > Response response > ) > throws IOException, ServletException > { > if (pattern == null) > { > getNext ().invoke (request, response); > return; > } > > // try to get saved request URL from previous valve instance in a > // chain > String sUrl = (String) request.getAttribute (ATTRIBUTE_URL); > if (sUrl == null) > { > // build the request URL from scratch > StringBuffer sbUrl = request.getRequestURL (); > String sQueryString = request.getQueryString (); > if (sQueryString != null) > { > sbUrl.append ("?"); > sbUrl.append (sQueryString); > } > sUrl = sbUrl.toString (); > // save the request URL for other instances of the same valve > request.setAttribute (ATTRIBUTE_URL, sUrl); > } > > Matcher matcher = this.pattern.matcher (sUrl); > if (matcher.matches ()) > { > int iTotalGroups = matcher.groupCount (); > // NOTE: capturing groups are 1-based, not 0-based... > Object [] asGroups = new String [iTotalGroups + 1]; > for (int i = 1; i <= iTotalGroups; ++i) > { > asGroups [i] = matcher.group (i); > } > String sTarget = > MessageFormat.format (sTargetTemplate, asGroups); > response.setStatus (Response.SC_MOVED_PERMANENTLY); > response.setHeader ("Location", > response.encodeRedirectURL (sTarget)); > return; > } > else > { > getNext ().invoke (request, response); > return; > } > } // invoke > > > /** > * Method which returns information about the valve. > * @see org.apache.catalina.Valve#getInfo() > */ > @Override > /*======*/ > public String getInfo > /*======*/ > () > { > return info; > } // getInfo > > > /** > * Provide a String representation as a combination of valve class name > * and container name. > */ > @Override > /*========*/ > public String toString > /*========*/ > () > { > StringBuffer sb = new StringBuffer ("RedirectValve["); > if (container != null) > { > sb.append (container.getName ()); > } > sb.append ("]"); > return sb.toString (); > } // toString > > } // class RedirectValve
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 51468
: 27246 |
27247
|
27248