users@jersey.java.net

Re: How to obtain HttpServletRequest (or get RemoteAddress)

From: Martin Grotzke <martin.grotzke_at_freiheit.com>
Date: Thu, 06 Mar 2008 15:08:12 +0100

Hi Paul,

thanx for your answer!

> 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).
Jep, I'd like to switch to the ServletContainer approach for being able
to have a complete test-scenario. But I don't know if I have time for
this this week.
Is the ServletContainer thing what you wrote in the posting
"Intergrating Jetty and Jersey"?

Cheers,
Martin


On Thu, 2008-03-06 at 09:46 +0100, Paul Sandoz wrote:
> 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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
-- 
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