Skip to content

Commit c81501f

Browse files
committed
move to util
1 parent 3bfe6c9 commit c81501f

File tree

11 files changed

+948
-525
lines changed

11 files changed

+948
-525
lines changed

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import io.clientcore.core.http.models.HttpResponseException;
1717
import java.lang.reflect.ParameterizedType;
1818
import io.clientcore.core.utils.CoreUtils;
19-
import java.io.IOException;
20-
import java.nio.charset.StandardCharsets;
2119

2220
/**
2321
* Initializes a new instance of the HostEdgeCase1ServiceImpl type.
@@ -59,32 +57,15 @@ public byte[] getByteArray(String url, int numberOfBytes) {
5957
if (!expectedResponse) {
6058
BinaryData value = networkResponse.getValue();
6159
if (value == null || value.toBytes().length == 0) {
62-
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
60+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
6361
} else {
6462
ParameterizedType returnType = null;
6563
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
66-
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
64+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
6765
}
6866
}
6967
BinaryData responseBody = networkResponse.getValue();
7068
return responseBody != null ? responseBody.toBytes() : null;
7169
}
7270
}
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-
}
9071
}

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import io.clientcore.core.http.models.HttpResponseException;
1717
import java.lang.reflect.ParameterizedType;
1818
import io.clientcore.core.utils.CoreUtils;
19-
import java.io.IOException;
20-
import java.nio.charset.StandardCharsets;
2119

2220
/**
2321
* Initializes a new instance of the HostEdgeCase2ServiceImpl type.
@@ -59,32 +57,15 @@ public byte[] getByteArray(String uri, int numberOfBytes) {
5957
if (!expectedResponse) {
6058
BinaryData value = networkResponse.getValue();
6159
if (value == null || value.toBytes().length == 0) {
62-
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
60+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
6361
} else {
6462
ParameterizedType returnType = null;
6563
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
66-
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
64+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
6765
}
6866
}
6967
BinaryData responseBody = networkResponse.getValue();
7068
return responseBody != null ? responseBody.toBytes() : null;
7169
}
7270
}
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-
}
9071
}

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import io.clientcore.core.http.models.HttpResponseException;
1717
import java.lang.reflect.ParameterizedType;
1818
import io.clientcore.core.utils.CoreUtils;
19-
import java.io.IOException;
20-
import java.nio.charset.StandardCharsets;
2119

2220
/**
2321
* Initializes a new instance of the ParameterizedHostServiceImpl type.
@@ -59,32 +57,15 @@ public byte[] getByteArray(String scheme, String host, int numberOfBytes) {
5957
if (!expectedResponse) {
6058
BinaryData value = networkResponse.getValue();
6159
if (value == null || value.toBytes().length == 0) {
62-
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
60+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
6361
} else {
6462
ParameterizedType returnType = null;
6563
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
66-
throw instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
64+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
6765
}
6866
}
6967
BinaryData responseBody = networkResponse.getValue();
7068
return responseBody != null ? responseBody.toBytes() : null;
7169
}
7270
}
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-
}
9071
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
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;
1918
import java.lang.reflect.ParameterizedType;
20-
import io.clientcore.core.utils.Base64Uri;
19+
import io.clientcore.core.utils.CoreUtils;
2120
import io.clientcore.core.serialization.SerializationFormat;
2221

2322
/**
@@ -58,9 +57,16 @@ public HttpBinJSON get(String scheme, String hostPart1, String hostPart2) {
5857
int responseCode = networkResponse.getStatusCode();
5958
boolean expectedResponse = responseCode == 200;
6059
if (!expectedResponse) {
61-
String errorMessage = networkResponse.getValue().toString();
62-
networkResponse.close();
63-
throw new HttpResponseException(errorMessage, networkResponse, null);
60+
BinaryData value = networkResponse.getValue();
61+
if (value == null || value.toBytes().length == 0) {
62+
networkResponse.close();
63+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
64+
} else {
65+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON.class);
66+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
67+
networkResponse.close();
68+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
69+
}
6470
}
6571
HttpBinJSON deserializedResult;
6672
ParameterizedType returnType = CoreUtils.createParameterizedType(HttpBinJSON.class);

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

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import io.clientcore.core.serialization.SerializationFormat;
1818
import io.clientcore.core.utils.CoreUtils;
1919
import io.clientcore.core.http.models.HttpResponseException;
20-
import io.clientcore.core.http.models.HttpHeader;
2120
import java.lang.reflect.ParameterizedType;
21+
import io.clientcore.core.http.models.HttpHeader;
2222

2323
/**
2424
* Initializes a new instance of the SimpleXmlSerializableServiceImpl type.
@@ -67,9 +67,16 @@ public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
6767
int responseCode = networkResponse.getStatusCode();
6868
boolean expectedResponse = responseCode == 200;
6969
if (!expectedResponse) {
70-
String errorMessage = networkResponse.getValue().toString();
71-
networkResponse.close();
72-
throw new HttpResponseException(errorMessage, networkResponse, null);
70+
BinaryData value = networkResponse.getValue();
71+
if (value == null || value.toBytes().length == 0) {
72+
networkResponse.close();
73+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
74+
} else {
75+
ParameterizedType returnType = null;
76+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
77+
networkResponse.close();
78+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
79+
}
7380
}
7481
networkResponse.close();
7582
}
@@ -93,9 +100,16 @@ public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
93100
int responseCode = networkResponse.getStatusCode();
94101
boolean expectedResponse = responseCode == 200;
95102
if (!expectedResponse) {
96-
String errorMessage = networkResponse.getValue().toString();
97-
networkResponse.close();
98-
throw new HttpResponseException(errorMessage, networkResponse, null);
103+
BinaryData value = networkResponse.getValue();
104+
if (value == null || value.toBytes().length == 0) {
105+
networkResponse.close();
106+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
107+
} else {
108+
ParameterizedType returnType = null;
109+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
110+
networkResponse.close();
111+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
112+
}
99113
}
100114
networkResponse.close();
101115
}
@@ -113,9 +127,16 @@ public SimpleXmlSerializable getXml(String contentType) {
113127
int responseCode = networkResponse.getStatusCode();
114128
boolean expectedResponse = responseCode == 200;
115129
if (!expectedResponse) {
116-
String errorMessage = networkResponse.getValue().toString();
117-
networkResponse.close();
118-
throw new HttpResponseException(errorMessage, networkResponse, null);
130+
BinaryData value = networkResponse.getValue();
131+
if (value == null || value.toBytes().length == 0) {
132+
networkResponse.close();
133+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
134+
} else {
135+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
136+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
137+
networkResponse.close();
138+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
139+
}
119140
}
120141
SimpleXmlSerializable deserializedResult;
121142
ParameterizedType returnType = CoreUtils.createParameterizedType(SimpleXmlSerializable.class);
@@ -143,9 +164,16 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
143164
int responseCode = networkResponse.getStatusCode();
144165
boolean expectedResponse = responseCode == 200;
145166
if (!expectedResponse) {
146-
String errorMessage = networkResponse.getValue().toString();
147-
networkResponse.close();
148-
throw new HttpResponseException(errorMessage, networkResponse, null);
167+
BinaryData value = networkResponse.getValue();
168+
if (value == null || value.toBytes().length == 0) {
169+
networkResponse.close();
170+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
171+
} else {
172+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
173+
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
174+
networkResponse.close();
175+
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
176+
}
149177
}
150178
SimpleXmlSerializable deserializedResult;
151179
ParameterizedType returnType = CoreUtils.createParameterizedType(SimpleXmlSerializable.class);

0 commit comments

Comments
 (0)