[ 
https://issues.apache.org/jira/browse/CXF-7682?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16454426#comment-16454426
 ] 

ASF GitHub Bot commented on CXF-7682:
-------------------------------------

dkulp closed pull request #394: CXF-7682: 
context.get(MessageContext.HTTP_REQUEST_HEADERS) always ret…
URL: https://github.com/apache/cxf/pull/394
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageContextImpl.java
 
b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageContextImpl.java
index 5fd99dcda3b..f3722f40598 100644
--- 
a/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageContextImpl.java
+++ 
b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageContextImpl.java
@@ -47,10 +47,10 @@ public Object get(Object key) {
             if (((Map<?, ?>)o).isEmpty()) {
                 return null;
             }
-            if (!isResponse() && 
MessageContext.HTTP_RESPONSE_HEADERS.equals(key)) {
+            if (!isResponse() && isOutbound() && 
MessageContext.HTTP_RESPONSE_HEADERS.equals(key)) {
                 return null;
             }
-            if (isRequestor() && 
MessageContext.HTTP_REQUEST_HEADERS.equals(key)) {
+            if (isRequestor() && !isOutbound() && 
MessageContext.HTTP_REQUEST_HEADERS.equals(key)) {
                 return null;
             }
         }
diff --git 
a/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java 
b/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
index d8cab8dfb8c..62bc3ce6e00 100644
--- a/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
+++ b/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
@@ -22,8 +22,12 @@
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Proxy;
 import java.net.URL;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.ResourceBundle;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.xml.namespace.QName;
@@ -31,6 +35,12 @@
 import javax.xml.ws.BindingProvider;
 import javax.xml.ws.Dispatch;
 import javax.xml.ws.WebServiceException;
+import javax.xml.ws.handler.Handler;
+import javax.xml.ws.handler.LogicalHandler;
+import javax.xml.ws.handler.LogicalMessageContext;
+import javax.xml.ws.handler.MessageContext;
+import javax.xml.ws.handler.soap.SOAPHandler;
+import javax.xml.ws.handler.soap.SOAPMessageContext;
 
 import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.endpoint.ClientImpl;
@@ -63,6 +73,7 @@
                     "SoapPort");
     private final String address = 
"http://localhost:9000/SoapContext/SoapPort";;
     private Destination d;
+    private Map<String, List<String>> headers = new HashMap<>();
 
     @Before
     public void setUp() throws Exception {
@@ -319,6 +330,89 @@ public void testClientProxyFactory() {
         assertEquals("jack", 
((BindingProvider)greeter3).getRequestContext().get("test"));
     }
 
+    @Test
+    public void testLogicalHandler() {
+        URL url = getClass().getResource("/wsdl/hello_world.wsdl");
+        javax.xml.ws.Service s = javax.xml.ws.Service
+            .create(url, serviceName);
+        Greeter greeter = s.getPort(portName, Greeter.class);
+        d.setMessageObserver(new MessageReplayObserver("sayHiResponse.xml"));
+
+        List<Handler> chain = 
((BindingProvider)greeter).getBinding().getHandlerChain();
+        chain.add(new LogicalHandler<LogicalMessageContext>() {
+            public void close(MessageContext arg0) {
+            }
+
+            public boolean handleFault(LogicalMessageContext arg0) {
+                return true;
+            }
+
+            public boolean handleMessage(LogicalMessageContext context) {
+
+                Boolean outbound = (Boolean) 
context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
+                if (outbound) {
+                    headers = (Map<String, List<String>>) 
context.get(MessageContext.HTTP_REQUEST_HEADERS);
+                    if (headers == null) {
+                        headers = new HashMap<String, List<String>>();
+                        context.put(MessageContext.HTTP_REQUEST_HEADERS, 
headers);
+                    }
+                    headers.put("My-Custom-Header", 
Collections.singletonList("value"));
+                }
+                return true;
+            }
+        });
+        ((BindingProvider)greeter).getBinding().setHandlerChain(chain);
+
+        String response = greeter.sayHi();
+        assertNotNull(response);
+        assertTrue("custom header should be present", 
headers.containsKey("My-Custom-Header"));
+        assertTrue("existing SOAPAction header should not be removed", 
headers.containsKey("SOAPAction"));
+    }
+
+    @Test
+    public void testSoapHandler() {
+        URL url = getClass().getResource("/wsdl/hello_world.wsdl");
+        javax.xml.ws.Service s = javax.xml.ws.Service
+            .create(url, serviceName);
+        Greeter greeter = s.getPort(portName, Greeter.class);
+        d.setMessageObserver(new MessageReplayObserver("sayHiResponse.xml"));
+
+        List<Handler> chain = 
((BindingProvider)greeter).getBinding().getHandlerChain();
+        chain.add(new SOAPHandler<SOAPMessageContext>() {
+
+                public boolean handleMessage(SOAPMessageContext context) {
+
+                    Boolean outbound = (Boolean) 
context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
+                    if (outbound) {
+                        headers = (Map<String, List<String>>) 
context.get(MessageContext.HTTP_REQUEST_HEADERS);
+                        if (headers == null) {
+                            headers = new HashMap<String, List<String>>();
+                            context.put(MessageContext.HTTP_REQUEST_HEADERS, 
headers);
+                        }
+                        headers.put("My-Custom-Header", 
Collections.singletonList("value"));
+                    }
+                    return true;
+                }
+
+                public boolean handleFault(SOAPMessageContext smc) {
+                    return true;
+                }
+
+                public Set<QName> getHeaders() {
+                    return null;
+                }
+
+                public void close(MessageContext messageContext) {
+                }
+        });
+        ((BindingProvider)greeter).getBinding().setHandlerChain(chain);
+
+        String response = greeter.sayHi();
+        assertNotNull(response);
+        assertTrue("custom header should be present", 
headers.containsKey("My-Custom-Header"));
+        assertTrue("existing SOAPAction header should not be removed", 
headers.containsKey("SOAPAction"));
+
+    }
 
     public static class FaultThrower extends AbstractPhaseInterceptor<Message> 
{
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> context.get(MessageContext.HTTP_REQUEST_HEADERS) always returns null for 
> client
> -------------------------------------------------------------------------------
>
>                 Key: CXF-7682
>                 URL: https://issues.apache.org/jira/browse/CXF-7682
>             Project: CXF
>          Issue Type: Bug
>          Components: JAX-WS Runtime
>            Reporter: Jimmy Praet
>            Priority: Major
>
> I have a client-side LogicalHandler that adds some HTTP headers and am 
> noticing it causes the existing SOAPAction HTTP header (already added 
> automatically by the JAX-WS runtime) to be removed.
> My handler code looks like this:
> {code:java}
> public boolean handleMessage(LogicalMessageContext context) {
>   Boolean outbound = (Boolean) 
> context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
>   if (outbound) {
>     Map<String, List<String>> headers =
>         (Map<String, List<String>>) 
> context.get(MessageContext.HTTP_REQUEST_HEADERS);
>     if (headers == null) {
>       headers = new HashMap<String, List<String>>();
>       context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
>     }
>     for (Map.Entry<String, String> entry : Tracer.getEntries().entrySet()) {
>       headers.put(entry.getKey(), 
> Collections.singletonList(entry.getValue()));
>     }
>   }
>   return true;
> }
> {code}
> I had a quick look at the code and noticed the 
> [LogicalMessageContextImpl#get(Object) 
> |https://github.com/apache/cxf/blob/a36af6323505211479c875fb9923cc6dcbc6ac95/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/LogicalMessageContextImpl.java#L53]
>  always returns null on the client side (isRequestor() = true).
> So my handler will create a new header map and put it on the context, which 
> is internally mapped to key "org.apache.cxf.message.Message.PROTOCOL_HEADERS" 
> [here|https://github.com/apache/cxf/blob/master/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/context/WrappedMessageContext.java#L462].



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to