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.ByteArrayInputStream;
020    import java.io.IOException;
021    import java.io.InputStream;
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(byte[] data, Exchange exchange) throws Exception {
038            return asRequestEntity(data, exchange);
039        }
040    
041        @Converter
042        public RequestEntity toRequestEntity(InputStream inStream, Exchange exchange) throws Exception {
043            return asRequestEntity(inStream, exchange);
044        }
045    
046        @Converter
047        public RequestEntity toRequestEntity(String str, Exchange exchange) throws Exception {
048            if (GZIPHelper.isGzip(exchange.getIn())) {
049                byte[] data = exchange.getContext().getTypeConverter().convertTo(byte[].class, str);
050                return asRequestEntity(data, exchange);
051            } else {
052                // will use the default StringRequestEntity
053                return null;
054            }
055        }
056    
057        private RequestEntity asRequestEntity(InputStream in, Exchange exchange) throws IOException {
058            if (exchange != null
059                && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
060                return new InputStreamRequestEntity(GZIPHelper.compressGzip(exchange.getIn()
061                    .getHeader(Exchange.CONTENT_ENCODING, String.class), in), ExchangeHelper
062                    .getContentType(exchange));
063            } else {
064                return new InputStreamRequestEntity(in);
065            }
066        }
067    
068        private RequestEntity asRequestEntity(byte[] data, Exchange exchange) throws Exception {
069            if (exchange != null
070                && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
071                return new InputStreamRequestEntity(GZIPHelper.compressGzip(exchange.getIn()
072                    .getHeader(Exchange.CONTENT_ENCODING, String.class), data), ExchangeHelper
073                    .getContentType(exchange));
074            } else {
075                return new InputStreamRequestEntity(new ByteArrayInputStream(data));
076            }
077        }
078    }
079