0

Like Spring framework, I want to create a Pointcut to execute some logic before executing the method. Is it possible to do that in Helidon MP?

@Pointcut("execution(public * *(..))")
private void anyPublicOperation(String input) {}
Shama
  • 178
  • 2
  • 9
Newbie
  • 11
  • 3

2 Answers2

2

Helidon MP, like all MicroProfile implementations, is centered around CDI, which offers, for this purpose, interceptors and decorators.

Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
0

I have already done that using Interceptor. Thanks! Here is the example:

  • Creating a custom annotation with @InterceptorBinding
@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SoapSecure {
  String tokenParam() default "token";
}
  • Creating the interceptor
@Priority(1)
@Interceptor
@SoapSecure
@Slf4j
public class SoapAuthenticationInterceptor {

    @Inject
    private AuthService authService;

    @AroundInvoke
    public Object validateToken(InvocationContext invocationContext) throws Exception {
        Method method = invocationContext.getMethod();
        log.info("Validate the token from SOAP APIs: " + method.getName());

        String tokenParam = method
                .getAnnotation(SoapSecure.class)
                .tokenParam();

        Parameter[] params = method.getParameters();
        String accessToken = null;
        for (Parameter p : params) {
            if (p.getName().equals(tokenParam)) {
                // validate the access token
                authService.validateAccessToken(Objects.toString(method.invoke(p)));
            }
        }

        return invocationContext.proceed();
    }
}

Then use it:

 @SoapSecure
 public boolean test(String token){}
Newbie
  • 11
  • 3