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 | |
private final HttpConnector connector; |
34 | 0 | private volatile HttpClient client = null; |
35 | |
private 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 | } |
43 | |
|
44 | |
protected void doConnect() throws Exception |
45 | |
{ |
46 | 0 | if (client == null) |
47 | |
{ |
48 | 0 | client = connector.doClientConnect(); |
49 | |
} |
50 | 0 | } |
51 | |
|
52 | |
protected void doDisconnect() throws Exception |
53 | |
{ |
54 | 0 | client = null; |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
protected MuleMessage doRequest(long timeout) throws Exception |
69 | |
{ |
70 | 0 | HttpMethod httpMethod = new GetMethod(endpoint.getEndpointURI().getAddress()); |
71 | 0 | connector.setupClientAuthorization(null, httpMethod, client, endpoint); |
72 | |
|
73 | 0 | boolean releaseConn = false; |
74 | |
try |
75 | |
{ |
76 | 0 | HttpClient client = new HttpClient(); |
77 | 0 | client.executeMethod(httpMethod); |
78 | |
|
79 | 0 | if (httpMethod.getStatusCode() == HttpStatus.SC_OK) |
80 | |
{ |
81 | 0 | MuleMessage res = (MuleMessage) receiveTransformer.transform(httpMethod); |
82 | 0 | if (StringUtils.EMPTY.equals(res.getPayload())) |
83 | |
{ |
84 | 0 | releaseConn = true; |
85 | |
} |
86 | 0 | return res; |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | releaseConn = true; |
91 | 0 | throw new ReceiveException( |
92 | |
HttpMessages.requestFailedWithStatus(httpMethod.getStatusLine().toString()), |
93 | |
endpoint, timeout); |
94 | |
} |
95 | |
} |
96 | 0 | catch (ReceiveException e) |
97 | |
{ |
98 | 0 | releaseConn = true; |
99 | 0 | throw e; |
100 | |
} |
101 | 0 | catch (Exception e) |
102 | |
{ |
103 | 0 | releaseConn = true; |
104 | 0 | throw new ReceiveException(endpoint, timeout, e); |
105 | |
} |
106 | |
finally |
107 | |
{ |
108 | 0 | if (releaseConn) |
109 | |
{ |
110 | 0 | httpMethod.releaseConnection(); |
111 | |
} |
112 | |
} |
113 | |
} |
114 | |
|
115 | |
protected void doDispose() |
116 | |
{ |
117 | |
|
118 | 0 | } |
119 | |
} |