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