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
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 if (isGzip(contentEncoding)) {
048 ByteArrayOutputStream os = new ByteArrayOutputStream();
049 GZIPOutputStream gzip = new GZIPOutputStream(os);
050 try {
051 IOHelper.copy(in, gzip);
052 gzip.finish();
053 return new ByteArrayInputStream(os.toByteArray());
054 } finally {
055 IOHelper.close(gzip, "gzip");
056 IOHelper.close(os, "byte array output stream");
057 }
058 } else {
059 return in;
060 }
061 }
062
063 public static InputStream compressGzip(String contentEncoding, byte[] data) throws IOException {
064 if (isGzip(contentEncoding)) {
065 ByteArrayOutputStream os = null;
066 GZIPOutputStream gzip = null;
067 try {
068 os = new ByteArrayOutputStream();
069 gzip = new GZIPOutputStream(os);
070 gzip.write(data);
071 gzip.finish();
072 return new ByteArrayInputStream(os.toByteArray());
073 } finally {
074 IOHelper.close(gzip, "gzip");
075 IOHelper.close(os, "byte array output stream");
076 }
077 } else {
078 return new ByteArrayInputStream(data);
079 }
080 }
081
082 public static byte[] compressGZIP(byte[] data) throws IOException {
083 ByteArrayOutputStream os = new ByteArrayOutputStream();
084 GZIPOutputStream gzip = new GZIPOutputStream(os);
085 try {
086 gzip.write(data);
087 gzip.finish();
088 return os.toByteArray();
089 } finally {
090 IOHelper.close(gzip, "gzip");
091 IOHelper.close(os, "byte array");
092 }
093 }
094
095 public static boolean isGzip(Message message) {
096 return isGzip(message.getHeader(Exchange.CONTENT_ENCODING, String.class), message.getExchange());
097 }
098
099 public static boolean isGzip(String header , Exchange exchange) {
100 if (exchange == null || !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
101 return isGzip(header);
102 } else {
103 return false;
104 }
105 }
106
107 public static boolean isGzip(String header) {
108 return header != null && header.toLowerCase().contains("gzip");
109 }
110
111 }