Coverage Report - org.apache.camel.component.http.HttpProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpProducer
90% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.http;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Message;
 22  
 import org.apache.camel.Producer;
 23  
 import org.apache.camel.impl.DefaultProducer;
 24  
 import org.apache.commons.httpclient.Header;
 25  
 import org.apache.commons.httpclient.HttpClient;
 26  
 import org.apache.commons.httpclient.HttpMethod;
 27  
 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
 28  
 import org.apache.commons.httpclient.methods.GetMethod;
 29  
 import org.apache.commons.httpclient.methods.PostMethod;
 30  
 import org.apache.commons.httpclient.methods.RequestEntity;
 31  
 
 32  
 import java.io.InputStream;
 33  
 
 34  
 /**
 35  
  * @version $Revision: 1.1 $
 36  
  */
 37  
 public class HttpProducer extends DefaultProducer<HttpExchange> implements Producer<HttpExchange> {
 38  1
     private HttpClient httpClient = new HttpClient();
 39  
 
 40  
     public HttpProducer(HttpEndpoint endpoint) {
 41  1
         super(endpoint);
 42  1
     }
 43  
 
 44  
     public void process(Exchange exchange) throws Exception {
 45  1
         HttpMethod method = createMethod(exchange);
 46  1
         int responseCode = httpClient.executeMethod(method);
 47  
 
 48  
         // lets store the result in the output message.
 49  1
         InputStream in = method.getResponseBodyAsStream();
 50  1
         Message out = exchange.getOut(true);
 51  1
         out.setBody(in);
 52  
 
 53  
         // lets set the headers
 54  1
         Header[] headers = method.getResponseHeaders();
 55  3
         for (Header header : headers) {
 56  2
             String name = header.getName();
 57  2
             String value = header.getValue();
 58  2
             out.setHeader(name, value);
 59  
         }
 60  
 
 61  1
         out.setHeader("http.responseCode", responseCode);
 62  1
     }
 63  
 
 64  
     protected HttpMethod createMethod(Exchange exchange) {
 65  1
         String uri = getEndpoint().getEndpointUri();
 66  1
         RequestEntity requestEntity = createRequestEntity(exchange);
 67  1
         if (requestEntity == null) {
 68  0
             return new GetMethod(uri);
 69  
         }
 70  
         // TODO we might be PUT? - have some better way to explicitly choose method
 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  
             }
 85  
             else {
 86  0
                 return new ByteArrayRequestEntity(data);
 87  
             }
 88  
         }
 89  0
         return entity;
 90  
     }
 91  
 }