|
2 | 2 |
|
3 | 3 | import dev.latvian.apps.tinyserver.content.ByteContent;
|
4 | 4 | import dev.latvian.apps.tinyserver.content.FileContent;
|
| 5 | +import dev.latvian.apps.tinyserver.content.MimeType; |
5 | 6 | import dev.latvian.apps.tinyserver.content.ResponseContent;
|
6 | 7 |
|
7 | 8 | import java.nio.charset.StandardCharsets;
|
8 | 9 | import java.nio.file.Path;
|
| 10 | +import java.util.function.Consumer; |
9 | 11 |
|
10 | 12 | public interface HTTPResponse {
|
11 | 13 | static HTTPResponse ok() {
|
@@ -42,8 +44,22 @@ static HTTPResponse redirectPermanently(String location) {
|
42 | 44 |
|
43 | 45 | void build(HTTPResponseBuilder payload) throws Exception;
|
44 | 46 |
|
45 |
| - default HTTPResponse header(String header, String value) { |
46 |
| - return new HTTPResponseWithHeader(this, header, value); |
| 47 | + default HTTPResponse header(String header, Object value) { |
| 48 | + return new HTTPResponseWithHeader(this, header, String.valueOf(value)); |
| 49 | + } |
| 50 | + |
| 51 | + default HTTPResponse cookie(String key, String value) { |
| 52 | + return new HTTPResponseWithCookie(this, key, value); |
| 53 | + } |
| 54 | + |
| 55 | + default HTTPResponse cookie(String key, String value, int maxAge) { |
| 56 | + return new HTTPResponseWithCookie(this, key, value, new HTTPResponseWithCookie.Builder().maxAge(maxAge)); |
| 57 | + } |
| 58 | + |
| 59 | + default HTTPResponse cookie(String key, String value, Consumer<HTTPResponseWithCookie.Builder> properties) { |
| 60 | + var builder = new HTTPResponseWithCookie.Builder(); |
| 61 | + properties.accept(builder); |
| 62 | + return new HTTPResponseWithCookie(this, key, value, builder); |
47 | 63 | }
|
48 | 64 |
|
49 | 65 | default HTTPResponse noCache() {
|
@@ -75,16 +91,14 @@ default HTTPResponse content(Path file) {
|
75 | 91 | }
|
76 | 92 |
|
77 | 93 | default HTTPResponse text(String text) {
|
78 |
| - return content(text.getBytes(StandardCharsets.UTF_8), "text/plain; charset=utf-8"); |
| 94 | + return content(text.getBytes(StandardCharsets.UTF_8), MimeType.TEXT); |
79 | 95 | }
|
80 | 96 |
|
81 | 97 | default HTTPResponse text(Iterable<String> text) {
|
82 | 98 | return text(String.join("\n", text));
|
83 | 99 | }
|
84 | 100 |
|
85 | 101 | default HTTPResponse json(String json) {
|
86 |
| - return content(json.getBytes(StandardCharsets.UTF_8), "application/json; charset=utf-8"); |
| 102 | + return content(json.getBytes(StandardCharsets.UTF_8), MimeType.JSON); |
87 | 103 | }
|
88 |
| - |
89 |
| - |
90 | 104 | }
|
0 commit comments