Java Url Rewrite Tool: UrlRewriteFilter

Source: Internet
Author: User

Url rewrite is a common requirement in the web development. apache Web Server uses its module mod_rewrite to offere a powerful Url rewrite function. in this post, I make some introduction to UrlRewriteFilter which is a very powerful tool just like Apache's mod_rewrite. here is the detailed manul UrlRewriteFilter. it is east to use in your Java web project following the steps. it is also apparently That UrlRewriteFilter takes great advantage of Java Servlet's filter. in the following recipe, I want to highligh the java annotation implementation. urlRewriteFilter provides three kinds of annotations: @ HttpUrl, @ HttpParam @ HttpJson [java] @ Retention (RetentionPolicy. SOURCE) @ Target (ElementType. METHOD) public @ interface HttpUrl {String value ();/*** A weighting to be applied to the url, if high Er it will move this url to the "top" if lower more towards the * "bottom ". ** @ return weight */int weight () default 0;} [java] @ Retention (RetentionPolicy. SOURCE) @ Target (ElementType. PARAMETER) public @ interface HttpParam {/*** If not set will use the name of the parameter (case insensitive ). * can be a expression ie, $1 (the first group of @ HttpUrl regexp), % {header: user-agent} (the user age Nt header ). ** @ return value */String value () default "[unassigned]";} [java] @ Retention (RetentionPolicy. SOURCE) @ Target (ElementType. METHOD) public @ interface HttpJson {String value () default "[unassigned]";/*** A weighting to be applied to the url, if higher it will move this url to the "top" if lower more towards the * "bottom ". ** @ return weight */int weight () default 0;} For those Annotions, UrlRewriteFilter project creates a annotation processor named UrlRewriteAnnotationProcessor to handle them. [java] public boolean process (Set <? Extends TypeElement> annotations, RoundEnvironment roundEnv) {if (isBlank (dest) {if (roundEnv. processingOver () infoMsg (getClass (). getSimpleName () + ":-AurlrewriteDest not specified, annotations ignored"); return true;} debugMsg ("process"); Set <? Extends Element> urlDeclarations = roundEnv. getElementsAnnotatedWith (HttpUrl. class); for (Element element: urlDeclarations) {processedAnnotations. add (new ProcessedHttpUrlAnnotation (element);} Set <? Extends Element> jsonDeclarations = roundEnv. getElementsAnnotatedWith (HttpJson. class); for (Element element: jsonDeclarations) {processedJsonAnnotations. add (new Annotations (element);} Set <? Extends Element> predictiondeclarations = roundEnv. getElementsAnnotatedWith (HttpExceptionHandler. class); for (Element element: exceptionDeclarations) {httpExceptionHandlers. add (new ProcessedHttpExceptionAnnotation (element);} if (roundEnv. processingOver () {if (processedAnnotations. size ()> 0) {infoMsg ("Got" + processedAnnotations. size () + "@ HttpUrl annotations");} if (processedJsonAnnotati Ons. size ()> 0) {infoMsg ("Got" + processedJsonAnnotations. size () + "@ HttpJson annotations"); processedAnnotations. addAll (processedJsonAnnotations);} Collections. sort (processedAnnotations); if (httpExceptionHandlers. size ()> 0) {infoMsg ("Got" + httpExceptionHandlers. size () + "@ HttpExceptionHandler annotations"); Collections. sort (httpExceptionHandlers);} try {File destFile = new File (dest ); If (! DestFile. exists () {checkDirsExistMkdir (destFile. getParentFile (); destFile. createNewFile () ;}if (! DestFile. canWrite () {throw new IOException ("cannot write to" + destFile. getName ();} if (errorDuringProcessing) {errorMsg ("Error occured during processing deleting generated file. "); destFile. delete ();} else {PrintWriter pw = new PrintWriter (destFile); infoMsg ("Writing to" + destFile); outputRules (pw); outputExceptionHandlers (pw); pw. close () ;}} catch (FileNotFoundException e) {errorMsg (e); e. printStackTrace ();} catch (IOException e) {errorMsg (e); e. printStackTrace () ;}} return true ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.