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.helper;
018    
019    import java.io.ByteArrayInputStream;
020    import java.io.ByteArrayOutputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.util.zip.GZIPInputStream;
024    import java.util.zip.GZIPOutputStream;
025    
026    import org.apache.camel.Exchange;
027    import org.apache.camel.Message;
028    import org.apache.camel.util.IOHelper;
029    import org.apache.camel.util.ObjectHelper;
030    
031    /**
032     * Helper class to help wrapping content into GZIP input and output streams.
033     */
034    public final class GZIPHelper {
035    
036        private GZIPHelper() {
037        }
038        
039        public static InputStream uncompressGzip(String contentEncoding, InputStream in) throws IOException {
040            if (isGzip(contentEncoding)) {
041                return new GZIPInputStream(in);
042            } else {
043                return in;
044            }
045        }
046        
047        public static InputStream compressGzip(String contentEncoding, InputStream in) throws IOException {
048    
049            if (isGzip(contentEncoding)) {
050                ByteArrayOutputStream os = new ByteArrayOutputStream();
051                GZIPOutputStream gzip = new GZIPOutputStream(os);
052                try {
053                    IOHelper.copy(in, gzip);
054                    gzip.finish();
055                    return new ByteArrayInputStream(os.toByteArray());
056                } finally {
057                    ObjectHelper.close(gzip, "gzip", null);
058                    ObjectHelper.close(os, "byte array output stream", null);
059                }
060            } else {
061                return in;
062            }
063    
064        }
065    
066        public static InputStream compressGzip(String contentEncoding, byte[] data) throws IOException {
067            if (isGzip(contentEncoding)) {
068                ByteArrayOutputStream os = null;
069                GZIPOutputStream gzip = null;
070                try {
071                    os = new ByteArrayOutputStream();
072                    gzip = new GZIPOutputStream(os);
073                    gzip.write(data);
074                    gzip.finish();
075                    return new ByteArrayInputStream(os.toByteArray());
076                } finally {
077                    ObjectHelper.close(gzip, "gzip", null);
078                    ObjectHelper.close(os, "byte array", null);
079                }
080            } else {
081                return new ByteArrayInputStream(data);
082            }
083        }
084    
085        public static byte[] compressGZIP(byte[] data) throws IOException {
086            ByteArrayOutputStream os = new ByteArrayOutputStream();
087            GZIPOutputStream gzip = new GZIPOutputStream(os);
088            try {
089                gzip.write(data);
090                gzip.finish();
091                return os.toByteArray();
092            } finally {
093                gzip.close();
094                os.close();
095            }
096        }
097    
098        public static boolean isGzip(Message message) {        
099            return isGzip(message.getHeader(Exchange.CONTENT_ENCODING, String.class), message.getExchange());
100        }
101        
102        public static boolean isGzip(String header , Exchange exchange) {
103            if (exchange == null || !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
104                return isGzip(header);
105            } else {
106                return false;
107            }
108        }
109    
110        public static boolean isGzip(String header) {
111            return header != null && header.toLowerCase().contains("gzip");
112        }
113    
114    }