You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by jl...@apache.org on 2007/03/23 13:03:38 UTC

svn commit: r521687 - in /incubator/cxf/trunk: api/src/main/java/org/apache/cxf/endpoint/ common/common/src/main/java/org/apache/cxf/staxutils/ common/common/src/test/java/org/apache/cxf/staxutils/ rt/core/src/test/java/org/apache/cxf/endpoint/ systest...

Author: jliu
Date: Fri Mar 23 05:03:37 2007
New Revision: 521687

URL: http://svn.apache.org/viewvc?view=rev&rev=521687
Log:
System test for a simple implementation of service routing

Added:
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/ClientServerVersioningTest.java   (with props)
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/MediatorInInterceptor.java   (with props)
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/Server.java   (with props)
Modified:
    incubator/cxf/trunk/api/src/main/java/org/apache/cxf/endpoint/Server.java
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
    incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
    incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/endpoint/DummyServer.java

Modified: incubator/cxf/trunk/api/src/main/java/org/apache/cxf/endpoint/Server.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/endpoint/Server.java?view=diff&rev=521687&r1=521686&r2=521687
==============================================================================
--- incubator/cxf/trunk/api/src/main/java/org/apache/cxf/endpoint/Server.java (original)
+++ incubator/cxf/trunk/api/src/main/java/org/apache/cxf/endpoint/Server.java Fri Mar 23 05:03:37 2007
@@ -20,6 +20,7 @@
 package org.apache.cxf.endpoint;
 
 import org.apache.cxf.transport.Destination;
+import org.apache.cxf.transport.MessageObserver;
 
 public interface Server  {
 
@@ -30,5 +31,7 @@
     Destination getDestination();
     
     Endpoint getEndpoint();
+    
+    MessageObserver getMessageObserver();
         
 }

Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?view=diff&rev=521687&r1=521686&r2=521687
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Fri Mar 23 05:03:37 2007
@@ -137,6 +137,23 @@
         }
     }
 
