Added ResponseBuilder#setContent(WebResource)

This commit is contained in:
Risto Lahtela 2020-03-20 10:38:29 +02:00
parent 4fd19bbc5e
commit 38bb2ce61c
2 changed files with 8 additions and 3 deletions

View File

@ -16,6 +16,7 @@
*/ */
package com.djrapitops.plan.delivery.web.resolver; package com.djrapitops.plan.delivery.web.resolver;
import com.djrapitops.plan.delivery.web.resource.WebResource;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -75,6 +76,10 @@ public class ResponseBuilder {
return setStatus(302).setHeader("Location", url).setContent(new byte[0]); return setStatus(302).setHeader("Location", url).setContent(new byte[0]);
} }
public ResponseBuilder setContent(WebResource resource) {
return setContent(resource.asBytes());
}
public ResponseBuilder setContent(byte[] bytes) { public ResponseBuilder setContent(byte[] bytes) {
response.bytes = bytes; response.bytes = bytes;
return setHeader("Content-Length", bytes.length) return setHeader("Content-Length", bytes.length)

View File

@ -204,7 +204,7 @@ public class ResponseFactory {
try { try {
return Response.builder() return Response.builder()
.setMimeType(MimeType.IMAGE) .setMimeType(MimeType.IMAGE)
.setContent(getResource(fileName).asBytes()) .setContent(getResource(fileName))
.setStatus(200) .setStatus(200)
.build(); .build();
} catch (UncheckedIOException e) { } catch (UncheckedIOException e) {
@ -228,7 +228,7 @@ public class ResponseFactory {
try { try {
return Response.builder() return Response.builder()
.setMimeType(type) .setMimeType(type)
.setContent(getResource(fileName).asBytes()) .setContent(getResource(fileName))
.build(); .build();
} catch (UncheckedIOException e) { } catch (UncheckedIOException e) {
return notFound404("Font File not found from jar: " + fileName + ", " + e.toString()); return notFound404("Font File not found from jar: " + fileName + ", " + e.toString());
@ -243,7 +243,7 @@ public class ResponseFactory {
try { try {
return Response.builder() return Response.builder()
.setMimeType(MimeType.FAVICON) .setMimeType(MimeType.FAVICON)
.setContent(getResource("favicon.ico").asBytes()) .setContent(getResource("favicon.ico"))
.build(); .build();
} catch (UncheckedIOException e) { } catch (UncheckedIOException e) {
return forInternalError(e, "Could not read favicon"); return forInternalError(e, "Could not read favicon");