You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by avonm <ma...@insiel.it> on 2011/03/14 17:35:03 UTC

http proxy for mtom ws

Hi,

we are trying to develop a proxy to be used for mtom web services. 

The proxy will need to read information in the soap header to determine
which is the url of the service, and will use a recipient list to perform
the redirect.

A first initial implementation was the following 

public class HttpServer {

    private static String url =
"http://localhost:8765/jtrouter/?enableMultipartFilter=false";

    public static void main(String[] args) throws Exception {
        HttpServer server = new HttpServer();
        System.out.println("Starting HttpServer... press ctrl + c to stop
it");
        server.server();
        System.out.println("... started and listening on: " + url);
        Thread.sleep(999999999);
    }

    public void server() throws Exception {
        CamelContext camel = new DefaultCamelContext();
        camel.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
            	            	
                from("jetty:http://localhost:8765/jtrouter/")
                .bean(RecipientListTest1.class);

            }
        });
        camel.start();
        

    }
}

public class RecipientListTest1 {
	
	@RecipientList
	public String[] route(Exchange exchange) throws
ParserConfigurationException, SAXException, IOException, MessagingException
	{
        Message msg = exchange.getIn();

        InputStream isBody = msg.getBody(InputStream.class);
                
		String rec =
"http://localhost:8080/helloworld_ws/helloworldws?bridgeEndpoint=true";
		
		return new String[] {rec};
		
	}
}

and it was enough to get an error (received by the proxy when it invokes the
url of the ws)

[                 qtp9788629-13] DefaultErrorHandler            ERROR Failed
delivery for exchangeId: ID-AVON-DELL-3275-1300118139000-0-3. Exhausted
after delivery attempt: 1 caught:
org.apache.camel.component.http.HttpOperationFailedException: HTTP operation
failed invoking http://localhost:8080/helloworld_ws/helloworldws with
statusCode: 500
org.apache.camel.component.http.HttpOperationFailedException: HTTP operation
failed invoking http://localhost:8080/helloworld_ws/helloworldws with
statusCode: 500
	at
org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:179)[camel-http-2.6.0.jar:2.6.0]
	at
org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:114)[camel-http-2.6.0.jar:2.6.0]
	at
org.apache.camel.impl.converter.AsyncProcessorTypeConverter$ProcessorToAsyncProcessorBridge.process(AsyncProcessorTypeConverter.java:50)[camel-core-2.6.0.jar:2.6.0]
	at
org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[camel-core-2.6.0.jar:2.6.0]
	at
org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:299)[camel-core-2.6.0.jar:2.6.0]
	at
org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:208)[camel-core-2.6.0.jar:2.6.0]
	...
	
to solve this we modified this way

            public void configure() throws Exception {            	            	
                from("jetty:http://localhost:8765/jtrouter/")
                .convertBodyTo(StreamSource.class)
                .bean(RecipientListTest2.class);
            }

public class RecipientListTest2 {
	
	@RecipientList
	public String[] route(Exchange exchange) throws
ParserConfigurationException, SAXException, IOException, MessagingException
	{
        Message msg = exchange.getIn();

        InputStream isBody =
msg.getBody(StreamSource.class).getInputStream();
                
		String rec =
"http://localhost:8080/helloworld_ws/helloworldws?bridgeEndpoint=true";
		
		return new String[] {rec};
		
	}

and it worked, but to access the soap header we needed to add other
statements

public class RecipientListTest3 {
	
	@RecipientList
	public String[] route(Exchange exchange) throws
ParserConfigurationException, SAXException, IOException, MessagingException
	{
        Message msg = exchange.getIn();

        InputStream isBody =
msg.getBody(StreamSource.class).getInputStream();
        
        ByteArrayDataSource ds = new ByteArrayDataSource(isBody,
"text/xml");

        MimeMultipart mimeMultipart = new MimeMultipart(ds);
        
        DataHandler dh =
mimeMultipart.getBodyPart("&lt;root.message@cxf.apache.org&gt;").getDataHandler();

        InputStream isdh = dh.getInputStream();
        
        // ....        
        
		String rec =
"http://localhost:8080/helloworld_ws/helloworldws?bridgeEndpoint=true";
		
		return new String[] {rec};
		
	}
}

When running the modified route

            public void configure() throws Exception {            	            	
                from("jetty:http://localhost:8765/jtrouter/")
                .convertBodyTo(StreamSource.class)
                .bean(RecipientListTest3.class);
            }

we had again the error (received by the proxy when it invokes the url of the
ws)

[                qtp28171097-10] DefaultErrorHandler            ERROR Failed
delivery for exchangeId: ID-AVON-DELL-3301-1300118640297-0-3. Exhausted
after delivery attempt: 1 caught:
org.apache.camel.component.http.HttpOperationFailedException: HTTP operation
failed invoking http://localhost:8080/helloworld_ws/helloworldws with
statusCode: 500
org.apache.camel.component.http.HttpOperationFailedException: HTTP operation
failed invoking http://localhost:8080/helloworld_ws/helloworldws with
statusCode: 500
	at
org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:179)[camel-http-2.6.0.jar:2.6.0]
	at
org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:114)[camel-http-2.6.0.jar:2.6.0]
	at
org.apache.camel.impl.converter.AsyncProcessorTypeConverter$ProcessorToAsyncProcessorBridge.process(AsyncProcessorTypeConverter.java:50)[camel-core-2.6.0.jar:2.6.0]
	at
org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:70)[camel-core-2.6.0.jar:2.6.0]
	at
org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:299)[camel-core-2.6.0.jar:2.6.0]
	at
org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:208)[camel-core-2.6.0.jar:2.6.0]
	at
org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:102)[camel-core-2.6.0.jar:2.6.0]
....

We could 'solve' this error by performing a copy of the input stream 

            public void configure() throws Exception {            	            	
                from("jetty:http://localhost:8765/jtrouter/")
                .convertBodyTo(StreamSource.class)
                .bean(RecipientListTest4.class);
            }

public class RecipientListTest4 {
	
	@RecipientList
	public String[] route(Exchange exchange) throws
ParserConfigurationException, SAXException, IOException, MessagingException
	{
        Message msg = exchange.getIn();

        InputStream isBody =
msg.getBody(StreamSource.class).getInputStream();       

        byte[] buf = null;

        try {
          
          buf = new byte[ isBody.available() ];
          isBody.read( buf );

        }catch(IOException ioe){
            ioe.printStackTrace();
        }

        isBody = new ByteArrayInputStream(buf);

        ByteArrayDataSource ds = new ByteArrayDataSource(isBody,
"text/xml");

        MimeMultipart mimeMultipart = new MimeMultipart(ds);
        
        DataHandler dh =
mimeMultipart.getBodyPart("&lt;root.message@cxf.apache.org&gt;").getDataHandler();

        InputStream isdh = dh.getInputStream();
        
        // ....        

        ByteArrayInputStream is = new ByteArrayInputStream(buf);
        
        StreamSource ss = new StreamSource();
        ss.setInputStream(is);
        msg.setBody(ss);
                		        
		String rec =
"http://localhost:8080/helloworld_ws/helloworldws?bridgeEndpoint=true";
		
		return new String[] {rec};
		
	}
}

Clearly we aren't satisfied with this solution because it loads in memory
all the inputstream, is there a better way?	

Thanks,

Mauro



--
View this message in context: http://camel.465427.n5.nabble.com/http-proxy-for-mtom-ws-tp3556654p3556654.html
Sent from the Camel - Users mailing list archive at Nabble.com.