|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.opamp.client.internal.connectivity.http; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | +import okhttp3.Call; |
| 12 | +import okhttp3.Callback; |
| 13 | +import okhttp3.MediaType; |
| 14 | +import okhttp3.OkHttpClient; |
| 15 | +import okhttp3.RequestBody; |
| 16 | +import okio.BufferedSink; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | +import org.jetbrains.annotations.Nullable; |
| 19 | + |
| 20 | +public final class OkHttpSender implements HttpSender { |
| 21 | + private final OkHttpClient client; |
| 22 | + private final String url; |
| 23 | + |
| 24 | + public static OkHttpSender create(String url) { |
| 25 | + return create(url, new OkHttpClient()); |
| 26 | + } |
| 27 | + |
| 28 | + public static OkHttpSender create(String url, OkHttpClient client) { |
| 29 | + return new OkHttpSender(url, client); |
| 30 | + } |
| 31 | + |
| 32 | + private static final String CONTENT_TYPE = "application/x-protobuf"; |
| 33 | + private static final MediaType MEDIA_TYPE = MediaType.parse(CONTENT_TYPE); |
| 34 | + |
| 35 | + private OkHttpSender(String url, OkHttpClient client) { |
| 36 | + this.url = url; |
| 37 | + this.client = client; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public CompletableFuture<Response> send(BodyWriter writer, int contentLength) { |
| 42 | + CompletableFuture<Response> future = new CompletableFuture<>(); |
| 43 | + okhttp3.Request.Builder builder = new okhttp3.Request.Builder().url(url); |
| 44 | + builder.addHeader("Content-Type", CONTENT_TYPE); |
| 45 | + |
| 46 | + RequestBody body = new RawRequestBody(writer, contentLength, MEDIA_TYPE); |
| 47 | + builder.post(body); |
| 48 | + |
| 49 | + client |
| 50 | + .newCall(builder.build()) |
| 51 | + .enqueue( |
| 52 | + new Callback() { |
| 53 | + @Override |
| 54 | + public void onResponse(@NotNull Call call, @NotNull okhttp3.Response response) { |
| 55 | + if (response.isSuccessful() && response.body() != null) { |
| 56 | + future.complete(new OkHttpResponse(response)); |
| 57 | + } else { |
| 58 | + future.completeExceptionally( |
| 59 | + new HttpErrorException(response.code(), response.message())); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 65 | + future.completeExceptionally(e); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + return future; |
| 70 | + } |
| 71 | + |
| 72 | + private static class OkHttpResponse implements Response { |
| 73 | + private final okhttp3.Response response; |
| 74 | + |
| 75 | + private OkHttpResponse(okhttp3.Response response) { |
| 76 | + if (response.body() == null) { |
| 77 | + throw new IllegalStateException("null response body not expected"); |
| 78 | + } |
| 79 | + this.response = response; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int statusCode() { |
| 84 | + return response.code(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String statusMessage() { |
| 89 | + return response.message(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public InputStream bodyInputStream() { |
| 94 | + return response.body().byteStream(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String getHeader(String name) { |
| 99 | + return response.headers().get(name); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void close() { |
| 104 | + response.close(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static class RawRequestBody extends RequestBody { |
| 109 | + private final BodyWriter writer; |
| 110 | + private final int contentLength; |
| 111 | + private final MediaType contentType; |
| 112 | + |
| 113 | + private RawRequestBody(BodyWriter writer, int contentLength, MediaType contentType) { |
| 114 | + this.writer = writer; |
| 115 | + this.contentLength = contentLength; |
| 116 | + this.contentType = contentType; |
| 117 | + } |
| 118 | + |
| 119 | + @Nullable |
| 120 | + @Override |
| 121 | + public MediaType contentType() { |
| 122 | + return contentType; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public long contentLength() { |
| 127 | + return contentLength; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void writeTo(@NotNull BufferedSink bufferedSink) throws IOException { |
| 132 | + writer.writeTo(bufferedSink.outputStream()); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments