users@jersey.java.net

Re: How to obtain HttpServletRequest (or get RemoteAddress)

From: Martin Grotzke <martin.grotzke_at_freiheit.com>
Date: Wed, 05 Mar 2008 19:38:12 +0100

Hi Paul,

thanx a lot for this info, this works fine when using a servlet
container. Is this intended to work also in tests that use
TestHttpRequestContext? When I run a test, the HttpServletRequest is
always null...

I create an instance of the resource by myself and make it then
accessable via the ComponentProvider like this:

final ComponentProvider componentProvider = new ComponentProvider() {
    
    public Object getInstance( Scope scope, Class c ) throws InstantiationException, IllegalAccessException {
        if ( MyResource.class.equals( c ) ) {
            return myResource;
        }
        return null;
    }

    public Object getInstance( Scope scope, Constructor contructor, Object[] parameters ) throws InstantiationException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        return null;
    }

    public void inject( Object instance ) {
    }
    
};

The call to the WebApplication looks like this:

public ContainerResponse call(WebApplicationImpl a, String method, String path, Object entity) {

    byte[] requestEntity = writeEntity( new ResponseHttpHeadersImpl(),
            entity, a.getMessageBodyContext() );
            
    AbstractContainerRequest request = new TestHttpRequestContext(
            a.getMessageBodyContext(),
            method,
            new ByteArrayInputStream(requestEntity),
            path,
            "/");
    AbstractContainerResponse response = new TestHttpResponseContext(
            a.getMessageBodyContext(),
            request);

    a.handleRequest(request, response);
    
    return response;
}

I hope these are the essential things, or is there anything missing?

Thanx && cheers,
Martin


On Wed, 2008-03-05 at 15:00 +0100, Paul Sandoz wrote:
> Martin Grotzke wrote:
> > I want to obtain the remote address of the client and would fetch this
> > from the HttpServletRequest. So how can I get access to the current
> > request in jersey?
> >
>
> @Path("/")
> public class Foo {
> @Resource HttpServletRequest request;
> ...
> }
>
> Perhaps this information should be part of UriInfo? Basically it is the
> resolved host name in the client request URI?
>
> Paul.
>
-- 
Martin Grotzke
Dipl.-Inf.
freiheit.com technologies gmbh
Straßenbahnring 22 / 20251 Hamburg, Germany
fon       +49 (0)40 / 890584-0
fax       +49 (0)40 / 890584-20
HRB Hamburg 70814
eb0e 645c 9730 c8a3 ee2f  1b9a 5de5 21cb c259 fe34
Geschäftsführer: Claudia Dietze, Stefan Richter, Jörg Kirchhof