001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.component.http;
019
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Message;
022 import org.apache.camel.Producer;
023 import org.apache.camel.impl.DefaultProducer;
024 import org.apache.commons.httpclient.Header;
025 import org.apache.commons.httpclient.HttpClient;
026 import org.apache.commons.httpclient.HttpMethod;
027 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
028 import org.apache.commons.httpclient.methods.GetMethod;
029 import org.apache.commons.httpclient.methods.PostMethod;
030 import org.apache.commons.httpclient.methods.RequestEntity;
031
032 import java.io.InputStream;
033
034 /**
035 * @version $Revision: 1.1 $
036 */
037 public class HttpProducer extends DefaultProducer<HttpExchange> implements Producer<HttpExchange> {
038 private HttpClient httpClient = new HttpClient();
039
040 public HttpProducer(HttpEndpoint endpoint) {
041 super(endpoint);
042 }
043
044 public void process(Exchange exchange) throws Exception {
045 HttpMethod method = createMethod(exchange);
046 int responseCode = httpClient.executeMethod(method);
047
048 // lets store the result in the output message.
049 InputStream in = method.getResponseBodyAsStream();
050 Message out = exchange.getOut(true);
051 out.setBody(in);
052
053 // lets set the headers
054 Header[] headers = method.getResponseHeaders();
055 for (Header header : headers) {
056 String name = header.getName();
057 String value = header.getValue();
058 out.setHeader(name, value);
059 }
060
061 out.setHeader("http.responseCode", responseCode);
062 }
063
064 protected HttpMethod createMethod(Exchange exchange) {
065 String uri = getEndpoint().getEndpointUri();
066 RequestEntity requestEntity = createRequestEntity(exchange);
067 if (requestEntity == null) {
068 return new GetMethod(uri);
069 }
070 // TODO we might be PUT? - have some better way to explicitly choose method
071 PostMethod method = new PostMethod(uri);
072 method.setRequestEntity(requestEntity);
073 return method;
074 }
075
076 protected RequestEntity createRequestEntity(Exchange exchange) {
077 Message in = exchange.getIn();
078 RequestEntity entity = in.getBody(RequestEntity.class);
079 if (entity == null) {
080 byte[] data = in.getBody(byte[].class);
081 String contentType = in.getHeader("Content-Type", String.class);
082 if (contentType != null) {
083 return new ByteArrayRequestEntity(data, contentType);
084 }
085 else {
086 return new ByteArrayRequestEntity(data);
087 }
088 }
089 return entity;
090 }
091 }