I am new to Dropwizard and we need to fetch the HttpServletRequest
object in our AspectJ project(native not AOP). AspectJ project is used as a framework(jar) in different projects of different framework
Below code is get HttpServletRequest
for spring boot project.
Class<?> requestHolder = Class.forName("org.springframework.web.context.request.RequestContextHolder");
Method method = requestHolder.getMethod("currentRequestAttributes");
Object currentAttributes = method.invoke(requestHolder);
Class<?> servletAttributes = Class.forName("org.springframework.web.context.request.ServletRequestAttributes");
currentAttributes = servletAttributes.cast(currentAttributes);
method = currentAttributes.getClass().getMethod("getRequest");
Object httpRequest = method.invoke(currentAttributes);
if (httpRequest instanceof HttpServletRequest) {
return (HttpServletRequest) httpRequest;
}
How to do for Jersey/Dopwizard?
You could inject the HttpServletRequest Object in your code as below
@Inject
private Provider<HttpServletRequest> requestProvider;
You could provide a method in you WebService class to access this request object. In your aspects you could use reflection to invoke this method and access the HttpServletRequest
AspectJ Part: Create an annotation that can be applied over the rest service method that triggers the aspect advice before/around/after method execution based on how you want to write it. Within the advice we invoke the getHTTPServlet method call by fetching the target object from the joinPoint