Returning a Object from filter back to resource












2















I am currently developing a REST webservice that uses a filter in order to handle the authentication (JWT token). The filter works fine but i am unable to return an object back to the resource (The object contains information about domain access and admin id).
The environment is CXF, Jersey, Jackson(JSON REST) running on a WSO2 application server version 5.3.0



I was using the ContainerRequestContext approach but I am getting a NullPointerException every time I try to access the context in my resource code. I have made a simple test case webservice to specifically try the object returning, but this also gets a NullPointerException in the resource code. Keep in mind the ContainerRequestContext does work in the filter code; I tested this earlier with console debugging.



See below for the code I am using now.
All help is greatly appreciated.



ContextTest.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/")
@Produces("application/json")
public class ContextTest
{
@Context
ResourceContext resourceContext;

ContainerRequestContext containerRequestContext;

public ContextTest()
{

}

@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");

Map<String, String> myResponse = new HashMap<String, String>();

containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

if(containerRequestContext != null)
{
if(containerRequestContext.getProperty("AuthID") != null)
{
if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");

GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}


AuthorizationNeededInterface.java



package test.codehq.fab1.contexttest.cxf3test1;

import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{

}


AuthorizationNeeded.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.io.IOException;
import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;

@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{
@Context
ResourceContext resourceContext;

@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");
resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
}
}


cxf-servlet.xml



<?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
<bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

<jaxrs:server id="cxf3test1Service" address="/cxf3test1">
<jaxrs:serviceBeans>
<ref bean="ContextTestBean"/>
</jaxrs:serviceBeans>

<jaxrs:providers>
<ref bean="jsonProvider" />
<ref bean="AuthorizationNeededProvider" />
</jaxrs:providers>

<jaxrs:features>
<cxf:logging/>
</jaxrs:features>
</jaxrs:server>
</beans>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestCXF3test1</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>


pom.xml



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestCXF3test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
</dependencies>
</project>


When doing a request on the 'echos' resource i get the following exception:



java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor
org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider.<init>(PerRequestResourceProvider.java:57)
org.apache.cxf.jaxrs.impl.ResourceContextImpl.getResource(ResourceContextImpl.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.apache.cxf.jaxrs.impl.tl.ThreadLocalInvocationHandler.invoke(ThreadLocalInvocationHandler.java:48)
com.sun.proxy.$Proxy1694.getResource(Unknown Source)
test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded.filter(AuthorizationNeeded.java:23)
org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerRequestFilters(JAXRSUtils.java:1688)









share|improve this question

























  • You're not even using Jersey man. You are using CXF. You can't use both of them at the same time. Jersey supports the injection of the ContainerRequestContext, but this is not portable (it's not a feature of the JAX-RS specification). It is specific to the Jersey implementation. If you want to use Jersey instead of CXF, then you need to configure the Jersey servlet in your web.xml, not the CXF one (looks for examples for Jersey 1.x where the ServletContainer is declared as the <servlet-class>.

    – Paul Samsotha
    Nov 27 '18 at 9:35













  • As an aside, please use Java naming convention; variable and method names begin with lowercase letters. Another aside, your resource classes are not supposed to have any of the following annotations: @WebService, @XmlRootElement, @XmlAccessorType, @Provider.

    – Paul Samsotha
    Nov 27 '18 at 9:39













  • Thank you Paul for pointing out my cxf/jersey mistake. I have tried to convert the project to a Jersey based solution and in fact that works. I now have updated the code in my question to reflect the minor changes and also tried that for a CXF based solution. I can't get it to work. I'm getting the following error everytime a do a request on the 'echos' resource: java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor

    – Patrick Peters
    Dec 4 '18 at 15:59


















2















I am currently developing a REST webservice that uses a filter in order to handle the authentication (JWT token). The filter works fine but i am unable to return an object back to the resource (The object contains information about domain access and admin id).
The environment is CXF, Jersey, Jackson(JSON REST) running on a WSO2 application server version 5.3.0



I was using the ContainerRequestContext approach but I am getting a NullPointerException every time I try to access the context in my resource code. I have made a simple test case webservice to specifically try the object returning, but this also gets a NullPointerException in the resource code. Keep in mind the ContainerRequestContext does work in the filter code; I tested this earlier with console debugging.



See below for the code I am using now.
All help is greatly appreciated.



ContextTest.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/")
@Produces("application/json")
public class ContextTest
{
@Context
ResourceContext resourceContext;

ContainerRequestContext containerRequestContext;

public ContextTest()
{

}

@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");

Map<String, String> myResponse = new HashMap<String, String>();

containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

if(containerRequestContext != null)
{
if(containerRequestContext.getProperty("AuthID") != null)
{
if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");

GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}


AuthorizationNeededInterface.java



package test.codehq.fab1.contexttest.cxf3test1;

import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{

}


AuthorizationNeeded.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.io.IOException;
import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;

@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{
@Context
ResourceContext resourceContext;

@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");
resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
}
}


cxf-servlet.xml



<?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
<bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

<jaxrs:server id="cxf3test1Service" address="/cxf3test1">
<jaxrs:serviceBeans>
<ref bean="ContextTestBean"/>
</jaxrs:serviceBeans>

<jaxrs:providers>
<ref bean="jsonProvider" />
<ref bean="AuthorizationNeededProvider" />
</jaxrs:providers>

<jaxrs:features>
<cxf:logging/>
</jaxrs:features>
</jaxrs:server>
</beans>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestCXF3test1</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>


pom.xml



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestCXF3test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
</dependencies>
</project>


When doing a request on the 'echos' resource i get the following exception:



java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor
org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider.<init>(PerRequestResourceProvider.java:57)
org.apache.cxf.jaxrs.impl.ResourceContextImpl.getResource(ResourceContextImpl.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.apache.cxf.jaxrs.impl.tl.ThreadLocalInvocationHandler.invoke(ThreadLocalInvocationHandler.java:48)
com.sun.proxy.$Proxy1694.getResource(Unknown Source)
test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded.filter(AuthorizationNeeded.java:23)
org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerRequestFilters(JAXRSUtils.java:1688)









share|improve this question

























  • You're not even using Jersey man. You are using CXF. You can't use both of them at the same time. Jersey supports the injection of the ContainerRequestContext, but this is not portable (it's not a feature of the JAX-RS specification). It is specific to the Jersey implementation. If you want to use Jersey instead of CXF, then you need to configure the Jersey servlet in your web.xml, not the CXF one (looks for examples for Jersey 1.x where the ServletContainer is declared as the <servlet-class>.

    – Paul Samsotha
    Nov 27 '18 at 9:35













  • As an aside, please use Java naming convention; variable and method names begin with lowercase letters. Another aside, your resource classes are not supposed to have any of the following annotations: @WebService, @XmlRootElement, @XmlAccessorType, @Provider.

    – Paul Samsotha
    Nov 27 '18 at 9:39













  • Thank you Paul for pointing out my cxf/jersey mistake. I have tried to convert the project to a Jersey based solution and in fact that works. I now have updated the code in my question to reflect the minor changes and also tried that for a CXF based solution. I can't get it to work. I'm getting the following error everytime a do a request on the 'echos' resource: java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor

    – Patrick Peters
    Dec 4 '18 at 15:59
















2












2








2








I am currently developing a REST webservice that uses a filter in order to handle the authentication (JWT token). The filter works fine but i am unable to return an object back to the resource (The object contains information about domain access and admin id).
The environment is CXF, Jersey, Jackson(JSON REST) running on a WSO2 application server version 5.3.0



I was using the ContainerRequestContext approach but I am getting a NullPointerException every time I try to access the context in my resource code. I have made a simple test case webservice to specifically try the object returning, but this also gets a NullPointerException in the resource code. Keep in mind the ContainerRequestContext does work in the filter code; I tested this earlier with console debugging.



See below for the code I am using now.
All help is greatly appreciated.



ContextTest.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/")
@Produces("application/json")
public class ContextTest
{
@Context
ResourceContext resourceContext;

ContainerRequestContext containerRequestContext;

public ContextTest()
{

}

@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");

Map<String, String> myResponse = new HashMap<String, String>();

containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

if(containerRequestContext != null)
{
if(containerRequestContext.getProperty("AuthID") != null)
{
if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");

GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}


AuthorizationNeededInterface.java



package test.codehq.fab1.contexttest.cxf3test1;

import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{

}


AuthorizationNeeded.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.io.IOException;
import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;

@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{
@Context
ResourceContext resourceContext;

@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");
resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
}
}


cxf-servlet.xml



<?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
<bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

<jaxrs:server id="cxf3test1Service" address="/cxf3test1">
<jaxrs:serviceBeans>
<ref bean="ContextTestBean"/>
</jaxrs:serviceBeans>

<jaxrs:providers>
<ref bean="jsonProvider" />
<ref bean="AuthorizationNeededProvider" />
</jaxrs:providers>

<jaxrs:features>
<cxf:logging/>
</jaxrs:features>
</jaxrs:server>
</beans>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestCXF3test1</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>


pom.xml



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestCXF3test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
</dependencies>
</project>


When doing a request on the 'echos' resource i get the following exception:



java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor
org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider.<init>(PerRequestResourceProvider.java:57)
org.apache.cxf.jaxrs.impl.ResourceContextImpl.getResource(ResourceContextImpl.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.apache.cxf.jaxrs.impl.tl.ThreadLocalInvocationHandler.invoke(ThreadLocalInvocationHandler.java:48)
com.sun.proxy.$Proxy1694.getResource(Unknown Source)
test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded.filter(AuthorizationNeeded.java:23)
org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerRequestFilters(JAXRSUtils.java:1688)









share|improve this question
















I am currently developing a REST webservice that uses a filter in order to handle the authentication (JWT token). The filter works fine but i am unable to return an object back to the resource (The object contains information about domain access and admin id).
The environment is CXF, Jersey, Jackson(JSON REST) running on a WSO2 application server version 5.3.0



I was using the ContainerRequestContext approach but I am getting a NullPointerException every time I try to access the context in my resource code. I have made a simple test case webservice to specifically try the object returning, but this also gets a NullPointerException in the resource code. Keep in mind the ContainerRequestContext does work in the filter code; I tested this earlier with console debugging.



See below for the code I am using now.
All help is greatly appreciated.



ContextTest.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/")
@Produces("application/json")
public class ContextTest
{
@Context
ResourceContext resourceContext;

ContainerRequestContext containerRequestContext;

public ContextTest()
{

}

@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");

Map<String, String> myResponse = new HashMap<String, String>();

containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

if(containerRequestContext != null)
{
if(containerRequestContext.getProperty("AuthID") != null)
{
if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");

GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}


AuthorizationNeededInterface.java



package test.codehq.fab1.contexttest.cxf3test1;

import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{

}


AuthorizationNeeded.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.io.IOException;
import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;

@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{
@Context
ResourceContext resourceContext;

@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");
resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
}
}


cxf-servlet.xml



<?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
<bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

<jaxrs:server id="cxf3test1Service" address="/cxf3test1">
<jaxrs:serviceBeans>
<ref bean="ContextTestBean"/>
</jaxrs:serviceBeans>

<jaxrs:providers>
<ref bean="jsonProvider" />
<ref bean="AuthorizationNeededProvider" />
</jaxrs:providers>

<jaxrs:features>
<cxf:logging/>
</jaxrs:features>
</jaxrs:server>
</beans>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestCXF3test1</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>


pom.xml



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestCXF3test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
</dependencies>
</project>


When doing a request on the 'echos' resource i get the following exception:



java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor
org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider.<init>(PerRequestResourceProvider.java:57)
org.apache.cxf.jaxrs.impl.ResourceContextImpl.getResource(ResourceContextImpl.java:50)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.apache.cxf.jaxrs.impl.tl.ThreadLocalInvocationHandler.invoke(ThreadLocalInvocationHandler.java:48)
com.sun.proxy.$Proxy1694.getResource(Unknown Source)
test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded.filter(AuthorizationNeeded.java:23)
org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerRequestFilters(JAXRSUtils.java:1688)






java rest jersey cxf wso2carbon






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 12 '18 at 11:16







Patrick Peters

















asked Nov 27 '18 at 8:34









Patrick PetersPatrick Peters

113




113













  • You're not even using Jersey man. You are using CXF. You can't use both of them at the same time. Jersey supports the injection of the ContainerRequestContext, but this is not portable (it's not a feature of the JAX-RS specification). It is specific to the Jersey implementation. If you want to use Jersey instead of CXF, then you need to configure the Jersey servlet in your web.xml, not the CXF one (looks for examples for Jersey 1.x where the ServletContainer is declared as the <servlet-class>.

    – Paul Samsotha
    Nov 27 '18 at 9:35













  • As an aside, please use Java naming convention; variable and method names begin with lowercase letters. Another aside, your resource classes are not supposed to have any of the following annotations: @WebService, @XmlRootElement, @XmlAccessorType, @Provider.

    – Paul Samsotha
    Nov 27 '18 at 9:39













  • Thank you Paul for pointing out my cxf/jersey mistake. I have tried to convert the project to a Jersey based solution and in fact that works. I now have updated the code in my question to reflect the minor changes and also tried that for a CXF based solution. I can't get it to work. I'm getting the following error everytime a do a request on the 'echos' resource: java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor

    – Patrick Peters
    Dec 4 '18 at 15:59





















  • You're not even using Jersey man. You are using CXF. You can't use both of them at the same time. Jersey supports the injection of the ContainerRequestContext, but this is not portable (it's not a feature of the JAX-RS specification). It is specific to the Jersey implementation. If you want to use Jersey instead of CXF, then you need to configure the Jersey servlet in your web.xml, not the CXF one (looks for examples for Jersey 1.x where the ServletContainer is declared as the <servlet-class>.

    – Paul Samsotha
    Nov 27 '18 at 9:35













  • As an aside, please use Java naming convention; variable and method names begin with lowercase letters. Another aside, your resource classes are not supposed to have any of the following annotations: @WebService, @XmlRootElement, @XmlAccessorType, @Provider.

    – Paul Samsotha
    Nov 27 '18 at 9:39













  • Thank you Paul for pointing out my cxf/jersey mistake. I have tried to convert the project to a Jersey based solution and in fact that works. I now have updated the code in my question to reflect the minor changes and also tried that for a CXF based solution. I can't get it to work. I'm getting the following error everytime a do a request on the 'echos' resource: java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor

    – Patrick Peters
    Dec 4 '18 at 15:59



















You're not even using Jersey man. You are using CXF. You can't use both of them at the same time. Jersey supports the injection of the ContainerRequestContext, but this is not portable (it's not a feature of the JAX-RS specification). It is specific to the Jersey implementation. If you want to use Jersey instead of CXF, then you need to configure the Jersey servlet in your web.xml, not the CXF one (looks for examples for Jersey 1.x where the ServletContainer is declared as the <servlet-class>.

– Paul Samsotha
Nov 27 '18 at 9:35







You're not even using Jersey man. You are using CXF. You can't use both of them at the same time. Jersey supports the injection of the ContainerRequestContext, but this is not portable (it's not a feature of the JAX-RS specification). It is specific to the Jersey implementation. If you want to use Jersey instead of CXF, then you need to configure the Jersey servlet in your web.xml, not the CXF one (looks for examples for Jersey 1.x where the ServletContainer is declared as the <servlet-class>.

– Paul Samsotha
Nov 27 '18 at 9:35















As an aside, please use Java naming convention; variable and method names begin with lowercase letters. Another aside, your resource classes are not supposed to have any of the following annotations: @WebService, @XmlRootElement, @XmlAccessorType, @Provider.

– Paul Samsotha
Nov 27 '18 at 9:39







As an aside, please use Java naming convention; variable and method names begin with lowercase letters. Another aside, your resource classes are not supposed to have any of the following annotations: @WebService, @XmlRootElement, @XmlAccessorType, @Provider.

– Paul Samsotha
Nov 27 '18 at 9:39















Thank you Paul for pointing out my cxf/jersey mistake. I have tried to convert the project to a Jersey based solution and in fact that works. I now have updated the code in my question to reflect the minor changes and also tried that for a CXF based solution. I can't get it to work. I'm getting the following error everytime a do a request on the 'echos' resource: java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor

– Patrick Peters
Dec 4 '18 at 15:59







Thank you Paul for pointing out my cxf/jersey mistake. I have tried to convert the project to a Jersey based solution and in fact that works. I now have updated the code in my question to reflect the minor changes and also tried that for a CXF based solution. I can't get it to work. I'm getting the following error everytime a do a request on the 'echos' resource: java.lang.RuntimeException: Resource class interface javax.ws.rs.container.ContainerRequestContext has no valid constructor

– Patrick Peters
Dec 4 '18 at 15:59














1 Answer
1






active

oldest

votes


















0















UPDATE I have solved the question. The code that doesn't work is still in this question so keep that in mind. I also posted the code of
the CXF and Jersey versions that do work. See those versions below the
dotted line. I think that in CXF the ContainerRequestContext is not available in a resource class. I don't know why Jersey makes this possible, but that just looks like the way they implemented it. I have found the solution by looking at an example from lefloh, How to Pass Object from ContainerRequestFilter to Resource
Thank you, lefloh for pointing out the fact that ContainerRequestFilter properties are synchronized with HttpServletRequest.







CXF version (working)




ContextTest.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.ManagedBean;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@ManagedBean
@Path("/")
@Produces("application/json")
public class ContextTest
{
// Keep in mind that the ContainerRequestContext
// attributes are automatically synced with
// HttpServletRequest. When using CXF the
// ContainerRequestContext can not be directly
// accessed in the Resource, so we need to use
// HttpServletRequest
@Context
HttpServletRequest httpServletRequest;

public ContextTest()
{

}

@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");

Map<String, String> myResponse = new HashMap<String, String>();

if(httpServletRequest != null)
{
if(httpServletRequest.getAttribute("AuthID") != null)
{
if(((String)httpServletRequest.getAttribute("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");

GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}


AuthorizationNeededInterface.java



package test.codehq.fab1.contexttest.cxf3test1;

import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{

}


AuthorizationNeeded.java



package test.codehq.fab1.contexttest.cxf3test1;

import java.io.IOException;

import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;

@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{

public AuthorizationNeeded()
{

}

@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");

conRequestContext.setProperty("AuthID", "1234");
}
}


cxf-servlet.xml



<?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
<bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

<jaxrs:server id="cxf3test1Service" address="/cxf3test1">
<jaxrs:serviceBeans>
<ref bean="ContextTestBean"/>
</jaxrs:serviceBeans>

<jaxrs:providers>
<ref bean="ContextTestBean"/>
<ref bean="jsonProvider" />
<ref bean="AuthorizationNeededProvider" />
</jaxrs:providers>
</jaxrs:server>
</beans>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestCXF3test1</display-name>

<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>


pom.xml



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestCXF3test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-compatible</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
</dependencies>
</project>



Jersey version (working)




ContextTest.java



package test.codehq.fab1.contexttest.jersey2test1;

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/")
@Produces("application/json")
public class ContextTest
{
@Context
ResourceContext resourceContext;

ContainerRequestContext containerRequestContext;

public ContextTest()
{

}

@AuthorizationNeededInterface
@GET
@Path("/echos/{echoRequestText}")
@Produces({MediaType.APPLICATION_JSON})
public Response echo(@PathParam("echoRequestText") String echoRequestText)
{
System.out.println("DEBUG: IN RESOURCE");

Map<String, String> myResponse = new HashMap<String, String>();

containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

if(containerRequestContext != null)
{
if(containerRequestContext.getProperty("AuthID") != null)
{
if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
{
myResponse.put("echo_response", echoRequestText);
}
else myResponse.put("echo_response", "Invalid AuthID <1>");
}
else myResponse.put("echo_response", "Invalid AuthID <2>");
}
else myResponse.put("echo_response", "Invalid AuthID <3>");

GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
return Response.ok().entity(genericEntity).build();
}
}


AuthorizationNeededInterface.java



package test.codehq.fab1.contexttest.jersey2test1;

import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface AuthorizationNeededInterface
{

}


AuthorizationNeeded.java



package test.codehq.fab1.contexttest.jersey2test1;

import java.io.IOException;
import javax.ws.rs.ext.Provider;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;

@AuthorizationNeededInterface
@Provider
@Priority(1000)
public class AuthorizationNeeded implements ContainerRequestFilter
{
@Context
ResourceContext resourceContext;

@Override
public void filter(ContainerRequestContext conRequestContext) throws IOException
{
System.out.println("DEBUG: IN FILTER");
resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
}
}


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ContextTestJersey2test1</display-name>
<servlet>
<description>Jersey Filter test</description>
<display-name>contexttest-jersey2test1</display-name>
<servlet-name>contexttest-jersey2test1</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>test.codehq.fab1.contexttest.jersey2test1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>contexttest-jersey2test1</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>


pom.xml



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.codinghq.fab1</groupId>
<artifactId>ContextTestJersey2Test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.9.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json</artifactId>
<version>2.0-m05-1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
</dependency>
</dependencies>
</project>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53495565%2freturning-a-object-from-filter-back-to-resource%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    UPDATE I have solved the question. The code that doesn't work is still in this question so keep that in mind. I also posted the code of
    the CXF and Jersey versions that do work. See those versions below the
    dotted line. I think that in CXF the ContainerRequestContext is not available in a resource class. I don't know why Jersey makes this possible, but that just looks like the way they implemented it. I have found the solution by looking at an example from lefloh, How to Pass Object from ContainerRequestFilter to Resource
    Thank you, lefloh for pointing out the fact that ContainerRequestFilter properties are synchronized with HttpServletRequest.







    CXF version (working)




    ContextTest.java



    package test.codehq.fab1.contexttest.cxf3test1;

    import java.util.HashMap;
    import java.util.Map;

    import javax.annotation.ManagedBean;
    import javax.servlet.http.HttpServletRequest;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.GenericEntity;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;

    @ManagedBean
    @Path("/")
    @Produces("application/json")
    public class ContextTest
    {
    // Keep in mind that the ContainerRequestContext
    // attributes are automatically synced with
    // HttpServletRequest. When using CXF the
    // ContainerRequestContext can not be directly
    // accessed in the Resource, so we need to use
    // HttpServletRequest
    @Context
    HttpServletRequest httpServletRequest;

    public ContextTest()
    {

    }

    @AuthorizationNeededInterface
    @GET
    @Path("/echos/{echoRequestText}")
    @Produces({MediaType.APPLICATION_JSON})
    public Response echo(@PathParam("echoRequestText") String echoRequestText)
    {
    System.out.println("DEBUG: IN RESOURCE");

    Map<String, String> myResponse = new HashMap<String, String>();

    if(httpServletRequest != null)
    {
    if(httpServletRequest.getAttribute("AuthID") != null)
    {
    if(((String)httpServletRequest.getAttribute("AuthID")).compareTo("1234") == 0)
    {
    myResponse.put("echo_response", echoRequestText);
    }
    else myResponse.put("echo_response", "Invalid AuthID <1>");
    }
    else myResponse.put("echo_response", "Invalid AuthID <2>");
    }
    else myResponse.put("echo_response", "Invalid AuthID <3>");

    GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
    return Response.ok().entity(genericEntity).build();
    }
    }


    AuthorizationNeededInterface.java



    package test.codehq.fab1.contexttest.cxf3test1;

    import javax.ws.rs.NameBinding;
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;

    import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.ElementType.TYPE;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;

    @NameBinding
    @Retention(RUNTIME)
    @Target({TYPE, METHOD})
    public @interface AuthorizationNeededInterface
    {

    }


    AuthorizationNeeded.java



    package test.codehq.fab1.contexttest.cxf3test1;

    import java.io.IOException;

    import javax.ws.rs.ext.Provider;
    import javax.annotation.Priority;
    import javax.ws.rs.container.ContainerRequestContext;
    import javax.ws.rs.container.ContainerRequestFilter;

    @AuthorizationNeededInterface
    @Provider
    @Priority(1000)
    public class AuthorizationNeeded implements ContainerRequestFilter
    {

    public AuthorizationNeeded()
    {

    }

    @Override
    public void filter(ContainerRequestContext conRequestContext) throws IOException
    {
    System.out.println("DEBUG: IN FILTER");

    conRequestContext.setProperty("AuthID", "1234");
    }
    }


    cxf-servlet.xml



    <?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
    <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
    <bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

    <jaxrs:server id="cxf3test1Service" address="/cxf3test1">
    <jaxrs:serviceBeans>
    <ref bean="ContextTestBean"/>
    </jaxrs:serviceBeans>

    <jaxrs:providers>
    <ref bean="ContextTestBean"/>
    <ref bean="jsonProvider" />
    <ref bean="AuthorizationNeededProvider" />
    </jaxrs:providers>
    </jaxrs:server>
    </beans>


    web.xml



    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>ContextTestCXF3test1</display-name>

    <servlet>
    <description>Apache CXF Endpoint</description>
    <display-name>cxf</display-name>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <session-config>
    <session-timeout>60</session-timeout>
    </session-config>
    </web-app>


    pom.xml



    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test.codinghq.fab1</groupId>
    <artifactId>ContextTestCXF3test1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.1</version>
    <configuration>
    <warSourceDirectory>WebContent</warSourceDirectory>
    </configuration>
    </plugin>
    </plugins>
    </build>
    <dependencies>
    <dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
    </dependency>
    <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
    </dependency>
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-bundle-compatible</artifactId>
    <version>3.2.7</version>
    </dependency>
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-core</artifactId>
    <version>3.2.7</version>
    </dependency>
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    <version>3.2.7</version>
    </dependency>
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-extension-providers</artifactId>
    <version>3.2.7</version>
    </dependency>
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-features-logging</artifactId>
    <version>3.2.7</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-providers</artifactId>
    <version>2.9.7</version>
    <type>pom</type>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.7</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.7</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.7</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.9.7</version>
    </dependency>
    <dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.21.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.21.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.21.RELEASE</version>
    </dependency>
    </dependencies>
    </project>



    Jersey version (working)




    ContextTest.java



    package test.codehq.fab1.contexttest.jersey2test1;

    import java.util.HashMap;
    import java.util.Map;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.container.ContainerRequestContext;
    import javax.ws.rs.container.ResourceContext;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.GenericEntity;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;

    @Path("/")
    @Produces("application/json")
    public class ContextTest
    {
    @Context
    ResourceContext resourceContext;

    ContainerRequestContext containerRequestContext;

    public ContextTest()
    {

    }

    @AuthorizationNeededInterface
    @GET
    @Path("/echos/{echoRequestText}")
    @Produces({MediaType.APPLICATION_JSON})
    public Response echo(@PathParam("echoRequestText") String echoRequestText)
    {
    System.out.println("DEBUG: IN RESOURCE");

    Map<String, String> myResponse = new HashMap<String, String>();

    containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

    if(containerRequestContext != null)
    {
    if(containerRequestContext.getProperty("AuthID") != null)
    {
    if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
    {
    myResponse.put("echo_response", echoRequestText);
    }
    else myResponse.put("echo_response", "Invalid AuthID <1>");
    }
    else myResponse.put("echo_response", "Invalid AuthID <2>");
    }
    else myResponse.put("echo_response", "Invalid AuthID <3>");

    GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
    return Response.ok().entity(genericEntity).build();
    }
    }


    AuthorizationNeededInterface.java



    package test.codehq.fab1.contexttest.jersey2test1;

    import javax.ws.rs.NameBinding;
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;

    import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.ElementType.TYPE;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;

    @NameBinding
    @Retention(RUNTIME)
    @Target({TYPE, METHOD})
    public @interface AuthorizationNeededInterface
    {

    }


    AuthorizationNeeded.java



    package test.codehq.fab1.contexttest.jersey2test1;

    import java.io.IOException;
    import javax.ws.rs.ext.Provider;
    import javax.annotation.Priority;
    import javax.ws.rs.container.ContainerRequestContext;
    import javax.ws.rs.container.ContainerRequestFilter;
    import javax.ws.rs.container.ResourceContext;
    import javax.ws.rs.core.Context;

    @AuthorizationNeededInterface
    @Provider
    @Priority(1000)
    public class AuthorizationNeeded implements ContainerRequestFilter
    {
    @Context
    ResourceContext resourceContext;

    @Override
    public void filter(ContainerRequestContext conRequestContext) throws IOException
    {
    System.out.println("DEBUG: IN FILTER");
    resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
    }
    }


    web.xml



    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>ContextTestJersey2test1</display-name>
    <servlet>
    <description>Jersey Filter test</description>
    <display-name>contexttest-jersey2test1</display-name>
    <servlet-name>contexttest-jersey2test1</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>test.codehq.fab1.contexttest.jersey2test1</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>contexttest-jersey2test1</servlet-name>
    <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <session-config>
    <session-timeout>60</session-timeout>
    </session-config>
    </web-app>


    pom.xml



    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test.codinghq.fab1</groupId>
    <artifactId>ContextTestJersey2Test1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.1</version>
    <configuration>
    <warSourceDirectory>WebContent</warSourceDirectory>
    </configuration>
    </plugin>
    </plugins>
    </build>
    <dependencies>
    <dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
    </dependency>
    <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-providers</artifactId>
    <version>2.9.7</version>
    <type>pom</type>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.7</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.7</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.7</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.0.7.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.0.7.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.7.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.9.7</version>
    </dependency>
    <dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.27</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json</artifactId>
    <version>2.0-m05-1</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.27</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.27</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.27</version>
    </dependency>
    </dependencies>
    </project>





    share|improve this answer




























      0















      UPDATE I have solved the question. The code that doesn't work is still in this question so keep that in mind. I also posted the code of
      the CXF and Jersey versions that do work. See those versions below the
      dotted line. I think that in CXF the ContainerRequestContext is not available in a resource class. I don't know why Jersey makes this possible, but that just looks like the way they implemented it. I have found the solution by looking at an example from lefloh, How to Pass Object from ContainerRequestFilter to Resource
      Thank you, lefloh for pointing out the fact that ContainerRequestFilter properties are synchronized with HttpServletRequest.







      CXF version (working)




      ContextTest.java



      package test.codehq.fab1.contexttest.cxf3test1;

      import java.util.HashMap;
      import java.util.Map;

      import javax.annotation.ManagedBean;
      import javax.servlet.http.HttpServletRequest;
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.PathParam;
      import javax.ws.rs.Produces;
      import javax.ws.rs.core.Context;
      import javax.ws.rs.core.GenericEntity;
      import javax.ws.rs.core.MediaType;
      import javax.ws.rs.core.Response;

      @ManagedBean
      @Path("/")
      @Produces("application/json")
      public class ContextTest
      {
      // Keep in mind that the ContainerRequestContext
      // attributes are automatically synced with
      // HttpServletRequest. When using CXF the
      // ContainerRequestContext can not be directly
      // accessed in the Resource, so we need to use
      // HttpServletRequest
      @Context
      HttpServletRequest httpServletRequest;

      public ContextTest()
      {

      }

      @AuthorizationNeededInterface
      @GET
      @Path("/echos/{echoRequestText}")
      @Produces({MediaType.APPLICATION_JSON})
      public Response echo(@PathParam("echoRequestText") String echoRequestText)
      {
      System.out.println("DEBUG: IN RESOURCE");

      Map<String, String> myResponse = new HashMap<String, String>();

      if(httpServletRequest != null)
      {
      if(httpServletRequest.getAttribute("AuthID") != null)
      {
      if(((String)httpServletRequest.getAttribute("AuthID")).compareTo("1234") == 0)
      {
      myResponse.put("echo_response", echoRequestText);
      }
      else myResponse.put("echo_response", "Invalid AuthID <1>");
      }
      else myResponse.put("echo_response", "Invalid AuthID <2>");
      }
      else myResponse.put("echo_response", "Invalid AuthID <3>");

      GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
      return Response.ok().entity(genericEntity).build();
      }
      }


      AuthorizationNeededInterface.java



      package test.codehq.fab1.contexttest.cxf3test1;

      import javax.ws.rs.NameBinding;
      import java.lang.annotation.Retention;
      import java.lang.annotation.Target;

      import static java.lang.annotation.ElementType.METHOD;
      import static java.lang.annotation.ElementType.TYPE;
      import static java.lang.annotation.RetentionPolicy.RUNTIME;

      @NameBinding
      @Retention(RUNTIME)
      @Target({TYPE, METHOD})
      public @interface AuthorizationNeededInterface
      {

      }


      AuthorizationNeeded.java



      package test.codehq.fab1.contexttest.cxf3test1;

      import java.io.IOException;

      import javax.ws.rs.ext.Provider;
      import javax.annotation.Priority;
      import javax.ws.rs.container.ContainerRequestContext;
      import javax.ws.rs.container.ContainerRequestFilter;

      @AuthorizationNeededInterface
      @Provider
      @Priority(1000)
      public class AuthorizationNeeded implements ContainerRequestFilter
      {

      public AuthorizationNeeded()
      {

      }

      @Override
      public void filter(ContainerRequestContext conRequestContext) throws IOException
      {
      System.out.println("DEBUG: IN FILTER");

      conRequestContext.setProperty("AuthID", "1234");
      }
      }


      cxf-servlet.xml



      <?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
      <bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
      <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
      <bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

      <jaxrs:server id="cxf3test1Service" address="/cxf3test1">
      <jaxrs:serviceBeans>
      <ref bean="ContextTestBean"/>
      </jaxrs:serviceBeans>

      <jaxrs:providers>
      <ref bean="ContextTestBean"/>
      <ref bean="jsonProvider" />
      <ref bean="AuthorizationNeededProvider" />
      </jaxrs:providers>
      </jaxrs:server>
      </beans>


      web.xml



      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>ContextTestCXF3test1</display-name>

      <servlet>
      <description>Apache CXF Endpoint</description>
      <display-name>cxf</display-name>
      <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
      <servlet-name>cxf</servlet-name>
      <url-pattern>/services/*</url-pattern>
      </servlet-mapping>

      <session-config>
      <session-timeout>60</session-timeout>
      </session-config>
      </web-app>


      pom.xml



      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>test.codinghq.fab1</groupId>
      <artifactId>ContextTestCXF3test1</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
      <sourceDirectory>src</sourceDirectory>
      <plugins>
      <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
      <source>1.8</source>
      <target>1.8</target>
      </configuration>
      </plugin>
      <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <version>3.2.1</version>
      <configuration>
      <warSourceDirectory>WebContent</warSourceDirectory>
      </configuration>
      </plugin>
      </plugins>
      </build>
      <dependencies>
      <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
      <version>2.1</version>
      </dependency>
      <dependency>
      <groupId>javax.inject</groupId>
      <artifactId>javax.inject</artifactId>
      <version>1</version>
      </dependency>
      <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-bundle-compatible</artifactId>
      <version>3.2.7</version>
      </dependency>
      <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-core</artifactId>
      <version>3.2.7</version>
      </dependency>
      <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>3.2.7</version>
      </dependency>
      <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-extension-providers</artifactId>
      <version>3.2.7</version>
      </dependency>
      <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-features-logging</artifactId>
      <version>3.2.7</version>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-providers</artifactId>
      <version>2.9.7</version>
      <type>pom</type>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.7</version>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.7</version>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.7</version>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-json-provider</artifactId>
      <version>2.9.7</version>
      </dependency>
      <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.3.2</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.21.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.21.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.21.RELEASE</version>
      </dependency>
      </dependencies>
      </project>



      Jersey version (working)




      ContextTest.java



      package test.codehq.fab1.contexttest.jersey2test1;

      import java.util.HashMap;
      import java.util.Map;
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.PathParam;
      import javax.ws.rs.Produces;
      import javax.ws.rs.container.ContainerRequestContext;
      import javax.ws.rs.container.ResourceContext;
      import javax.ws.rs.core.Context;
      import javax.ws.rs.core.GenericEntity;
      import javax.ws.rs.core.MediaType;
      import javax.ws.rs.core.Response;

      @Path("/")
      @Produces("application/json")
      public class ContextTest
      {
      @Context
      ResourceContext resourceContext;

      ContainerRequestContext containerRequestContext;

      public ContextTest()
      {

      }

      @AuthorizationNeededInterface
      @GET
      @Path("/echos/{echoRequestText}")
      @Produces({MediaType.APPLICATION_JSON})
      public Response echo(@PathParam("echoRequestText") String echoRequestText)
      {
      System.out.println("DEBUG: IN RESOURCE");

      Map<String, String> myResponse = new HashMap<String, String>();

      containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

      if(containerRequestContext != null)
      {
      if(containerRequestContext.getProperty("AuthID") != null)
      {
      if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
      {
      myResponse.put("echo_response", echoRequestText);
      }
      else myResponse.put("echo_response", "Invalid AuthID <1>");
      }
      else myResponse.put("echo_response", "Invalid AuthID <2>");
      }
      else myResponse.put("echo_response", "Invalid AuthID <3>");

      GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
      return Response.ok().entity(genericEntity).build();
      }
      }


      AuthorizationNeededInterface.java



      package test.codehq.fab1.contexttest.jersey2test1;

      import javax.ws.rs.NameBinding;
      import java.lang.annotation.Retention;
      import java.lang.annotation.Target;

      import static java.lang.annotation.ElementType.METHOD;
      import static java.lang.annotation.ElementType.TYPE;
      import static java.lang.annotation.RetentionPolicy.RUNTIME;

      @NameBinding
      @Retention(RUNTIME)
      @Target({TYPE, METHOD})
      public @interface AuthorizationNeededInterface
      {

      }


      AuthorizationNeeded.java



      package test.codehq.fab1.contexttest.jersey2test1;

      import java.io.IOException;
      import javax.ws.rs.ext.Provider;
      import javax.annotation.Priority;
      import javax.ws.rs.container.ContainerRequestContext;
      import javax.ws.rs.container.ContainerRequestFilter;
      import javax.ws.rs.container.ResourceContext;
      import javax.ws.rs.core.Context;

      @AuthorizationNeededInterface
      @Provider
      @Priority(1000)
      public class AuthorizationNeeded implements ContainerRequestFilter
      {
      @Context
      ResourceContext resourceContext;

      @Override
      public void filter(ContainerRequestContext conRequestContext) throws IOException
      {
      System.out.println("DEBUG: IN FILTER");
      resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
      }
      }


      web.xml



      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>ContextTestJersey2test1</display-name>
      <servlet>
      <description>Jersey Filter test</description>
      <display-name>contexttest-jersey2test1</display-name>
      <servlet-name>contexttest-jersey2test1</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>test.codehq.fab1.contexttest.jersey2test1</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
      <servlet-name>contexttest-jersey2test1</servlet-name>
      <url-pattern>/rest/*</url-pattern>
      </servlet-mapping>
      <session-config>
      <session-timeout>60</session-timeout>
      </session-config>
      </web-app>


      pom.xml



      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>test.codinghq.fab1</groupId>
      <artifactId>ContextTestJersey2Test1</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
      <sourceDirectory>src</sourceDirectory>
      <plugins>
      <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
      <source>1.8</source>
      <target>1.8</target>
      </configuration>
      </plugin>
      <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <version>3.2.1</version>
      <configuration>
      <warSourceDirectory>WebContent</warSourceDirectory>
      </configuration>
      </plugin>
      </plugins>
      </build>
      <dependencies>
      <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
      <version>2.1</version>
      </dependency>
      <dependency>
      <groupId>javax.inject</groupId>
      <artifactId>javax.inject</artifactId>
      <version>1</version>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-providers</artifactId>
      <version>2.9.7</version>
      <type>pom</type>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.7</version>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.7</version>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.7</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.0.7.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>3.0.7.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.0.7.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-json-provider</artifactId>
      <version>2.9.7</version>
      </dependency>
      <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.3.2</version>
      </dependency>
      <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet-core</artifactId>
      <version>2.27</version>
      </dependency>
      <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json</artifactId>
      <version>2.0-m05-1</version>
      </dependency>
      <dependency>
      <groupId>org.glassfish.jersey.core</groupId>
      <artifactId>jersey-server</artifactId>
      <version>2.27</version>
      </dependency>
      <dependency>
      <groupId>org.glassfish.jersey.inject</groupId>
      <artifactId>jersey-hk2</artifactId>
      <version>2.27</version>
      </dependency>
      <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>2.27</version>
      </dependency>
      </dependencies>
      </project>





      share|improve this answer


























        0












        0








        0








        UPDATE I have solved the question. The code that doesn't work is still in this question so keep that in mind. I also posted the code of
        the CXF and Jersey versions that do work. See those versions below the
        dotted line. I think that in CXF the ContainerRequestContext is not available in a resource class. I don't know why Jersey makes this possible, but that just looks like the way they implemented it. I have found the solution by looking at an example from lefloh, How to Pass Object from ContainerRequestFilter to Resource
        Thank you, lefloh for pointing out the fact that ContainerRequestFilter properties are synchronized with HttpServletRequest.







        CXF version (working)




        ContextTest.java



        package test.codehq.fab1.contexttest.cxf3test1;

        import java.util.HashMap;
        import java.util.Map;

        import javax.annotation.ManagedBean;
        import javax.servlet.http.HttpServletRequest;
        import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.PathParam;
        import javax.ws.rs.Produces;
        import javax.ws.rs.core.Context;
        import javax.ws.rs.core.GenericEntity;
        import javax.ws.rs.core.MediaType;
        import javax.ws.rs.core.Response;

        @ManagedBean
        @Path("/")
        @Produces("application/json")
        public class ContextTest
        {
        // Keep in mind that the ContainerRequestContext
        // attributes are automatically synced with
        // HttpServletRequest. When using CXF the
        // ContainerRequestContext can not be directly
        // accessed in the Resource, so we need to use
        // HttpServletRequest
        @Context
        HttpServletRequest httpServletRequest;

        public ContextTest()
        {

        }

        @AuthorizationNeededInterface
        @GET
        @Path("/echos/{echoRequestText}")
        @Produces({MediaType.APPLICATION_JSON})
        public Response echo(@PathParam("echoRequestText") String echoRequestText)
        {
        System.out.println("DEBUG: IN RESOURCE");

        Map<String, String> myResponse = new HashMap<String, String>();

        if(httpServletRequest != null)
        {
        if(httpServletRequest.getAttribute("AuthID") != null)
        {
        if(((String)httpServletRequest.getAttribute("AuthID")).compareTo("1234") == 0)
        {
        myResponse.put("echo_response", echoRequestText);
        }
        else myResponse.put("echo_response", "Invalid AuthID <1>");
        }
        else myResponse.put("echo_response", "Invalid AuthID <2>");
        }
        else myResponse.put("echo_response", "Invalid AuthID <3>");

        GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
        return Response.ok().entity(genericEntity).build();
        }
        }


        AuthorizationNeededInterface.java



        package test.codehq.fab1.contexttest.cxf3test1;

        import javax.ws.rs.NameBinding;
        import java.lang.annotation.Retention;
        import java.lang.annotation.Target;

        import static java.lang.annotation.ElementType.METHOD;
        import static java.lang.annotation.ElementType.TYPE;
        import static java.lang.annotation.RetentionPolicy.RUNTIME;

        @NameBinding
        @Retention(RUNTIME)
        @Target({TYPE, METHOD})
        public @interface AuthorizationNeededInterface
        {

        }


        AuthorizationNeeded.java



        package test.codehq.fab1.contexttest.cxf3test1;

        import java.io.IOException;

        import javax.ws.rs.ext.Provider;
        import javax.annotation.Priority;
        import javax.ws.rs.container.ContainerRequestContext;
        import javax.ws.rs.container.ContainerRequestFilter;

        @AuthorizationNeededInterface
        @Provider
        @Priority(1000)
        public class AuthorizationNeeded implements ContainerRequestFilter
        {

        public AuthorizationNeeded()
        {

        }

        @Override
        public void filter(ContainerRequestContext conRequestContext) throws IOException
        {
        System.out.println("DEBUG: IN FILTER");

        conRequestContext.setProperty("AuthID", "1234");
        }
        }


        cxf-servlet.xml



        <?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
        <bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
        <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
        <bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

        <jaxrs:server id="cxf3test1Service" address="/cxf3test1">
        <jaxrs:serviceBeans>
        <ref bean="ContextTestBean"/>
        </jaxrs:serviceBeans>

        <jaxrs:providers>
        <ref bean="ContextTestBean"/>
        <ref bean="jsonProvider" />
        <ref bean="AuthorizationNeededProvider" />
        </jaxrs:providers>
        </jaxrs:server>
        </beans>


        web.xml



        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
        <display-name>ContextTestCXF3test1</display-name>

        <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
        </servlet-mapping>

        <session-config>
        <session-timeout>60</session-timeout>
        </session-config>
        </web-app>


        pom.xml



        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>test.codinghq.fab1</groupId>
        <artifactId>ContextTestCXF3test1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
        <source>1.8</source>
        <target>1.8</target>
        </configuration>
        </plugin>
        <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
        </plugin>
        </plugins>
        </build>
        <dependencies>
        <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
        </dependency>
        <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-bundle-compatible</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-features-logging</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-providers</artifactId>
        <version>2.9.7</version>
        <type>pom</type>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.21.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.21.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.21.RELEASE</version>
        </dependency>
        </dependencies>
        </project>



        Jersey version (working)




        ContextTest.java



        package test.codehq.fab1.contexttest.jersey2test1;

        import java.util.HashMap;
        import java.util.Map;
        import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.PathParam;
        import javax.ws.rs.Produces;
        import javax.ws.rs.container.ContainerRequestContext;
        import javax.ws.rs.container.ResourceContext;
        import javax.ws.rs.core.Context;
        import javax.ws.rs.core.GenericEntity;
        import javax.ws.rs.core.MediaType;
        import javax.ws.rs.core.Response;

        @Path("/")
        @Produces("application/json")
        public class ContextTest
        {
        @Context
        ResourceContext resourceContext;

        ContainerRequestContext containerRequestContext;

        public ContextTest()
        {

        }

        @AuthorizationNeededInterface
        @GET
        @Path("/echos/{echoRequestText}")
        @Produces({MediaType.APPLICATION_JSON})
        public Response echo(@PathParam("echoRequestText") String echoRequestText)
        {
        System.out.println("DEBUG: IN RESOURCE");

        Map<String, String> myResponse = new HashMap<String, String>();

        containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

        if(containerRequestContext != null)
        {
        if(containerRequestContext.getProperty("AuthID") != null)
        {
        if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
        {
        myResponse.put("echo_response", echoRequestText);
        }
        else myResponse.put("echo_response", "Invalid AuthID <1>");
        }
        else myResponse.put("echo_response", "Invalid AuthID <2>");
        }
        else myResponse.put("echo_response", "Invalid AuthID <3>");

        GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
        return Response.ok().entity(genericEntity).build();
        }
        }


        AuthorizationNeededInterface.java



        package test.codehq.fab1.contexttest.jersey2test1;

        import javax.ws.rs.NameBinding;
        import java.lang.annotation.Retention;
        import java.lang.annotation.Target;

        import static java.lang.annotation.ElementType.METHOD;
        import static java.lang.annotation.ElementType.TYPE;
        import static java.lang.annotation.RetentionPolicy.RUNTIME;

        @NameBinding
        @Retention(RUNTIME)
        @Target({TYPE, METHOD})
        public @interface AuthorizationNeededInterface
        {

        }


        AuthorizationNeeded.java



        package test.codehq.fab1.contexttest.jersey2test1;

        import java.io.IOException;
        import javax.ws.rs.ext.Provider;
        import javax.annotation.Priority;
        import javax.ws.rs.container.ContainerRequestContext;
        import javax.ws.rs.container.ContainerRequestFilter;
        import javax.ws.rs.container.ResourceContext;
        import javax.ws.rs.core.Context;

        @AuthorizationNeededInterface
        @Provider
        @Priority(1000)
        public class AuthorizationNeeded implements ContainerRequestFilter
        {
        @Context
        ResourceContext resourceContext;

        @Override
        public void filter(ContainerRequestContext conRequestContext) throws IOException
        {
        System.out.println("DEBUG: IN FILTER");
        resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
        }
        }


        web.xml



        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
        <display-name>ContextTestJersey2test1</display-name>
        <servlet>
        <description>Jersey Filter test</description>
        <display-name>contexttest-jersey2test1</display-name>
        <servlet-name>contexttest-jersey2test1</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>test.codehq.fab1.contexttest.jersey2test1</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
        <servlet-name>contexttest-jersey2test1</servlet-name>
        <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>
        <session-config>
        <session-timeout>60</session-timeout>
        </session-config>
        </web-app>


        pom.xml



        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>test.codinghq.fab1</groupId>
        <artifactId>ContextTestJersey2Test1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
        <source>1.8</source>
        <target>1.8</target>
        </configuration>
        </plugin>
        <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
        </plugin>
        </plugins>
        </build>
        <dependencies>
        <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
        </dependency>
        <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-providers</artifactId>
        <version>2.9.7</version>
        <type>pom</type>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.27</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json</artifactId>
        <version>2.0-m05-1</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.27</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.27</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.27</version>
        </dependency>
        </dependencies>
        </project>





        share|improve this answer














        UPDATE I have solved the question. The code that doesn't work is still in this question so keep that in mind. I also posted the code of
        the CXF and Jersey versions that do work. See those versions below the
        dotted line. I think that in CXF the ContainerRequestContext is not available in a resource class. I don't know why Jersey makes this possible, but that just looks like the way they implemented it. I have found the solution by looking at an example from lefloh, How to Pass Object from ContainerRequestFilter to Resource
        Thank you, lefloh for pointing out the fact that ContainerRequestFilter properties are synchronized with HttpServletRequest.







        CXF version (working)




        ContextTest.java



        package test.codehq.fab1.contexttest.cxf3test1;

        import java.util.HashMap;
        import java.util.Map;

        import javax.annotation.ManagedBean;
        import javax.servlet.http.HttpServletRequest;
        import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.PathParam;
        import javax.ws.rs.Produces;
        import javax.ws.rs.core.Context;
        import javax.ws.rs.core.GenericEntity;
        import javax.ws.rs.core.MediaType;
        import javax.ws.rs.core.Response;

        @ManagedBean
        @Path("/")
        @Produces("application/json")
        public class ContextTest
        {
        // Keep in mind that the ContainerRequestContext
        // attributes are automatically synced with
        // HttpServletRequest. When using CXF the
        // ContainerRequestContext can not be directly
        // accessed in the Resource, so we need to use
        // HttpServletRequest
        @Context
        HttpServletRequest httpServletRequest;

        public ContextTest()
        {

        }

        @AuthorizationNeededInterface
        @GET
        @Path("/echos/{echoRequestText}")
        @Produces({MediaType.APPLICATION_JSON})
        public Response echo(@PathParam("echoRequestText") String echoRequestText)
        {
        System.out.println("DEBUG: IN RESOURCE");

        Map<String, String> myResponse = new HashMap<String, String>();

        if(httpServletRequest != null)
        {
        if(httpServletRequest.getAttribute("AuthID") != null)
        {
        if(((String)httpServletRequest.getAttribute("AuthID")).compareTo("1234") == 0)
        {
        myResponse.put("echo_response", echoRequestText);
        }
        else myResponse.put("echo_response", "Invalid AuthID <1>");
        }
        else myResponse.put("echo_response", "Invalid AuthID <2>");
        }
        else myResponse.put("echo_response", "Invalid AuthID <3>");

        GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
        return Response.ok().entity(genericEntity).build();
        }
        }


        AuthorizationNeededInterface.java



        package test.codehq.fab1.contexttest.cxf3test1;

        import javax.ws.rs.NameBinding;
        import java.lang.annotation.Retention;
        import java.lang.annotation.Target;

        import static java.lang.annotation.ElementType.METHOD;
        import static java.lang.annotation.ElementType.TYPE;
        import static java.lang.annotation.RetentionPolicy.RUNTIME;

        @NameBinding
        @Retention(RUNTIME)
        @Target({TYPE, METHOD})
        public @interface AuthorizationNeededInterface
        {

        }


        AuthorizationNeeded.java



        package test.codehq.fab1.contexttest.cxf3test1;

        import java.io.IOException;

        import javax.ws.rs.ext.Provider;
        import javax.annotation.Priority;
        import javax.ws.rs.container.ContainerRequestContext;
        import javax.ws.rs.container.ContainerRequestFilter;

        @AuthorizationNeededInterface
        @Provider
        @Priority(1000)
        public class AuthorizationNeeded implements ContainerRequestFilter
        {

        public AuthorizationNeeded()
        {

        }

        @Override
        public void filter(ContainerRequestContext conRequestContext) throws IOException
        {
        System.out.println("DEBUG: IN FILTER");

        conRequestContext.setProperty("AuthID", "1234");
        }
        }


        cxf-servlet.xml



        <?xml version="1.0" encoding="UTF-8"?><beans xmlns:cxf="http://cxf.apache.org/core" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
        <bean id="ContextTestBean" class="test.codehq.fab1.contexttest.cxf3test1.ContextTest"/>
        <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
        <bean id="AuthorizationNeededProvider" class="test.codehq.fab1.contexttest.cxf3test1.AuthorizationNeeded"/>

        <jaxrs:server id="cxf3test1Service" address="/cxf3test1">
        <jaxrs:serviceBeans>
        <ref bean="ContextTestBean"/>
        </jaxrs:serviceBeans>

        <jaxrs:providers>
        <ref bean="ContextTestBean"/>
        <ref bean="jsonProvider" />
        <ref bean="AuthorizationNeededProvider" />
        </jaxrs:providers>
        </jaxrs:server>
        </beans>


        web.xml



        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
        <display-name>ContextTestCXF3test1</display-name>

        <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
        </servlet-mapping>

        <session-config>
        <session-timeout>60</session-timeout>
        </session-config>
        </web-app>


        pom.xml



        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>test.codinghq.fab1</groupId>
        <artifactId>ContextTestCXF3test1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
        <source>1.8</source>
        <target>1.8</target>
        </configuration>
        </plugin>
        <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
        </plugin>
        </plugins>
        </build>
        <dependencies>
        <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
        </dependency>
        <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-bundle-compatible</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-features-logging</artifactId>
        <version>3.2.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-providers</artifactId>
        <version>2.9.7</version>
        <type>pom</type>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.21.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.21.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.21.RELEASE</version>
        </dependency>
        </dependencies>
        </project>



        Jersey version (working)




        ContextTest.java



        package test.codehq.fab1.contexttest.jersey2test1;

        import java.util.HashMap;
        import java.util.Map;
        import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.PathParam;
        import javax.ws.rs.Produces;
        import javax.ws.rs.container.ContainerRequestContext;
        import javax.ws.rs.container.ResourceContext;
        import javax.ws.rs.core.Context;
        import javax.ws.rs.core.GenericEntity;
        import javax.ws.rs.core.MediaType;
        import javax.ws.rs.core.Response;

        @Path("/")
        @Produces("application/json")
        public class ContextTest
        {
        @Context
        ResourceContext resourceContext;

        ContainerRequestContext containerRequestContext;

        public ContextTest()
        {

        }

        @AuthorizationNeededInterface
        @GET
        @Path("/echos/{echoRequestText}")
        @Produces({MediaType.APPLICATION_JSON})
        public Response echo(@PathParam("echoRequestText") String echoRequestText)
        {
        System.out.println("DEBUG: IN RESOURCE");

        Map<String, String> myResponse = new HashMap<String, String>();

        containerRequestContext = resourceContext.getResource(ContainerRequestContext.class);

        if(containerRequestContext != null)
        {
        if(containerRequestContext.getProperty("AuthID") != null)
        {
        if(((String)containerRequestContext.getProperty("AuthID")).compareTo("1234") == 0)
        {
        myResponse.put("echo_response", echoRequestText);
        }
        else myResponse.put("echo_response", "Invalid AuthID <1>");
        }
        else myResponse.put("echo_response", "Invalid AuthID <2>");
        }
        else myResponse.put("echo_response", "Invalid AuthID <3>");

        GenericEntity<Map<String, String>> genericEntity = new GenericEntity<Map<String, String>>(myResponse){};
        return Response.ok().entity(genericEntity).build();
        }
        }


        AuthorizationNeededInterface.java



        package test.codehq.fab1.contexttest.jersey2test1;

        import javax.ws.rs.NameBinding;
        import java.lang.annotation.Retention;
        import java.lang.annotation.Target;

        import static java.lang.annotation.ElementType.METHOD;
        import static java.lang.annotation.ElementType.TYPE;
        import static java.lang.annotation.RetentionPolicy.RUNTIME;

        @NameBinding
        @Retention(RUNTIME)
        @Target({TYPE, METHOD})
        public @interface AuthorizationNeededInterface
        {

        }


        AuthorizationNeeded.java



        package test.codehq.fab1.contexttest.jersey2test1;

        import java.io.IOException;
        import javax.ws.rs.ext.Provider;
        import javax.annotation.Priority;
        import javax.ws.rs.container.ContainerRequestContext;
        import javax.ws.rs.container.ContainerRequestFilter;
        import javax.ws.rs.container.ResourceContext;
        import javax.ws.rs.core.Context;

        @AuthorizationNeededInterface
        @Provider
        @Priority(1000)
        public class AuthorizationNeeded implements ContainerRequestFilter
        {
        @Context
        ResourceContext resourceContext;

        @Override
        public void filter(ContainerRequestContext conRequestContext) throws IOException
        {
        System.out.println("DEBUG: IN FILTER");
        resourceContext.getResource(ContainerRequestContext.class).setProperty("AuthID", "1234");
        }
        }


        web.xml



        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
        <display-name>ContextTestJersey2test1</display-name>
        <servlet>
        <description>Jersey Filter test</description>
        <display-name>contexttest-jersey2test1</display-name>
        <servlet-name>contexttest-jersey2test1</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>test.codehq.fab1.contexttest.jersey2test1</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
        <servlet-name>contexttest-jersey2test1</servlet-name>
        <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>
        <session-config>
        <session-timeout>60</session-timeout>
        </session-config>
        </web-app>


        pom.xml



        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>test.codinghq.fab1</groupId>
        <artifactId>ContextTestJersey2Test1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
        <source>1.8</source>
        <target>1.8</target>
        </configuration>
        </plugin>
        <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
        </plugin>
        </plugins>
        </build>
        <dependencies>
        <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1</version>
        </dependency>
        <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-providers</artifactId>
        <version>2.9.7</version>
        <type>pom</type>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.9.7</version>
        </dependency>
        <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.27</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json</artifactId>
        <version>2.0-m05-1</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.27</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.27</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.27</version>
        </dependency>
        </dependencies>
        </project>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 12 '18 at 11:18









        Patrick PetersPatrick Peters

        113




        113
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53495565%2freturning-a-object-from-filter-back-to-resource%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Unable to find Lightning Node

            Futebolista