|
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 |
| |
|
24 |
| |
|
25 |
| |
|
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 |
27
| public RestExecutor() { }
|
|
33 |
| |
|
34 |
74
| public String getDefaultUrlAndPort() { return DEFAULT_URL_AND_PORT; }
|
|
35 |
| |
|
36 |
49
| public InvocationStrategy getDefaultInvocationStrategy() { return new RestInvocationStrategy(); }
|
|
37 |
| |
|
38 |
47
| public Object execute(Class pvProxyClass, Object pvProxy, Method pvMethod, Object[] pvArgs) throws Exception {
|
|
39 |
47
| Object lvResult = super.execute(pvProxyClass, pvProxy, pvMethod, pvArgs);
|
|
40 |
| |
|
41 |
47
| String lvParams = new ParameterSerializer().serialize(pvArgs);
|
|
42 |
| if (log.isDebugEnabled()) { log.debug("RestClient invocation parameter: " + lvParams); } |
|
43 |
| |
|
44 |
44
| if (lvParams.length() > 0) { url = url + "&" + lvParams; }
|
|
45 |
| |
|
46 |
47
| PostMethod lvMethod = new PostMethod(url);
|
|
47 |
47
| try {
|
|
48 |
47
| lvMethod.setRequestHeader(new Header("Content-Type", "text/xml"));
|
|
49 |
| |
|
50 |
47
| HttpMethodParams lvHttpMethodParams = new HttpMethodParams();
|
|
51 |
47
| lvHttpMethodParams.setVersion(HttpVersion.HTTP_1_1);
|
|
52 |
47
| lvMethod.setParams(lvHttpMethodParams);
|
|
53 |
| |
|
54 |
47
| lvMethod.setRequestHeader(new Header("User-Agent","Crispy-REST"));
|
|
55 |
47
| int stat = httpClient.executeMethod(lvMethod);
|
|
56 |
| |
|
57 |
47
| if (stat == HttpStatus.SC_OK) {
|
|
58 |
47
| Reader lvReader = new InputStreamReader(lvMethod.getResponseBodyAsStream(), lvMethod.getResponseCharSet());
|
|
59 |
47
| BufferedReader br = null;
|
|
60 |
47
| StringBuffer resultXmlStr = new StringBuffer();
|
|
61 |
47
| try {
|
|
62 |
47
| br = new BufferedReader(lvReader);
|
|
63 |
47
| String line = null;
|
|
64 |
47
| while (true) {
|
|
65 |
94
| line = br.readLine();
|
|
66 |
47
| if (line == null) break;
|
|
67 |
47
| resultXmlStr.append(line);
|
|
68 |
| } |
|
69 |
| } finally { |
|
70 |
47
| if (br != null) {
|
|
71 |
47
| br.close();
|
|
72 |
| } |
|
73 |
| } |
|
74 |
| if (log.isDebugEnabled()) { log.debug("Result from RestClient is: " + resultXmlStr.toString()); } |
|
75 |
| |
|
76 |
47
| lvResult = Deserializer.deserialize(resultXmlStr.toString());
|
|
77 |
47
| lvResult = new Converter().makeComplex(lvResult, pvMethod.getReturnType());
|
|
78 |
47
| if (lvResult instanceof ResponseFail) {
|
|
79 |
0
| ResponseFail lvFail = (ResponseFail) lvResult;
|
|
80 |
0
| throw new InvocationException(lvFail.toString());
|
|
81 |
| } |
|
82 |
| } else { |
|
83 |
0
| throw new InvocationException("Invalid HttpStatus in RestExecutor: " + stat);
|
|
84 |
| } |
|
85 |
| |
|
86 |
| } catch (Exception e) { |
|
87 |
0
| e.printStackTrace();
|
|
88 |
0
| throw new InvocationException("Error in RestExecutor.", e);
|
|
89 |
| } |
|
90 |
| finally { |
|
91 |
47
| lvMethod.releaseConnection();
|
|
92 |
| } |
|
93 |
47
| return lvResult;
|
|
94 |
| } |
|
95 |
| |
|
96 |
| } |