+    public static boolean toNextTag(DepthXMLStreamReader reader, QName endTag) {
+        try {
+            int depth = reader.getDepth();
+            int event = reader.getEventType();
+            while (reader.getDepth() >= depth && reader.hasNext()) {
+                if (event == XMLStreamReader.START_ELEMENT && reader.getName().equals(endTag) 
+                    && reader.getDepth() == depth + 1) {
+                    return true;
+                }
+                event = reader.next();
+            }
+            return false;
+        } catch (XMLStreamException e) {
+            throw new RuntimeException("Couldn't parse stream.", e);
+        }
+    }    
+    
     public static void writeStartElement(XMLStreamWriter writer, String prefix, String name, String namespace)
         throws XMLStreamException {
         if (prefix == null) {

Modified: incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java?view=diff&rev=521687&r1=521686&r2=521687
==============================================================================
--- incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java (original)
+++ incubator/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java Fri Mar 23 05:03:37 2007
@@ -21,6 +21,7 @@
 
 import java.io.*;
 
+import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
@@ -59,6 +60,16 @@
         assertTrue(StaxUtils.toNextElement(reader));
         assertEquals("Body", reader.getLocalName());
     }
+    
+    @Test
+    public void testToNextTag() throws Exception {
+        String soapMessage = "./resources/headerSoapReq.xml";
+        XMLStreamReader r = StaxUtils.createXMLStreamReader(getTestStream(soapMessage));
+        DepthXMLStreamReader reader = new DepthXMLStreamReader(r);
+        reader.nextTag();
+        StaxUtils.toNextTag(reader, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"));
+        assertEquals("Body", reader.getLocalName());
+    }   
     
     @Test
     public void testCopy() throws Exception {

Modified: incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/endpoint/DummyServer.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/endpoint/DummyServer.java?view=diff&rev=521687&r1=521686&r2=521687
==============================================================================
--- incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/endpoint/DummyServer.java (original)
+++ incubator/cxf/trunk/rt/core/src/test/java/org/apache/cxf/endpoint/DummyServer.java Fri Mar 23 05:03:37 2007
@@ -19,6 +19,7 @@
 package org.apache.cxf.endpoint;
 
 import org.apache.cxf.transport.Destination;
+import org.apache.cxf.transport.MessageObserver;
 
 public class DummyServer implements Server {
     private ServerRegistryImpl serverRegistry;
@@ -45,5 +46,8 @@
         serverRegistry.unregister(this);
         
     }
-
+    
+    public MessageObserver getMessageObserver() {
+        return null;
+    }
 }

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/ClientServerVersioningTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/ClientServerVersioningTest.java?view=auto&rev=521687
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/ClientServerVersioningTest.java (added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/ClientServerVersioningTest.java Fri Mar 23 05:03:37 2007
@@ -0,0 +1,68 @@
+/**
+ * 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.versioning;
+
+import java.lang.reflect.UndeclaredThrowableException;
+
+import javax.xml.namespace.QName;
+
+
+import org.apache.cxf.systest.jaxws.ServerMixedStyle;
+import org.apache.cxf.testutil.common.AbstractClientServerTestBase;
+import org.apache.hello_world_mixedstyle.Greeter;
+import org.apache.hello_world_mixedstyle.SOAPService;
+import org.apache.hello_world_mixedstyle.types.GreetMe1;
+import org.apache.hello_world_mixedstyle.types.GreetMeResponse;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class ClientServerVersioningTest extends AbstractClientServerTestBase {
+
+    private final QName portName = new QName("http://apache.org/hello_world_mixedstyle", "SoapPort");
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        assertTrue("server did not launch correctly", launchServer(ServerMixedStyle.class));
+    }
+    
+    @Test
+    public void testVersionBasedRouting() throws Exception {
+
+        SOAPService service = new SOAPService();
+        assertNotNull(service);
+
+        try {
+            Greeter greeter = service.getPort(portName, Greeter.class);
+
+            GreetMe1 request = new GreetMe1();
+            request.setRequestType("Bonjour");
+            GreetMeResponse greeting = greeter.greetMe(request);
+            assertNotNull("no response received from service", greeting);
+            assertEquals("Hello Bonjour", greeting.getResponseType());
+
+            String reply = greeter.sayHi();
+            assertNotNull("no response received from service", reply);
+            assertEquals("Bonjour", reply);
+        } catch (UndeclaredThrowableException ex) {
+            throw (Exception)ex.getCause();
+        }
+    }
+
+}

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/ClientServerVersioningTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/ClientServerVersioningTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/MediatorInInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/MediatorInInterceptor.java?view=auto&rev=521687
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/MediatorInInterceptor.java (added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/MediatorInInterceptor.java Fri Mar 23 05:03:37 2007
@@ -0,0 +1,126 @@
+/**
+ * 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.versioning;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.logging.Logger;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.binding.soap.SoapMessage;
+import org.apache.cxf.binding.soap.SoapVersion;
+import org.apache.cxf.binding.soap.SoapVersionFactory;
+import org.apache.cxf.bus.CXFBusFactory;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.endpoint.ServerRegistry;
+import org.apache.cxf.interceptor.InterceptorChain;
+import org.apache.cxf.interceptor.StaxInInterceptor;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.staxutils.DepthXMLStreamReader;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.apache.cxf.transport.MessageObserver;
+
+
+public class MediatorInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
+    private static final Logger LOG = Logger.getLogger(MediatorInInterceptor.class.getName());
+
+    public MediatorInInterceptor() {
+        super();
+        setPhase(Phase.POST_STREAM);
+        addBefore(StaxInInterceptor.class.getName());
+    }
+
+    public void handleMessage(SoapMessage message) {
+        if (isGET(message)) {
+            LOG.info("StaxInInterceptor skipped in HTTP GET method");
+            return;
+        }
+
+        String schemaNamespace = "";
+        InterceptorChain chain = message.getInterceptorChain();
+
+        try {
+            //create a buffered stream so that we can roll back to original stream after finishing scaning
+            InputStream is = message.getContent(InputStream.class);
+            BufferedInputStream pis = new BufferedInputStream(is);
+            pis.mark(pis.available());
+            message.setContent(InputStream.class, pis);
+
+            //TODO: need to process attachements
+
+            
+            //Scan the schema namespace, which is used to indicate the service version
+            String encoding = (String)message.get(Message.ENCODING);
+            XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(pis, encoding);
+            DepthXMLStreamReader xmlReader = new DepthXMLStreamReader(reader);
+
+            if (xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
+                String ns = xmlReader.getNamespaceURI();
+                SoapVersion soapVersion = SoapVersionFactory.getInstance().getSoapVersion(ns);
+                StaxUtils.toNextTag(xmlReader, soapVersion.getBody());
+
+                // advance just past body.
+                xmlReader.nextTag();
+            }
+
+            schemaNamespace = xmlReader.getName().getNamespaceURI();
+
+            //Roll back to the original inputStream
+            pis.reset();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (XMLStreamException e) {
+            e.printStackTrace();
+        }
+
+        Bus bus = CXFBusFactory.getDefaultBus();
+        ServerRegistry serverRegistry = bus.getExtension(ServerRegistry.class);
+        List<Server> servers = serverRegistry.getServers();
+
+        Server targetServer = null;
+        for (Server server : servers) {
+            targetServer = server;
+            String address = server.getEndpoint().getEndpointInfo().getAddress();
+            if (schemaNamespace.indexOf("2007/03/21") != -1) {
+                if (address.indexOf("SoapContext2") != -1) {
+                    break;
+                }
+            } else if (address.indexOf("SoapContext1") != -1) {
+                break;
+            }
+        }
+
+        //Redirect the request
+        MessageObserver ob = targetServer.getMessageObserver();
+        ob.onMessage(message);
+
+        chain.abort();
+    }
+
+}

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/MediatorInInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/MediatorInInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/Server.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/Server.java?view=auto&rev=521687
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/Server.java (added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/Server.java Fri Mar 23 05:03:37 2007
@@ -0,0 +1,63 @@
+/**
+ * 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.versioning;
+
+import javax.xml.ws.Endpoint;
+
+import org.apache.cxf.jaxws.EndpointImpl;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.hello_world_mixedstyle.GreeterImplMixedStyle;
+
+
+public class Server extends AbstractBusTestServerBase {
+
+    protected void run() {
+        Object implementor1 = new GreeterImplMixedStyle();
+        String address1 = "http://localhost:9027/SoapContext1/SoapPort";
+        Endpoint.publish(address1, implementor1);
+
+        Object implementor2 = new GreeterImplMixedStyle();
+        String address2 = "http://localhost:9027/SoapContext2/SoapPort";
+        Endpoint.publish(address2, implementor2);
+        
+        //A dummy service that acts as a routing mediator
+        Object implementor = new GreeterImplMixedStyle();
+        String address = "http://localhost:9027/SoapContext/SoapPort";
+        javax.xml.ws.Endpoint jaxwsEndpoint = Endpoint.publish(address, implementor);  
+        
+        //Register a MediatorInInterceptor on this dummy service
+        EndpointImpl jaxwsEndpointImpl = (EndpointImpl)jaxwsEndpoint;
+        org.apache.cxf.endpoint.Server server = jaxwsEndpointImpl.getServer();
+        org.apache.cxf.endpoint.Endpoint endpoint = server.getEndpoint();
+        endpoint.getInInterceptors().add(new MediatorInInterceptor());
+    }
+
+    public static void main(String[] args) {
+        try {
+            Server s = new Server();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+}

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/Server.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/versioning/Server.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date