Accessing HttpServletRequest in controllers

368 views
Skip to first unread message

Tristan Louet

unread,
Jun 26, 2013, 10:33:54 AM6/26/13
to ninja-f...@googlegroups.com
Hello ninjas !

I'm using the Ninja Web Framework on Google AppEngine, and I would like to access the HttpServletRequest in my controllers. It used to be that way, I was using them, but when I upgraded to ninja 1.3, I noticed that all requests were null into controllers.
At this time I just took them off my controllers since information picked into requests were actually available into the context (request parameters and server host name), but now I'd like to use Google's authentication with UserService API.
But I see here I need the request to do that.

So, firstly, is it a normal behavior that I cannot access a request by adding it in my controller's signature ?
If so, how can I access it ?
If not, any clue of why I cannot ?

Thanks :)

Best,

Tristan

Raphael André Bauer

unread,
Jun 26, 2013, 1:30:01 PM6/26/13
to ninja-f...@googlegroups.com
Hi Tristan,


first of all you are right - support for directly accessing the
httpservletrequest has been removed for some time now. I can see some
reasons for accessing it - so it should not be a big problem
re-enabling it.

Apart from that... You don't need the httpservletrequest for the stuff
you are doing (except req.getUserPrincipal().getName())...
Check out: UserService userService = UserServiceFactory.getUserService();

The example only generates login and logout urls that you can also use
and embed into an html template... :)


Cheers,


Raphael

Tristan Louet

unread,
Jun 27, 2013, 4:37:30 AM6/27/13
to ninja-f...@googlegroups.com
Hi Raphael, and thank you for your answer !

Indeed there's enough information into the UserService to do what I want.

However, I also noticed that my image-upload feature is now broken, because it use the Google's blobstore therefore it needs the HttpServletRequest to extract BlobKey from it.

So in the end, I really need the request. You're saying it should bot be a big problem re-enabling the support for requests, do you mean I need to do something in my project to do so ?
If you've got a solution for that I'm really interested :) This project is the first I'm doing with Java Servlets, I'm no expert in that domain so I hope I'm not asking for to obvious things like how to access a HttpServletRequest ;)

Best,

Tristan

Raphael André Bauer

unread,
Jun 27, 2013, 11:08:50 AM6/27/13
to ninja-f...@googlegroups.com
Hi Tristan,


no - these are not obvious questions. Actually I re-enabled injecting
httpservlet request into controllers again.

A simple workaround for you now is:

In your controller where you want to access the httpservletrequest:

@Inject
Provider<HttpServletRequest) httpServletRequestProvider;

and in your controller method you can then use:

httpServletRequestProvider.get().getContextPath... and so on...

Again - the next version of Ninja will have a simpler way of getting
the HttpServletRequest. But for now this will do the job :)


Cheers,

Raphael

Tristan Louet

unread,
Jun 27, 2013, 5:00:57 PM6/27/13
to ninja-f...@googlegroups.com
Hi Raphael,

much thanks for the tip, I'll try it tomorrow at work :)

Best,

Tristan

Alexander Lochschmied

unread,
Sep 16, 2016, 12:00:46 AM9/16/16
to ninja-framework
Hello,

is there now a better way to do this?

Thanks,
Alexander

Raphael André Bauer

unread,
Sep 19, 2016, 5:36:21 PM9/19/16
to ninja-f...@googlegroups.com
Not sure if this is "better", but you can cast Context like that:

public Result getRealm(Context context) {

NinjaServletContext ninjaServletContext = (NinjaServletContext) context;
ninjaServletContext.getHttpServletRequest()
...
}
this is ofc only possible when Ninja is running inside a servlet container...

Cheers,

Raphael
> --
> You received this message because you are subscribed to the Google Groups
> "ninja-framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ninja-framewo...@googlegroups.com.
> To post to this group, send email to ninja-f...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ninja-framework/2d9602c1-d0c6-45e6-8718-1596ebac6b90%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages