hstaudacher / osgi-jax-rs-connector

An OSGi - JAX-RS 2.0 Connector, software repository available on the link below
http://hstaudacher.github.io/osgi-jax-rs-connector
Other
190 stars 98 forks source link

@Context annotation for HttpServletRequest is returning null #100

Closed parassingla18 closed 9 years ago

parassingla18 commented 9 years ago

Hi,

I have followed your security integration example and i am trying to override the following method public Principal authenticate(ContainerRequestContext requestContext) {}

While doing so, i need to fetch the HttpServletRequest object in order to fetch the session and other attributes in it. I have tried injecting it in my securityHandler() using @Context annotation but it is always returning null.

Following is the code snippet i am using.

public class MySecurityHandler implements AuthenticationHandler, AuthorizationHandler {

@Context private HttpServletRequest servletRequest;

    @Override
    public Principal authenticate(ContainerRequestContext requestContext) {   
        String username  = this.servletRequest.getAttribute("user");
}

}

But my servletRequest object is always null.

Any Suggestion/Recommendation in the above issue will be a lot of help.

Regards, Paras

hstaudacher commented 9 years ago

I think the @Context only works within Resources and feature or am I wrong?

parassingla18 commented 9 years ago

Thanks for your quick reply. So, Is there any other way of intercepting the HttpServletRequest before it goes to the REST API ?

I want to intercept each GET/POST request that is being made to my resource and perform some operations on HttpServletRequest object. Please suggest me a way to do it. Further Is it possible to extend the Jersey's default ServletContainer class so that i can perform the operations in Service() method?

hstaudacher commented 9 years ago

What's missing in the passed ContainerRequestContext? We use it to look for headers and cookies.

Regarding the service() method I think this is not possible right now.

parassingla18 commented 9 years ago

Let say we want to get the attributes which are present in HttpSession. How do we get that using ContainerRequestContext?

For eg: i need to fetch an attribute from the session called username. i can do that using following code snippet.

HttpSession session = req.getSession();// where req. is HttpServletRequest. String username = (String)session.getAttribute("username");

How can i achieve the same using ContainerRequestContext object?

hstaudacher commented 9 years ago

Hmm, I see. Can you try to annotate the MySecurityHandler with @Provider and registering it as OSGi service. After this please try to inject the HttpServletRequest with the @Context annotation again. Please leave a message if this has worked ;).

hstaudacher commented 9 years ago

Will close this issue. Please reopen if the problem still exists.

thugcee commented 8 years ago

This is probably not connected with osgi-jax-rs-connector, but I don't know where to ask about problems with Jersey in OSGi container.

I try to use @Context HttpServletRequest field injection in ContainerResponseFilter service (which is registered as a @Provider and is working OK without this field). Just after adding the field and restarting my bundle I get only

Jersey is not ready yet

There is no single line of any problems in logs and I don't know where to look. Any ideas?

aneelmonangi commented 3 years ago

Is there any update on this issue. Still facing this issue. Please help, if any fix available