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

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


The following commit(s) were added to refs/heads/4.0.x-fixes by this push:
     new aaff1cf7dc [CXF-9148]should cache HttpServletRequest attributes as 
well in HttpServletRequestSnapshot
aaff1cf7dc is described below

commit aaff1cf7dce6c62aea04996371f69c8d31efd42d
Author: Freeman Fang <[email protected]>
AuthorDate: Thu Jul 17 18:28:16 2025 -0400

    [CXF-9148]should cache HttpServletRequest attributes as well in 
HttpServletRequestSnapshot
    
    (cherry picked from commit e3f0ac39e7a9099ac5cacf197923c4613722955e)
---
 .../transport/http/HttpServletRequestSnapshot.java | 29 ++++++++++++++++++++++
 .../org/apache/cxf/systest/hc/jaxrs/BookStore.java | 12 +++++++++
 2 files changed, 41 insertions(+)

diff --git 
a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpServletRequestSnapshot.java
 
b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpServletRequestSnapshot.java
index ad5057085d..ff7a06ff09 100644
--- 
a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpServletRequestSnapshot.java
+++ 
b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpServletRequestSnapshot.java
@@ -19,7 +19,10 @@
 package org.apache.cxf.transport.http;
 
 import java.security.Principal;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
@@ -59,8 +62,11 @@ public class HttpServletRequestSnapshot extends 
HttpServletRequestWrapper {
     private HttpSession session;
     private Principal principal;
     private Enumeration<String> requestHeaderNames;
+    private Enumeration<String> attributeNames;
     private Map<String, Enumeration<String>> headersMap =
         new java.util.concurrent.ConcurrentHashMap<>();
+    private Map<String, Object> attributesMap =
+        new java.util.concurrent.ConcurrentHashMap<>();
     private String requestedSessionId;
 
     public HttpServletRequestSnapshot(HttpServletRequest request) {
@@ -77,6 +83,16 @@ public class HttpServletRequestSnapshot extends 
HttpServletRequestWrapper {
             String key = tmp.nextElement();
             headersMap.put(key, request.getHeaders(key));
         }
+        tmp = request.getAttributeNames();
+        List<String> newVector = new ArrayList<>();
+        while (tmp.hasMoreElements()) {
+            String key = tmp.nextElement();
+            newVector.add(key);
+            if (request.getAttribute(key) != null) {
+                attributesMap.put(key, request.getAttribute(key));
+            }
+        }
+        attributeNames = Collections.enumeration(newVector);
         localAddr = request.getLocalAddr();
         local = request.getLocale();
         localName = request.getLocalName();
@@ -125,6 +141,14 @@ public class HttpServletRequestSnapshot extends 
HttpServletRequestWrapper {
         }
         return null;
     }
+    
+    @Override
+    public Object getAttribute(String name) {
+        if (attributesMap.get(name) != null) {
+            return attributesMap.get(name);
+        }
+        return null;
+    }
 
     @SuppressWarnings({
         "unchecked", "rawtypes"
@@ -133,6 +157,11 @@ public class HttpServletRequestSnapshot extends 
HttpServletRequestWrapper {
     public Enumeration getHeaderNames() {
         return this.requestHeaderNames;
     }
+    
+    @Override
+    public Enumeration<String> getAttributeNames() {
+        return this.attributeNames;
+    }
 
     @SuppressWarnings({
         "unchecked", "rawtypes"
diff --git 
a/systests/transports/src/test/java/org/apache/cxf/systest/hc/jaxrs/BookStore.java
 
b/systests/transports/src/test/java/org/apache/cxf/systest/hc/jaxrs/BookStore.java
index 632c91e770..a233dd83ca 100644
--- 
a/systests/transports/src/test/java/org/apache/cxf/systest/hc/jaxrs/BookStore.java
+++ 
b/systests/transports/src/test/java/org/apache/cxf/systest/hc/jaxrs/BookStore.java
@@ -24,6 +24,7 @@ import java.io.OutputStream;
 import java.util.HashMap;
 import java.util.Map;
 
+import jakarta.servlet.http.HttpServletRequest;
 import jakarta.ws.rs.Consumes;
 import jakarta.ws.rs.DELETE;
 import jakarta.ws.rs.GET;
@@ -50,6 +51,7 @@ import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 
+
 @Path("/bookstore")
 @GZIP(threshold = 1)
 public class BookStore {
@@ -61,6 +63,9 @@ public class BookStore {
     private UriInfo ui;
     @Context
     private MessageContext messageContext;
+    
+    @Context
+    private HttpServletRequest request;
 
     public BookStore() {
         init();
@@ -167,6 +172,8 @@ public class BookStore {
         }).build();
     }
 
+
+    
     @POST
     @Path("/oneway")
     @Oneway
@@ -174,6 +181,11 @@ public class BookStore {
         if 
(!PhaseInterceptorChain.getCurrentMessage().getExchange().isOneWay()) {
             throw new WebApplicationException();
         }
+        try {
+            request.getAttribute("someAttr"); //shouldn't get NPE here
+        } catch (NullPointerException ex) {
+            throw new WebApplicationException();
+        }
     }
     
     @POST

Reply via email to