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