From 5ba1c5d67a8555ba3b75e8c96f28b6bc49bcebb6 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 21 Aug 2024 08:10:14 -0500 Subject: [PATCH] Work around Brotli4j EncoderJNI.Operation permission issue. --- .../brotli4j/encoder/EncoderJNIOp.java | 40 +++++++++++++++++++ .../compression/brotli/BrotliEncoderSink.java | 13 +++--- 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 jetty-core/jetty-compression/jetty-compression-brotli/src/main/java/com/aayushatharva/brotli4j/encoder/EncoderJNIOp.java diff --git a/jetty-core/jetty-compression/jetty-compression-brotli/src/main/java/com/aayushatharva/brotli4j/encoder/EncoderJNIOp.java b/jetty-core/jetty-compression/jetty-compression-brotli/src/main/java/com/aayushatharva/brotli4j/encoder/EncoderJNIOp.java new file mode 100644 index 000000000000..3096afa0bbc9 --- /dev/null +++ b/jetty-core/jetty-compression/jetty-compression-brotli/src/main/java/com/aayushatharva/brotli4j/encoder/EncoderJNIOp.java @@ -0,0 +1,40 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package com.aayushatharva.brotli4j.encoder; + +/** + * A hack to allow the EncoderJNI.Operation to be seen by Jetty's code. + * + * Remove this and use the EncoderJNI.Operation once the fix at brotli4j is released. + * + * See Request release of brotli4j + */ +public enum EncoderJNIOp +{ + PROCESS(EncoderJNI.Operation.PROCESS), + FLUSH(EncoderJNI.Operation.FLUSH), + FINISH(EncoderJNI.Operation.FINISH); + + private final EncoderJNI.Operation op; + + private EncoderJNIOp(EncoderJNI.Operation op) + { + this.op = op; + } + + public EncoderJNI.Operation getOp() + { + return op; + } +} diff --git a/jetty-core/jetty-compression/jetty-compression-brotli/src/main/java/org/eclipse/jetty/compression/brotli/BrotliEncoderSink.java b/jetty-core/jetty-compression/jetty-compression-brotli/src/main/java/org/eclipse/jetty/compression/brotli/BrotliEncoderSink.java index d8a5410d9591..cea066ccfbac 100644 --- a/jetty-core/jetty-compression/jetty-compression-brotli/src/main/java/org/eclipse/jetty/compression/brotli/BrotliEncoderSink.java +++ b/jetty-core/jetty-compression/jetty-compression-brotli/src/main/java/org/eclipse/jetty/compression/brotli/BrotliEncoderSink.java @@ -19,6 +19,7 @@ import com.aayushatharva.brotli4j.encoder.Encoder; import com.aayushatharva.brotli4j.encoder.EncoderJNI; +import com.aayushatharva.brotli4j.encoder.EncoderJNIOp; import org.eclipse.jetty.compression.EncoderSink; import org.eclipse.jetty.io.Content; import org.eclipse.jetty.util.BufferUtil; @@ -106,7 +107,7 @@ protected WriteRecord encode(boolean last, ByteBuffer content) // only encode if inputBuffer is full. if (!inputBuffer.hasRemaining()) { - ByteBuffer output = encode(EncoderJNI.Operation.PROCESS); + ByteBuffer output = encode(EncoderJNIOp.PROCESS); if (output != null) return new WriteRecord(false, output, Callback.NOOP); } @@ -128,7 +129,7 @@ protected WriteRecord encode(boolean last, ByteBuffer content) case FLUSHING -> { inputBuffer.limit(inputBuffer.position()); - ByteBuffer output = encode(EncoderJNI.Operation.FLUSH); + ByteBuffer output = encode(EncoderJNIOp.FLUSH); state.compareAndSet(State.FLUSHING, State.FINISHING); if (output != null) return new WriteRecord(false, output, Callback.NOOP); @@ -136,7 +137,7 @@ protected WriteRecord encode(boolean last, ByteBuffer content) case FINISHING -> { inputBuffer.limit(inputBuffer.position()); - ByteBuffer output = encode(EncoderJNI.Operation.FINISH); + ByteBuffer output = encode(EncoderJNIOp.FINISH); state.compareAndSet(State.FINISHING, State.FINISHED); return new WriteRecord(true, output != null ? output : EMPTY_BUFFER, Callback.NOOP); } @@ -148,7 +149,7 @@ protected WriteRecord encode(boolean last, ByteBuffer content) } } - protected ByteBuffer encode(EncoderJNI.Operation op) + protected ByteBuffer encode(EncoderJNIOp op) { try { @@ -167,11 +168,11 @@ else if (encoder.hasMoreOutput()) } else if (encoder.hasRemainingInput()) { - encoder.push(op, 0); + encoder.push(op.getOp(), 0); } else if (!inputPushed) { - encoder.push(op, inputBuffer.limit()); + encoder.push(op.getOp(), inputBuffer.limit()); inputPushed = true; } else