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:24:52 +0100

Ok, you were faster :)

Thanx!


On Thu, 2008-03-06 at 14:58 +0100, Paul Sandoz wrote:
> Hi Martin,
>
> You may want to look at my recent response to Jonathan about Jetty [1].
>
> If you use Jetty in embedded mode with ServletContainer you can test
> your resources that inject HttpServletRequest.
>
> Paul.
>
> [1] https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=751
>
> 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
> >
>
> --
> | ? + ? = To question
> ----------------\
> Paul Sandoz
> x38109
> +33-4-76188109
>
> ---------------------------------------------------------------------
> 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