Rewrite Handler
Rewritehandler matches a collection of rules based on that request, and then requests for changes based on the matching rules.
The most common requirement is to overwrite the URI. But not limited to: rules can be configured to redirect responses, set cookie or response code responses, change headers. Wait a minute.
High speed start
The standard jetty announcement includes the Jetty-rewrite module jar, in Lib/jetty-rewrite-*.jar, and a sample configuration file, in Etc/jetty-rewrite.xml.
In order to activate the rewrite module, use the sample configuration file. Start jetty with the following command, for example:
Note: Suppose you are using sample test WebApp to perform a standard jetty announcement. There is a rewrite module of demo in http://localhost:8080/rewrite/.
Configure rules
Rules are configured by using Jetty.xml.
The following sample file shows how to add rewrite handler to the server:
<configure id= "Server" class= "Org.eclipse.jetty.server.Server" > <!--Create and Configure the rewrite handler --<new id= "Rewrite" class= "Org.eclipse.jetty.rewrite.handler.RewriteHandler" > <set name= "rewriterequ Esturi ">true</Set> <set name=" Rewritepathinfo ">false</Set> <set name=" Originalpathattrib Ute ">requestedPath</Set> <!--redirect the response. This is a redirect which was visible to the browser. After the redirect, the browser address bar is show/redirected---<call name= "AddRule" > <ARG&G T <new class= "Org.eclipse.jetty.rewrite.handler.RedirectPatternRule" > <set name= "pattern" >/redirect/* </Set> <set name= "Replacement" >/redirected</Set> </New> </Arg> </Call> <!--rewrite the request URI. This is a internal rewrite, visible to server, but the Browser'll still Show/some/old/context-<call name= "AddRule" > <Arg> <new CLA ss= "Org.eclipse.jetty.rewrite.handler.RewritePatternRule" > <set name= "pattern" >/some/old/context</s et> <set name= "Replacement" >/some/new/context</Set> </New> </Arg> </Call> <!--reverse the order of the path sections. Internal rewrite-<call name= "AddRule" > <Arg> <new class= "ORG.ECLIPSE.JETTY.REWR Ite.handler.RewriteRegexRule "> <set name=" regex ">/reverse/([^/]*)/(. *) </Set> <set Name= "Replacement" >/reverse/$2/$1</Set> </New> </Arg> </Call> </new& Gt <!--Add the rewrite handler to the server--<set name= "handler" ><ref id= "rewrite"/></SET>&L T;/configure>
See Etc/jetty-rewrite.xml for a number of other configuration examples.
Examples of embedding
The following is an example of embedding jetty, and doing the same thing as the above configuration file:
Server server = new server (); Rewritehandler rewrite = new Rewritehandler (); Rewrite.setrewriterequesturi (true); Rewrite.setrewritepathinfo (false) ; Rewrite.originalpathattribute ("Requestedpath"); Redirectpatternrule redirect = new Redirectpatternrule (); Redirect.setpattern ("/redirect/*"); Redirect.setreplacement ("/redirected"); Rewrite.addrule (redirect); Rewritepatternrule oldtonew = new Rewritepatternrule (); Oldtonew.setpattern ("/some/old/context"); o Ldtonew.setreplacement ("/some/new/context"); Rewrite.addrule (oldtonew); Rewriteregexrule reverse = new Rewriteregexrule () Reverse.setregex ("/reverse/([^/]*)/(. *)"); reverse.setreplacement ("/reverse/$2/$1"); Rewrite.addrule (reverse); Server.sethandler (rewrite);
Rules
There are several different types of rules.
PatternRule
Matches the requested URI with the servlet pattern syntax.
Cookiepatternrule
Add a cookie to the response.
Headerpatternrule
Adds/changes the header in the response.
Redirectpatternrule
Redirects the response.
Responsepatternrule
Send a response code (status or error).
Rewritepatternrule
Rewrite the URI.
Regexrule
Match the request URI with a regular table.
Redirectregexrule
Redirects the response.
Rewriteregexrule
Rewrite URI
Headerrule
Match request headers.
Match either in a header name + a specific value, or in the presence of a header (plus whatever value).
Forwardedschemaheaderrule
Set the request plan (default is HTTPS).
Other
Other more bizarre rules.
Msiesslrule
Disable for IE5 and IE6 to remain active for SSL.
Legacyrule
Implement the Legacy API for Rewritehandler.
Rulecontainer
Organize the rules together.
Virtualhostrulecontainer
The included rules apply only to a specific virtual host or to a set of virtual hosts.
Write your own definition of handlers
Handler is the jetty component that processes the request.
Some jetty users do not need to write jetty handler, but instead use the servlet API (http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/ servlet/package-summary.html). You can reuse existing jetty handlers for context, security, sessions, and servlets, no matter what the extension. However, some users may have special needs or worry about footstep issues and disable the full servlet API. For them to implement a jetty handler is a straightforward way to provide dynamic Web content with minimal modifications.
Handler API
The handler interface provides the core of jetty content generation and processing. The classes that implement this interface are used to reconcile requests, filter requests, and produce content.
The core APIs of the handler interface are:
public void handle (String target, request baserequest, HttpServletRequest request, httpservletresponse response) Throws IOException, Servletexception
The implementation of this method can process a request, pass the request to a handler (or servlet), or it can change and/or wrap the request, and then pass it on. There are three types of handler:
1) Coordinate handlers: Transfer request to other handlers (Handlercollection, contexthandlercollection);
2) filter handlers: Enlarge one request, then pass it to other handlers (Handlerwrapper, Contexthandler, Sessionhandler);
3) Generate Handlers: Generate Content (Resourcehandler and Servlethandler).
Goal
The goal of a handler is to handle the identifier of the requested resource. This is typically the URI from an HTTP request. However, the target can be different from the requested URI in the following two environments:
1) Assume that the request is distributed to a resource of a command. A named servlet, for example. The target is the name of the resource.
2) Assume that the request is through the request scheduler. The target is the URI of the included resource. is different from the URI of the real request.
Requests and responses
The request and response objects used in the signature of the processing method are servlet request and servlet Response. These are the standard APIs.
More often, the jetty implementations that enter these classes are required: request and response.
However, because requests and responses can be packaged by handlers, filters, and Servlets, it is impossible to pass the implementation directly. The following methods are used to obtain the core implementation object under whatever wrapper:
Request Base_request = Request Instanceof request? (Request) Request:HttpConnection.getCurrentConnection (). Getrequest (); Response base_response = Response instanceof Response?(Response) Response:HttpConnection.getCurrentConnection (). GetResponse ();
Note assume that handler passes the request to a handler. He should pass the Request/response object without using the underlying object. This is to preserve the encapsulation that was made by the upstream handlers.
Distribute
The distribution parameters show the status of the call processing, which can be:
1) Request = = 1: The original request received from a connector;
2) FORWARD = = 2: A request to be forwarded by a requestdispatcher;
3) Include = = 4: A request contained by a requestdispatcher;
4) Error = = 8: The request to be forwarded to an error handler by the container.
These are interesting to most servlets and related handlers. For example, security handler only distributes application authentication and authorization to request.
Processing requests
A handler can handle a request by:
1) a response is generated;
2) filtering requests and/or responses;
3) Pass the request and response to another handler.
The following details are described.
Generate a response
Onehandler shows how to produce a response.
You can use the usual Servlet response API. You will typically set some state, content headers, and then output content:
Response.setcontenttype ("text/html"); Response.setstatus (HTTPSERVLETRESPONSE.SC_OK); Response.getWriter (). println ("
Handler need to mark it. The processing of the request should not be passed to the other handlers:
Request Base_request = (Request instanceof request)? (Request) Request:HttpConnection.getCurrentConnection (). Getrequest (); base_request.sethandled (true);
filtering requests and/or responsesOnce the underlying request or response object is fetched. You can change it.
Generally you will make changes to complete:
1) splitting URIs into ContextPath, Servletpath, and pathinfo components;
2) associate requests and resources for static content;
3) Associate the request with the session;
4) Associate the request with the security principal;
5) Change the URI and path during request distribution to a resource.
You can also update the context of the request:
1) Set the current thread context class loader;
2) Set the thread local variable to represent the current ServletContext.
Usually jetty passes a changed request to a handler. and the finally block can only revert the changes:
try{ base_request.setsession (a_session); Next_handler.handle (Target,request,response,dispatch);} finally{ base_request.setsession (old_session);}
The class that implements the Handlerwrapper class is typical of such a type of handler filter.
Passing requests and responses to another handlerA handler can simply inspect the request, then use the target, request URI, or other information to select a handler as the next processing request handler. These handlers typically implement Handlercontainer interfaces.
Examples include:
Class Handler Collection
Handlerlist
Contexthandlercollection
A lot of other handlers informationSee Jetty Latest Source xref and jetty Latest Javadoc for specific information on each jetty handler.
For a lot of other jetty information, see Jetty Overview.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Jetty: Development Guide handlers