users@jersey.java.net

Re: How to obtain HttpServletRequest (or get RemoteAddress)

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 06 Mar 2008 09:46:06 +0100

On Mar 5, 2008, at 7:38 PM, Martin Grotzke wrote:

> 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?

No, because most of the unit tests are executed without using a
container and no network. If there is no Web container then one
cannot get access to the HttpServletRequest. Thus the test code below
is not designed to test if you are using any container specific
artifacts. (If you look at the code for ServletContainer it adds
further injectables.)



> 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?
>

There is a big gaping hole in the unit tests for testing using
servlet. I am looking for volunteers to experiment with running in-
process Jetty instances so we can test ServletContainer, and then it
would be possible to execute unit tests (with HttpServletRequest
injected) (in a similar manner to that for the LW HTTP server and
Grizzly).

Paul.

> 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