anij
anij

Reputation: 1372

Is there any way to log every web service call coming to an Application in Jersey

I'm using Jersey 1.8 in My Application. Now, there are lots of service classes. Is there any way to log all the request coming to different services. That means, is it possible to create something like a generic filter via which all request will go, from where I can track and log all incoming requests.

Upvotes: 2

Views: 964

Answers (3)

Douglas Tober
Douglas Tober

Reputation: 300

If you are using Jersey 2+

@Provider
public final class GeneralAccessHandler implements ContainerRequestFilter, ContainerResponseFilter {

    @Context
    private HttpServletRequest servReq;

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        System.out.println("Method: " + requestContext.getMethod());
        System.out.println("Path: " + requestContext.getUriInfo().getPath());
        System.out.println("User Agent: " + requestContext.getHeaderString("user-agent"));
        final String addr = servReq.getRemoteAddr();
        final int port = servReq.getRemotePort();
        System.out.println("Client: " + addr + ":" + port);
        System.out.println("URI: " + requestContext.getUriInfo().getRequestUri());
    }

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        //When there is no response
    }
}

Register with servlet as mentioned by accepted answer:

<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>your.fully.qualified.GeneralAccessHandler</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>your.fully.qualified.GeneralAccessHandler</param-value>
</init-param>

Upvotes: 0

Martin Seeler
Martin Seeler

Reputation: 6982

Yes, it's quite easy:

Create a provider, e.g. GenerelAccessHandler.java:

@Provider
public final class GeneralAccessHandler implements ContainerRequestFilter, ContainerResponseFilter {

    /** The incoming request we obtained. */
    @Context
    private final HttpServletRequest mHttpServletRequest;

    /** Some useful informations about the accesspoint in our service. */
    @Context
    private final UriInfo mUriInfo;

    @Override
    public ContainerRequest filter(final ContainerRequest request) {
        if (null != mHttpServletRequest) {
            System.out.println("Method: " + mHttpServletRequest.getMethod());
            System.out.println("Session: " + mHttpServletRequest.getSession().getId());
            System.out.println("User Agent: " + mHttpServletRequest.getHeader("user-agent"));
            final String addr = mHttpServletRequest.getRemoteAddr();
            final int port = mHttpServletRequest.getRemotePort();
            System.out.println("Client: " + addr + ":" + port);
        }
        if (null != mUriInfo) {
            System.out.println("URI: " + mUriInfo.getRequestUri().getPath());
        }
        return request;
    }

    @Override
    public ContainerResponse filter(final ContainerRequest pReq, final ContainerResponse pResp) {
        System.out.println("Outgoing Response")
        return pResp;
    }

}

And in your web.xml you can add two init-params to your Servlet, pointing to this AccessHandler like this:

<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>your.fully.qualified.GeneralAccessHandler</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>your.fully.qualified.GeneralAccessHandler</param-value>
</init-param>

This class will now be the first and last place for each Request. You can do whatever you want here. Log it with your logger, use MDC from logback, etc pp.

Hope this helps!

Upvotes: 3

DaShaun
DaShaun

Reputation: 3880

You can implement the com.sun.jersey.spi.container.ContainerRequestFilter and add it as an init-param to your Jersey Servlet.

I'm using the JerseySpringServlet here:

<servlet>
    <servlet-name>Jersey Spring Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.domain.my.implements.containerrequestfilter.MyFilter</param-value>
    </init-param>
</servlet>

Upvotes: 0

Related Questions