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.http4;
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.http4.helper.GZIPHelper;
026    import org.apache.camel.util.ExchangeHelper;
027    import org.apache.http.HttpEntity;
028    import org.apache.http.entity.InputStreamEntity;
029    
030    /**
031     * Some converter methods to make it easier to convert the body to RequestEntity types.
032     */
033    @Converter
034    public class HttpEntityConverter {
035    
036        @Converter
037        public HttpEntity toHttpEntity(byte[] data, Exchange exchange) throws Exception {
038            return asHttpEntity(data, exchange);
039        }
040    
041        @Converter
042        public HttpEntity toHttpEntity(InputStream inStream, Exchange exchange) throws Exception {
043            return asHttpEntity(inStream, exchange);
044        }
045    
046        @Converter
047        public HttpEntity toHttpEntity(String str, Exchange exchange) throws Exception {
048            if (GZIPHelper.isGzip(exchange.getIn())) {
049                byte[] data = exchange.getContext().getTypeConverter().convertTo(byte[].class, str);
050                return asHttpEntity(data, exchange);
051            } else {
052                // will use the default StringRequestEntity
053                return null;
054            }
055        }
056    
057        private HttpEntity asHttpEntity(InputStream in, Exchange exchange) throws IOException {
058            String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
059            String contentType = ExchangeHelper.getContentType(exchange);
060    
061            InputStreamEntity entity = null;
062            if (exchange != null
063                && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
064                entity = new InputStreamEntity(GZIPHelper.compressGzip(contentEncoding, in), -1);        
065            } else {
066                entity = new InputStreamEntity(in, -1);
067            }
068            entity.setContentEncoding(contentEncoding);
069            entity.setContentType(contentType);
070            return entity;
071        }
072    
073        private HttpEntity asHttpEntity(byte[] data, Exchange exchange) throws Exception {
074            String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
075            String contentType = ExchangeHelper.getContentType(exchange);
076    
077            InputStreamEntity entity = null;
078            if (exchange != null
079                && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
080                entity = new InputStreamEntity(GZIPHelper.compressGzip(contentEncoding, data), -1);        
081            } else {
082                entity = new InputStreamEntity(new ByteArrayInputStream(data), -1);
083            }
084            entity.setContentEncoding(contentEncoding);
085            entity.setContentType(contentType);
086    
087            return entity;
088        }
089    }