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

Add Brotli4J compression related classes, files and dependencies #1717

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions http/http-advanced-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>brotli4j</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.ts.http.advanced.reactive;

import jakarta.enterprise.context.ApplicationScoped;

import io.netty.handler.codec.compression.BrotliOptions;
import io.netty.handler.codec.compression.StandardCompressionOptions;
import io.quarkus.vertx.http.HttpServerOptionsCustomizer;
import io.vertx.core.http.HttpServerOptions;

@ApplicationScoped
public class Brotli4JHttpServerConfig implements HttpServerOptionsCustomizer {
// It depends on compression level that we want to apply, in this case we use level 4, but we won't test the level compression features.
private static final int compressionLevel = 4;

@Override
public void customizeHttpServer(HttpServerOptions options) {
options.addCompressor(getBrotliOptions(compressionLevel));
}

@Override
public void customizeHttpsServer(HttpServerOptions options) {
options.addCompressor(getBrotliOptions(compressionLevel));
}

private static BrotliOptions getBrotliOptions(int compressionLevel) {
return StandardCompressionOptions.brotli();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.ts.http.advanced.reactive;

import java.util.HashMap;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/compression")
public class Brotli4JResource {

public final static String DEFAULT_TEXT_PLAIN = "In life, you have to trust that every little bit helps. As you know," +
" every small step forward counts." +
" It's the accumulation of these efforts that ultimately leads to success." +
" So, don't underestimate the power of persistence and determination in achieving your dreams";

@Inject
Brotli4JRestMock brotli4JRestMock;

@GET
@Path("/brotli/json")
@Produces(MediaType.APPLICATION_JSON)
public HashMap<String, Object> jsonHttpCompressionResponse() {
return brotli4JRestMock.returnResponse(Brotli4JRestMock.ResponseType.JSON);
}

@GET
@Path("/brotli/xml")
@Produces(MediaType.APPLICATION_XML)
public String xmlHttpCompressionResponse() {
return brotli4JRestMock.returnResponse(Brotli4JRestMock.ResponseType.XML).get("xml").toString();
}

@POST
@Path("/decompression")
@Produces(MediaType.TEXT_PLAIN)
public String decompressionHttpResponse(byte[] compressedData) {
String decompressedData = new String(compressedData);
return decompressedData;
}

@GET
@Path("/default/text")
@Produces(MediaType.TEXT_PLAIN)
public String textPlainDefaultHttpCompressionResponse() {
return DEFAULT_TEXT_PLAIN;
}

@GET
@Path("/text")
@Produces(MediaType.TEXT_PLAIN)
public String textPlainHttpCompressionResponse() {
return brotli4JRestMock.returnTextPlainResponse(Brotli4JRestMock.ResponseType.TEXT);
}

@GET
@Path("/text/big")
@Produces(MediaType.TEXT_PLAIN)
public String textPlainBigHttpCompressionResponse() {
return brotli4JRestMock.returnTextPlainResponse(Brotli4JRestMock.ResponseType.BIG_TEXT);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.quarkus.ts.http.advanced.reactive;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Scanner;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;

import org.jboss.logging.Logger;

import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;

@ApplicationScoped
public class Brotli4JRestMock {

private static final Logger LOGGER = Logger.getLogger(Brotli4JRestMock.class);

private static HashMap<String, Object> jsonResponse = null;
private static HashMap<String, Object> xmlResponse = null;

private String textResponse = "";
private String bigTextResponse = "";

private final ObjectMapper objectMapper;
private final String textPlainFilePath;
private final String jsonFilePath;
private final String xmlFilePath;
private final String textBigPlainFilePath;

public Brotli4JRestMock(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
this.textPlainFilePath = "/sample.txt";
this.jsonFilePath = "/sample.json";
this.xmlFilePath = "/sample.xml";
this.textBigPlainFilePath = "/big_sample.txt";
}

@PostConstruct
public void init() {
loadJsonFile(jsonFilePath);
loadXmlResponse(xmlFilePath);
jcarranzan marked this conversation as resolved.
Show resolved Hide resolved
textResponse = loadTextData(textPlainFilePath);
bigTextResponse = loadTextData(textBigPlainFilePath);
}

private void loadJsonFile(String jsonFilePath) {
try (InputStream inputStream = getClass().getResourceAsStream(jsonFilePath)) {
assert inputStream != null;
byte[] bytes = inputStream.readAllBytes();
jsonResponse = objectMapper.readValue(bytes, new TypeReference<>() {
});
} catch (StreamReadException | DatabindException e) {
LOGGER.error("Error occurred while deserializing JSON file: " + e.getMessage());
} catch (IOException e) {
LOGGER.error("Error occurred while reading the JSON file: " + e.getMessage());
}
}

private void loadXmlResponse(String xmlFilePath) {
try (InputStream inputStream = getClass().getResourceAsStream(xmlFilePath)) {
assert inputStream != null;
Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
while (scanner.hasNextLine()) {
stringBuilder.append(scanner.nextLine()).append("\n");
}
xmlResponse = new HashMap<>();
xmlResponse.put("xml", stringBuilder.toString());
} catch (IOException e) {
throw new RuntimeException("Error loading XML response", e);
}
}

public HashMap<String, Object> returnResponse(ResponseType type) {
if (type == ResponseType.JSON) {
return jsonResponse;
} else if (type == ResponseType.XML) {
return xmlResponse;
} else {
throw new IllegalArgumentException("Invalid response type: " + type);
}
}

public String loadTextData(String filePath) {
try (InputStream inputStream = getClass().getResourceAsStream(filePath)) {
if (inputStream != null) {
byte[] textData = inputStream.readAllBytes();
return new String(textData, StandardCharsets.UTF_8);
} else {
throw new IOException("File not found: " + filePath);
}
} catch (IOException e) {
throw new RuntimeException("Error loading text data from file: " + filePath, e);
}
}

public String returnTextPlainResponse(ResponseType type) {
if (type == ResponseType.TEXT) {
return textResponse;
} else if (type == ResponseType.BIG_TEXT) {
return bigTextResponse;
} else {
throw new IllegalArgumentException("Invalid response type: " + type);
}
}

public enum ResponseType {
JSON,
XML,
TEXT,
BIG_TEXT
}

}
Loading