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.BufferedInputStream;
020    import java.io.ByteArrayInputStream;
021    import java.io.ByteArrayOutputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.util.zip.GZIPInputStream;
025    import java.util.zip.GZIPOutputStream;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.camel.Message;
031    import org.apache.commons.httpclient.Header;
032    import org.apache.commons.httpclient.HttpMethod;
033    
034    /**
035     * 
036     * Helper/Utility class to help wrapping
037     * content into GZIP Input/Output Streams.
038     * 
039     *
040     */
041    public final class GZIPHelper {
042    
043        public static final String CONTENT_ENCODING = "Content-Encoding";
044        public static final String GZIP = "gzip";
045        
046    
047        // No need for instatiating, so avoid it.
048        private GZIPHelper() { }
049        
050        public static void setGZIPMessageHeader(Message message) {
051            message.setHeader(CONTENT_ENCODING, GZIP);
052        }
053        
054        public static void setGZIPContentEncoding(HttpServletResponse response) {
055            response.setHeader(CONTENT_ENCODING, GZIP);
056        }
057    
058        // --------- Methods To Decompress ----------
059    
060        public static InputStream getInputStream(HttpMethod method)
061            throws IOException {
062    
063            Header header = method.getRequestHeader(CONTENT_ENCODING);
064            String contentEncoding =  header != null ? header.getValue() : null;
065            return getGZIPWrappedInputStream(contentEncoding, 
066                method.getResponseBodyAsStream());
067        }
068    
069        public static InputStream getInputStream(HttpServletRequest request) throws IOException {
070            InputStream dataStream = request.getInputStream();
071            String contentEncoding = request.getHeader(CONTENT_ENCODING);
072            return getGZIPWrappedInputStream(contentEncoding, dataStream);
073        }
074    
075        public static InputStream getGZIPWrappedInputStream(String gzipEncoding,
076            InputStream inStream) throws IOException {
077            if (containsGzip(gzipEncoding)) {
078                return new GZIPInputStream(new BufferedInputStream(inStream));
079            } else {
080                return inStream;
081            }
082        }
083    
084        public static InputStream toGZIPInputStreamIfRequested(String gzipEncoding, byte[] array)
085            throws Exception {
086            if (containsGzip(gzipEncoding)) {
087                // GZip byte array content
088                ByteArrayOutputStream outputByteArray = new ByteArrayOutputStream();
089                GZIPOutputStream gzipOutputStream = new GZIPOutputStream(
090                    outputByteArray);
091                gzipOutputStream.write(array);
092                gzipOutputStream.close();
093                return new ByteArrayInputStream(outputByteArray.toByteArray());
094            } else {
095                return new ByteArrayInputStream(array);
096            }
097        }
098    
099        // -------------- Methods To Compress --------------
100    
101        public static byte[] compressArrayIfGZIPRequested(String gzipEncoding,
102            byte[] array) throws IOException {
103            if (containsGzip(gzipEncoding)) {
104                return getGZIPWrappedOutputStream(array).toByteArray();
105            } else {
106                return array;
107            }
108        }
109        
110        public static byte[] compressArrayIfGZIPRequested(String gzipEncoding,
111                byte[] array, HttpServletResponse response) throws IOException {
112            if (containsGzip(gzipEncoding)) {
113                return getGZIPWrappedOutputStream(array).toByteArray();
114            } else {
115                return array;
116            }
117        }
118        
119        public static ByteArrayOutputStream getGZIPWrappedOutputStream(byte[] array) throws IOException {
120            ByteArrayOutputStream compressed = new ByteArrayOutputStream();
121            GZIPOutputStream gzout = new GZIPOutputStream(compressed);
122            gzout.write(array);
123            gzout.close();
124            return compressed;
125        }
126    
127        private static boolean containsGzip(String str) {
128            return str != null && str.toLowerCase().indexOf(GZIP) >= 0;
129        }
130    
131    }