You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2010/01/27 17:28:47 UTC

svn commit: r903712 - in /cxf/trunk: api/src/main/java/org/apache/cxf/message/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ systests/jaxrs/ systests/jaxrs/src/test/java/or...

Author: sergeyb
Date: Wed Jan 27 16:28:46 2010
New Revision: 903712

URL: http://svn.apache.org/viewvc?rev=903712&view=rev
Log:
Connecting JMS Transport with JAXRS endpoints

Added:
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java   (with props)
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JMSServer.java   (with props)
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml   (with props)
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/interceptor/JAXRSInInterceptor.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
    cxf/trunk/systests/jaxrs/pom.xml

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=903712&r1=903711&r2=903712&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 Jan 27 16:28:46 2010
@@ -67,6 +67,7 @@
      */
     String DECOUPLED_CHANNEL_MESSAGE = "decoupled.channel.message";
     String PARTIAL_RESPONSE_MESSAGE = "org.apache.cxf.partial.response";
+    String ONE_WAY_MESSAGE = "OnewayMessage";
     
     String PROTOCOL_HEADERS = Message.class.getName() + ".PROTOCOL_HEADERS";
     String RESPONSE_CODE = Message.class.getName() + ".RESPONSE_CODE";

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java?rev=903712&r1=903711&r2=903712&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java Wed Jan 27 16:28:46 2010
@@ -46,6 +46,7 @@
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.service.Service;
@@ -125,7 +126,7 @@
         Service service = message.getExchange().get(Service.class);
         List<ClassResourceInfo> resources = ((JAXRSServiceImpl)service).getClassResourceInfos();
 
-        String acceptTypes = (String)message.get(Message.ACCEPT_CONTENT_TYPE);
+        String acceptTypes = HttpUtils.getProtocolHeader(message, Message.ACCEPT_CONTENT_TYPE, null);
         if (acceptTypes == null) {
             acceptTypes = "*/*";
             message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
@@ -150,13 +151,13 @@
 
         message.getExchange().put(JAXRSUtils.ROOT_RESOURCE_CLASS, resource);
 
-        String httpMethod = (String)message.get(Message.HTTP_REQUEST_METHOD);
+        String httpMethod = HttpUtils.getProtocolHeader(message, Message.HTTP_REQUEST_METHOD, "POST");
         OperationResourceInfo ori = null;     
         
         boolean operChecked = false;
         List<ProviderInfo<RequestHandler>> shs = ProviderFactory.getInstance(message).getRequestHandlers();
         for (ProviderInfo<RequestHandler> sh : shs) {
-            String newAcceptTypes = (String)message.get(Message.ACCEPT_CONTENT_TYPE);
+            String newAcceptTypes = HttpUtils.getProtocolHeader(message, Message.ACCEPT_CONTENT_TYPE, "*/*");
             if (!acceptTypes.equals(newAcceptTypes) || (ori == null && !operChecked)) {
                 acceptTypes = newAcceptTypes;
                 acceptContentTypes = JAXRSUtils.sortMediaTypes(newAcceptTypes);
@@ -241,6 +242,9 @@
             plainOperationName = ori.getClassResourceInfo().getServiceClass().getSimpleName()
                 + "#" + plainOperationName;
         }
-        message.getExchange().put("org.apache.cxf.resource.operation.name", plainOperationName);    
+        message.getExchange().put("org.apache.cxf.resource.operation.name", plainOperationName);
+        
+        message.getExchange().setOneWay(
+            MessageUtils.isTrue(HttpUtils.getProtocolHeader(message, Message.ONE_WAY_MESSAGE, null)));
     }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java?rev=903712&r1=903711&r2=903712&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java Wed Jan 27 16:28:46 2010
@@ -38,6 +38,7 @@
 import javax.ws.rs.core.Response;
 
 import org.apache.cxf.common.util.UrlUtils;
+import org.apache.cxf.jaxrs.impl.HttpHeadersImpl;
 import org.apache.cxf.jaxrs.impl.PathSegmentImpl;
 import org.apache.cxf.jaxrs.model.ParameterType;
 import org.apache.cxf.message.Message;
@@ -146,18 +147,25 @@
     }
     
     public static String getPathToMatch(Message m, boolean addSlash) {
-        String requestAddress = (String)m.get(Message.REQUEST_URI);
+        String requestAddress = getProtocolHeader(m, Message.REQUEST_URI, "/");
         String baseAddress = getBaseAddress(m);
         return getPathToMatch(requestAddress, baseAddress, addSlash);
     }
     
