diff --git a/DIFFERENCES.md b/DIFFERENCES.md index 9dbf65a6ec..d005737a08 100644 --- a/DIFFERENCES.md +++ b/DIFFERENCES.md @@ -114,7 +114,7 @@ Additional to `GZip`, this version includes [Brotli](https://en.wikipedia.org/wi which is [widely supported](https://caniuse.com/brotli) by modern browsers. In order to use this compression you will need first to add the -[com.nixxcode.jvmbrotli](https://mvnrepository.com/artifact/com.nixxcode.jvmbrotli) dependency +[com.aayushatharva.brotli4j](https://mvnrepository.com/artifact/com.aayushatharva.brotli4j) dependency in your project (Gradle, Maven, etc). **NOTE**: If you are using Gradle, you may need to also include the native library according to your diff --git a/pom.xml b/pom.xml index 17e719c672..7375e436a0 100644 --- a/pom.xml +++ b/pom.xml @@ -194,9 +194,9 @@ - com.nixxcode.jvmbrotli - jvmbrotli - 0.2.0 + com.aayushatharva.brotli4j + brotli4j + 1.12.0 true diff --git a/src/main/java/spark/Response.java b/src/main/java/spark/Response.java index 774b5f3444..6296ee6d29 100644 --- a/src/main/java/spark/Response.java +++ b/src/main/java/spark/Response.java @@ -50,7 +50,7 @@ public enum Compression { GZIP_COMPRESSED, // It will request Spark to compress the output (brotli) and set the BROTLI header // NOTE: Brotli is a successor to gzip, it is supported by all major web browsers. It provides better compression than gzip. - // Brotli Library is included with `com.nixxcode.jvmbrotli` library + // Brotli Library is included with `com.aayushatharva.brotli4j` library BROTLI_COMPRESS, // It will notify Spark that the output is already BROTLI compressed. // In such case, Spark won't compress the output and will ensure the BROTLI header is set. diff --git a/src/main/java/spark/utils/CompressUtil.java b/src/main/java/spark/utils/CompressUtil.java index eba185ec91..5e59f3a4f4 100644 --- a/src/main/java/spark/utils/CompressUtil.java +++ b/src/main/java/spark/utils/CompressUtil.java @@ -51,6 +51,19 @@ public class CompressUtil { private static final String GZIP = "gzip"; private static final String BROTLI = "br"; + + private static final boolean IS_BROTLI_AVAILABLE; + static { + boolean available = false; + try { + Class brotli = Class.forName("com.aayushatharva.brotli4j.Brotli4jLoader"); + available = (Boolean) brotli.getMethod("isAvailable").invoke(null); + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | + InvocationTargetException e) { + LOG.debug("Brotli was not found: {} Cause: {}", e.getMessage(), e.getCause()); + } + IS_BROTLI_AVAILABLE = available; + } // Hide constructor private CompressUtil() {} @@ -83,7 +96,7 @@ public static OutputStream checkAndWrap(HttpServletRequest httpRequest, HttpServ } /** * Checks if the HTTP request/response accepts and wants to compress outputStream and i that case wraps the response output stream in a - * {@link java.util.zip.GZIPOutputStream} or 'com.nixxcode.jvmbrotli.enc.BrotliOutputStream'. + * {@link java.util.zip.GZIPOutputStream} or 'com.aayushatharva.brotli4j.encoder.BrotliOutputStream'. * * @param httpRequest the HTTP servlet request. * @param httpResponse the HTTP servlet response. @@ -175,15 +188,7 @@ private static void addContentEncodingHeader(HttpServletResponse response, Strin * @return true if brotli is available */ public static boolean isBrotliAvailable() { - boolean available = false; - try { - Class brotli = Class.forName("com.nixxcode.jvmbrotli.common.BrotliLoader"); - available = (Boolean) brotli.getMethod("isBrotliAvailable").invoke(null); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | - InvocationTargetException e) { - LOG.debug("Brotli was not found: {} Cause: {}", e.getMessage(), e.getCause()); - } - return available; + return IS_BROTLI_AVAILABLE; } /** @@ -199,8 +204,8 @@ private static OutputStream getBrotliOutputStream(OutputStream response, int q) if (q <= 0) q = 10; if (q > 11) q = 11; try { - Class brotliOutputStream = Class.forName("com.nixxcode.jvmbrotli.enc.BrotliOutputStream"); - Class encoderParams = Class.forName("com.nixxcode.jvmbrotli.enc.Encoder$Parameters"); + Class brotliOutputStream = Class.forName("com.aayushatharva.brotli4j.encoder.BrotliOutputStream"); + Class encoderParams = Class.forName("com.aayushatharva.brotli4j.encoder.Encoder$Parameters"); Object encoderParamsInstance = encoderParams.getDeclaredConstructor().newInstance(); Object params = encoderParams.getMethod("setQuality", int.class).invoke(encoderParamsInstance, q); responseStream = (OutputStream) brotliOutputStream.getConstructor(OutputStream.class, encoderParams) diff --git a/src/test/java/spark/examples/brotli/BrotliClient.java b/src/test/java/spark/examples/brotli/BrotliClient.java index 101e198912..8309774b49 100644 --- a/src/test/java/spark/examples/brotli/BrotliClient.java +++ b/src/test/java/spark/examples/brotli/BrotliClient.java @@ -8,7 +8,7 @@ import spark.utils.IOUtils; -import com.nixxcode.jvmbrotli.dec.BrotliInputStream; +import com.aayushatharva.brotli4j.decoder.BrotliInputStream; /** * Created by A.Lepe (2022-07-27) based on GzipClient