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.ObjectHelper; 029 030 /** 031 * Helper class to help wrapping content into GZIP input and output streams. 032 */ 033 public final class GZIPHelper { 034 035 public static final String CONTENT_ENCODING = "Content-Encoding"; 036 public static final String GZIP = "gzip"; 037 038 039 // No need for instatiating, so avoid it. 040 private GZIPHelper() { 041 } 042 043 /** 044 * @deprecated set the header using {@link Message#setHeader(String, Object)} 045 */ 046 public static void setGZIPMessageHeader(Message message) { 047 message.setHeader(CONTENT_ENCODING, GZIP); 048 } 049 050 /** 051 * @deprecated set the header using {@link HttpServletResponse#setHeader(String, String)} 052 */ 053 public static void setGZIPContentEncoding(HttpServletResponse response) { 054 response.setHeader(CONTENT_ENCODING, GZIP); 055 } 056 057 public static InputStream toGZIPInputStream(String contentEncoding, InputStream in) throws IOException { 058 if (isGzip(contentEncoding)) { 059 return new GZIPInputStream(in); 060 } else { 061 return in; 062 } 063 } 064 065 public static InputStream toGZIPInputStream(String contentEncoding, byte[] data) throws Exception { 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 ObjectHelper.close(gzip, "gzip", null); 077 ObjectHelper.close(os, "byte array", null); 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 gzip.close(); 093 os.close(); 094 } 095 } 096 097 public static boolean isGzip(Message message) { 098 return isGzip(message.getHeader(CONTENT_ENCODING, String.class)); 099 } 100 101 public static boolean isGzip(String header) { 102 return header != null && header.toLowerCase().contains("gzip"); 103 } 104 105 /** 106 * @deprecated use isGzip 107 */ 108 public static boolean containsGzip(String str) { 109 return str != null && str.toLowerCase().indexOf(GZIP) >= 0; 110 } 111 112 113 }