Coverage Report - org.apache.camel.component.http.HttpProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpProducer
90% 
100% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * @version $Revision: 1.1 $
 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  
         // lets store the result in the output message.
 48  1
         InputStream in = method.getResponseBodyAsStream();
 49  1
         Message out = exchange.getOut(true);
 50  1
         out.setBody(in);
 51  
 
 52  
         // lets set the headers
 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  
         // TODO we might be PUT? - have some better way to explicitly choose
 70  
         // 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  
             } else {
 85  0
                 return new ByteArrayRequestEntity(data);
 86  
             }
 87  
         }
 88  0
         return entity;
 89  
     }
 90  
 }