Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworking jetty-compression for JPMS #12531

Merged
merged 27 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa14b45
Reworking jetty-compression for JPMS
joakime Nov 13, 2024
0a1abb0
Fix pom
joakime Nov 13, 2024
0e6f50b
Merge remote-tracking branch 'origin/jetty-12.1.x' into fix/12.1.x/co…
joakime Nov 14, 2024
9c8c9aa
Changes from review
joakime Nov 14, 2024
713dfd9
Changes from review
joakime Nov 14, 2024
6244dbd
Cleaning up source
joakime Nov 14, 2024
177500b
Merge remote-tracking branch 'origin/jetty-12.1.x' into fix/12.1.x/co…
joakime Nov 15, 2024
b068d44
Moving (include/exclude) verb to before noun
joakime Nov 15, 2024
78921e6
Renaming jetty-compression-api to jetty-compression-common
joakime Nov 15, 2024
a95c780
Renaming module compression-api to commpression-common
joakime Nov 15, 2024
af163a1
Removing class.cast(obj) usages
joakime Nov 15, 2024
fa97456
Fix typo
joakime Nov 15, 2024
a6cf5a6
always add bean
joakime Nov 15, 2024
242c62a
use modern switch/case
joakime Nov 15, 2024
dc8b219
cleanup unused source
joakime Nov 15, 2024
232a12f
configurable gzip (input|output) stream
joakime Nov 15, 2024
9d8b1e7
simplify if statement
joakime Nov 15, 2024
2974ba9
follow jetty style for mod+xml naming
joakime Nov 15, 2024
2f9a788
fix pom syntax
joakime Nov 15, 2024
6f28e1f
fix pom syntax
joakime Nov 15, 2024
9588513
fix pom syntax
joakime Nov 15, 2024
bfe4293
fix pom syntax
joakime Nov 15, 2024
c4e1c16
Changes from review
joakime Nov 18, 2024
29343db
Fixing typo on method name (plural)
joakime Nov 18, 2024
5ad683c
Fix module lib references
joakime Nov 18, 2024
cc61b43
ManagedAttribute typos
joakime Nov 18, 2024
174a392
Merge remote-tracking branch 'origin/jetty-12.1.x' into fix/12.1.x/co…
joakime Nov 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@
<artifactId>brotli4j</artifactId>
<version>${brotli4j.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.compression</groupId>
<artifactId>jetty-compression-api</artifactId>
<version>${project.version}</version>
<artifactId>jetty-compression-common</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ compression
experimental

[depend]
compression-api
compression

[files]
maven://com.aayushatharva.brotli4j/brotli4j/${brotli4j.version}|lib/compression/brotli4j-${brotli4j.version}.jar

[lib]
lib/compression/jetty-compression-brotli-${jetty.version}.jar
lib/compression/brotli4j-${brotli4j.version}.jar

[xml]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ========================================================================
// 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
// ========================================================================
//

module org.eclipse.jetty.compression.brotli
{
requires transitive org.eclipse.jetty.compression;
requires org.slf4j;
requires com.aayushatharva.brotli4j;

exports org.eclipse.jetty.compression.brotli;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.eclipse.jetty.compression.DecoderSource;
import org.eclipse.jetty.compression.EncoderConfig;
import org.eclipse.jetty.compression.EncoderSink;
import org.eclipse.jetty.compression.brotli.internal.BrotliDecoderSource;
import org.eclipse.jetty.compression.brotli.internal.BrotliEncoderSink;
import org.eclipse.jetty.http.CompressedContentFormat;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
Expand Down Expand Up @@ -99,10 +101,10 @@ public DecoderConfig getDefaultDecoderConfig()
}

@Override
public void setDefaultDecoderConfig(DecoderConfig config)
public InputStream newDecoderInputStream(InputStream in, DecoderConfig config) throws IOException
{
BrotliDecoderConfig brotliDecoderConfig = BrotliDecoderConfig.class.cast(config);
this.defaultDecoderConfig = brotliDecoderConfig;
BrotliDecoderConfig brotliDecoderConfig = (BrotliDecoderConfig)config;
return new BrotliInputStream(in, brotliDecoderConfig.getBufferSize());
}

@Override
Expand All @@ -112,10 +114,10 @@ public EncoderConfig getDefaultEncoderConfig()
}

@Override
public void setDefaultEncoderConfig(EncoderConfig config)
public DecoderSource newDecoderSource(Content.Source source, DecoderConfig config)
{
BrotliEncoderConfig brotliEncoderConfig = BrotliEncoderConfig.class.cast(config);
this.defaultEncoderConfig = Objects.requireNonNull(brotliEncoderConfig);
BrotliDecoderConfig brotliDecoderConfig = (BrotliDecoderConfig)config;
return new BrotliDecoderSource(source, brotliDecoderConfig);
}

