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.Converter; 023 import org.apache.camel.Exchange; 024 import org.apache.camel.component.http.helper.GZIPHelper; 025 import org.apache.camel.util.ExchangeHelper; 026 import org.apache.commons.httpclient.methods.InputStreamRequestEntity; 027 import org.apache.commons.httpclient.methods.RequestEntity; 028 029 /** 030 * Some converter methods to make it easier to convert the body to RequestEntity types. 031 */ 032 @Converter 033 public class RequestEntityConverter { 034 035 @Converter 036 public RequestEntity toRequestEntity(byte[] data, Exchange exchange) throws Exception { 037 return asRequestEntity(data, exchange); 038 } 039 040 @Converter 041 public RequestEntity toRequestEntity(InputStream inStream, Exchange exchange) throws Exception { 042 return asRequestEntity(inStream, exchange); 043 } 044 045 @Converter 046 public RequestEntity toRequestEntity(String str, Exchange exchange) throws Exception { 047 if (GZIPHelper.isGzip(exchange.getIn())) { 048 byte[] data = exchange.getContext().getTypeConverter().convertTo(byte[].class, str); 049 return asRequestEntity(data, exchange); 050 } else { 051 // will use the default StringRequestEntity 052 return null; 053 } 054 } 055 056 private RequestEntity asRequestEntity(InputStream in, Exchange exchange) throws IOException { 057 return new InputStreamRequestEntity( 058 GZIPHelper.toGZIPInputStream( 059 exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class), 060 in), ExchangeHelper.getContentType(exchange)); 061 } 062 063 private RequestEntity asRequestEntity(byte[] data, Exchange exchange) throws Exception { 064 return new InputStreamRequestEntity( 065 GZIPHelper.toGZIPInputStream( 066 exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class), 067 data), ExchangeHelper.getContentType(exchange)); 068 } 069 } 070