You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/10/19 01:07:51 UTC

svn commit: r465412 - in /incubator/servicemix/trunk: servicemix-core/src/main/java/org/apache/servicemix/ servicemix-http/src/main/java/org/apache/servicemix/http/processors/ servicemix-http/src/test/java/org/apache/servicemix/http/

Author: gnodet
Date: Wed Oct 18 16:07:50 2006
New Revision: 465412

URL: http://svn.apache.org/viewvc?view=rev&rev=465412
Log:
SM-695: Dynamic HTTP provider endpoint
Patch provided by James Lorrenzen, thx

Modified:
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/JbiConstants.java
    incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/processors/ProviderProcessor.java
    incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpProviderTest.java

Modified: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/JbiConstants.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/JbiConstants.java?view=diff&rev=465412&r1=465411&r2=465412
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/JbiConstants.java (original)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/JbiConstants.java Wed Oct 18 16:07:50 2006
@@ -40,5 +40,7 @@
     String STATELESS_PROVIDER = "org.apache.servicemix.provider.stateless";
     
     String SENDER_ENDPOINT = "org.apache.servicemix.senderEndpoint";
+
+    String HTTP_DESTINATION_URI = "org.apache.servicemix.http.destination.uri";
     
 }

Modified: incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/processors/ProviderProcessor.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/processors/ProviderProcessor.java?view=diff&rev=465412&r1=465411&r2=465412
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/processors/ProviderProcessor.java (original)
+++ incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/processors/ProviderProcessor.java Wed Oct 18 16:07:50 2006
@@ -46,6 +46,8 @@
 import org.apache.commons.httpclient.methods.RequestEntity;
 import org.apache.commons.httpclient.protocol.Protocol;
 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.servicemix.JbiConstants;
 import org.apache.servicemix.common.ExchangeProcessor;
 import org.apache.servicemix.http.HttpComponent;
