Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot inject HttpServletRequest in ContainerRequestFilter via @Context jersey2.x and weblogic 12.1.3

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.

like image 419
ravi.sankar.ch Avatar asked Oct 28 '15 19:10

ravi.sankar.ch


2 Answers

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.

like image 84
PentaKon Avatar answered Nov 16 '22 00:11

PentaKon


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.

like image 22
jediz Avatar answered Nov 16 '22 01:11

jediz