@Override
Expand Down Expand Up @@ -149,31 +151,30 @@ public HttpField getXContentEncodingField()
}

@Override
public InputStream newDecoderInputStream(InputStream in, DecoderConfig config) throws IOException
public OutputStream newEncoderOutputStream(OutputStream out, EncoderConfig config) throws IOException
{
BrotliDecoderConfig brotliDecoderConfig = BrotliDecoderConfig.class.cast(config);
return new BrotliInputStream(in, config.getBufferSize());
BrotliEncoderConfig brotliEncoderConfig = (BrotliEncoderConfig)config;
return new BrotliOutputStream(out, brotliEncoderConfig.asEncoderParams());
}

@Override
public DecoderSource newDecoderSource(Content.Source source, DecoderConfig config)
public EncoderSink newEncoderSink(Content.Sink sink, EncoderConfig config)
{
BrotliDecoderConfig brotliDecoderConfig = BrotliDecoderConfig.class.cast(config);
return new BrotliDecoderSource(this, source, brotliDecoderConfig);
BrotliEncoderConfig brotliEncoderConfig = (BrotliEncoderConfig)config;
return new BrotliEncoderSink(sink, brotliEncoderConfig);
}

@Override
public OutputStream newEncoderOutputStream(OutputStream out, EncoderConfig config) throws IOException
public void setDefaultDecoderConfig(DecoderConfig config)
{
BrotliEncoderConfig brotliEncoderConfig = BrotliEncoderConfig.class.cast(config);
return new BrotliOutputStream(out, brotliEncoderConfig.asEncoderParams());
this.defaultDecoderConfig = (BrotliDecoderConfig)config;
}

@Override
public EncoderSink newEncoderSink(Content.Sink sink, EncoderConfig config)
public void setDefaultEncoderConfig(EncoderConfig config)
{
BrotliEncoderConfig brotliEncoderConfig = BrotliEncoderConfig.class.cast(config);
return new BrotliEncoderSink(this, sink, brotliEncoderConfig);
BrotliEncoderConfig brotliEncoderConfig = (BrotliEncoderConfig)config;
this.defaultEncoderConfig = Objects.requireNonNull(brotliEncoderConfig);
}

private ByteOrder getByteOrder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class BrotliDecoderConfig implements DecoderConfig
/**
* Default Buffer Size as found in Brotli4j.
*/
public static final int DEFAULT_BUFFER_SIZE = 16384;
public static final int MIN_BUFFER_SIZE = 32;
private static final int DEFAULT_BUFFER_SIZE = 16384;
private static final int MIN_BUFFER_SIZE = 32;
private int bufferSize = DEFAULT_BUFFER_SIZE;

