Java + Play Framework 2 with nested action compositions in the same class

你离开我真会死。 提交于 2020-01-15 09:09:10

问题


I was looking how to implement a filter in Play Framework 2 and, for the best of my knowledge, the best approach is to implement that using action composition.

That said, I started doing some testing and I was able to make that to work. The only limitation is when I have nested annotations in the same class.

My current use case

Perform domain validation when domain parameter is present in the request.
If the domain is not present in the local database, perform syntax validation only. Otherwise, full validation is required (what is full validation is not important here).

My current code using action composition

The controller has two annotations:

  • one for most of the cases where the full domain verification is required.
  • another annotation specifically for addCustomer where I want to perform syntax validation only.

There are two "problems" though. I was expecting the DomainVerifierFiler action to be called only once with the augmented property (syntaxVerificationOnly). Here is what happens:

  • The "filter" is being called twice for addCustomer;
  • The first one to be called is the method annotation (class annotation always overrides the method);
 @DomainVerificationFilter
 public class CustomerController {

      public Result updateCustomer(String domain) {}

      public Result deleteCustomer(String domain) {}

      @DomainVerificationFilter(syntaxVerificationOnly = true)
      public Result addCustomer(String domain) {}
 }
 @With(DomainVerifierAction.class)
 @Target({ElementType.TYPE, ElementType.METHOD})
 @Retention(RetentionPolicy.RUNTIME)
 public @interface DomainVerifierFilter {
    boolean syntaxVerificationOnly() default false;
 }
 public class DomainVerifierAction extends Action<DomainVerifierFilter> {
        @Override
        public Result call(Http.Context ctx) throws Throwable {
           if (configuration.syntaxVerificationOnly()) {
                //sintax verification
            } else {
                //full verification
            }
        }
 }

Question: Is there any fix for this? Am I doing the wrong approach here? Any suggestions?

The current workaround is that I'm checking if it is the second call to the same action. If yes, I go ahead without checking. But this is clearly not desired.

Another possibility is to add the annotation in every method. This is also not desired because I have lots of methods with the domain as the parameter. And, 99.99% I need to perform full domain verification.

Kr, Alan


回答1:


The only way I can see this working is to change the annotation, so you can provide (at class level) an optional list of methods to ignore or to be verified only by syntax (whichever suits better your application).

In fact, it could be better to have 2 different annotations, one at class level and one at method level.

Otherwise, I'm afraid it will always call the annotation twice, as it exists twice in the code.



来源:https://stackoverflow.com/questions/17412458/java-play-framework-2-with-nested-action-compositions-in-the-same-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!