| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.component.http; |
| 18 |
|
|
| 19 |
|
import java.io.InputStream; |
| 20 |
|
|
| 21 |
|
import org.apache.camel.Exchange; |
| 22 |
|
import org.apache.camel.Message; |
| 23 |
|
import org.apache.camel.Producer; |
| 24 |
|
import org.apache.camel.impl.DefaultProducer; |
| 25 |
|
import org.apache.commons.httpclient.Header; |
| 26 |
|
import org.apache.commons.httpclient.HttpClient; |
| 27 |
|
import org.apache.commons.httpclient.HttpMethod; |
| 28 |
|
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; |
| 29 |
|
import org.apache.commons.httpclient.methods.GetMethod; |
| 30 |
|
import org.apache.commons.httpclient.methods.PostMethod; |
| 31 |
|
import org.apache.commons.httpclient.methods.RequestEntity; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
public class HttpProducer extends DefaultProducer<HttpExchange> implements Producer<HttpExchange> { |
| 37 |
1 |
private HttpClient httpClient = new HttpClient(); |
| 38 |
|
|
| 39 |
|
public HttpProducer(HttpEndpoint endpoint) { |
| 40 |
1 |
super(endpoint); |
| 41 |
1 |
} |
| 42 |
|
|
| 43 |
|
public void process(Exchange exchange) throws Exception { |
| 44 |
1 |
HttpMethod method = createMethod(exchange); |
| 45 |
1 |
int responseCode = httpClient.executeMethod(method); |
| 46 |
|
|
| 47 |
|
|
| 48 |
1 |
InputStream in = method.getResponseBodyAsStream(); |
| 49 |
1 |
Message out = exchange.getOut(true); |
| 50 |
1 |
out.setBody(in); |
| 51 |
|
|
| 52 |
|
|
| 53 |
1 |
Header[] headers = method.getResponseHeaders(); |
| 54 |
3 |
for (Header header : headers) { |
| 55 |
2 |
String name = header.getName(); |
| 56 |
2 |
String value = header.getValue(); |
| 57 |
2 |
out.setHeader(name, value); |
| 58 |
|
} |
| 59 |
|
|
| 60 |
1 |
out.setHeader("http.responseCode", responseCode); |
| 61 |
1 |
} |
| 62 |
|
|
| 63 |
|
protected HttpMethod createMethod(Exchange exchange) { |
| 64 |
1 |
String uri = getEndpoint().getEndpointUri(); |
| 65 |
1 |
RequestEntity requestEntity = createRequestEntity(exchange); |
| 66 |
1 |
if (requestEntity == null) { |
| 67 |
0 |
return new GetMethod(uri); |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
1 |
PostMethod method = new PostMethod(uri); |
| 72 |
1 |
method.setRequestEntity(requestEntity); |
| 73 |
1 |
return method; |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
protected RequestEntity createRequestEntity(Exchange exchange) { |
| 77 |
1 |
Message in = exchange.getIn(); |
| 78 |
1 |
RequestEntity entity = in.getBody(RequestEntity.class); |
| 79 |
1 |
if (entity == null) { |
| 80 |
1 |
byte[] data = in.getBody(byte[].class); |
| 81 |
1 |
String contentType = in.getHeader("Content-Type", String.class); |
| 82 |
1 |
if (contentType != null) { |
| 83 |
1 |
return new ByteArrayRequestEntity(data, contentType); |
| 84 |
|
} else { |
| 85 |
0 |
return new ByteArrayRequestEntity(data); |
| 86 |
|
} |
| 87 |
|
} |
| 88 |
0 |
return entity; |
| 89 |
|
} |
| 90 |
|
} |