View Javadoc
1   /**
2    * This file Copyright (c) 2008-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.cache.util;
35  
36  import info.magnolia.cms.util.RequestHeaderUtil;
37  import info.magnolia.module.cache.CacheModule;
38  import info.magnolia.objectfactory.Components;
39  
40  import java.io.ByteArrayInputStream;
41  import java.io.ByteArrayOutputStream;
42  import java.io.IOException;
43  import java.util.Arrays;
44  import java.util.List;
45  import java.util.zip.GZIPInputStream;
46  import java.util.zip.GZIPOutputStream;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  /**
51   * Provide useful methods for working with gzip'd byte[]. Code originated
52   * from net.sf.ehcache.constructs.web.PageInfo under the Apache License Version 2.0.
53   *
54   * @version $Revision: $ ($Author: $)
55   */
56  public class GZipUtil {
57      private static final List GZIP_MIMETYPES = Arrays.asList(new String[]{
58              "application/x-compressed",
59              "application/x-gzip",
60              "application/gnutar",
61              "multipart/x-gzip"
62      });
63  
64      private static final int FOUR_KB = 4196;
65      private static final int GZIP_MAGIC_NUMBER_BYTE_1 = 31;
66      private static final int GZIP_MAGIC_NUMBER_BYTE_2 = -117;
67  
68      public static byte[] gzip(byte[] ungzipped) throws IOException {
69          if (isGZipped(ungzipped)) {
70              throw new IllegalStateException("The byte[] is already gzipped. It should not be gzipped again.");
71          }
72          final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
73          final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes);
74          gzipOutputStream.write(ungzipped);
75          gzipOutputStream.close();
76          return bytes.toByteArray();
77      }
78  
79      public static byte[] ungzip(final byte[] gzipped) throws IOException {
80          final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped));
81          final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length);
82          final byte[] buffer = new byte[FOUR_KB];
83          int bytesRead = 0;
84          while (bytesRead != -1) {
85              bytesRead = inputStream.read(buffer, 0, FOUR_KB);
86              if (bytesRead != -1) {
87                  byteArrayOutputStream.write(buffer, 0, bytesRead);
88              }
89          }
90          byte[] ungzipped = byteArrayOutputStream.toByteArray();
91          inputStream.close();
92          byteArrayOutputStream.close();
93          return ungzipped;
94      }
95  
96      /**
97       * Checks the first two bytes of the candidate byte array for the magic number 0x677a.
98       */
99      public static boolean isGZipped(byte[] candidate) {
100         if (candidate == null || candidate.length < 2) {
101             return false;
102         } else {
103             return (candidate[0] == GZIP_MAGIC_NUMBER_BYTE_1 && candidate[1] == GZIP_MAGIC_NUMBER_BYTE_2);
104         }
105     }
106 
107     public static boolean isGZipMimeType(String contentType) {
108         return GZIP_MIMETYPES.contains(contentType);
109     }
110 
111     /**
112      * True if the response should be gzipped. Uses the compression configuration of the cache and checks if the client accepts gzip responses.
113      */
114     public static boolean isAcceptsGzip(HttpServletRequest request){
115         CacheModule module = Components.getComponent(CacheModule.class);
116         boolean compressionVote = module.getCompression().getVoters().vote(request)>0;
117         boolean requestAcceptsGzip = RequestHeaderUtil.acceptsGzipEncoding(request);
118         return requestAcceptsGzip && compressionVote;
119     }
120 
121 }