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