Skip to content

Commit 3bfe6c9

Browse files
committed
update after rebase
1 parent cb802d0 commit 3bfe6c9

File tree

7 files changed

+260
-134
lines changed

7 files changed

+260
-134
lines changed

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/HostEdgeCase1ServiceImpl.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import io.clientcore.core.serialization.json.JsonSerializer;
1515
import io.clientcore.core.serialization.xml.XmlSerializer;
1616
import io.clientcore.core.http.models.HttpResponseException;
17+
import java.lang.reflect.ParameterizedType;
18+
import io.clientcore.core.utils.CoreUtils;
19+
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
1721

1822
/**
1923
* Initializes a new instance of the HostEdgeCase1ServiceImpl type.
@@ -53,11 +57,34 @@ public byte[] getByteArray(String url, int numberOfBytes) {
5357
int responseCode = networkResponse.getStatusCode();
5458
boolean expectedResponse = responseCode == 200;
5559
if (!expectedResponse) {
56-
String errorMessage = networkResponse.getValue().toString();
57-
throw new HttpResponseException(errorMessage, networkResponse, null);
60+
BinaryData value = networkResponse.getValue();
61+
if (value == null || value.toBytes().length == 0) {
62+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
63+
} else {
64+
ParameterizedType returnType = null;
65+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
66+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
67+
}
5868
}
5969
BinaryData responseBody = networkResponse.getValue();
6070
return responseBody != null ? responseBody.toBytes() : null;
6171
}
6272
}
73+
74+
private static HttpResponseException instantiateUnexpectedException(int responseCode, Response<BinaryData> response, BinaryData data, Object decodedValue) {
75+
StringBuilder exceptionMessage = new StringBuilder("Status code ").append(responseCode).append(", ");
76+
String contentType = response.getHeaders().getValue(HttpHeaderName.CONTENT_TYPE);
77+
if ("application/octet-stream".equalsIgnoreCase(contentType)) {
78+
String contentLength = response.getHeaders().getValue(HttpHeaderName.CONTENT_LENGTH);
79+
exceptionMessage.append("(").append(contentLength).append("-byte body)");
80+
} else if (data == null || data.toBytes().length == 0) {
81+
exceptionMessage.append("(empty body)");
82+
} else {
83+
exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"');
84+
}
85+
if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException) {
86+
return new HttpResponseException(exceptionMessage.toString(), response, (Throwable) decodedValue);
87+
}
88+
return new HttpResponseException(exceptionMessage.toString(), response, decodedValue);
89+
}
6390
}

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/HostEdgeCase2ServiceImpl.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import io.clientcore.core.serialization.json.JsonSerializer;
1515
import io.clientcore.core.serialization.xml.XmlSerializer;
1616
import io.clientcore.core.http.models.HttpResponseException;
17+
import java.lang.reflect.ParameterizedType;
18+
import io.clientcore.core.utils.CoreUtils;
19+
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
1721

1822
/**
1923
* Initializes a new instance of the HostEdgeCase2ServiceImpl type.
@@ -53,11 +57,34 @@ public byte[] getByteArray(String uri, int numberOfBytes) {
5357
int responseCode = networkResponse.getStatusCode();
5458
boolean expectedResponse = responseCode == 200;
5559
if (!expectedResponse) {
56-
String errorMessage = networkResponse.getValue().toString();
57-
throw new HttpResponseException(errorMessage, networkResponse, null);
60+
BinaryData value = networkResponse.getValue();
61+
if (value == null || value.toBytes().length == 0) {
62+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
63+
} else {
64+
ParameterizedType returnType = null;
65+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
66+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
67+
}
5868
}
5969
BinaryData responseBody = networkResponse.getValue();
6070
return responseBody != null ? responseBody.toBytes() : null;
6171
}
6272
}
73+
74+
private static HttpResponseException instantiateUnexpectedException(int responseCode, Response<BinaryData> response, BinaryData data, Object decodedValue) {
75+
StringBuilder exceptionMessage = new StringBuilder("Status code ").append(responseCode).append(", ");
76+
String contentType = response.getHeaders().getValue(HttpHeaderName.CONTENT_TYPE);
77+
if ("application/octet-stream".equalsIgnoreCase(contentType)) {
78+
String contentLength = response.getHeaders().getValue(HttpHeaderName.CONTENT_LENGTH);
79+
exceptionMessage.append("(").append(contentLength).append("-byte body)");
80+
} else if (data == null || data.toBytes().length == 0) {
81+
exceptionMessage.append("(empty body)");
82+
} else {
83+
exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"');
84+
}
85+
if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException) {
86+
return new HttpResponseException(exceptionMessage.toString(), response, (Throwable) decodedValue);
87+
}
88+
return new HttpResponseException(exceptionMessage.toString(), response, decodedValue);
89+
}
6390
}

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/ParameterizedHostServiceImpl.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
// Licensed under the MIT License.
33
package io.clientcore.annotation.processor.test;
44

5-
import io.clientcore.annotation.processor.test.implementation.ParameterizedHostService;
65
import io.clientcore.core.http.models.HttpHeaderName;
76
import io.clientcore.core.http.models.HttpMethod;
87
import io.clientcore.core.http.models.HttpRequest;
9-
import io.clientcore.core.http.models.HttpResponseException;
108
import io.clientcore.core.http.models.Response;
119
import io.clientcore.core.http.pipeline.HttpPipeline;
12-
import io.clientcore.core.instrumentation.logging.ClientLogger;
10+
import io.clientcore.core.implementation.utils.UriEscapers;
1311
import io.clientcore.core.models.binarydata.BinaryData;
12+
import io.clientcore.annotation.processor.test.implementation.ParameterizedHostService;
13+
import io.clientcore.core.instrumentation.logging.ClientLogger;
1414
import io.clientcore.core.serialization.json.JsonSerializer;
1515
import io.clientcore.core.serialization.xml.XmlSerializer;
16+
import io.clientcore.core.http.models.HttpResponseException;
17+
import java.lang.reflect.ParameterizedType;
1618
import io.clientcore.core.utils.CoreUtils;
1719
import java.io.IOException;
18-
import java.lang.reflect.ParameterizedType;
1920
import java.nio.charset.StandardCharsets;
2021

2122
/**
@@ -56,20 +57,17 @@ public byte[] getByteArray(String scheme, String host, int numberOfBytes) {
5657
int responseCode = networkResponse.getStatusCode();
5758
boolean expectedResponse = responseCode == 200;
5859
if (!expectedResponse) {
59-
String errorMessage = networkResponse.getValue().toString();
60-
throw new HttpResponseException(errorMessage, networkResponse, null);
61-
}
62-
BinaryData responseBody = networkResponse.getValue();
63-
return responseBody != null ? responseBody.toBytes() : null;
64-
if (!expectedResponse) {
65-
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
60+
BinaryData value = networkResponse.getValue();
61+
if (value == null || value.toBytes().length == 0) {
6662
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
6763
} else {
6864
ParameterizedType returnType = null;
69-
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(),
70-
CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
65+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
66+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
7167
}
7268
}
69+
BinaryData responseBody = networkResponse.getValue();
70+
return responseBody != null ? responseBody.toBytes() : null;
7371
}
7472
}
7573

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/SpecialReturnBodiesServiceImpl.java

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
import io.clientcore.core.serialization.json.JsonSerializer;
1616
import io.clientcore.core.serialization.xml.XmlSerializer;
1717
import io.clientcore.core.http.models.HttpResponseException;
18+
import io.clientcore.core.utils.CoreUtils;
19+
import java.lang.reflect.ParameterizedType;
20+
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
1822

1923
/**
2024
* Initializes a new instance of the SpecialReturnBodiesServiceImpl type.
@@ -54,9 +58,16 @@ public BinaryData getBinaryData(String url) {
5458
int responseCode = networkResponse.getStatusCode();
5559
boolean expectedResponse = responseCode == 200;
5660
if (!expectedResponse) {
57-
String errorMessage = networkResponse.getValue().toString();
58-
networkResponse.close();
59-
throw new HttpResponseException(errorMessage, networkResponse, null);
61+
BinaryData value = networkResponse.getValue();
62+
if (value == null || value.toBytes().length == 0) {
63+
networkResponse.close();
64+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
65+
} else {
66+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.core.models.binarydata.BinaryData.class);
67+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
68+
networkResponse.close();
69+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
70+
}
6071
}
6172
return networkResponse.getValue();
6273
}
@@ -71,9 +82,16 @@ public Response<BinaryData> getBinaryDataWithResponse(String url) {
7182
int responseCode = networkResponse.getStatusCode();
7283
boolean expectedResponse = responseCode == 200;
7384
if (!expectedResponse) {
74-
String errorMessage = networkResponse.getValue().toString();
75-
networkResponse.close();
76-
throw new HttpResponseException(errorMessage, networkResponse, null);
85+
BinaryData value = networkResponse.getValue();
86+
if (value == null || value.toBytes().length == 0) {
87+
networkResponse.close();
88+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
89+
} else {
90+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.core.http.models.Response.class, BinaryData.class);
91+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
92+
networkResponse.close();
93+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
94+
}
7795
}
7896
return networkResponse;
7997
}
@@ -88,8 +106,14 @@ public byte[] getByteArray(String url) {
88106
int responseCode = networkResponse.getStatusCode();
89107
boolean expectedResponse = responseCode == 200;
90108
if (!expectedResponse) {
91-
String errorMessage = networkResponse.getValue().toString();
92-
throw new HttpResponseException(errorMessage, networkResponse, null);
109+
BinaryData value = networkResponse.getValue();
110+
if (value == null || value.toBytes().length == 0) {
111+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
112+
} else {
113+
ParameterizedType returnType = null;
114+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
115+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
116+
}
93117
}
94118
BinaryData responseBody = networkResponse.getValue();
95119
return responseBody != null ? responseBody.toBytes() : null;
@@ -106,8 +130,14 @@ public Response<byte[]> getByteArrayWithResponse(String url) {
106130
int responseCode = networkResponse.getStatusCode();
107131
boolean expectedResponse = responseCode == 200;
108132
if (!expectedResponse) {
109-
String errorMessage = networkResponse.getValue().toString();
110-
throw new HttpResponseException(errorMessage, networkResponse, null);
133+
BinaryData value = networkResponse.getValue();
134+
if (value == null || value.toBytes().length == 0) {
135+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
136+
} else {
137+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.core.http.models.Response.class);
138+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
139+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
140+
}
111141
}
112142
BinaryData responseBody = networkResponse.getValue();
113143
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), responseBody != null ? responseBody.toBytes() : null);
@@ -124,9 +154,16 @@ public InputStream getInputStream(String url) {
124154
int responseCode = networkResponse.getStatusCode();
125155
boolean expectedResponse = responseCode == 200;
126156
if (!expectedResponse) {
127-
String errorMessage = networkResponse.getValue().toString();
128-
networkResponse.close();
129-
throw new HttpResponseException(errorMessage, networkResponse, null);
157+
BinaryData value = networkResponse.getValue();
158+
if (value == null || value.toBytes().length == 0) {
159+
networkResponse.close();
160+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
161+
} else {
162+
ParameterizedType returnType = CoreUtils.createParameterizedType(java.io.InputStream.class);
163+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
164+
networkResponse.close();
165+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
166+
}
130167
}
131168
return networkResponse.getValue().toStream();
132169
}
@@ -141,10 +178,34 @@ public Response<InputStream> getInputStreamWithResponse(String url) {
141178
int responseCode = networkResponse.getStatusCode();
142179
boolean expectedResponse = responseCode == 200;
143180
if (!expectedResponse) {
144-
String errorMessage = networkResponse.getValue().toString();
145-
networkResponse.close();
146-
throw new HttpResponseException(errorMessage, networkResponse, null);
181+
BinaryData value = networkResponse.getValue();
182+
if (value == null || value.toBytes().length == 0) {
183+
networkResponse.close();
184+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
185+
} else {
186+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.core.http.models.Response.class, InputStream.class);
187+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
188+
networkResponse.close();
189+
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
190+
}
147191
}
148192
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), networkResponse.getValue().toStream());
149193
}
194+
195+
private static HttpResponseException instantiateUnexpectedException(int responseCode, Response<BinaryData> response, BinaryData data, Object decodedValue) {
196+
StringBuilder exceptionMessage = new StringBuilder("Status code ").append(responseCode).append(", ");
197+
String contentType = response.getHeaders().getValue(HttpHeaderName.CONTENT_TYPE);
198+
if ("application/octet-stream".equalsIgnoreCase(contentType)) {
199+
String contentLength = response.getHeaders().getValue(HttpHeaderName.CONTENT_LENGTH);
200+
exceptionMessage.append("(").append(contentLength).append("-byte body)");
201+
} else if (data == null || data.toBytes().length == 0) {
202+
exceptionMessage.append("(empty body)");
203+
} else {
204+
exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"');
205+
}
206+
if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException) {
207+
return new HttpResponseException(exceptionMessage.toString(), response, (Throwable) decodedValue);
208+
}
209+
return new HttpResponseException(exceptionMessage.toString(), response, decodedValue);
210+
}
150211
}

0 commit comments

Comments
 (0)