This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 870ced5c56b20925949e2860d2b099ae0b9883a0
Author: Andriy Redko <[email protected]>
AuthorDate: Wed Sep 9 20:02:12 2020 -0400

    CXF-8227: Failure to comply with JAX-RS spec with ContainerRequestContext 
and WriterInterceptorContext (#692)
    
    * CXF-8227: Failure to comply with JAX-RS spec with ContainerRequestContext 
and WriterInterceptorContext. Addressing review comments
    (cherry picked from commit 6d5fddfa467cb7d9a54c066029b2a6d055fb5546)
    
    (cherry picked from commit f35bf79d7c44286281e2c1fb857052255da55419)
    
    # Conflicts:
    #   
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ServletRequestPropertyHolder.java
---
 .../cxf/jaxrs/impl/PropertyHolderFactory.java      | 59 ++++++++++++++++++++++
 .../apache/cxf/systest/jaxrs/BookApplication.java  | 12 ++++-
 .../jaxrs/JAXRSClientServerNonSpringBookTest.java  | 19 ++++++-
 3 files changed, 88 insertions(+), 2 deletions(-)

diff --git 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PropertyHolderFactory.java
 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PropertyHolderFactory.java
index 23be014..052eb48 100644
--- 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PropertyHolderFactory.java
+++ 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PropertyHolderFactory.java
@@ -20,8 +20,13 @@ package org.apache.cxf.jaxrs.impl;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.http.HttpServletRequest;
 
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.message.Message;
@@ -41,6 +46,60 @@ public final class PropertyHolderFactory {
         void setProperty(String name, Object value);
         Collection<String> getPropertyNames();
     }
+    
+    private static class ServletRequestPropertyHolder extends 
MessagePropertyHolder {
+        private static final String ENDPOINT_ADDRESS_PROPERTY = 
"org.apache.cxf.transport.endpoint.address";
+        private final HttpServletRequest request;
+        
+        ServletRequestPropertyHolder(Message m) {
+            super(m);
+            request = (HttpServletRequest)m.get("HTTP.REQUEST");
+        }
+
+        @Override
+        public Object getProperty(String name) {
+            final Object value = request.getAttribute(name);
+
+            if (value != null) {
+                return value;
+            }
+            
+            return super.getProperty(name);
+        }
+
+        @Override
+        public void removeProperty(String name) {
+            super.removeProperty(name);
+            request.removeAttribute(name);
+        }
+
+        @Override
+        public void setProperty(String name, Object value) {
+            if (value == null) {
+                removeProperty(name);
+            } else {
+                super.setProperty(name, value);
+                request.setAttribute(name, value);
+            }
+        }
+
+        @Override
+        public Collection<String> getPropertyNames() {
+            final Set<String> list = new LinkedHashSet<>();
+            
+            Enumeration<String> attrNames = request.getAttributeNames();
+            while (attrNames.hasMoreElements()) {
+                String name = attrNames.nextElement();
+                if (!ENDPOINT_ADDRESS_PROPERTY.equals(name)) {
+                    list.add(name);
+                }
+            }
+            
+            list.addAll(super.getPropertyNames());
+            return list;
+        }
+
+    }
 
     private static class MessagePropertyHolder implements PropertyHolder {
         private static final String PROPERTY_KEY = "jaxrs.filter.properties";
diff --git 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
index 2297900..2896a3e 100644
--- 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
+++ 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java
@@ -106,6 +106,12 @@ public class BookApplication extends Application {
         public void aroundWriteTo(WriterInterceptorContext context) throws 
IOException,
             WebApplicationException {
             context.getHeaders().putSingle("BookWriter", "TheBook");
+            
+            final Object property = context.getProperty("property");
+            if (property != null) {
+                context.getHeaders().putSingle("X-Property-WriterInterceptor", 
property);
+            }
+            
             context.proceed();
         }
 
@@ -131,7 +137,11 @@ public class BookApplication extends Application {
                 || uri.contains("/application6")) {
                 context.getHeaders().put("BOOK", Arrays.asList("1", "2"));
             }
-
+            
+            final String value = 
context.getUriInfo().getQueryParameters().getFirst("property");
+            if (value != null) {
+                context.setProperty("property", value);
+            }
         }
 
     }
diff --git 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
index d8c9685..1fc5f95 100644
--- 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
+++ 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java
@@ -20,8 +20,10 @@
 package org.apache.cxf.systest.jaxrs;
 
 import java.io.InputStream;
+import java.util.AbstractMap.SimpleEntry;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import javax.ws.rs.InternalServerErrorException;
 import javax.ws.rs.WebApplicationException;
@@ -181,6 +183,15 @@ public class JAXRSClientServerNonSpringBookTest extends 
AbstractBusClientServerT
             doTestPerRequest("http://localhost:"; + PORT + 
"/application11/thebooks/bookstore2/bookheaders");
         assertEquals("TheBook", r.getHeaderString("BookWriter"));
     }
+    
+    @Test
+    public void testGetBook123PropagatingContextPropertyToWriterInterceptor() 
throws Exception {
+        Response r = 
+            doTestPerRequest("http://localhost:"; + PORT + 
"/application6/thebooks/bookstore2/bookheaders",
+                new SimpleEntry<>("property", "PropValue"));
+        assertEquals("PropValue", 
r.getHeaderString("X-Property-WriterInterceptor"));
+    }
+
 
     @Test
     public void testGetBook123TwoApplications() throws Exception {
@@ -188,10 +199,16 @@ public class JAXRSClientServerNonSpringBookTest extends 
AbstractBusClientServerT
         doTestPerRequest("http://localhost:"; + PORT + 
"/application6/the%20books2/bookstore2/book%20headers");
     }
 
-    private Response doTestPerRequest(String address) throws Exception {
+    @SafeVarargs
+    private final Response doTestPerRequest(String address, Map.Entry<String, 
String> ... params) throws Exception {
         WebClient wc = WebClient.create(address);
         
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
         wc.accept("application/xml");
+        
+        for (Map.Entry<String, String> param: params) {
+            wc.query(param.getKey(), param.getValue());
+        }
+        
         Response r = wc.get();
         Book book = r.readEntity(Book.class);
         assertEquals("CXF in Action", book.getName());

Reply via email to