|
| 1 | +package io.avaje.http.client; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.http.HttpClient; |
| 5 | +import java.net.http.HttpHeaders; |
| 6 | +import java.net.http.HttpRequest; |
| 7 | +import java.net.http.HttpResponse; |
| 8 | +import java.time.Duration; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +class DHttpClientContext implements HttpClientContext { |
| 13 | + |
| 14 | + private final HttpClient httpClient; |
| 15 | + private final String baseUrl; |
| 16 | + private final Duration requestTimeout; |
| 17 | + private final BodyAdapter bodyAdapter; |
| 18 | + private final RequestListener requestListener; |
| 19 | + |
| 20 | + DHttpClientContext(HttpClient httpClient, String baseUrl, Duration requestTimeout, BodyAdapter bodyAdapter, RequestListener requestListener) { |
| 21 | + this.httpClient = httpClient; |
| 22 | + this.baseUrl = baseUrl; |
| 23 | + this.requestTimeout = requestTimeout; |
| 24 | + this.bodyAdapter = bodyAdapter; |
| 25 | + this.requestListener = requestListener; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public HttpClientRequest request() { |
| 30 | + return new DHttpClientRequest(this, requestTimeout); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public BodyAdapter converters() { |
| 35 | + return bodyAdapter; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public UrlBuilder url() { |
| 40 | + return new UrlBuilder(baseUrl); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public HttpClient httpClient() { |
| 45 | + return httpClient; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void checkResponse(HttpResponse<?> response) { |
| 50 | + if (response.statusCode() >= 300) { |
| 51 | + throw new HttpException(response, this); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + void check(HttpResponse<byte[]> response) { |
| 56 | + if (response.statusCode() >= 300) { |
| 57 | + throw new HttpException(this, response); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public BodyContent readContent(HttpResponse<byte[]> httpResponse) { |
| 63 | + byte[] bodyBytes = decodeContent(httpResponse); |
| 64 | + final String contentType = getContentType(httpResponse); |
| 65 | + return new BodyContent(contentType, bodyBytes); |
| 66 | + } |
| 67 | + |
| 68 | + String getContentType(HttpResponse<byte[]> httpResponse) { |
| 69 | + return firstHeader(httpResponse.headers(), "Content-Type", "content-type"); |
| 70 | + } |
| 71 | + |
| 72 | + String getContentEncoding(HttpResponse<byte[]> httpResponse) { |
| 73 | + return firstHeader(httpResponse.headers(), "Content-Encoding", "content-encoding"); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public byte[] decodeContent(String encoding, byte[] body) { |
| 78 | + if (encoding.equals("gzip")) { |
| 79 | + return GzipUtil.gzipDecode(body); |
| 80 | + } |
| 81 | + // todo: register decoders with context and use them |
| 82 | + return body; |
| 83 | + } |
| 84 | + |
| 85 | + public byte[] decodeContent(HttpResponse<byte[]> httpResponse) { |
| 86 | + String encoding = getContentEncoding(httpResponse); |
| 87 | + return encoding == null ? httpResponse.body() : decodeContent(encoding, httpResponse.body()); |
| 88 | + } |
| 89 | + |
| 90 | + String firstHeader(HttpHeaders headers, String... names) { |
| 91 | + final Map<String, List<String>> map = headers.map(); |
| 92 | + for (String key : names) { |
| 93 | + final List<String> values = map.get(key); |
| 94 | + if (values != null && !values.isEmpty()) { |
| 95 | + return values.get(0); |
| 96 | + } |
| 97 | + } |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + <T> HttpResponse<T> send(HttpRequest.Builder requestBuilder, HttpResponse.BodyHandler<T> bodyHandler) { |
| 102 | + final HttpRequest request = applyFilters(requestBuilder).build(); |
| 103 | + try { |
| 104 | + return httpClient.send(request, bodyHandler); |
| 105 | + } catch (IOException e) { |
| 106 | + throw new HttpException(499, e); |
| 107 | + } catch (InterruptedException e) { |
| 108 | + Thread.currentThread().interrupt(); |
| 109 | + throw new HttpException(499, e); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private HttpRequest.Builder applyFilters(HttpRequest.Builder hreq) { |
| 114 | + return hreq; |
| 115 | + } |
| 116 | + |
| 117 | + BodyContent write(Object bean, String contentType) { |
| 118 | + return bodyAdapter.beanWriter(bean.getClass()).write(bean, contentType); |
| 119 | + } |
| 120 | + |
| 121 | + <T> T readBean(Class<T> cls, BodyContent content) { |
| 122 | + return bodyAdapter.beanReader(cls).read(content); |
| 123 | + } |
| 124 | + |
| 125 | + <T> List<T> readList(Class<T> cls, BodyContent content) { |
| 126 | + return bodyAdapter.listReader(cls).read(content); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + void afterResponse(DHttpClientRequest request) { |
| 131 | + if (requestListener != null) { |
| 132 | + requestListener.response(request.listenerEvent()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + void afterResponseHandler(DHttpClientRequest request) { |
| 137 | + if (requestListener != null) { |
| 138 | + requestListener.response(request.listenerEvent()); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments