1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.http; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.endpoint.InboundEndpoint; |
15 | |
import org.mule.api.transformer.Transformer; |
16 | |
import org.mule.api.transport.ReceiveException; |
17 | |
import org.mule.transport.AbstractMessageRequester; |
18 | |
import org.mule.transport.http.i18n.HttpMessages; |
19 | |
import org.mule.transport.http.transformers.HttpClientMethodResponseToObject; |
20 | |
import org.mule.util.StringUtils; |
21 | |
|
22 | |
import org.apache.commons.httpclient.HttpClient; |
23 | |
import org.apache.commons.httpclient.HttpMethod; |
24 | |
import org.apache.commons.httpclient.HttpStatus; |
25 | |
import org.apache.commons.httpclient.methods.GetMethod; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class HttpClientMessageRequester extends AbstractMessageRequester |
31 | |
{ |
32 | |
|
33 | |
protected final HttpConnector connector; |
34 | 0 | protected volatile HttpClient client = null; |
35 | |
protected final Transformer receiveTransformer; |
36 | |
|
37 | |
public HttpClientMessageRequester(InboundEndpoint endpoint) |
38 | |
{ |
39 | 0 | super(endpoint); |
40 | 0 | this.connector = (HttpConnector) endpoint.getConnector(); |
41 | 0 | this.receiveTransformer = new HttpClientMethodResponseToObject(); |
42 | 0 | this.receiveTransformer.setMuleContext(connector.getMuleContext()); |
43 | 0 | } |
44 | |
|
45 | |
protected void doConnect() throws Exception |
46 | |
{ |
47 | 0 | if (client == null) |
48 | |
{ |
49 | 0 | client = connector.doClientConnect(); |
50 | |
} |
51 | 0 | } |
52 | |
|
53 | |
protected void doDisconnect() throws Exception |
54 | |
{ |
55 | 0 | client = null; |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
protected MuleMessage doRequest(long timeout) throws Exception |
70 | |
{ |
71 | 0 | HttpMethod httpMethod = new GetMethod(endpoint.getEndpointURI().getAddress()); |
72 | 0 | connector.setupClientAuthorization(null, httpMethod, client, endpoint); |
73 | |
|
74 | 0 | boolean releaseConn = false; |
75 | |
try |
76 | |
{ |
77 | 0 | HttpClient client = new HttpClient(); |
78 | 0 | client.executeMethod(httpMethod); |
79 | |
|
80 | 0 | if (httpMethod.getStatusCode() == HttpStatus.SC_OK) |
81 | |
{ |
82 | 0 | MuleMessage res = (MuleMessage) receiveTransformer.transform(httpMethod); |
83 | 0 | if (StringUtils.EMPTY.equals(res.getPayload())) |
84 | |
{ |
85 | 0 | releaseConn = true; |
86 | |
} |
87 | 0 | return res; |
88 | |
} |
89 | |
else |
90 | |
{ |
91 | 0 | releaseConn = true; |
92 | 0 | throw new ReceiveException( |
93 | |
HttpMessages.requestFailedWithStatus(httpMethod.getStatusLine().toString()), |
94 | |
endpoint, timeout); |
95 | |
} |
96 | |
} |
97 | 0 | catch (ReceiveException e) |
98 | |
{ |
99 | 0 | releaseConn = true; |
100 | 0 | throw e; |
101 | |
} |
102 | 0 | catch (Exception e) |
103 | |
{ |
104 | 0 | releaseConn = true; |
105 | 0 | throw new ReceiveException(endpoint, timeout, e); |
106 | |
} |
107 | |
finally |
108 | |
{ |
109 | 0 | if (releaseConn) |
110 | |
{ |
111 | 0 | httpMethod.releaseConnection(); |
112 | |
} |
113 | |
} |
114 | |
} |
115 | |
|
116 | |
protected void doDispose() |
117 | |
{ |
118 | |
|
119 | 0 | } |
120 | |
} |