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.IOException; 020 import java.io.InputStream; 021 022 import org.apache.camel.Message; 023 import org.apache.camel.RuntimeCamelException; 024 import org.apache.camel.component.http.helper.LoadingByteArrayOutputStream; 025 import org.apache.camel.impl.PollingConsumerSupport; 026 import org.apache.camel.spi.HeaderFilterStrategy; 027 import org.apache.commons.httpclient.Header; 028 import org.apache.commons.httpclient.HttpClient; 029 import org.apache.commons.httpclient.HttpMethod; 030 import org.apache.commons.httpclient.methods.GetMethod; 031 import org.apache.commons.io.IOUtils; 032 033 /** 034 * A polling HTTP consumer which by default performs a GET 035 * 036 * @version $Revision: 682597 $ 037 */ 038 public class HttpPollingConsumer extends PollingConsumerSupport<HttpExchange> { 039 private final HttpEndpoint endpoint; 040 private HttpClient httpClient; 041 042 public HttpPollingConsumer(HttpEndpoint endpoint) { 043 super(endpoint); 044 this.endpoint = endpoint; 045 httpClient = endpoint.createHttpClient(); 046 } 047 048 public HttpExchange receive() { 049 return receiveNoWait(); 050 } 051 052 public HttpExchange receive(long timeout) { 053 return receiveNoWait(); 054 } 055 056 public HttpExchange receiveNoWait() { 057 HttpExchange exchange = endpoint.createExchange(); 058 HttpMethod method = createMethod(); 059 060 try { 061 int responseCode = httpClient.executeMethod(method); 062 // lets store the result in the output message. 063 LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream(); 064 InputStream is = method.getResponseBodyAsStream(); 065 IOUtils.copy(is, bos); 066 bos.flush(); 067 is.close(); 068 Message message = exchange.getIn(); 069 message.setBody(bos.createInputStream()); 070 071 // lets set the headers 072 Header[] headers = method.getResponseHeaders(); 073 HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy(); 074 for (Header header : headers) { 075 String name = header.getName(); 076 String value = header.getValue(); 077 if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value)) { 078 message.setHeader(name, value); 079 } 080 } 081 082 message.setHeader("http.responseCode", responseCode); 083 return exchange; 084 } catch (IOException e) { 085 throw new RuntimeCamelException(e); 086 } finally { 087 method.releaseConnection(); 088 } 089 } 090 091 // Properties 092 //------------------------------------------------------------------------- 093 public HttpClient getHttpClient() { 094 return httpClient; 095 } 096 097 public void setHttpClient(HttpClient httpClient) { 098 this.httpClient = httpClient; 099 } 100 101 // Implementation methods 102 //------------------------------------------------------------------------- 103 protected HttpMethod createMethod() { 104 String uri = endpoint.getEndpointUri(); 105 return new GetMethod(uri); 106 } 107 108 protected void doStart() throws Exception { 109 } 110 111 protected void doStop() throws Exception { 112 } 113 }