1 package net.sf.crispy.impl; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.io.Reader; 6 import java.lang.reflect.Method; 7 8 import net.sf.crispy.InvocationException; 9 import net.sf.crispy.InvocationStrategy; 10 import net.sf.crispy.impl.rest.Deserializer; 11 import net.sf.crispy.impl.rest.ParameterSerializer; 12 import net.sf.crispy.impl.rest.ResponseFail; 13 import net.sf.crispy.strategy.RestInvocationStrategy; 14 import net.sf.crispy.util.Converter; 15 16 import org.apache.commons.httpclient.Header; 17 import org.apache.commons.httpclient.HttpStatus; 18 import org.apache.commons.httpclient.HttpVersion; 19 import org.apache.commons.httpclient.methods.PostMethod; 20 import org.apache.commons.httpclient.params.HttpMethodParams; 21 22 /** 23 * Remote-Call for REST (REpresentational State Transfer). 24 * 25 * @author Linke 26 * 27 */ 28 public class RestExecutor extends AbstractHttpExecutor { 29 30 public final static String DEFAULT_URL_AND_PORT = "http://localhost:8095/crispy/rest"; 31 32 public RestExecutor() { } 33 34 public String getDefaultUrlAndPort() { return DEFAULT_URL_AND_PORT; } 35 36 public InvocationStrategy getDefaultInvocationStrategy() { return new RestInvocationStrategy(); } 37 38 public Object execute(Class pvProxyClass, Object pvProxy, Method pvMethod, Object[] pvArgs) throws Exception { 39 Object lvResult = super.execute(pvProxyClass, pvProxy, pvMethod, pvArgs); 40 41 String lvParams = new ParameterSerializer().serialize(pvArgs); 42 if (log.isDebugEnabled()) { log.debug("RestClient invocation parameter: " + lvParams); } 43 44 if (lvParams.length() > 0) { url = url + "&" + lvParams; } 45 46 PostMethod lvMethod = new PostMethod(url); 47 try { 48 lvMethod.setRequestHeader(new Header("Content-Type", "text/xml")); 49 50 HttpMethodParams lvHttpMethodParams = new HttpMethodParams(); 51 lvHttpMethodParams.setVersion(HttpVersion.HTTP_1_1); 52 lvMethod.setParams(lvHttpMethodParams); 53 54 lvMethod.setRequestHeader(new Header("User-Agent","Crispy-REST")); 55 int stat = httpClient.executeMethod(lvMethod); 56 57 if (stat == HttpStatus.SC_OK) { 58 Reader lvReader = new InputStreamReader(lvMethod.getResponseBodyAsStream(), lvMethod.getResponseCharSet()); 59 BufferedReader br = null; 60 StringBuffer resultXmlStr = new StringBuffer(); 61 try { 62 br = new BufferedReader(lvReader); 63 String line = null; 64 while (true) { 65 line = br.readLine(); 66 if (line == null) break; 67 resultXmlStr.append(line); 68 } 69 } finally { 70 if (br != null) { 71 br.close(); 72 } 73 } 74 if (log.isDebugEnabled()) { log.debug("Result from RestClient is: " + resultXmlStr.toString()); } 75 76 lvResult = Deserializer.deserialize(resultXmlStr.toString()); 77 lvResult = new Converter().makeComplex(lvResult, pvMethod.getReturnType()); 78 if (lvResult instanceof ResponseFail) { 79 ResponseFail lvFail = (ResponseFail) lvResult; 80 throw new InvocationException(lvFail.toString()); 81 } 82 } else { 83 throw new InvocationException("Invalid HttpStatus in RestExecutor: " + stat); 84 } 85 86 } catch (Exception e) { 87 e.printStackTrace(); 88 throw new InvocationException("Error in RestExecutor.", e); 89 } 90 finally { 91 lvMethod.releaseConnection(); 92 } 93 return lvResult; 94 } 95 96 }