cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1352069 - in /cxf/trunk: api/src/main/java/org/apache/cxf/message/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/ rt/transports/jms/src/main/java/org/apache/cx...
Date Wed, 20 Jun 2012 12:46:00 GMT
Author: sergeyb
Date: Wed Jun 20 12:45:59 2012
New Revision: 1352069

URL: http://svn.apache.org/viewvc?rev=1352069&view=rev
Log:
[CXF-4350] Getting JAX-RS client code to support JMS, applying a patch from Willem with minor
updates

Modified:
    cxf/trunk/api/src/main/java/org/apache/cxf/message/Message.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/LocalClientState.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
    cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/message/Message.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/message/Message.java?rev=1352069&r1=1352068&r2=1352069&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/message/Message.java (original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/message/Message.java Wed Jun 20 12:45:59 2012
@@ -32,6 +32,12 @@ import org.apache.cxf.transport.Destinat
 public interface Message extends StringMap {
     
     String TRANSPORT = "org.apache.cxf.transport";
+    
+    /*
+     * Boolean property which can be used to check that the current request
+     * is part of the SOAP (JAX-WS) or non-SOAP/REST (JAX-RS) execution context.
+     */
+    String REST_MESSAGE = "org.apache.cxf.rest.message";
 
     /**
      * Boolean property specifying if the message is a request message.

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=1352069&r1=1352068&r2=1352069&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
Wed Jun 20 12:45:59 2012
@@ -86,6 +86,7 @@ public abstract class AbstractClient imp
     protected static final String RESPONSE_CONTEXT = "ResponseContext";
     protected static final String KEEP_CONDUIT_ALIVE = "KeepConduitAlive";
     
+    private static final String HTTP_SCHEME = "http";
     private static final String PROXY_PROPERTY = "jaxrs.proxy";
     private static final Logger LOG = LogUtils.getL7dLogger(AbstractClient.class);
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractClient.class);
@@ -708,7 +709,7 @@ public abstract class AbstractClient imp
         
         String address = (String)message.get(Message.ENDPOINT_ADDRESS);
         // custom conduits may override the initial/current address
-        if (!address.equals(currentURI.toString())) {
+        if (address.startsWith(HTTP_SCHEME) && !address.equals(currentURI.toString()))
{
             URI baseAddress = URI.create(address);
             currentURI = calculateNewRequestURI(baseAddress, currentURI, proxy);
             message.put(Message.ENDPOINT_ADDRESS, currentURI.toString());
@@ -745,10 +746,22 @@ public abstract class AbstractClient imp
         m.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
         m.put(Message.INBOUND_MESSAGE, Boolean.FALSE);
         
+        m.put(Message.REST_MESSAGE, Boolean.TRUE);
+        
         m.put(Message.HTTP_REQUEST_METHOD, httpMethod);
         m.put(Message.PROTOCOL_HEADERS, headers);
-        m.put(Message.ENDPOINT_ADDRESS, currentURI.toString());
-        m.put(Message.REQUEST_URI, currentURI.toString());
+        if (currentURI.isAbsolute()) {
+            m.put(Message.ENDPOINT_ADDRESS, currentURI.toString());
+        } else {
+            m.put(Message.ENDPOINT_ADDRESS, state.getBaseURI().toString());
+        }
+        
+        Object requestURIProperty = cfg.getRequestContext().get(Message.REQUEST_URI);
+        if (requestURIProperty == null) {
+            m.put(Message.REQUEST_URI, currentURI.toString());
+        } else {
+            m.put(Message.REQUEST_URI, requestURIProperty.toString());
+        }
         
         m.put(Message.CONTENT_TYPE, headers.getFirst(HttpHeaders.CONTENT_TYPE));
         

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/LocalClientState.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/LocalClientState.java?rev=1352069&r1=1352068&r2=1352069&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/LocalClientState.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/LocalClientState.java
Wed Jun 20 12:45:59 2012
@@ -24,6 +24,7 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.UriBuilder;
 
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 
 /**
@@ -31,6 +32,7 @@ import org.apache.cxf.jaxrs.impl.Metadat
  *
  */
 public class LocalClientState implements ClientState {
+    private static final String HTTP_SCHEME = "http";
     
     private MultivaluedMap<String, String> requestHeaders = new MetadataMap<String,
String>();
     private MultivaluedMap<String, String> templates;
@@ -44,7 +46,12 @@ public class LocalClientState implements
     
     public LocalClientState(URI baseURI) {
         this.baseURI = baseURI;
-        this.currentBuilder = UriBuilder.fromUri(baseURI);
+        String scheme = baseURI.getScheme();
+        if (!StringUtils.isEmpty(scheme) && scheme.startsWith(HTTP_SCHEME)) {
+            this.currentBuilder = UriBuilder.fromUri(baseURI);
+        } else {
+            this.currentBuilder = UriBuilder.fromUri("/");
+        }
     }
     
     public LocalClientState(LocalClientState cs) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java?rev=1352069&r1=1352068&r2=1352069&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSOutInterceptor.java
Wed Jun 20 12:45:59 2012
@@ -109,6 +109,7 @@ public class JAXRSOutInterceptor extends
         if (isResponseAlreadyHandled(message)) {
             return;
         }
+        message.put(Message.REST_MESSAGE, Boolean.TRUE);
         
         MessageContentsList objs = MessageContentsList.getContentsList(message);
         if (objs == null || objs.size() == 0) {

Modified: cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java?rev=1352069&r1=1352068&r2=1352069&view=diff
==============================================================================
--- cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java (original)
+++ cxf/trunk/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSUtils.java Wed
Jun 20 12:45:59 2012
@@ -49,6 +49,7 @@ import org.apache.cxf.common.logging.Log
 import org.apache.cxf.common.util.SOAPConstants;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.HttpHeaderHelper;
+import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.security.SecurityContext;
 import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
 import org.apache.cxf.transport.jms.spec.JMSSpecConstants;
@@ -208,7 +209,10 @@ public final class JMSUtils {
                     // set the message encoding
                     inMessage.put(org.apache.cxf.message.Message.ENCODING, getEncoding(val));
                 }
-
+                if (name.equals(org.apache.cxf.message.Message.RESPONSE_CODE)) {
+                    inMessage.getExchange().put(org.apache.cxf.message.Message.RESPONSE_CODE,

+                                                Integer.valueOf(val));
+                }
             }
             inMessage.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, protHeaders);
 
@@ -592,67 +596,113 @@ public final class JMSUtils {
     private static void prepareJMSMessageProperties(JMSMessageHeadersType messageProperties,
                                                     org.apache.cxf.message.Message outMessage,
                                                     JMSConfiguration jmsConfig) {
-        if (!messageProperties.isSetSOAPJMSTargetService()) {
-            messageProperties.setSOAPJMSTargetService(jmsConfig.getTargetService());
-        }
-        if (!messageProperties.isSetSOAPJMSBindingVersion()) {
-            messageProperties.setSOAPJMSBindingVersion("1.0");
-        }
-        messageProperties.setSOAPJMSContentType(getContentType(outMessage));
-        if (getContentEncoding(outMessage) != null) {
-            messageProperties.setSOAPJMSContentEncoding(getContentEncoding(outMessage));
-        }
-        String soapAction = null;
+        
         // Retrieve or create protocol headers
         Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>)outMessage
             .get(org.apache.cxf.message.Message.PROTOCOL_HEADERS));
-        if (headers != null) {
-            List<String> action = headers.remove(SOAPConstants.SOAP_ACTION);
-            if (action != null && action.size() > 0) {
-                soapAction = action.get(0);
-            }
-        }
-        
-        if (soapAction == null) {
-            soapAction = messageProperties.getSOAPJMSSOAPAction();
-        }
         
-        if (soapAction == null) {
-            soapAction = extractActionFromSoap12(outMessage);
-        }
+        boolean isSoapMessage = 
+            !MessageUtils.isTrue(outMessage.get(org.apache.cxf.message.Message.REST_MESSAGE));
         
-        if (soapAction != null) {
-            messageProperties.setSOAPJMSSOAPAction(soapAction);
-        }
-        if (!messageProperties.isSetSOAPJMSIsFault()) {
-            if (outMessage.getContent(Exception.class) != null) {
-                messageProperties.setSOAPJMSIsFault(true);
-            } else {
-                messageProperties.setSOAPJMSIsFault(false);
+        if (isSoapMessage) {
+            if (!messageProperties.isSetSOAPJMSTargetService()) {
+                messageProperties.setSOAPJMSTargetService(jmsConfig.getTargetService());
+            }
+            if (!messageProperties.isSetSOAPJMSBindingVersion()) {
+                messageProperties.setSOAPJMSBindingVersion("1.0");
+            }
+            messageProperties.setSOAPJMSContentType(getContentType(outMessage));
+            if (getContentEncoding(outMessage) != null) {
+                messageProperties.setSOAPJMSContentEncoding(getContentEncoding(outMessage));
             }
+            String soapAction = null;
+            
+            if (headers != null) {
+                List<String> action = headers.remove(SOAPConstants.SOAP_ACTION);
+                if (action != null && action.size() > 0) {
+                    soapAction = action.get(0);
+                }
+            }
+            
+            if (soapAction == null) {
+                soapAction = messageProperties.getSOAPJMSSOAPAction();
+            }
+            
+            if (soapAction == null) {
+                soapAction = extractActionFromSoap12(outMessage);
+            }
+            
+            if (soapAction != null) {
+                messageProperties.setSOAPJMSSOAPAction(soapAction);
+            }
+            if (!messageProperties.isSetSOAPJMSIsFault()) {
+                if (outMessage.getContent(Exception.class) != null) {
+                    messageProperties.setSOAPJMSIsFault(true);
+                } else {
+                    messageProperties.setSOAPJMSIsFault(false);
+                }
+            }
+            if (!messageProperties.isSetSOAPJMSRequestURI()) {
+                messageProperties.setSOAPJMSRequestURI(jmsConfig.getRequestURI());
+            }
+        } else {
+            if (MessageUtils.isRequestor(outMessage)) {
+                addJMSPropertiesFromMessage(messageProperties, 
+                                            outMessage, 
+                                            org.apache.cxf.message.Message.HTTP_REQUEST_METHOD,
+                                            org.apache.cxf.message.Message.REQUEST_URI,
+                                            org.apache.cxf.message.Message.ACCEPT_CONTENT_TYPE);
+            } else {
+                addJMSPropertyFromMessage(messageProperties, 
+                                          outMessage, 
+                                          org.apache.cxf.message.Message.RESPONSE_CODE);
+            }
+            addJMSPropertyFromMessage(messageProperties, 
+                                      outMessage, 
+                                      org.apache.cxf.message.Message.CONTENT_TYPE);
         }
-        if (!messageProperties.isSetSOAPJMSRequestURI()) {
-            messageProperties.setSOAPJMSRequestURI(jmsConfig.getRequestURI());
-        }
-        for (Map.Entry<String, List<String>> ent : headers.entrySet()) {
-            JMSPropertyType prop = new JMSPropertyType();
-            prop.setName(ent.getKey());
-            if (ent.getValue().size() > 1) {
-                StringBuilder b = new StringBuilder();
-                for (String s : ent.getValue()) {
-                    if (b.length() > 0) {
-                        b.append(',');
+        if (headers != null) {
+            for (Map.Entry<String, List<String>> ent : headers.entrySet()) {
+                JMSPropertyType prop = new JMSPropertyType();
+                prop.setName(ent.getKey());
+                if (ent.getValue().size() > 1) {
+                    StringBuilder b = new StringBuilder();
+                    for (String s : ent.getValue()) {
+                        if (b.length() > 0) {
+                            b.append(',');
+                        }
+                        b.append(s);
                     }
-                    b.append(s);
+                    prop.setValue(b.toString());
+                } else {
+                    prop.setValue(ent.getValue().get(0));
                 }
-                prop.setValue(b.toString());
-            } else {
-                prop.setValue(ent.getValue().get(0));
+                messageProperties.getProperty().add(prop);
             }
-            messageProperties.getProperty().add(prop);
         }
     }
 
+    private static void addJMSPropertiesFromMessage(JMSMessageHeadersType messageProperties,
+                                                    org.apache.cxf.message.Message message,

+                                                    String... keys) {
+        for (String key : keys) {
+            addJMSPropertyFromMessage(messageProperties, message, key);
+        }
+        
+    }
+    
+    private static void addJMSPropertyFromMessage(JMSMessageHeadersType messageProperties,
+                                                  org.apache.cxf.message.Message message,

+                                                  String key) {
+        Object value = message.get(key);
+        if (value != null) {
+            JMSPropertyType prop = new JMSPropertyType();
+            prop.setName(key);
+            prop.setValue(value.toString());
+            messageProperties.getProperty().add(prop);
+        }
+    }
+    
     /**
      * @param messageProperties
      * @param inMessageProperties

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java?rev=1352069&r1=1352068&r2=1352069&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java
Wed Jun 20 12:45:59 2012
@@ -39,6 +39,8 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 
+import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
+import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.apache.cxf.testutil.common.EmbeddedJMSBrokerLauncher;
@@ -73,6 +75,56 @@ public class JAXRSJmsTest extends Abstra
     }
     
     @Test
+    public void testGetBookFromWebClient() throws Exception {
+        // setup the the client
+        String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text"
+             + "?jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory"
+             + "&replyToName=dynamicQueues/test.jmstransport.response"
+             + "&jndiURL=tcp://localhost:" + JMS_PORT
+             + "&jndiConnectionFactoryName=ConnectionFactory";
+               
+        WebClient client = WebClient.create(endpointAddressUrlEncoded);
+        WebClient.getConfig(client).getRequestContext()
+            .put(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/books/123");
+        
+        Book book = client.get(Book.class);
+        assertEquals("Get a wrong response code.", 200, client.getResponse().getStatus());
+        assertEquals("Get a wrong book id.", 123, book.getId());
+    }
+    
+    @Test
+    public void testGetBookFromWebClientWithPath() throws Exception {
+        // setup the the client
+        String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text"
+             + "?jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory"
+             + "&replyToName=dynamicQueues/test.jmstransport.response"
+             + "&jndiURL=tcp://localhost:" + JMS_PORT
+             + "&jndiConnectionFactoryName=ConnectionFactory";
+               
+        WebClient client = WebClient.create(endpointAddressUrlEncoded);
+        client.path("bookstore").path("books").path("123");
+        
+        Book book = client.get(Book.class);
+        assertEquals("Get a wrong response code.", 200, client.getResponse().getStatus());
+        assertEquals("Get a wrong book id.", 123, book.getId());
+    }
+    
+    @Test
+    public void testGetBookFromProxyClient() throws Exception {
+        // setup the the client
+        String endpointAddressUrlEncoded = "jms:jndi:dynamicQueues/test.jmstransport.text"
+             + "?jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory"
+             + "&replyToName=dynamicQueues/test.jmstransport.response"
+             + "&jndiURL=tcp://localhost:" + JMS_PORT
+             + "&jndiConnectionFactoryName=ConnectionFactory";
+               
+        JMSBookStore client = JAXRSClientFactory.create(endpointAddressUrlEncoded, JMSBookStore.class);
+        Book book = client.getBook("123");
+        assertEquals("Get a wrong response code.", 200, WebClient.client(client).getResponse().getStatus());
+        assertEquals("Get a wrong book id.", 123, book.getId());
+    }
+    
+    @Test
     public void testGetBook() throws Exception {
         Context ctx = getContext();
         ConnectionFactory factory = (ConnectionFactory)ctx.lookup("ConnectionFactory");



Mime
View raw message