Java Url Rewrite Tool : UrlRewriteFilter

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

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public @interface HttpUrl {

    String value();

    /**
     * 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;

}
@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 agent header).
     *
     * @return value
     */
    String value() default "[ unassigned ]";

}
@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.

 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 ProcessedHttpJsonAnnotation(element));
        }

        Set<? extends Element> exceptionDeclarations = 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 (processedJsonAnnotations.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;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值