Skip to content

Commit 4c4cbc2

Browse files
committed
Add client-decompression setting and allow .gz requests to the integrated webserver
1 parent e8df7ec commit 4c4cbc2

4 files changed

Lines changed: 52 additions & 22 deletions

File tree

common/src/main/java/de/bluecolored/bluemap/common/WebFilesManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ private static class Settings {
143143
private String mapDataRoot = "maps";
144144
private String liveDataRoot = "maps";
145145

146+
private boolean clientDecompression = false;
147+
146148
private Set<String> maps = new LinkedHashSet<>();
147149
private Set<String> scripts = new LinkedHashSet<>();
148150
private Set<String> styles = new LinkedHashSet<>();
@@ -170,6 +172,8 @@ public void setFrom(WebappConfig config) {
170172
this.styles.clear();
171173
this.scripts.clear();
172174

175+
this.clientDecompression = config.isClientDecompression();
176+
173177
addFrom(config);
174178
}
175179

common/src/main/java/de/bluecolored/bluemap/common/config/WebappConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class WebappConfig {
6565
private String mapDataRoot = "maps";
6666
private String liveDataRoot = "maps";
6767

68+
private boolean clientDecompression = false;
69+
6870
private LinkedHashSet<String> scripts = new LinkedHashSet<>();
6971
private LinkedHashSet<String> styles = new LinkedHashSet<>();
7072

common/src/main/java/de/bluecolored/bluemap/common/web/MapStorageRequestHandler.java

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,21 @@ public class MapStorageRequestHandler implements HttpRequestHandler {
5656

5757
private @NonNull MapStorage mapStorage;
5858

59-
@SuppressWarnings("resource")
6059
@Override
6160
public HttpResponse handle(HttpRequest request) {
6261
String path = request.getPath();
62+
boolean requestGzipped = false;
6363

6464
//normalize path
6565
if (path.startsWith("/")) path = path.substring(1);
6666
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
6767

68+
//provide compressed if requested
69+
if (path.endsWith(".gz")) {
70+
path = path.substring(0, path.length() - 3);
71+
requestGzipped = true;
72+
}
73+
6874
try {
6975

7076
// provide map-tiles
@@ -85,7 +91,7 @@ public HttpResponse handle(HttpRequest request) {
8591
if (lod == 0) response.addHeader("Content-Type", "application/octet-stream");
8692
else response.addHeader("Content-Type", "image/png");
8793

88-
writeToResponse(in, response, request);
94+
writeToResponse(in, response, request, requestGzipped);
8995
return response;
9096
}
9197

@@ -102,7 +108,7 @@ public HttpResponse handle(HttpRequest request) {
102108
response.addHeader("Cache-Control", "public");
103109
response.addHeader("Cache-Control", "max-age=" + TimeUnit.DAYS.toSeconds(1));
104110
response.addHeader("Content-Type", ContentTypeRegistry.fromFileName(path));
105-
writeToResponse(in, response, request);
111+
writeToResponse(in, response, request, requestGzipped);
106112
return response;
107113
}
108114

@@ -115,28 +121,41 @@ public HttpResponse handle(HttpRequest request) {
115121
return new HttpResponse(HttpStatusCode.NOT_FOUND);
116122
}
117123

118-
private void writeToResponse(CompressedInputStream data, HttpResponse response, HttpRequest request) throws IOException {
124+
private void writeToResponse(CompressedInputStream data, HttpResponse response, HttpRequest request, boolean requestGzipped) throws IOException {
119125
Compression compression = data.getCompression();
120-
if (
121-
compression != Compression.NONE &&
122-
request.hasHeaderValue("Accept-Encoding", compression.getId())
123-
) {
124-
response.addHeader("Content-Encoding", compression.getId());
125-
response.setBody(data);
126-
} else if (
127-
compression != Compression.GZIP &&
128-
!response.hasHeaderValue("Content-Type", "image/png") &&
129-
request.hasHeaderValue("Accept-Encoding", Compression.GZIP.getId())
130-
) {
131-
response.addHeader("Content-Encoding", Compression.GZIP.getId());
132-
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
133-
try (data; OutputStream os = Compression.GZIP.compress(byteOut)) {
134-
data.decompress().transferTo(os);
126+
if (!requestGzipped) {
127+
if (
128+
compression != Compression.NONE &&
129+
request.hasHeaderValue("Accept-Encoding", compression.getId())
130+
) {
131+
response.addHeader("Content-Encoding", compression.getId());
132+
response.setBody(data);
133+
} else if (
134+
compression != Compression.GZIP &&
135+
!response.hasHeaderValue("Content-Type", "image/png") &&
136+
request.hasHeaderValue("Accept-Encoding", Compression.GZIP.getId())
137+
) {
138+
response.addHeader("Content-Encoding", Compression.GZIP.getId());
139+
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
140+
try (data; OutputStream os = Compression.GZIP.compress(byteOut)) {
141+
data.decompress().transferTo(os);
142+
}
143+
byte[] compressedData = byteOut.toByteArray();
144+
response.setBody(new ByteArrayInputStream(compressedData));
145+
} else {
146+
response.setBody(data.decompress());
135147
}
136-
byte[] compressedData = byteOut.toByteArray();
137-
response.setBody(new ByteArrayInputStream(compressedData));
138148
} else {
139-
response.setBody(data.decompress());
149+
if (compression == Compression.GZIP) {
150+
response.setBody(data);
151+
} else {
152+
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
153+
try (data; OutputStream os = Compression.GZIP.compress(byteOut)) {
154+
data.decompress().transferTo(os);
155+
}
156+
byte[] compressedData = byteOut.toByteArray();
157+
response.setBody(new ByteArrayInputStream(compressedData));
158+
}
140159
}
141160
}
142161

common/src/main/resources/de/bluecolored/bluemap/config/webapp.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ lowres-slider-min: 500
6363
# Default is "maps"
6464
#live-data-root: "https://cdn.my-domain.com/livedata"
6565

66+
# This will configure the webapp to request the map-tiles compressed and decompress them manually.
67+
# Enable this only if you intend to host the webapp statically, with an external webserver without additional configuration.
68+
# Default is "false"
69+
#client-decompression: true
70+
6671
# Here you can add URLs to custom scripts (js) so they will be loaded by the webapp.
6772
# You can place them somewhere in BlueMap's webroot and add the (relative) link here.
6873
scripts: [

0 commit comments

Comments
 (0)