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    import javax.servlet.http.HttpServletResponse;
026    
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        public static final String CONTENT_ENCODING = "Content-Encoding";
037        public static final String GZIP = "gzip";
038    
039    
040        // No need for instatiating, so avoid it.
041        private GZIPHelper() {
042        }
043        
044        /**
045         * @deprecated set the header using {@link Message#setHeader(String, Object)}
046         */
047        public static void setGZIPMessageHeader(Message message) {
048            message.setHeader(CONTENT_ENCODING, GZIP);
049        }
050    
051        /**
052         * @deprecated set the header using {@link HttpServletResponse#setHeader(String, String)}
053         */
054        public static void setGZIPContentEncoding(HttpServletResponse response) {
055            response.setHeader(CONTENT_ENCODING, GZIP);
056        }
057    
058        public static InputStream uncompressGzip(String contentEncoding, InputStream in) throws IOException {
059            if (isGzip(contentEncoding)) {
060                return new GZIPInputStream(in);
061            } else {
062                return in;
063            }
064        }
065        
066        public static InputStream compressGzip(String contentEncoding, InputStream in) throws IOException {
067    
068            if (isGzip(contentEncoding)) {
069                ByteArrayOutputStream os = new ByteArrayOutputStream();
070                GZIPOutputStream gzip = new GZIPOutputStream(os);
071                try {
072                    IOHelper.copy(in, gzip);
073                    gzip.finish();
074                    return new ByteArrayInputStream(os.toByteArray());
075                } finally {
076                    ObjectHelper.close(gzip, "gzip", null);
077                    ObjectHelper.close(os, "byte array output stream", null);
078                }
079            } else {
080                return in;
081            }
082    
083        }
084    
085        public static InputStream compressGzip(String contentEncoding, byte[] data) throws IOException {
086            if (isGzip(contentEncoding)) {
087                ByteArrayOutputStream os = null;
088                GZIPOutputStream gzip = null;
089                try {
090                    os = new ByteArrayOutputStream();
091                    gzip = new GZIPOutputStream(os);
092                    gzip.write(data);
093                    gzip.finish();
094                    return new ByteArrayInputStream(os.toByteArray());
095                } finally {
096                    ObjectHelper.close(gzip, "gzip", null);
097                    ObjectHelper.close(os, "byte array", null);
098                }
099            } else {
100                return new ByteArrayInputStream(data);
101            }
102        }
103    
104        public static byte[] compressGZIP(byte[] data) throws IOException {
105            ByteArrayOutputStream os = new ByteArrayOutputStream();
106            GZIPOutputStream gzip = new GZIPOutputStream(os);
107            try {
108                gzip.write(data);
109                gzip.finish();
110                return os.toByteArray();
111            } finally {
112                gzip.close();
113                os.close();
114            }
115        }
116    
117        public static boolean isGzip(Message message) {
118            return isGzip(message.getHeader(CONTENT_ENCODING, String.class));
119        }
120    
121        public static boolean isGzip(String header) {
122            return header != null && header.toLowerCase().contains("gzip");
123        }
124    
125        /**
126         * @deprecated use isGzip
127         */
128        public static boolean containsGzip(String str) {
129            return str != null && str.toLowerCase().indexOf(GZIP) >= 0;
130        }
131    
132    
133    }