package org.jboss.remoting.transport.http;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jboss.logging.Logger;
import org.jboss.remoting.CannotConnectException;
import org.jboss.remoting.ConnectionFailedException;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.RemoteClientInvoker;
import org.jboss.remoting.marshal.Marshaller;
import org.jboss.remoting.marshal.UnMarshaller;
import org.jboss.remoting.marshal.http.HTTPMarshaller;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HTTPClientInvoker extends RemoteClientInvoker
{
protected final Logger log = Logger.getLogger(getClass());
public HTTPClientInvoker(InvokerLocator locator)
{
super(locator);
}
protected Object transport(String sessionId, Object invocation, Map metadata,
Marshaller marshaller, UnMarshaller unmarshaller)
throws IOException, ConnectionFailedException
{
String targetURL = getLocator().getOriginalURI();
Object httpResponse = useHttpURLConnection(targetURL, invocation, metadata, marshaller, unmarshaller);
return httpResponse;
}
private Object useHttpURLConnection(String url, Object invocation, Map metadata,
Marshaller marshaller, UnMarshaller unmarshaller)
{
Object result = null;
try
{
URL externalURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) externalURL.openConnection();
boolean isPost = true;
if(metadata != null)
{
String type = (String) metadata.get("TYPE");
if(type != null && type.equals("GET"))
{
isPost = false;
}
Map header = (Map) metadata.get("HEADER");
if(header != null)
{
Set keys = header.keySet();
Iterator itr = keys.iterator();
while(itr.hasNext())
{
String key = (String) itr.next();
String value = (String) header.get(key);
log.debug("Setting request header with " + key + " : " + value);
conn.setRequestProperty(key, value);
}
}
}
if(isPost)
{
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
if(metadata != null)
{
String soapAction = (String)metadata.get("SOAPAction");
if (soapAction != null)
{
conn.setRequestProperty("SOAPAction", soapAction);
}
}
OutputStream stream = conn.getOutputStream();
marshaller.write(invocation, stream);
InputStream is = conn.getInputStream();
Map headers = conn.getHeaderFields();
result = unmarshaller.read(is, headers);
}
else
{
throw new Exception("HTTP GET opperation not currently supported.");
}
}
catch(Exception e)
{
log.debug("Error invoking http client invoker.", e);
throw new CannotConnectException("Can not connect http client invoker.", e);
}
return result;
}
private Object useApacheHttpClient(String url, Object invocation, Marshaller marshaller)
throws IOException
{
PostMethod post = new PostMethod(url);
post.setRequestHeader("Content-type", "application/soap+xml");
String body = (String) invocation;
post.setRequestContentLength(body.length());
post.setRequestBody(body);
HttpClient client = new HttpClient();
int status = client.executeMethod(post);
return post.getResponseBodyAsString();
}
protected void handleConnect() throws ConnectionFailedException
{
}
protected void handleDisconnect()
{
}
protected String getDefaultDataType()
{
return HTTPMarshaller.DATATYPE;
}
}