@@ -67,18 +69,22 @@
  */
 public class ProviderProcessor implements ExchangeProcessor {
 
+    private static Log log = LogFactory.getLog(ProviderProcessor.class);
+
     protected HttpEndpoint endpoint;
-    protected HostConfiguration host;
     protected SoapHelper soapHelper;
     protected DeliveryChannel channel;
-    private String relUri;
     private Map methods;
     
     public ProviderProcessor(HttpEndpoint endpoint) {
         this.endpoint = endpoint;
         this.soapHelper = new SoapHelper(endpoint);
-        java.net.URI uri = java.net.URI.create(endpoint.getLocationURI());
-        relUri = uri.getPath();
+        this.methods = new ConcurrentHashMap();
+    }
+
+    private String getRelUri(String locationUri) {
+        java.net.URI uri = java.net.URI.create(locationUri);
+        String relUri = uri.getPath();
         if (!relUri.startsWith("/")) {
             relUri = "/" + relUri;
         }
@@ -88,7 +94,7 @@
         if (uri.getFragment() != null) {
             relUri += "#" + uri.getFragment();
         }
-        this.methods = new ConcurrentHashMap();
+        return relUri;
     }
 
     public void process(MessageExchange exchange) throws Exception {
@@ -105,7 +111,17 @@
         if (nm == null) {
             throw new IllegalStateException("Exchange has no input message");
         }
-        PostMethod method = new PostMethod(relUri);
+
+        String locationURI = endpoint.getLocationURI();
+
+        // Incorporated because of JIRA SM-695
+        Object newDestinationURI = nm.getProperty(JbiConstants.HTTP_DESTINATION_URI);
+        if (newDestinationURI != null) {
+            locationURI = (String) newDestinationURI;
+            log.debug("Location URI overridden: " + locationURI);
+        }
+
+        PostMethod method = new PostMethod(getRelUri(locationURI));
         SoapMessage soapMessage = new SoapMessage();
         soapHelper.getJBIMarshaler().fromNMS(soapMessage, nm);
         Context context = soapHelper.createContext(soapMessage);
@@ -149,7 +165,7 @@
             if (endpoint.getBasicAuthentication() != null) {
                 endpoint.getBasicAuthentication().applyCredentials( getClient() );
             }
-            int response = getClient().executeMethod(host, method);
+            int response = getClient().executeMethod(getHostConfiguration(locationURI), method);
             if (response != HttpStatus.SC_OK && response != HttpStatus.SC_ACCEPTED) {
                 if (exchange instanceof InOnly == false) {
                     SoapReader reader = soapHelper.getSoapMarshaler().createReader();
@@ -225,24 +241,30 @@
         }
     }
 
-    public void start() throws Exception {
-        URI uri = new URI(endpoint.getLocationURI(), false);
+    private HostConfiguration getHostConfiguration(String locationURI) throws Exception {
+        HostConfiguration host;
+        URI uri = new URI(locationURI, false);
         if (uri.getScheme().equals("https")) {
             ProtocolSocketFactory sf = new CommonsHttpSSLSocketFactory(
                             endpoint.getSsl(),
                             endpoint.getKeystoreManager());
             Protocol protocol = new Protocol("https", sf, 443);
-            HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), protocol);
-            this.host = new HostConfiguration();
-            this.host.setHost(host);
+            HttpHost httphost = new HttpHost(uri.getHost(), uri.getPort(), protocol);
+            host = new HostConfiguration();
+            host.setHost(httphost);
         } else {
-            this.host = new HostConfiguration();
-            this.host.setHost(uri.getHost(), uri.getPort());
+            host = new HostConfiguration();
+            host.setHost(uri.getHost(), uri.getPort());
         }
+
+        return host;
+    }
+
+    public void start() throws Exception {
         channel = endpoint.getServiceUnit().getComponent().getComponentContext().getDeliveryChannel();
     }
     
-    protected HttpConfiguration getConfiguration(HttpEndpoint endpoint) {
+    protected HttpConfiguration getConfiguration() {
         HttpComponent comp = (HttpComponent) endpoint.getServiceUnit().getComponent();
         return comp.getConfiguration();
     }
@@ -271,8 +293,7 @@
     }
 	
     protected RequestEntity writeMessage(SoapWriter writer) throws Exception {
-        HttpComponent comp = (HttpComponent) endpoint.getServiceUnit().getComponent();
-        if (comp.getConfiguration().isStreamingEnabled()) {
+        if (getConfiguration().isStreamingEnabled()) {
             return new StreamingRequestEntity(writer);
         } else {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -282,7 +303,7 @@
     }
 
     protected HttpClient getClient() {
-        HttpComponent comp = (HttpComponent) endpoint.getServiceUnit().getComponent();
+        HttpComponent comp =  (HttpComponent) endpoint.getServiceUnit().getComponent();
         return comp.getClient();
     }
 

Modified: incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpProviderTest.java?view=diff&rev=465412&r1=465411&r2=465412
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpProviderTest.java (original)
+++ incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpProviderTest.java Wed Oct 18 16:07:50 2006
@@ -24,179 +24,247 @@
 import javax.jbi.messaging.ExchangeStatus;
 import javax.jbi.messaging.InOut;
 import javax.jbi.messaging.RobustInOnly;
-import javax.jbi.messaging.MessageExchange.Role;
 import javax.xml.namespace.QName;
 import javax.xml.transform.stream.StreamSource;
 
 import junit.framework.TestCase;
 
 import org.apache.servicemix.client.DefaultServiceMixClient;
+import org.apache.servicemix.components.http.HttpConnector;
 import org.apache.servicemix.components.util.EchoComponent;
 import org.apache.servicemix.jbi.container.ActivationSpec;
 import org.apache.servicemix.jbi.container.JBIContainer;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
-import org.apache.servicemix.soap.SoapHelper;
 import org.apache.servicemix.tck.Receiver;
 import org.apache.servicemix.tck.ReceiverComponent;
+import org.apache.servicemix.JbiConstants;
 
 public class HttpProviderTest extends TestCase {
 
-	protected JBIContainer container;
+    protected JBIContainer container;
 
-	protected void setUp() throws Exception {
-		container = new JBIContainer();
-		container.setUseMBeanServer(false);
-		container.setCreateMBeanServer(false);
-		container.setEmbedded(true);
-		container.init();
-	}
-
-	protected void tearDown() throws Exception {
-		if (container != null) {
-			container.shutDown();
-		}
-	}
-
-	protected long testInOnly(String msg, boolean streaming) throws Exception {
-		// HTTP Component
-		HttpComponent component = new HttpComponent();
-		component.getConfiguration().setStreamingEnabled(streaming);
-		container.activateComponent(component, "HttpProviderTest");
-
-		// Add a receiver component
-		Receiver receiver = new ReceiverComponent();
-		ActivationSpec asReceiver = new ActivationSpec("receiver", receiver);
-		asReceiver.setService(new QName("test", "receiver"));
-		container.activateComponent(asReceiver);
-
-		// Add the http receiver
-        HttpComponent connector = new HttpComponent();
-        HttpEndpoint endpoint = new HttpEndpoint();
-        endpoint.setRole(Role.CONSUMER);
-        endpoint.setLocationURI("http://localhost:8192/");
-        endpoint.setSoap(false);
-        endpoint.setDefaultMep(SoapHelper.IN_ONLY);
-        endpoint.setService(new QName("test", "receiver"));
-        endpoint.setEndpoint("BC");
-        connector.setEndpoints(new HttpEndpoint[] { endpoint });
-		ActivationSpec asConnector = new ActivationSpec("connector", connector);
-		container.activateComponent(asConnector);
-
-		// Start container
-		container.start();
-
-		// Deploy SU
-		URL url = getClass().getClassLoader().getResource("provider/http.wsdl");
-		File path = new File(new URI(url.toString()));
-		path = path.getParentFile();
-		component.getServiceUnitManager().deploy("provider",
-				path.getAbsolutePath());
-		component.getServiceUnitManager().start("provider");
-
-		// Call it
-		DefaultServiceMixClient client = new DefaultServiceMixClient(container);
-		RobustInOnly in = client.createRobustInOnlyExchange();
-		in.setInterfaceName(new QName("http://http.servicemix.org/Test",
-				"ProviderInterface"));
-		in.getInMessage().setContent(
-				new StreamSource(new ByteArrayInputStream(msg.getBytes())));
-
-		long t0 = System.currentTimeMillis();
-		client.sendSync(in);
-		long t1 = System.currentTimeMillis();
-		assertTrue(in.getStatus() == ExchangeStatus.DONE);
-
-		// Check we received the message
-		receiver.getMessageList().assertMessagesReceived(1);
-
-		component.getServiceUnitManager().stop("provider");
-		component.getServiceUnitManager().shutDown("provider");
-		component.getServiceUnitManager().undeploy("provider",
-				path.getAbsolutePath());
-
-		return t1 - t0;
-	}
-
-	protected String testInOut(String msg, boolean streaming) throws Exception {
-		// HTTP Component
-		HttpComponent component = new HttpComponent();
-		component.getConfiguration().setStreamingEnabled(streaming);
-		container.activateComponent(component, "HTTPComponent");
-
-		// Add a echo component
-		EchoComponent echo = new EchoComponent();
-		ActivationSpec asReceiver = new ActivationSpec("echo", echo);
-		asReceiver.setService(new QName("test", "echo"));
-		container.activateComponent(asReceiver);
-
-		// Add the http receiver
-        HttpComponent connector = new HttpComponent();
-        HttpEndpoint endpoint = new HttpEndpoint();
-        endpoint.setRole(Role.CONSUMER);
-        endpoint.setLocationURI("http://localhost:8192/");
-        endpoint.setSoap(false);
-        endpoint.setDefaultMep(SoapHelper.IN_OUT);
-        endpoint.setService(new QName("test", "echo"));
-        endpoint.setEndpoint("BC");
-        connector.setEndpoints(new HttpEndpoint[] { endpoint });
+    protected void setUp() throws Exception {
+        container = new JBIContainer();
+        container.setUseMBeanServer(false);
+        container.setCreateMBeanServer(false);
+        container.setEmbedded(true);
+        container.init();
+    }
+
+    protected void tearDown() throws Exception {
+        if (container != null) {
+            container.shutDown();
+        }
+    }
+
+    protected long testInOnly(String msg, boolean streaming) throws Exception {
+        // HTTP Component
+        HttpComponent component = new HttpComponent();
+        ((HttpLifeCycle) component.getLifeCycle()).getConfiguration()
+                .setStreamingEnabled(streaming);
+        container.activateComponent(component, "HttpProviderTest");
+
+        // Add a receiver component
+        Receiver receiver = new ReceiverComponent();
+        ActivationSpec asReceiver = new ActivationSpec("receiver", receiver);
+        asReceiver.setService(new QName("test", "receiver"));
+        container.activateComponent(asReceiver);
+
+        // Add the http receiver
+        HttpConnector connector = new HttpConnector("localhost", 8192);
+        connector.setDefaultInOut(false);
         ActivationSpec asConnector = new ActivationSpec("connector", connector);
+        asConnector.setDestinationService(new QName("test", "receiver"));
         container.activateComponent(asConnector);
 
-		// Start container
-		container.start();
+        // Start container
+        container.start();
 
-		// Deploy SU
-		URL url = getClass().getClassLoader().getResource("provider/http.wsdl");
-		File path = new File(new URI(url.toString()));
-		path = path.getParentFile();
-		component.getServiceUnitManager().deploy("provider",
-				path.getAbsolutePath());
-		component.getServiceUnitManager().start("provider");
-
-		// Call it
-		DefaultServiceMixClient client = new DefaultServiceMixClient(container);
-		InOut inout = client.createInOutExchange();
-		inout.setInterfaceName(new QName("http://http.servicemix.org/Test",
-				"ProviderInterface"));
-		inout.getInMessage().setContent(
-				new StreamSource(new ByteArrayInputStream(msg.getBytes())));
-
-		long t0 = System.currentTimeMillis();
-		client.sendSync(inout);
-		long t1 = System.currentTimeMillis();
-		assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
-
-		// Check we received the message
-		assertNotNull(inout.getOutMessage());
-		assertNotNull(inout.getOutMessage().getContent());
-		SourceTransformer sourceTransformer = new SourceTransformer();
-		String reply = sourceTransformer.toString(inout.getOutMessage()
-				.getContent());
-		String inputMesage = sourceTransformer.toString(new StreamSource(
-				new ByteArrayInputStream(msg.getBytes())));
-		System.out.println("Msg Sent [" + inputMesage + "]");
-		System.out.println("Msg Recieved [" + reply + "]");
-
-		assertEquals(inputMesage.length(), reply.length());
-		assertEquals(inputMesage, reply);
-
-		component.getServiceUnitManager().stop("provider");
-		component.getServiceUnitManager().shutDown("provider");
-		component.getServiceUnitManager().undeploy("provider",
-				path.getAbsolutePath());
-
-		System.out.println("Executed in " + (t1 - t0) + "ms");
-
-		return reply;
-	}
-
-	public void testInOnly() throws Exception {
-		testInOnly("<hello>world</hello>", false);
-	}
-
-	public void testInOut() throws Exception {
-		testInOut("<hello>world</hello>", true);
-	}
+        // Deploy SU
+        URL url = getClass().getClassLoader().getResource("provider/http.wsdl");
+        File path = new File(new URI(url.toString()));
+        path = path.getParentFile();
+        component.getServiceUnitManager().deploy("provider",
+                path.getAbsolutePath());
+        component.getServiceUnitManager().start("provider");
+
+        // Call it
+        DefaultServiceMixClient client = new DefaultServiceMixClient(container);
+        RobustInOnly in = client.createRobustInOnlyExchange();
+        in.setInterfaceName(new QName("http://http.servicemix.org/Test",
+                "ProviderInterface"));
+        in.getInMessage().setContent(
+                new StreamSource(new ByteArrayInputStream(msg.getBytes())));
+
+        long t0 = System.currentTimeMillis();
+        client.sendSync(in);
+        long t1 = System.currentTimeMillis();
+        assertTrue(in.getStatus() == ExchangeStatus.DONE);
+
+        // Check we received the message
+        receiver.getMessageList().assertMessagesReceived(1);
+
+        component.getServiceUnitManager().stop("provider");
+        component.getServiceUnitManager().shutDown("provider");
+        component.getServiceUnitManager().undeploy("provider",
+                path.getAbsolutePath());
+
+        return t1 - t0;
+    }
+
+    /**
+     * The http.wsdl specifies the location URI as localhost:8192.
+     * Set a NormalizedMessage property to override this value.
+     * Therefore don't start the HttpConnector on 8192, rather on
+     * another port to prove this functionality works.
+     * @param msg
+     * @param streaming
+     * @return
+     * @throws Exception
+     */
+    protected long testInOnlyOverrideDestination(String msg, boolean streaming)
+            throws Exception {
+        // HTTP Component
+        HttpComponent component = new HttpComponent();
+        ((HttpLifeCycle) component.getLifeCycle()).getConfiguration()
+                .setStreamingEnabled(streaming);
+        container.activateComponent(component, "HttpProviderTest");
+
+        // Add a receiver component
+        Receiver receiver = new ReceiverComponent();
+        ActivationSpec asReceiver = new ActivationSpec("receiver", receiver);
+        asReceiver.setService(new QName("test", "receiver"));
+        container.activateComponent(asReceiver);
+
+        // Add the http receiver
+        HttpConnector connector = new HttpConnector("localhost", 9192);
+        connector.setDefaultInOut(false);
+        ActivationSpec asConnector = new ActivationSpec("connector", connector);
+        asConnector.setDestinationService(new QName("test", "receiver"));
+        container.activateComponent(asConnector);
+
+        // Start container
+        container.start();
+
+        // Deploy SU
+        URL url = getClass().getClassLoader().getResource("provider/http.wsdl");
+        File path = new File(new URI(url.toString()));
+        path = path.getParentFile();
+        component.getServiceUnitManager().deploy("provider",
+                path.getAbsolutePath());
+        component.getServiceUnitManager().start("provider");
+
+        // Call it
+        DefaultServiceMixClient client = new DefaultServiceMixClient(container);
+        RobustInOnly in = client.createRobustInOnlyExchange();
+        in.setInterfaceName(new QName("http://http.servicemix.org/Test",
+                "ProviderInterface"));
+        in.getInMessage().setContent(
+                new StreamSource(new ByteArrayInputStream(msg.getBytes())));
+        in.getInMessage().setProperty(JbiConstants.HTTP_DESTINATION_URI,
+                "http://localhost:9192/CheckAvailability");
+
+        long t0 = System.currentTimeMillis();
+        client.sendSync(in);
+        long t1 = System.currentTimeMillis();
+        assertTrue(in.getStatus() == ExchangeStatus.DONE);
+
+        // Check we received the message
+        receiver.getMessageList().assertMessagesReceived(1);
+
+        component.getServiceUnitManager().stop("provider");
+        component.getServiceUnitManager().shutDown("provider");
+        component.getServiceUnitManager().undeploy("provider",
+                path.getAbsolutePath());
+
+        return t1 - t0;
+    }
+
+    protected String testInOut(String msg, boolean streaming) throws Exception {
+        // HTTP Component
+        HttpComponent component = new HttpComponent();
+        ((HttpLifeCycle) component.getLifeCycle()).getConfiguration()
+                .setStreamingEnabled(streaming);
+        container.activateComponent(component, "HTTPComponent");
+
+        // Add a echo component
+        EchoComponent echo = new EchoComponent();
+        ActivationSpec asReceiver = new ActivationSpec("echo", echo);
+        asReceiver.setService(new QName("test", "echo"));
+        container.activateComponent(asReceiver);
+
+        // Add the http receiver
+        HttpConnector connector = new HttpConnector("localhost", 8192);
+        connector.setDefaultInOut(true);
+        ActivationSpec asConnector = new ActivationSpec("connector", connector);
+        asConnector.setDestinationService(new QName("test", "echo"));
+        container.activateComponent(asConnector);
+
+        // Start container
+        container.start();
+
+        // Deploy SU
+        URL url = getClass().getClassLoader().getResource("provider/http.wsdl");
+        File path = new File(new URI(url.toString()));
+        path = path.getParentFile();
+        component.getServiceUnitManager().deploy("provider",
+                path.getAbsolutePath());
+        component.getServiceUnitManager().start("provider");
+
+        // Call it
+        DefaultServiceMixClient client = new DefaultServiceMixClient(container);
+        InOut inout = client.createInOutExchange();
+        inout.setInterfaceName(new QName("http://http.servicemix.org/Test",
+                "ProviderInterface"));
+        inout.getInMessage().setContent(
+                new StreamSource(new ByteArrayInputStream(msg.getBytes())));
+
+        long t0 = System.currentTimeMillis();
+        client.sendSync(inout);
+        long t1 = System.currentTimeMillis();
+        assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
+
+        // Check we received the message
+        assertNotNull(inout.getOutMessage());
+        assertNotNull(inout.getOutMessage().getContent());
+        SourceTransformer sourceTransformer = new SourceTransformer();
+        String reply = sourceTransformer.toString(inout.getOutMessage()
+                .getContent());
+        String inputMesage = sourceTransformer.toString(new StreamSource(
+                new ByteArrayInputStream(msg.getBytes())));
+        System.out.println("Msg Sent [" + inputMesage + "]");
+        System.out.println("Msg Recieved [" + reply + "]");
+
+        assertEquals(inputMesage.length(), reply.length());
+        assertEquals(inputMesage, reply);
+
+        component.getServiceUnitManager().stop("provider");
+        component.getServiceUnitManager().shutDown("provider");
+        component.getServiceUnitManager().undeploy("provider",
+                path.getAbsolutePath());
+
+        System.out.println("Executed in " + (t1 - t0) + "ms");
+
+        return reply;
+    }
+
+    public void testInOnly() throws Exception {
+        testInOnly("<hello>world</hello>", false);
+    }
+
+    /**
+     * JIRA SM-695.
+     * Tests the ability of the ProviderProcessor to override
+     * the locationURI using the property JbiConstants.HTTP_DESTINATION_URI
+     * @throws Exception
+     */
+    public void testInOnlyOverrideDestination() throws Exception {
+        testInOnlyOverrideDestination("<hello>world</hello>", false);
+    }
+
+    public void testInOut() throws Exception {
+        testInOut("<hello>world</hello>", true);
+    }
 
 	public void testPerfInOnlyWithBigMessage() throws Exception {
 		int nbRuns = 10;