Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache.util

File GZipUtil.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
24% of files have more coverage

Code metrics

8
27
5
1
121
65
10
0.37
5.4
5
2

Classes

Class Line # Actions
GZipUtil 56 27 0% 10 4
0.990%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /**
2    * This file Copyright (c) 2008-2018 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  27 toggle public static byte[] gzip(byte[] ungzipped) throws IOException {
69  27 if (isGZipped(ungzipped)) {
70  0 throw new IllegalStateException("The byte[] is already gzipped. It should not be gzipped again.");
71    }
72  27 final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
73  27 final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes);
74  27 gzipOutputStream.write(ungzipped);
75  27 gzipOutputStream.close();
76  27 return bytes.toByteArray();
77    }
78   
 
79  8 toggle public static byte[] ungzip(final byte[] gzipped) throws IOException {
80  8 final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped));
81  8 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length);
82  8 final byte[] buffer = new byte[FOUR_KB];
83  8 int bytesRead = 0;
84  36 while (bytesRead != -1) {
85  28 bytesRead = inputStream.read(buffer, 0, FOUR_KB);
86  28 if (bytesRead != -1) {
87  20 byteArrayOutputStream.write(buffer, 0, bytesRead);
88    }
89    }
90  8 byte[] ungzipped = byteArrayOutputStream.toByteArray();
91  8 inputStream.close();
92  8 byteArrayOutputStream.close();
93  8 return ungzipped;
94    }
95   
96    /**
97    * Checks the first two bytes of the candidate byte array for the magic number 0x677a.
98    */
 
99  57 toggle public static boolean isGZipped(byte[] candidate) {
100  57 if (candidate == null || candidate.length < 2) {
101  16 return false;
102    } else {
103  41 return (candidate[0] == GZIP_MAGIC_NUMBER_BYTE_1 && candidate[1] == GZIP_MAGIC_NUMBER_BYTE_2);
104    }
105    }
106   
 
107  0 toggle public static boolean isGZipMimeType(String contentType) {
108  0 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  7 toggle public static boolean isAcceptsGzip(HttpServletRequest request){
115  7 CacheModule module = Components.getComponent(CacheModule.class);
116  7 boolean compressionVote = module.getCompression().getVoters().vote(request)>0;
117  7 boolean requestAcceptsGzip = RequestHeaderUtil.acceptsGzipEncoding(request);
118  7 return requestAcceptsGzip && compressionVote;
119    }
120   
121    }