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