@Override
Expand All @@ -33,6 +33,6 @@ public int getBufferSize()
@Override
public void setBufferSize(int size)
{
this.bufferSize = this.bufferSize = Math.max(MIN_BUFFER_SIZE, size);
this.bufferSize = Math.max(MIN_BUFFER_SIZE, size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,25 @@ public class BrotliEncoderConfig implements EncoderConfig
*
* @see <a href="https://www.brotli.org/encode.html">Encoder Defaults</a>
*/
public static final int DEFAULT_QUALITY = 11;
/**
* Default Brotli Mode (Strategy).
*
* @see <a href="https://www.brotli.org/encode.html">Encoder Defaults</a>
*/
public static final Encoder.Mode DEFAULT_MODE = Encoder.Mode.GENERIC;
private static final int DEFAULT_QUALITY = 11;
/**
* Default Brotli Window.
*
* @see <a href="https://www.brotli.org/encode.html">Encoder Defaults</a>
*/
public static final int DEFAULT_WINDOW = 22;

public static final int MIN_BUFFER_SIZE = 32;
private static final int DEFAULT_WINDOW = 22;
private static final int MIN_BUFFER_SIZE = 32;

private int bufferSize = 4096;
private int strategy = 0;
private int quality = DEFAULT_QUALITY;
private Encoder.Mode mode = DEFAULT_MODE;
private int lgWindow = DEFAULT_WINDOW;

public Parameters asEncoderParams()
{
Parameters params = new Parameters();
params.setQuality(getCompressionLevel());
params.setMode(getMode());
params.setMode(Encoder.Mode.of(getStrategy()));
params.setWindow(getLgWindow());
return params;
}
Expand Down Expand Up @@ -122,15 +115,10 @@ public void setLgWindow(int window)
this.lgWindow = window;
}

public Encoder.Mode getMode()
{
return mode;
}

@Override
public int getStrategy()
{
return mode.ordinal();
return strategy;
}

/**
Expand All @@ -151,6 +139,6 @@ public void setStrategy(int strategy)
if ((strategy < 0) || (strategy > Encoder.Mode.values().length))
throw new IllegalArgumentException("Unsupported brotli strategy mode: " + strategy);

mode = Encoder.Mode.of(strategy);
this.strategy = strategy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,27 @@
// ========================================================================
//

package org.eclipse.jetty.compression.brotli;
package org.eclipse.jetty.compression.brotli.internal;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.aayushatharva.brotli4j.decoder.DecoderJNI;
import org.eclipse.jetty.compression.DecoderSource;
import org.eclipse.jetty.compression.brotli.BrotliDecoderConfig;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.util.BufferUtil;

public class BrotliDecoderSource extends DecoderSource
{
private static final ByteBuffer EMPTY_BUFFER = BufferUtil.EMPTY_BUFFER;
private final BrotliCompression compression;
private final DecoderJNI.Wrapper decoder;

public BrotliDecoderSource(BrotliCompression compression, Content.Source source, BrotliDecoderConfig config)
public BrotliDecoderSource(Content.Source source, BrotliDecoderConfig config)
{
super(source);
this.compression = compression;
try
{
this.decoder = new DecoderJNI.Wrapper(compression.getBufferSize());
this.decoder = new DecoderJNI.Wrapper(config.getBufferSize());
}
catch (IOException e)
{
Expand All @@ -58,10 +56,7 @@ protected Content.Chunk nextChunk(Content.Chunk readChunk) throws IOException
{
return last ? Content.Chunk.EOF : Content.Chunk.EMPTY;
}
case OK ->
{
decoder.push(0);
}
case OK -> decoder.push(0);
case NEEDS_MORE_INPUT ->
{
ByteBuffer input = decoder.getInputBuffer();
Expand All @@ -81,10 +76,7 @@ protected Content.Chunk nextChunk(Content.Chunk readChunk) throws IOException
// rely on status.OK to go to EOF
return Content.Chunk.from(output, false);
}
default ->
{
throw new IOException("Decoder failure: Corrupted input buffer");
}
default -> throw new IOException("Decoder failure: Corrupted input buffer");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
// ========================================================================
//

package org.eclipse.jetty.compression.brotli;
package org.eclipse.jetty.compression.brotli.internal;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicReference;

import com.aayushatharva.brotli4j.encoder.Encoder;
import com.aayushatharva.brotli4j.encoder.EncoderJNI;
import org.eclipse.jetty.compression.EncoderSink;
import org.eclipse.jetty.compression.brotli.BrotliEncoderConfig;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BrotliEncoderSink extends EncoderSink
{
Expand All @@ -44,23 +44,21 @@ enum State
/**
* Finish operation completed.
*/
FINISHED;
FINISHED
}

private static final Logger LOG = LoggerFactory.getLogger(BrotliEncoderSink.class);
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
private final BrotliCompression compression;
private final EncoderJNI.Wrapper encoder;
private final ByteBuffer inputBuffer;
private final AtomicReference<State> state = new AtomicReference<State>(State.PROCESSING);
private final AtomicReference<State> state = new AtomicReference<>(State.PROCESSING);

public BrotliEncoderSink(BrotliCompression compression, Content.Sink sink, BrotliEncoderConfig config)
public BrotliEncoderSink(Content.Sink sink, BrotliEncoderConfig config)
{
super(sink);
this.compression = compression;
try
{
this.encoder = new EncoderJNI.Wrapper(config.getBufferSize(), config.getCompressionLevel(), config.getLgWindow(), config.getMode());
Encoder.Mode mode = Encoder.Mode.of(config.getStrategy());
this.encoder = new EncoderJNI.Wrapper(config.getBufferSize(), config.getCompressionLevel(), config.getLgWindow(), mode);
this.inputBuffer = encoder.getInputBuffer();
}
catch (IOException e)
Expand Down Expand Up @@ -94,7 +92,7 @@ protected WriteRecord encode(boolean last, ByteBuffer content)
}

// the only place the input buffer gets set.
int len = BufferUtil.put(content, inputBuffer);
BufferUtil.put(content, inputBuffer);
// do not flip input buffer, that's not what Brotli4j expects/wants.
}
// content is fully consumed.
Expand Down Expand Up @@ -174,9 +172,4 @@ protected void release()
{
this.encoder.destroy();
}

private State getState()
{
return state.get();
}
}
Loading
Loading