Skip to content

Commit f401cea

Browse files
committed
impl changes
1 parent b065cf5 commit f401cea

File tree

5 files changed

+636
-72
lines changed

5 files changed

+636
-72
lines changed

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

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
import io.clientcore.core.instrumentation.logging.ClientLogger;
1313
import io.clientcore.core.serialization.json.JsonSerializer;
1414
import io.clientcore.core.serialization.xml.XmlSerializer;
15+
import java.lang.reflect.ParameterizedType;
16+
import io.clientcore.core.utils.CoreUtils;
17+
import java.io.IOException;
18+
import io.clientcore.core.http.models.HttpResponseException;
19+
import java.nio.charset.StandardCharsets;
1520

1621
/**
1722
* Initializes a new instance of the ParameterizedHostServiceImpl type.
@@ -52,10 +57,32 @@ public byte[] getByteArray(String scheme, String host, int numberOfBytes) {
5257
int responseCode = networkResponse.getStatusCode();
5358
boolean expectedResponse = responseCode == 200;
5459
if (!expectedResponse) {
55-
throw new RuntimeException("Unexpected response code: " + responseCode);
60+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
61+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
62+
} else {
63+
ParameterizedType returnType = null;
64+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
65+
}
5666
}
5767
BinaryData responseBody = networkResponse.getValue();
5868
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;
5969
return responseBodyBytes != null ? (responseBodyBytes.length == 0 ? null : responseBodyBytes) : null;
6070
}
71+
72+
private static HttpResponseException instantiateUnexpectedException(int responseCode, Response<BinaryData> response, BinaryData data, Object decodedValue) {
73+
StringBuilder exceptionMessage = new StringBuilder("Status code ").append(responseCode).append(", ");
74+
String contentType = response.getHeaders().getValue(HttpHeaderName.CONTENT_TYPE);
75+
if ("application/octet-stream".equalsIgnoreCase(contentType)) {
76+
String contentLength = response.getHeaders().getValue(HttpHeaderName.CONTENT_LENGTH);
77+
exceptionMessage.append("(").append(contentLength).append("-byte body)");
78+
} else if (data == null || data.toBytes().length == 0) {
79+
exceptionMessage.append("(empty body)");
80+
} else {
81+
exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"');
82+
}
83+
if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException) {
84+
return new HttpResponseException(exceptionMessage.toString(), response, (Throwable) decodedValue);
85+
}
86+
return new HttpResponseException(exceptionMessage.toString(), response, decodedValue);
87+
}
6188
}

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import io.clientcore.core.utils.CoreUtils;
1717
import java.lang.reflect.ParameterizedType;
1818
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;
1922

2023
/**
2124
* Initializes a new instance of the ParameterizedMultipleHostServiceImpl type.
@@ -56,7 +59,12 @@ public HttpBinJSON get(String scheme, String hostPart1, String hostPart2) {
5659
int responseCode = networkResponse.getStatusCode();
5760
boolean expectedResponse = responseCode == 200;
5861
if (!expectedResponse) {
59-
throw new RuntimeException("Unexpected response code: " + responseCode);
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+
}
6068
}
6169
Object result = null;
6270
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON.class);
@@ -71,4 +79,21 @@ public HttpBinJSON get(String scheme, String hostPart1, String hostPart2) {
7179
networkResponse.close();
7280
return (io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON) result;
7381
}
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);
98+
}
7499
}

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

+45-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
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.utils.CoreUtils;
1716
import java.lang.reflect.ParameterizedType;
17+
import io.clientcore.core.utils.CoreUtils;
1818
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;
1922

2023
/**
2124
* Initializes a new instance of the SimpleXmlSerializableServiceImpl type.
@@ -65,7 +68,12 @@ public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
6568
int responseCode = networkResponse.getStatusCode();
6669
boolean expectedResponse = responseCode == 200;
6770
if (!expectedResponse) {
68-
throw new RuntimeException("Unexpected response code: " + responseCode);
71+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
72+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
73+
} else {
74+
ParameterizedType returnType = null;
75+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
76+
}
6977
}
7078
networkResponse.close();
7179
}
@@ -90,7 +98,12 @@ public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
9098
int responseCode = networkResponse.getStatusCode();
9199
boolean expectedResponse = responseCode == 200;
92100
if (!expectedResponse) {
93-
throw new RuntimeException("Unexpected response code: " + responseCode);
101+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
102+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
103+
} else {
104+
ParameterizedType returnType = null;
105+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
106+
}
94107
}
95108
networkResponse.close();
96109
}
@@ -107,7 +120,12 @@ public SimpleXmlSerializable getXml(String contentType) {
107120
int responseCode = networkResponse.getStatusCode();
108121
boolean expectedResponse = responseCode == 200;
109122
if (!expectedResponse) {
110-
throw new RuntimeException("Unexpected response code: " + responseCode);
123+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
124+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
125+
} else {
126+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
127+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
128+
}
111129
}
112130
Object result = null;
113131
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
@@ -135,7 +153,12 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
135153
int responseCode = networkResponse.getStatusCode();
136154
boolean expectedResponse = responseCode == 200;
137155
if (!expectedResponse) {
138-
throw new RuntimeException("Unexpected response code: " + responseCode);
156+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
157+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
158+
} else {
159+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
160+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
161+
}
139162
}
140163
Object result = null;
141164
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
@@ -150,4 +173,21 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
150173
networkResponse.close();
151174
return (io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable) result;
152175
}
176+
177+
private static HttpResponseException instantiateUnexpectedException(int responseCode, Response<BinaryData> response, BinaryData data, Object decodedValue) {
178+
StringBuilder exceptionMessage = new StringBuilder("Status code ").append(responseCode).append(", ");
179+
String contentType = response.getHeaders().getValue(HttpHeaderName.CONTENT_TYPE);
180+
if ("application/octet-stream".equalsIgnoreCase(contentType)) {
181+
String contentLength = response.getHeaders().getValue(HttpHeaderName.CONTENT_LENGTH);
182+
exceptionMessage.append("(").append(contentLength).append("-byte body)");
183+
} else if (data == null || data.toBytes().length == 0) {
184+
exceptionMessage.append("(empty body)");
185+
} else {
186+
exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"');
187+
}
188+
if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException) {
189+
return new HttpResponseException(exceptionMessage.toString(), response, (Throwable) decodedValue);
190+
}
191+
return new HttpResponseException(exceptionMessage.toString(), response, decodedValue);
192+
}
153193
}

0 commit comments

Comments
 (0)