|
8 | 8 | import io.clientcore.core.http.models.HttpRequest;
|
9 | 9 | import io.clientcore.core.http.models.Response;
|
10 | 10 | import io.clientcore.core.http.pipeline.HttpPipeline;
|
11 |
| -import io.clientcore.core.implementation.utils.UriEscapers; |
12 | 11 | import io.clientcore.core.models.binarydata.BinaryData;
|
13 | 12 | import io.clientcore.annotation.processor.test.implementation.ParameterizedMultipleHostService;
|
14 | 13 | import io.clientcore.core.instrumentation.logging.ClientLogger;
|
15 | 14 | import io.clientcore.core.serialization.json.JsonSerializer;
|
16 | 15 | import io.clientcore.core.serialization.xml.XmlSerializer;
|
17 |
| -import io.clientcore.core.http.models.HttpResponseException; |
18 | 16 | import io.clientcore.core.utils.CoreUtils;
|
19 | 17 | import java.lang.reflect.ParameterizedType;
|
20 | 18 | import io.clientcore.core.serialization.SerializationFormat;
|
| 19 | +import java.io.IOException; |
| 20 | +import io.clientcore.core.http.models.HttpResponseException; |
| 21 | +import java.nio.charset.StandardCharsets; |
21 | 22 |
|
22 | 23 | /**
|
23 | 24 | * Initializes a new instance of the ParameterizedMultipleHostServiceImpl type.
|
@@ -47,31 +48,52 @@ public static ParameterizedMultipleHostService getNewInstance(HttpPipeline httpP
|
47 | 48 | return new ParameterizedMultipleHostServiceImpl(httpPipeline);
|
48 | 49 | }
|
49 | 50 |
|
50 |
| - @SuppressWarnings("cast") |
| 51 | + @SuppressWarnings({ "unchecked", "cast" }) |
51 | 52 | @Override
|
52 | 53 | public HttpBinJSON get(String scheme, String hostPart1, String hostPart2) {
|
53 |
| - String uri = scheme + "://" + hostPart1 + hostPart2 + "/get"; |
| 54 | + String url = scheme + "://" + hostPart1 + hostPart2 + "/get"; |
54 | 55 | // Create the HTTP request
|
55 |
| - HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri); |
| 56 | + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(url); |
56 | 57 | // Send the request through the httpPipeline
|
57 | 58 | Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
|
58 | 59 | int responseCode = networkResponse.getStatusCode();
|
59 | 60 | boolean expectedResponse = responseCode == 200;
|
60 | 61 | if (!expectedResponse) {
|
61 |
| - String errorMessage = networkResponse.getValue().toString(); |
62 |
| - networkResponse.close(); |
63 |
| - throw new HttpResponseException(errorMessage, networkResponse, null); |
| 62 | + if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) { |
| 63 | + throw instantiateUnexpectedException(responseCode, networkResponse, null, null); |
| 64 | + } else { |
| 65 | + ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON.class); |
| 66 | + throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType)); |
| 67 | + } |
64 | 68 | }
|
65 |
| - HttpBinJSON deserializedResult; |
66 |
| - ParameterizedType returnType = CoreUtils.createParameterizedType(HttpBinJSON.class); |
| 69 | + Object result = null; |
| 70 | + ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON.class); |
67 | 71 | SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
|
68 | 72 | if (jsonSerializer.supportsFormat(serializationFormat)) {
|
69 |
| - deserializedResult = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType); |
| 73 | + result = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType); |
70 | 74 | } else if (xmlSerializer.supportsFormat(serializationFormat)) {
|
71 |
| - deserializedResult = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType); |
| 75 | + result = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType); |
72 | 76 | } else {
|
73 | 77 | throw new RuntimeException(new UnsupportedOperationException("None of the provided serializers support the format: " + serializationFormat + "."));
|
74 | 78 | }
|
75 |
| - return deserializedResult; |
| 79 | + networkResponse.close(); |
| 80 | + return (io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON) result; |
| 81 | + } |
| 82 | + |
| 83 | + private static HttpResponseException instantiateUnexpectedException(int responseCode, Response<BinaryData> response, BinaryData data, Object decodedValue) { |
| 84 | + StringBuilder exceptionMessage = new StringBuilder("Status code ").append(responseCode).append(", "); |
| 85 | + String contentType = response.getHeaders().getValue(HttpHeaderName.CONTENT_TYPE); |
| 86 | + if ("application/octet-stream".equalsIgnoreCase(contentType)) { |
| 87 | + String contentLength = response.getHeaders().getValue(HttpHeaderName.CONTENT_LENGTH); |
| 88 | + exceptionMessage.append("(").append(contentLength).append("-byte body)"); |
| 89 | + } else if (data == null || data.toBytes().length == 0) { |
| 90 | + exceptionMessage.append("(empty body)"); |
| 91 | + } else { |
| 92 | + exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"'); |
| 93 | + } |
| 94 | + if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException) { |
| 95 | + return new HttpResponseException(exceptionMessage.toString(), response, (Throwable) decodedValue); |
| 96 | + } |
| 97 | + return new HttpResponseException(exceptionMessage.toString(), response, decodedValue); |
76 | 98 | }
|
77 | 99 | }
|
0 commit comments