Skip to content

Commit

Permalink
Merge pull request #51 from youngmonkeys/enable-gzip
Browse files Browse the repository at this point in the history
Enable compression use gzip
  • Loading branch information
vu-luong authored Aug 6, 2023
2 parents 65582cd + 8886c36 commit 43e461f
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 31 deletions.
2 changes: 1 addition & 1 deletion ezyhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>

<artifactId>ezyhttp-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>

<artifactId>ezyhttp-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>

<artifactId>ezyhttp-server-boot</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions ezyhttp-server-boot/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ resources.enable=true
resources.locations=abc,xyz,static
resources.upload.enable=true
graphql.enable=true
server.compression.min_size=30B
server.compression.included_methods=GET,POST
server.compression.excluded_methods=DELETE,PUT
server.compression.included_mime_types=text/html,text/css,text/javascript
server.compression.excluded_mime_types=image/tiff,image/png
2 changes: 1 addition & 1 deletion ezyhttp-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>

<artifactId>ezyhttp-server-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-graphql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>
<artifactId>ezyhttp-server-graphql</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>

<artifactId>ezyhttp-server-jetty</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.tvd12.ezyhttp.server.jetty;

import static com.tvd12.ezyfox.io.EzyStrings.isNotEmpty;
import static com.tvd12.ezyfox.util.EzyProcessor.processWithLogException;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
Expand All @@ -8,15 +11,18 @@
import javax.servlet.MultipartConfigElement;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

import com.tvd12.ezyfox.annotation.EzyProperty;
import com.tvd12.ezyfox.util.EzyLoggable;
import com.tvd12.ezyfox.util.EzyStoppable;
import com.tvd12.ezyhttp.core.util.FileSizes;
import com.tvd12.ezyhttp.server.core.ApplicationEntry;
import com.tvd12.ezyhttp.server.core.annotation.ApplicationBootstrap;
Expand All @@ -28,7 +34,7 @@
@ApplicationBootstrap
public class JettyApplicationBootstrap
extends EzyLoggable
implements ApplicationEntry {
implements ApplicationEntry, EzyStoppable {

@EzyProperty("server.port")
protected int port = 8080;
Expand Down Expand Up @@ -58,6 +64,24 @@ public class JettyApplicationBootstrap
@EzyProperty("server.multipart.max_request_size")
protected String multipartMaxRequestSize = "5MB";

@EzyProperty("server.compression.enable")
protected boolean compressionEnable = true;

@EzyProperty("server.compression.min_size")
protected String compressionMinSize;

@EzyProperty("server.compression.included_methods")
protected String[] compressionIncludedMethods;

@EzyProperty("server.compression.excluded_methods")
protected String[] compressionExcludedMethods;

@EzyProperty("server.compression.included_mime_types")
protected String[] compressionIncludedMimeTypes;

@EzyProperty("server.compression.excluded_mime_types")
protected String[] compressionExcludedMimeTypes;

@EzyProperty("cors.enable")
protected boolean corsEnable;

Expand Down Expand Up @@ -95,7 +119,12 @@ public void start() throws Exception {
connectors.add(managementConnector);
}
server.setConnectors(connectors.toArray(new Connector[0]));
ServletContextHandler servletHandler = newServletHandler();
Handler servletHandler = newServletHandler();
if (compressionEnable) {
GzipHandler gzipHandler = newGzipHandler();
gzipHandler.setHandler(servletHandler);
servletHandler = gzipHandler;
}
server.setHandler(servletHandler);
server.start();
logger.info("http server started on: {}:{}", host, port);
Expand All @@ -116,12 +145,31 @@ protected ServletContextHandler newServletHandler() {
));
logger.info("cors.enable = {}", corsEnable);
if (corsEnable) {
FilterHolder crossOriginFilter = newCrossOriginFilter();
addFilter(servletHandler, crossOriginFilter);
addFilter(servletHandler, newCrossOriginFilter());
}
return servletHandler;
}

protected GzipHandler newGzipHandler() {
GzipHandler gzipHandler = new GzipHandler();
if (isNotEmpty(compressionMinSize)) {
gzipHandler.setMinGzipSize((int) FileSizes.toByteSize(compressionMinSize));
}
if (compressionIncludedMethods != null) {
gzipHandler.setIncludedMethods(compressionIncludedMethods);
}
if (compressionExcludedMethods != null) {
gzipHandler.setExcludedMethods(compressionExcludedMethods);
}
if (compressionIncludedMimeTypes != null) {
gzipHandler.setIncludedMimeTypes(compressionIncludedMimeTypes);
}
if (compressionExcludedMimeTypes != null) {
gzipHandler.setExcludedMimeTypes(compressionExcludedMimeTypes);
}
return gzipHandler;
}

protected void addFilter(ServletContextHandler servletHandler, FilterHolder filter) {
servletHandler.addFilter(filter, "/*", EnumSet.of(DispatcherType.REQUEST));
}
Expand Down Expand Up @@ -151,4 +199,11 @@ protected FilterHolder newCrossOriginFilter() {
filter.setFilter(corsFilter);
return filter;
}

@Override
public void stop() {
if (server != null) {
processWithLogException(server::stop);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.tvd12.ezyhttp.server.jetty.test;

import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.testng.annotations.Test;

import com.tvd12.ezyfox.collect.Sets;
import com.tvd12.ezyhttp.server.jetty.JettyApplicationBootstrap;
import com.tvd12.test.assertion.Asserts;
import com.tvd12.test.reflect.FieldUtil;
import com.tvd12.test.reflect.MethodInvoker;
import com.tvd12.test.util.RandomUtil;
import org.testng.annotations.Test;

public class JettyApplicationBootstrapTest {

Expand All @@ -21,4 +25,78 @@ public void test() {
// then
Asserts.assertEquals(allowedHeaders, headers);
}

@Test
public void stopBeforeStartTest() {
// given
JettyApplicationBootstrap instance = new JettyApplicationBootstrap();
instance.setCompressionEnable(false);

// when
// then
instance.stop();
}

@Test
public void compressionDisableTest() throws Exception {
// given
JettyApplicationBootstrap instance = new JettyApplicationBootstrap();
instance.setCompressionEnable(false);

// when
instance.start();

// then
instance.stop();
}

@Test
public void newGzipHandlerTest() {
// given
JettyApplicationBootstrap instance = new JettyApplicationBootstrap();
int minSize = RandomUtil.randomInt(50, 100);
instance.setCompressionMinSize(minSize + "B");

String[] includedMethods = new String[] { "GET", "POST" };
instance.setCompressionIncludedMethods(includedMethods);

String[] excludedMethods = new String[] { "DELETE", "PUT" };
instance.setCompressionExcludedMethods(excludedMethods);

String[] includedMimeTypes = new String[] {
"text/html",
"text/css",
"text/javascript"
};
instance.setCompressionIncludedMimeTypes(includedMimeTypes);

String[] excludedMimeTypes= new String[] { "image/tiff", "image/png" };
instance.setCompressionExcludedMimeTypes(excludedMimeTypes);

// when
GzipHandler gzipHandler = MethodInvoker
.create()
.object(instance)
.method("newGzipHandler")
.invoke(GzipHandler.class);

// then
Asserts.assertEquals(gzipHandler.getMinGzipSize(), minSize);
Asserts.assertEquals(
Sets.newHashSet(gzipHandler.getIncludedMethods()),
Sets.newHashSet(includedMethods)
);
Asserts.assertEquals(
Sets.newHashSet(gzipHandler.getExcludedMethods()),
Sets.newHashSet(excludedMethods)
);
Asserts.assertEquals(
Sets.newHashSet(gzipHandler.getIncludedMimeTypes()),
Sets.newHashSet(includedMimeTypes)
);
Asserts.assertEquals(
Sets.newHashSet(gzipHandler.getExcludedMimeTypes()),
Sets.newHashSet(excludedMimeTypes)
);
}
}
2 changes: 1 addition & 1 deletion ezyhttp-server-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>
<artifactId>ezyhttp-server-management</artifactId>
<name>ezyhttp-server-management</name>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>
<artifactId>ezyhttp-server-thymeleaf</artifactId>

Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>

<artifactId>ezyhttp-server-tomcat</artifactId>
Expand Down
Loading

0 comments on commit 43e461f

Please sign in to comment.