0

I would like my AOP advice to have a handle to the currently executing Jersey resource's HttpContext. The spring-annotations sample mentions that the user could get hold of the request and authenticate etc.,, but how does one get hold of any of the values in the Context in the advice?

Currently my resource definition looks like this:

    @Singleton
    @Path("/persist")
    public class ContentResource {

        @PUT
        @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        @Auth
        public Content save(Content content){
           //Do some thing with the data
        }
    }

And the aspect is defined so:

    @Aspect
    public class AuthorizeProcessor {

        @Around(value="@annotation(package.Auth) and @annotation(auth)", argNames = "auth")
        public Object authorize(ProceedingJoinPoint pjp, Auth auth) throws Throwable{
            //How do I get the HttpContext here?
            return pjp.proceed();
        }
    }
calvinkrishy
  • 3,798
  • 8
  • 30
  • 45

1 Answers1

0

Granted this is most likely too late, but I did what you are doing by implementing a Servlet Filter in front of the service that does the authorization. This avoids the need for AOP entirely, and it gives you the actual ServletRequest directly without working around the system to get it.

Ironically, the question that you helped me to answer would likely help you here, if you really wanted AOP.

You can supply the Spring RequestContextFilter to the request, and then access the HttpServletRequest (as opposed to the HttpContext):

<filter>
    <filter-name>requestContextFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>requestContextFilter</filter-name>
    <url-pattern>/path/to/services/*</url-pattern>
</filter-mapping>

Access code down the filter chain:

/**
 * Get the current {@link HttpServletRequest} [hopefully] being made
 * containing the {@link HttpServletRequest#getAttribute(String) attribute}.
 * @return Never {@code null}.
 * @throws NullPointerException if the Servlet Filter for the {@link
 *                              RequestContextHolder} is not setup
 *                              appropriately.
 * @see org.springframework.web.filter.RequestContextFilter
 */
protected HttpServletRequest getRequest()
{
    // get the request from the Spring Context Holder (this is done for
    //  every request by a filter)
    ServletRequestAttributes attributes =
        (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();

    return attributes.getRequest();
}
Community
  • 1
  • 1
pickypg
  • 22,034
  • 5
  • 72
  • 84