Skip to content

Commit ff1e80f

Browse files
author
Gerald Unterrainer
committed
add gzip for requests
you may now retrieve the unzipped body using Attribute.REQUEST_BODY
1 parent e4c499b commit ff1e80f

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

Diff for: pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717

1818
<modelVersion>4.0.0</modelVersion>
1919
<artifactId>http-server</artifactId>
20-
<version>0.2.23</version>
20+
<version>0.2.24</version>
2121
<name>HttpServer</name>
2222
<packaging>jar</packaging>
2323

2424
<dependencies>
25+
<dependency>
26+
<groupId>com.squareup.okio</groupId>
27+
<artifactId>okio</artifactId>
28+
<version>1.17.2</version>
29+
</dependency>
2530
<dependency>
2631
<groupId>info.unterrainer.commons</groupId>
2732
<artifactId>jre-utils</artifactId>

Diff for: src/main/java/info/unterrainer/commons/httpserver/GenericHandlerGroup.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private void getList(final Context ctx) {
156156
}
157157

158158
private void create(final Context ctx) throws IOException {
159-
String b = ctx.body();
159+
String b = ctx.attribute(Attribute.REQUEST_BODY);
160160
try {
161161
J json = jsonMapper.fromStringTo(jsonType, b);
162162
P mappedJpa = orikaMapper.map(json, jpaType);
@@ -182,7 +182,7 @@ private void fullUpdate(final Context ctx) throws IOException {
182182
DaoTransaction<E> transaction = dao.getTransactionManager().beginTransaction(ctx);
183183
P jpa = hu.getJpaById(ctx, transaction.getManager(), dao);
184184
try {
185-
J json = jsonMapper.fromStringTo(jsonType, ctx.body());
185+
J json = jsonMapper.fromStringTo(jsonType, ctx.attribute(Attribute.REQUEST_BODY));
186186
P mappedJpa = orikaMapper.map(json, jpaType);
187187
mappedJpa.setId(jpa.getId());
188188
mappedJpa.setCreatedOn(jpa.getCreatedOn());

Diff for: src/main/java/info/unterrainer/commons/httpserver/HttpServer.java

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
import lombok.extern.slf4j.Slf4j;
4444
import ma.glasnost.orika.MapperFactory;
4545
import ma.glasnost.orika.impl.DefaultMapperFactory;
46+
import okio.BufferedSource;
47+
import okio.GzipSource;
48+
import okio.Okio;
4649

4750
@Slf4j
4851
public class HttpServer {
@@ -119,6 +122,7 @@ private void create() {
119122
javalin.before(ctx -> ctx.attribute(Attribute.JAVALIN_SERVER, this));
120123
javalin.before(ctx -> ctx.attribute(Attribute.RESPONSE_TYPE, ResponseType.JSON));
121124
javalin.before(ctx -> ctx.contentType("application/json"));
125+
javalin.before(this::unzip);
122126

123127
javalin.after(ctx -> render(ctx));
124128
javalin.after(ctx -> {
@@ -232,6 +236,22 @@ public HttpServer delete(final String path, final Handler handler, final Role...
232236
return this;
233237
}
234238

239+
private void unzip(final Context ctx) throws IOException {
240+
String body = ctx.body();
241+
242+
if (body == null)
243+
return;
244+
245+
if (ctx.header("Content-Encoding") == "gzip") {
246+
BufferedSource bs = Okio.buffer(Okio.source(ctx.bodyAsInputStream()));
247+
GzipSource gzipSource = new GzipSource(bs);
248+
body = Okio.buffer(gzipSource).readUtf8();
249+
}
250+
251+
ctx.bodyAsInputStream().reset();
252+
ctx.attribute(Attribute.REQUEST_BODY, body);
253+
}
254+
235255
private void render(final Context ctx) throws IOException {
236256

237257
Object dto = ctx.attribute(Attribute.RESPONSE_OBJECT);

Diff for: src/main/java/info/unterrainer/commons/httpserver/enums/Attribute.java

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public class Attribute {
44
public static final String JAVALIN_SERVER = "javalin_server";
55

6+
public static final String REQUEST_BODY = "request_body";
7+
68
public static final String RESPONSE_OBJECT = "response_object";
79
public static final String RESPONSE_STATUS = "response_status";
810
public static final String RESPONSE_TYPE = "response_type";

0 commit comments

Comments
 (0)