+    public static String getProtocolHeader(Message m, String name, String defaultValue) {
+        String value = (String)m.get(name);
+        if (value == null) {
+            value = new HttpHeadersImpl(m).getRequestHeaders().getFirst(name);
+        }
+        return value == null ? defaultValue : value;
+    }
     
     public static String getBaseAddress(Message m) {
+        String endpointAddress = getEndpointAddress(m);
         try {
-            String endpointAddress = getEndpointAddress(m);
             return new URL(endpointAddress).getPath();
         } catch (MalformedURLException ex) {
-            return (String)m.get(Message.BASE_PATH);
+            return endpointAddress == null ? "/" : endpointAddress;
         }
     }
     

Modified: cxf/trunk/systests/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/pom.xml?rev=903712&r1=903711&r2=903712&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/pom.xml (original)
+++ cxf/trunk/systests/jaxrs/pom.xml Wed Jan 27 16:28:46 2010
@@ -138,6 +138,16 @@
             <artifactId>cxf-rt-transports-http</artifactId>
             <version>${project.version}</version>
         </dependency>
+         <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-transports-jms</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.activemq</groupId>
+            <artifactId>activemq-core</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-rt-transports-http-jetty</artifactId>

Added: 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=903712&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java Wed Jan 27 16:28:46 2010
@@ -0,0 +1,160 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.jaxrs;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.testutil.common.EmbeddedJMSBrokerLauncher;
+import org.apache.cxf.transport.jms.JMSUtils;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JAXRSJmsTest extends AbstractBusClientServerTestBase {
+
+    protected static boolean serversStarted;
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        if (serversStarted) {
+            return;
+        }
+        Map<String, String> props = new HashMap<String, String>();                
+        if (System.getProperty("activemq.store.dir") != null) {
+            props.put("activemq.store.dir", System.getProperty("activemq.store.dir"));
+        }
+        props.put("java.util.logging.config.file", 
+                  System.getProperty("java.util.logging.config.file"));
+        
+        assertTrue("server did not launch correctly", 
+                   launchServer(EmbeddedJMSBrokerLauncher.class, props, null));
+        assertTrue("server did not launch correctly",
+                   launchServer(JMSServer.class, false));
+        serversStarted = true;
+    }
+    
+    
+    @Test
+    public void testAddGetBook() throws Exception {
+        Context ctx = getContext();
+        ConnectionFactory factory = (ConnectionFactory)ctx.lookup("ConnectionFactory");
+        
+        Destination destination = (Destination)ctx.lookup("dynamicQueues/test.jmstransport.text");
+        Destination replyToDestination = (Destination)ctx.lookup("dynamicQueues/test.jmstransport.response");
+                
+        Connection connection = null;
+        try {
+            connection = factory.createConnection();
+            connection.start();
+            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+            postBook(session, destination, replyToDestination);
+            MessageConsumer consumer = session.createConsumer(replyToDestination);
+            Message jmsMessage = consumer.receive(3000);
+            org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
+            JMSUtils.retrieveAndSetPayload(cxfMessage, jmsMessage, null);
+            Book b = readBook(cxfMessage.getContent(InputStream.class));
+            assertEquals(124L, b.getId());
+            assertEquals("JMS", b.getName());
+            session.close();
+        } finally {
+            if (connection != null) {
+                try {
+                    connection.stop();
+                    connection.close();
+                } catch (JMSException ex) {
+                    // ignore
+                }
+                
+            }
+            System.out.println("Hanging...");
+        }
+        
+    }
+    
+    private Context getContext() throws Exception {
+        Properties props = new Properties();
+        props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
+                          "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
+        props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61500");
+        return new InitialContext(props);
+        
+    }
+    
+    private void postBook(Session session, Destination destination, Destination replyTo) 
+        throws Exception {
+        MessageProducer producer = session.createProducer(destination);
+        
+        Message message = JMSUtils.createAndSetPayload(writeBook(new Book("JMS", 3L)), session, "text");
+        message.setJMSReplyTo(replyTo);
+        // or, if oneway,
+        // message.setStringProperty("OnewayMessage", "true");
+        
+        // all these properties are optional
+        // CXF JAXRS and JMS Transport will default to 
+        // Content-Type : text/xml
+        // Accept : */*
+        // POST
+        // Message.REQUEST_URI : "/"
+        
+        message.setStringProperty("Content-Type", "application/xml");
+        message.setStringProperty("Accept", "text/xml");
+        message.setStringProperty(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/books");
+        message.setStringProperty(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD, "POST");
+            
+                    
+        producer.send(message);
+        producer.close();
+    }
+    
+    private Book readBook(InputStream is) throws Exception {
+        JAXBContext c = JAXBContext.newInstance(new Class[]{Book.class});
+        Unmarshaller u = c.createUnmarshaller();
+        return (Book)u.unmarshal(is);
+    }
+    
+    private String writeBook(Book b) throws Exception {
+        JAXBContext c = JAXBContext.newInstance(new Class[]{Book.class});
+        Marshaller m = c.createMarshaller();
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        m.marshal(b, bos);
+        return bos.toString();
+    }
+    
+    
+}

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSJmsTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JMSServer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JMSServer.java?rev=903712&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JMSServer.java (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JMSServer.java Wed Jan 27 16:28:46 2010
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.systest.jaxrs;
+
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class JMSServer extends AbstractBusTestServerBase {
+
+
+    protected void run()  {
+        // create the application context
+        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
+            "org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml");
+        context.start();
+    }
+
+
+    public static void main(String[] args) {
+        try {
+            JMSServer s = new JMSServer();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+}

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JMSServer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JMSServer.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml?rev=903712&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml (added)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml Wed Jan 27 16:28:46 2010
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements. See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership. The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License. You may obtain a copy of the License at
+ 
+  http://www.apache.org/licenses/LICENSE-2.0
+ 
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied. See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+
+<beans 
+    xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:jms="http://cxf.apache.org/transports/jms"
+    xmlns:p="http://www.springframework.org/schema/p"
+    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
+    xsi:schemaLocation="
+http://cxf.apache.org/transports/jms http://cxf.apache.org/schemas/configuration/jms.xsd
+http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+http://cxf.apache.org/jaxrs
+http://cxf.apache.org/schemas/jaxrs.xsd">
+    
+  <import resource="classpath:META-INF/cxf/cxf.xml" />
+  <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />  
+  <import resource="classpath:META-INF/cxf/cxf-extension-jms.xml" />
+    
+  <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
+  	p:brokerURL="tcp://localhost:61500"/>
+  
+  <bean id="singleConnectionFactory"
+    class="org.springframework.jms.connection.SingleConnectionFactory" destroy-method="destroy">
+    <property name="targetConnectionFactory" ref="jmsConnectionFactory"/>
+  </bean>
+    
+  <bean id="jmsConfig" class="org.apache.cxf.transport.jms.JMSConfiguration"
+    p:connectionFactory-ref="singleConnectionFactory" 
+    p:timeToLive="500000"
+    p:concurrentConsumers="1"
+    p:maxSuspendedContinuations="0"
+  	p:maxConcurrentConsumers="1"/>
+
+  <jms:destination name="{http://books.com}BookService.jms-destination">     
+      <jms:address jndiConnectionFactoryName="ConnectionFactory" 
+                   jndiDestinationName="dynamicQueues/test.jmstransport.text">
+                   <jms:JMSNamingProperty name="java.naming.factory.initial" value="org.apache.activemq.jndi.ActiveMQInitialContextFactory"/>
+                   <jms:JMSNamingProperty name="java.naming.provider.url" value="tcp://localhost:61500"/>
+               </jms:address>
+      <jms:jmsConfig-ref>jmsConfig</jms:jmsConfig-ref>  
+  </jms:destination>
+
+  <jaxrs:server xmlns:s="http://books.com" 
+                serviceName="s:BookService"
+                transportId="http://cxf.apache.org/transports/jms"
+		        address="/">
+    <jaxrs:serviceBeans>
+      <bean class="org.apache.cxf.systest.jaxrs.BookStore" />
+    </jaxrs:serviceBeans>		
+  </jaxrs:server>
+
+</beans>

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/jms_server_config.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml