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