Джерси с весны АОП - PullRequest
       4

Джерси с весны АОП

0 голосов
/ 17 ноября 2011

Я бы хотел, чтобы мой совет АОП имел указатель на HttpContext исполняемого в настоящее время ресурса Джерси.Образец spring-annotations упоминает, что пользователь мог получить запрос и подтвердить его подлинность и т. Д., Но как получить любое из значений в контексте в рекомендации?

В настоящее время мое определение ресурса выглядит так:

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

А аспект определен так:

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

1 Ответ

0 голосов
/ 29 августа 2012

Скорее всего, это слишком поздно, но я сделал то, что вы делаете, внедрив Servlet Filter перед службой, которая выполняет авторизацию.Это полностью исключает необходимость в АОП и дает вам фактический запрос ServletRequest напрямую, не обходя его системой.

Как ни странно, вопрос , на который вы помогли мне ответить, скорее всего поможет.Вы здесь, если вы действительно хотели AOP.

Вы можете предоставить Spring RequestContextFilter для запроса, а затем получить доступ к HttpServletRequest (в отличие от 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>

Код доступа по цепочке фильтров:

/**
 * 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();
}
...