11

I was not able to inject HttpServletRequest in ContainerRequestFilter via @Context in Jersey 2.22.1 using weblogic 12.1.3. I researched several places that this issue exists and in many places I see that it is fixed in Jersey 2.4, but I am still seeing this issue. My implementation and code is attached. Please let me know if I am missing anything.

AuthFilter

@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthFilter implements ContainerRequestFilter {
    @Context
    HttpServletRequest webRequest;

    @Context
    HttpServletResponse webResponse;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        final HttpSession session = webRequest.getSession();
        final String userName = (String)session.getAttribute("USER_NAME");

web.xml

<servlet>
        <servlet-name>jersey-application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
                  <param-name>jersey.config.server.provider.packages</param-name>
                  <param-value> xyz.xyz.xyz.xyz.xyz.resource</param-value>
              </init-param>

    </servlet>
    <servlet>
        <servlet-name>authentication-servlet</servlet-name>
        <servlet-class>xyz.xyz.xyz.xyz.xyz.xyz.AuthenticationServlet</servlet-class>

    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-application</servlet-name>
        <url-pattern>/jaxrs/*</url-pattern>
    </servlet-mapping>

Pom.xml

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.22.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.22.1</version>
</dependency>

<dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>2.22.1</version>
        <exclusions>
            <exclusion>
                <artifactId>spring-context</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-beans</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-core</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-web</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jersey-server</artifactId>
                <groupId>org.glassfish.jersey.core</groupId>
            </exclusion>
            <exclusion>
                <artifactId>
                    jersey-container-servlet-core
                </artifactId>
                <groupId>org.glassfish.jersey.containers</groupId>
            </exclusion>
            <exclusion>
                <artifactId>hk2</artifactId>
                <groupId>org.glassfish.hk2</groupId>
            </exclusion>
        </exclusions>
    </dependency>

Weblogic.xml

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
     <library-ref>
         <library-name>jax-rs</library-name>
         <specification-version>2.0</specification-version>
         <implementation-version>2.5.1</implementation-version>
    </library-ref>
    <container-descriptor>
    <prefer-application-packages>
        <!-- jsr311 -->
        <package-name>javax.ws.rs.*</package-name>
        <!-- javassist -->
        <package-name>javassist.*</package-name>
        <!-- aop repackaged -->
        <package-name>org.aopalliance.*</package-name>

        <!-- jersey 2 -->
        <package-name>jersey.repackaged.*</package-name>
        <package-name>org.glassfish.jersey.*</package-name>
        <package-name>com.sun.research.ws.wadl.*</package-name>

        <!-- hk2 -->
        <package-name>org.glassfish.hk2.*</package-name>
        <package-name>org.jvnet.hk2.*</package-name>
        <package-name>org.jvnet.tiger_types.*</package-name>
    </prefer-application-packages>
        <show-archived-real-path-enabled>true</show-archived-real-path-enabled>
        <resource-reload-check-secs>0</resource-reload-check-secs>
    </container-descriptor>
    <context-root>XYZ</context-root>

</weblogic-web-app>

And the error I see is below

Root cause of ServletException.
A MultiException has 4 exceptions.  They are:
1. java.lang.IllegalArgumentException: interface org.glassfish.hk2.api.ProxyCtl is not visible from class loader
2. java.lang.IllegalArgumentException: While attempting to create a Proxy for javax.servlet.http.HttpServletRequest in scope org.glassfish.jersey.process.internal.RequestScoped an error occured while creating the proxy
3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of xyz.xyz.xyz.xyz.xyz.xyz.xyz.AuthFilter errors were found
4. java.lang.IllegalStateException: Unable to perform operation: resolve on xyz.xyz.xyz.xyz.xyz.xyz.xyz.AuthFilter

    at org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:89)
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:249)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)

Please let me know in case of any workaround for this issue.

ravi.sankar.ch
  • 113
  • 1
  • 6
  • I have the exact same problem. Removing @Context fixes the issue. Also injecting the Request to any other resource class works fine! I've tried everything i've found nothing seems to be working... – PentaKon Nov 26 '15 at 15:19
  • Did you find a solution to the problem? – PentaKon Nov 30 '15 at 13:23
  • Havent got a chance to check your solution. I moved to weblogic 12.2.1 which doesnt seem to have this issue, except that I have to remove the prefer-application-packages from weblogic.xml. Thanks for the reply will let you know if I get a chance to test that out. – ravi.sankar.ch Dec 01 '15 at 14:35
  • You didn't understand correctly. I did not provide a solution. Removing @Context stops the exception but we WANT to have @Context! Otherwise I can't get a reference to the servlet request inside the ContainerRequestFilter and I NEED that. – PentaKon Dec 02 '15 at 10:40
  • You were right. After upgrading to weblogic 12.2.1 this bug doesn't appear anymore. It must have been a bug with the previous version. Shame I lost 2 days trying to find a solution. – PentaKon Dec 02 '15 at 13:22
  • sorry I got you wrong. Removing that "@Context HttpServletRequest webRequest" all together would not give any error. As this issue is a existing one in few previous releases, I left comments in https://java.net/jira/browse/JERSEY-2820 but did not receive any responses – ravi.sankar.ch Dec 03 '15 at 15:14

2 Answers2

5

It seems this is a bug with Weblogic 12.1.3. I tried upgrading to Weblogic 12.2.1 and injecting HttpServletRequest using @Context works correctly even inside ContainerRequestFilter.

PentaKon
  • 4,139
  • 5
  • 43
  • 80
2

As an alternative to typically preferred injection by @Context, @Inject, or @Autowire, you can inject HttpServletRequest into a request-scoped bean using Spring's static method:

HttpServletRequest request= 
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

Note that this approach is not ideal, because static methods make your code harder to test and make your code more rigid.

Community
  • 1
  • 1
jediz
  • 4,459
  • 5
  • 36
  • 41
  • I also had to add `org.springframework.web.context.request.RequestContextListener` to my web.xml to make this work. `RequestContextHolder.currentRequestAttributes()` throws an `IllegalArgumentException` if it's not there. – marstran Feb 14 '18 at 10:08