I'm working through the article trying to put a layer of basic auditing functionality on top of an existing minimal Dropwizard project.
The article also contains an associated repository.
The gradle.build in that repo doesn't seem to have any extra dependencies besides core DW:
dependencies {
compile(
'io.dropwizard:dropwizard-core:' + dropwizardVersion
)
testCompile(
'junit:junit:4.11',
'org.hamcrest:hamcrest-core:1.3',
'org.mockito:mockito-all:1.9.5',
'org.unitils:unitils-core:3.4.2'
)
}
My build is in Maven and seemingly contains an equivalent list of dependencies:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-bom</artifactId>
<version>${dropwizard.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</dependency>
</dependencies>
What's not recognized are the imports for the RequestDispatcher / HttpContext when I'm trying to build an equivalent to this:
IntelliJ IDEA seems to recognize these on the classpath:
however what's needed is the reference to com.sun.jersey.api.core.HttpContext
instead.
Could someone please point me to what extra jar files I might be missing here, or perhaps some references to the working demoes of auditing functionality on top of DW.
Thank you in advance.
That article is using an older Dropwizard version when it was still using Jersey 1.x. Since you are using a newer version that uses 2.x, the way to do it now is to use a ContainerRequestFilter
. You can get the resource information by injecting ResourceInfo
. In Jersey 1.x the RequestDispatcher
was used as there was so such thing as a ResourceInfo
at that time, so this was a way to get the resource class and resource method.
@Provider
public class AuditRequestFilter implements ContainerRequestFilter {
@Context
private ResourceInfo info;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
Class<?> resourceClass = info.getResourceClass();
Method resourceMethod = info.getResourceMethod();
}
}
The just register the filter with Dropwizard
env.jersey().register(AuditRequestFilter.class);
Looking again at the code you linked to, it's probably better to use a ContainerResponseFilter
since you want to check the status code before auditing. Also, to get the remote address, you can inject HttpServletRequest
. You can also get URI information from the UriInfo
@Provider
public class AuditRequestFilter implements ContainerResponseFilter {
@Context
private HttpServletRequest request;
@Context
private ResourceInfo info;
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
int status = responseContext.getStatus();
String remoteAddr = request.getRemoteAddr();
UriInfo uriInfo = requestContext.getUriInfo();
Class<?> resourceClass = info.getResourceClass();
Method resourceMethod = info.getResourceMethod();
}
}
See also: