-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseRenderer.java
More file actions
87 lines (76 loc) · 2.96 KB
/
Copy pathResponseRenderer.java
File metadata and controls
87 lines (76 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.retailsvc.http.internal;
import com.retailsvc.http.Response;
import com.retailsvc.http.TypeMapper;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Map;
/** Writes a {@link Response} to an {@link HttpExchange}. */
public final class ResponseRenderer {
private static final String CONTENT_TYPE = "Content-Type";
private static final String DEFAULT_JSON = "application/json";
private static final String OCTET_STREAM = "application/octet-stream";
private final Map<String, TypeMapper> mappers;
public ResponseRenderer(Map<String, TypeMapper> mappers) {
this.mappers = Map.copyOf(mappers);
}
public void render(HttpExchange exchange, Response response) throws IOException {
try (exchange) {
Headers headers = exchange.getResponseHeaders();
response.headers().forEach(headers::add);
Object body = response.body();
int status = response.status();
if (body == null) {
exchange.sendResponseHeaders(status, -1);
} else if (body instanceof BodyWriter writer) {
renderStream(exchange, headers, status, response.contentType(), writer);
} else {
renderBytes(exchange, headers, status, response.contentType(), body);
}
}
}
private static void renderStream(
HttpExchange exchange, Headers headers, int status, String contentType, BodyWriter writer)
throws IOException {
if (contentType != null && !headers.containsKey(CONTENT_TYPE)) {
headers.add(CONTENT_TYPE, contentType);
}
long length = writer instanceof BodyWriter.Sized sized ? sized.length() : 0;
exchange.sendResponseHeaders(status, length);
try (OutputStream out = exchange.getResponseBody()) {
writer.writeTo(out);
}
}
private void renderBytes(
HttpExchange exchange, Headers headers, int status, String contentType, Object body)
throws IOException {
byte[] bytes;
String effectiveContentType;
if (body instanceof byte[] raw) {
bytes = raw;
effectiveContentType = contentType != null ? contentType : OCTET_STREAM;
} else {
effectiveContentType = contentType != null ? contentType : DEFAULT_JSON;
bytes = serialize(body, effectiveContentType);
}
if (!headers.containsKey(CONTENT_TYPE)) {
headers.add(CONTENT_TYPE, effectiveContentType);
}
exchange.sendResponseHeaders(status, bytes.length == 0 ? -1 : bytes.length);
if (bytes.length > 0) {
try (OutputStream out = exchange.getResponseBody()) {
out.write(bytes);
}
}
}
private byte[] serialize(Object body, String contentType) {
String mediaType = ContentTypeHeader.mediaType(contentType);
TypeMapper mapper = mappers.get(mediaType.toLowerCase(Locale.ROOT));
if (mapper == null) {
throw new IllegalStateException("No TypeMapper registered for " + contentType);
}
return mapper.writeTo(body);
}
}