1 package net.sf.crispy.impl; 2 3 import java.lang.reflect.Method; 4 5 import net.sf.crispy.ExceptionWrapper; 6 import net.sf.crispy.InvocationException; 7 import net.sf.crispy.InvocationStrategy; 8 import net.sf.crispy.impl.http.Serializer; 9 import net.sf.crispy.strategy.RestInvocationStrategy; 10 11 import org.apache.commons.httpclient.Header; 12 import org.apache.commons.httpclient.HttpStatus; 13 import org.apache.commons.httpclient.HttpVersion; 14 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; 15 import org.apache.commons.httpclient.methods.PostMethod; 16 import org.apache.commons.httpclient.params.HttpMethodParams; 17 18 19 /** 20 * Remote-Call with serialize Java Objects about HTTP protocoll. 21 * If Java classes not implement the interface <code>java.io.Serializable</code>, 22 * then cann object convert with <code>net.sf.crispy.util.Converter</code>. 23 * 24 * 25 * @author Linke 26 * 27 */ 28 public class HttpExecutor extends AbstractHttpExecutor { 29 30 public final static String DEFAULT_URL_AND_PORT = "http://localhost:8111/crispy/httpserializer"; 31 32 33 public String getDefaultUrlAndPort() { return DEFAULT_URL_AND_PORT; } 34 35 public InvocationStrategy getDefaultInvocationStrategy() { return new RestInvocationStrategy(); } 36 37 public Object execute(Class pvProxyClass, Object pvProxy, Method pvMethod, Object[] pvArgs) throws Exception { 38 Object lvResult = super.execute(pvProxyClass, pvProxy, pvMethod, pvArgs); 39 PostMethod lvMethod = new PostMethod(url); 40 try { 41 lvMethod.setRequestHeader(new Header("Content-Type", "application/x-java-serialized-object")); 42 43 HttpMethodParams lvHttpMethodParams = new HttpMethodParams(); 44 lvHttpMethodParams.setVersion(HttpVersion.HTTP_1_1); 45 46 lvMethod.setParams(lvHttpMethodParams); 47 lvMethod.setRequestEntity(new ByteArrayRequestEntity(Serializer.serialize(pvArgs))); 48 lvMethod.setRequestHeader(new Header("User-Agent","Crispy-Http")); 49 int stat = httpClient.executeMethod(lvMethod); 50 if (stat == HttpStatus.SC_OK) { 51 lvResult = Serializer.deserialize(lvMethod.getResponseBodyAsStream()); 52 } else { 53 throw new InvocationException("Invalid HttpStatus in HttpExecutor: " + lvMethod.getStatusText() + " (" + stat + ")"); 54 } 55 56 } catch (InvocationException e) { 57 throw e; 58 } catch (Exception e) { 59 e.printStackTrace(); 60 throw new InvocationException("Error in Crispy-Http-Executor.", e); 61 } 62 finally { 63 lvMethod.releaseConnection(); 64 } 65 66 lvResult = ExceptionWrapper.isResultExceptionThanThrowIt(lvResult); 67 68 return lvResult; 69 } 70 71 72 }