-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Support compression for reactive routes and resteasy reactive
- Loading branch information
Showing
24 changed files
with
633 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...active-routes/deployment/src/test/java/io/quarkus/vertx/web/compress/CompressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package io.quarkus.vertx.web.compress; | ||
|
||
import static io.restassured.RestAssured.get; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.vertx.http.Compressed; | ||
import io.quarkus.vertx.http.Uncompressed; | ||
import io.quarkus.vertx.web.Route; | ||
import io.restassured.response.ExtractableResponse; | ||
import io.restassured.response.Response; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class CompressionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(MyRoutes.class) | ||
.addAsManifestResource(new StringAsset(MyRoutes.MESSAGE), "resources/file.txt") | ||
.addAsManifestResource(new StringAsset(MyRoutes.MESSAGE), "resources/my.doc")) | ||
.overrideConfigKey("quarkus.http.enable-compression", "true"); | ||
|
||
@Test | ||
public void testRoutes() { | ||
assertCompressed("/compressed"); | ||
assertUncompressed("/uncompressed"); | ||
assertCompressed("/compressed-content-type"); | ||
assertUncompressed("/uncompressed-content-type"); | ||
assertCompressed("/content-type-implicitly-compressed"); | ||
assertUncompressed("/content-type-implicitly-uncompressed"); | ||
assertCompressed("/compression-disabled-manually"); | ||
assertCompressed("/file.txt"); | ||
assertUncompressed("/my.doc"); | ||
} | ||
|
||
private void assertCompressed(String path) { | ||
String bodyStr = get(path).then().statusCode(200).header("Content-Encoding", "gzip").extract().asString(); | ||
assertEquals("Hello compression!", bodyStr); | ||
} | ||
|
||
private void assertUncompressed(String path) { | ||
ExtractableResponse<Response> response = get(path) | ||
.then().statusCode(200).extract(); | ||
assertTrue(response.header("Content-Encoding") == null, response.headers().toString()); | ||
assertEquals(MyRoutes.MESSAGE, response.asString()); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyRoutes { | ||
|
||
static String MESSAGE = "Hello compression!"; | ||
|
||
@Compressed | ||
@Route | ||
String compressed() { | ||
return MESSAGE; | ||
} | ||
|
||
@Uncompressed | ||
@Route | ||
String uncompressed() { | ||
return MESSAGE; | ||
} | ||
|
||
@Uncompressed | ||
@Route | ||
void uncompressedContentType(RoutingContext context) { | ||
context.response().setStatusCode(200).putHeader("Content-type", "text/plain").end(MESSAGE); | ||
} | ||
|
||
@Compressed | ||
@Route | ||
void compressedContentType(RoutingContext context) { | ||
context.response().setStatusCode(200).putHeader("Content-type", "foo/bar").end(MESSAGE); | ||
} | ||
|
||
@Route | ||
void contentTypeImplicitlyCompressed(RoutingContext context) { | ||
context.response().setStatusCode(200).putHeader("Content-type", "text/plain").end(MESSAGE); | ||
} | ||
|
||
@Route | ||
void contentTypeImplicitlyUncompressed(RoutingContext context) { | ||
context.response().setStatusCode(200).putHeader("Content-type", "foo/bar").end(MESSAGE); | ||
} | ||
|
||
@Route | ||
void compressionDisabledManually(RoutingContext context) { | ||
context.response().headers().remove("Content-Encoding"); | ||
context.response().setStatusCode(200).putHeader("Content-type", "text/plain").end(MESSAGE); | ||
} | ||
|
||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...ive-routes/runtime/src/main/java/io/quarkus/vertx/web/runtime/HttpCompressionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.quarkus.vertx.web.runtime; | ||
|
||
import java.util.Set; | ||
|
||
import io.quarkus.vertx.http.runtime.HttpCompression; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.MultiMap; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class HttpCompressionHandler implements Handler<RoutingContext> { | ||
|
||
private final Handler<RoutingContext> routeHandler; | ||
private final HttpCompression compression; | ||
private final Set<String> compressedMediaTypes; | ||
|
||
public HttpCompressionHandler(Handler<RoutingContext> routeHandler, HttpCompression compression, | ||
Set<String> compressedMediaTypes) { | ||
this.routeHandler = routeHandler; | ||
this.compression = compression; | ||
this.compressedMediaTypes = compressedMediaTypes; | ||
} | ||
|
||
@Override | ||
public void handle(RoutingContext context) { | ||
context.addEndHandler(new Handler<AsyncResult<Void>>() { | ||
@Override | ||
public void handle(AsyncResult<Void> result) { | ||
if (result.succeeded()) { | ||
MultiMap headers = context.response().headers(); | ||
String contentEncoding = headers.get(HttpHeaders.CONTENT_ENCODING); | ||
if (contentEncoding != null && HttpHeaders.IDENTITY.toString().equals(contentEncoding)) { | ||
switch (compression) { | ||
case ON: | ||
headers.remove(HttpHeaders.CONTENT_ENCODING); | ||
break; | ||
case UNDEFINED: | ||
String contentType = headers.get(HttpHeaders.CONTENT_TYPE); | ||
if (contentType != null | ||
&& compressedMediaTypes.contains(contentType)) { | ||
headers.remove(HttpHeaders.CONTENT_ENCODING); | ||
} | ||
break; | ||
default: | ||
// OFF - no action is needed because the "Content-Encoding: identity" header is set | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
routeHandler.handle(context); | ||
} | ||
|
||
} |
Oops, something went wrong.