You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@servicemix.apache.org by qadro <ms...@interia.pl> on 2010/04/02 23:39:00 UTC

HTTP Provider with custom Marshaller problem

Hi there,

I'm preparing a simple HTTP bridge like:

user -> HTTP-consumer -> NMR -> HTTP-provider -> Google Translate

I am using a marshaler in the consumer which works perfectly (packing user's
request into XML) but I have a problem with Marshaler at the provider's
side.

provider su xbean.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:http="http://servicemix.apache.org/http/1.0"
       xmlns:tr="http://traduce.pl"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://servicemix.apache.org/http/1.0
http://servicemix.apache.org/schema/servicemix-http-3.2.3.xsd
       http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  <http:provider service="tr:googletranslate"
                 endpoint="json"
                 marshaler="#marshaler" />
                 
  <bean id="marshaler"
class="pl.traduce.su.googletranslate.HttpTranslationMarshaler" />    

</beans>


The Marshaler class is the following:
public class HttpTranslationMarshaler
	extends DefaultHttpProviderMarshaler
{

	/* (non-Javadoc)
	 * @see
org.apache.servicemix.http.endpoints.DefaultHttpProviderMarshaler#createRequest(javax.jbi.messaging.MessageExchange,
javax.jbi.messaging.NormalizedMessage,
org.apache.servicemix.http.jetty.SmxHttpExchange)
	 */
	@Override
	public void createRequest(MessageExchange me, NormalizedMessage nm,
			SmxHttpExchange she) throws Exception {
	    
		//super.createRequest(me, nm, she);
		
		String text = ((StringSource)nm.getContent()).getText().substring(6,
((StringSource)nm.getContent()).getText().length() - 6);//TODO: replace with
SAX
		
		she.setMethod(HttpMethods.GET);
		she.setURI("/translate_a/t?client=traduce.pl&text=" + text +
"&sl=en&tl=fr");
		she.setAddress(new InetSocketAddress("translate.google.com", 80));
		she.addRequestHeader(HttpHeaders.HOST_BUFFER, new
ByteArrayBuffer("translate.google.com"));
	
//she.setURL("http://translate.google.com:80/translate_a/t?client=traduce.pl&text="
+ text + "&sl=en&tl=fr");
				
		System.err.println("requesting Google!");
	}

	/* (non-Javadoc)
	 * @see
org.apache.servicemix.http.endpoints.HttpProviderMarshaler#handleResponse(javax.jbi.messaging.MessageExchange,
org.apache.servicemix.http.jetty.SmxHttpExchange)
	 */
	@Override
	public void handleResponse(MessageExchange me, SmxHttpExchange she)
			throws Exception {
		
		System.err.println("received answer!");
		System.err.println(she.getResponseContent());
		
		NormalizedMessage nm = me.createMessage();
		nm.setContent(new StringSource("got JSON!"));
		me.setMessage(nm, "out");	//or in??
	}
	
}         

All i get is:

requesting Google!
java.net.UnknownHostException: Remote socket address cannot be null.
	at org.mortbay.jetty.client.HttpClient.getDestination(HttpClient.java:160)
	at org.mortbay.jetty.client.HttpClient.send(HttpClient.java:134)
	at
org.apache.servicemix.http.endpoints.HttpProviderEndpoint.process(HttpProviderEndpoint.java:112)
	at
org.apache.servicemix.common.AsyncBaseLifeCycle.doProcess(AsyncBaseLifeCycle.java:538)
	at
org.apache.servicemix.common.AsyncBaseLifeCycle.processExchange(AsyncBaseLifeCycle.java:490)
	at
org.apache.servicemix.common.BaseLifeCycle.onMessageExchange(BaseLifeCycle.java:46)
	at
org.apache.servicemix.jbi.messaging.DeliveryChannelImpl.processInBound(DeliveryChannelImpl.java:620)
	at
org.apache.servicemix.jbi.nmr.flow.AbstractFlow.doRouting(AbstractFlow.java:172)
	at
org.apache.servicemix.jbi.nmr.flow.seda.SedaFlow.doRouting(SedaFlow.java:167)
	at
org.apache.servicemix.jbi.nmr.flow.seda.SedaQueue$1.run(SedaQueue.java:134)
	at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)

despite the fact that I DO SET the location...

Morover, such a standalone test works well:
		ready = false;
		
		HttpExchange she = new ContentExchange() {
		    @Override
			protected void onResponseComplete() throws IOException
			{
		    	int status = getResponseStatus();
			    if (status == 200) {
			    	System.out.println(getResponseContent());
			    }
			    else
			        System.err.println("ERROR" + status);
			    ready = true;
			}
		};
		String text = "Ala%20ma%20kota";
		
		she.setMethod(HttpMethods.GET);
		
		she.setURI("/translate_a/t?client=traduce.pl&text=" + text +
"&sl=en&tl=fr");
		she.setAddress(new InetSocketAddress("translate.google.com", 80));
		she.addRequestHeader(HttpHeaders.HOST_BUFFER, new
ByteArrayBuffer("translate.google.com"));
		
		System.err.println("requesting Google!");
		
		HttpClient client = new HttpClient();
		client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
		client.setMaxConnectionsPerAddress(2); // max 200 concurrent connections
to every address
		client.setThreadPool(new QueuedThreadPool(2)); // max 2 threads
		client.setTimeout(30000); // 30 seconds timeout; if no server reply, the
request expires
		client.start();
		
		client.send(she);
		
		while (!ready) {}


Do you have any ideas how to fix my Marshaler?
-- 
View this message in context: http://old.nabble.com/HTTP-Provider-with-custom-Marshaller-problem-tp28123182p28123182.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.


Re: HTTP Provider with custom Marshaller problem

Posted by Joe Luo <jo...@ymail.com>.
I am not sure how you compiled your SU since the API org.mortbay.jetty.client.HttpExchange.setAddress(...) only takes parameter of type org.mortbay.jetty.client.Address not java.net.InetSocketAddress. 

If there is no Address set on the HttpExchange, you would see the exception.

Regards,
/Joe

Send instant messages to your online friends http://uk.messenger.yahoo.com