Skip to content

Commit d566c6e

Browse files
authored
Better handling for response creation in annotation-processor (Azure#45188)
Better handling for response creation in annotation-processor
1 parent 5eaab08 commit d566c6e

16 files changed

Lines changed: 915 additions & 553 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static HostEdgeCase1Service getNewInstance(HttpPipeline httpPipeline) {
4343
return new HostEdgeCase1ServiceImpl(httpPipeline);
4444
}
4545

46-
@SuppressWarnings({ "unchecked", "cast" })
46+
@SuppressWarnings("cast")
4747
@Override
4848
public byte[] getByteArray(String url, int numberOfBytes) {
4949
String uri = url + "/bytes/" + numberOfBytes;
@@ -60,6 +60,8 @@ public byte[] getByteArray(String url, int numberOfBytes) {
6060
}
6161
BinaryData responseBody = networkResponse.getValue();
6262
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;
63-
return responseBodyBytes != null ? (responseBodyBytes.length == 0 ? null : responseBodyBytes) : null;
63+
// Close the network response as the body should be consumed.
64+
networkResponse.close();
65+
return responseBodyBytes;
6466
}
6567
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static HostEdgeCase2Service getNewInstance(HttpPipeline httpPipeline) {
4343
return new HostEdgeCase2ServiceImpl(httpPipeline);
4444
}
4545

46-
@SuppressWarnings({ "unchecked", "cast" })
46+
@SuppressWarnings("cast")
4747
@Override
4848
public byte[] getByteArray(String uri, int numberOfBytes) {
4949
String requestUri = uri + "/bytes/" + numberOfBytes;
@@ -60,6 +60,8 @@ public byte[] getByteArray(String uri, int numberOfBytes) {
6060
}
6161
BinaryData responseBody = networkResponse.getValue();
6262
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;
63-
return responseBodyBytes != null ? (responseBodyBytes.length == 0 ? null : responseBodyBytes) : null;
63+
// Close the network response as the body should be consumed.
64+
networkResponse.close();
65+
return responseBodyBytes;
6466
}
6567
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static ParameterizedHostService getNewInstance(HttpPipeline httpPipeline)
4343
return new ParameterizedHostServiceImpl(httpPipeline);
4444
}
4545

46-
@SuppressWarnings({ "unchecked", "cast" })
46+
@SuppressWarnings("cast")
4747
@Override
4848
public byte[] getByteArray(String scheme, String host, int numberOfBytes) {
4949
String uri = scheme + "://" + host + "/bytes/" + numberOfBytes;
@@ -60,6 +60,8 @@ public byte[] getByteArray(String scheme, String host, int numberOfBytes) {
6060
}
6161
BinaryData responseBody = networkResponse.getValue();
6262
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;
63-
return responseBodyBytes != null ? (responseBodyBytes.length == 0 ? null : responseBodyBytes) : null;
63+
// Close the network response as the body should be consumed.
64+
networkResponse.close();
65+
return responseBodyBytes;
6466
}
6567
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io.clientcore.core.serialization.xml.XmlSerializer;
1717
import io.clientcore.core.http.models.HttpResponseException;
1818
import io.clientcore.core.utils.CoreUtils;
19-
import io.clientcore.core.utils.Base64Uri;
2019
import java.lang.reflect.ParameterizedType;
2120
import io.clientcore.core.serialization.SerializationFormat;
2221

@@ -48,7 +47,7 @@ public static ParameterizedMultipleHostService getNewInstance(HttpPipeline httpP
4847
return new ParameterizedMultipleHostServiceImpl(httpPipeline);
4948
}
5049

51-
@SuppressWarnings({ "unchecked", "cast" })
50+
@SuppressWarnings("cast")
5251
@Override
5352
public HttpBinJSON get(String scheme, String hostPart1, String hostPart2) {
5453
String uri = scheme + "://" + hostPart1 + hostPart2 + "/get";
@@ -63,17 +62,18 @@ public HttpBinJSON get(String scheme, String hostPart1, String hostPart2) {
6362
networkResponse.close();
6463
throw new HttpResponseException(errorMessage, networkResponse, null);
6564
}
66-
Object result = null;
67-
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON.class);
65+
HttpBinJSON deserializedResult;
66+
ParameterizedType returnType = CoreUtils.createParameterizedType(HttpBinJSON.class);
6867
SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
6968
if (jsonSerializer.supportsFormat(serializationFormat)) {
70-
result = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType);
69+
deserializedResult = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType);
7170
} else if (xmlSerializer.supportsFormat(serializationFormat)) {
72-
result = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType);
71+
deserializedResult = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType);
7372
} else {
7473
throw new RuntimeException(new UnsupportedOperationException("None of the provided serializers support the format: " + serializationFormat + "."));
7574
}
75+
// Close the network response as the body should be consumed.
7676
networkResponse.close();
77-
return (io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON) result;
77+
return deserializedResult;
7878
}
7979
}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import io.clientcore.core.utils.CoreUtils;
1919
import io.clientcore.core.http.models.HttpResponseException;
2020
import io.clientcore.core.http.models.HttpHeader;
21-
import io.clientcore.core.utils.Base64Uri;
2221
import java.lang.reflect.ParameterizedType;
2322

2423
/**
@@ -49,7 +48,7 @@ public static SimpleXmlSerializableService getNewInstance(HttpPipeline httpPipel
4948
return new SimpleXmlSerializableServiceImpl(httpPipeline);
5049
}
5150

52-
@SuppressWarnings({ "unchecked", "cast" })
51+
@SuppressWarnings("cast")
5352
@Override
5453
public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
5554
String uri = "http://localhost/sendApplicationXml";
@@ -76,7 +75,7 @@ public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
7675
networkResponse.close();
7776
}
7877

79-
@SuppressWarnings({ "unchecked", "cast" })
78+
@SuppressWarnings("cast")
8079
@Override
8180
public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
8281
String uri = "http://localhost/sendTextXml";
@@ -103,7 +102,7 @@ public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
103102
networkResponse.close();
104103
}
105104

106-
@SuppressWarnings({ "unchecked", "cast" })
105+
@SuppressWarnings("cast")
107106
@Override
108107
public SimpleXmlSerializable getXml(String contentType) {
109108
String uri = "http://localhost/getXml";
@@ -121,21 +120,22 @@ public SimpleXmlSerializable getXml(String contentType) {
121120
networkResponse.close();
122121
throw new HttpResponseException(errorMessage, networkResponse, null);
123122
}
124-
Object result = null;
125-
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
123+
SimpleXmlSerializable deserializedResult;
124+
ParameterizedType returnType = CoreUtils.createParameterizedType(SimpleXmlSerializable.class);
126125
SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
127126
if (jsonSerializer.supportsFormat(serializationFormat)) {
128-
result = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType);
127+
deserializedResult = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType);
129128
} else if (xmlSerializer.supportsFormat(serializationFormat)) {
130-
result = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType);
129+
deserializedResult = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType);
131130
} else {
132131
throw new RuntimeException(new UnsupportedOperationException("None of the provided serializers support the format: " + serializationFormat + "."));
133132
}
133+
// Close the network response as the body should be consumed.
134134
networkResponse.close();
135-
return (io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable) result;
135+
return deserializedResult;
136136
}
137137

138-
@SuppressWarnings({ "unchecked", "cast" })
138+
@SuppressWarnings("cast")
139139
@Override
140140
public SimpleXmlSerializable getInvalidXml(String contentType) {
141141
String uri = "http://localhost/getInvalidXml";
@@ -153,17 +153,18 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
153153
networkResponse.close();
154154
throw new HttpResponseException(errorMessage, networkResponse, null);
155155
}
156-
Object result = null;
157-
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
156+
SimpleXmlSerializable deserializedResult;
157+
ParameterizedType returnType = CoreUtils.createParameterizedType(SimpleXmlSerializable.class);
158158
SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
159159
if (jsonSerializer.supportsFormat(serializationFormat)) {
160-
result = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType);
160+
deserializedResult = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType);
161161
} else if (xmlSerializer.supportsFormat(serializationFormat)) {
162-
result = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType);
162+
deserializedResult = CoreUtils.decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType);
163163
} else {
164164
throw new RuntimeException(new UnsupportedOperationException("None of the provided serializers support the format: " + serializationFormat + "."));
165165
}
166+
// Close the network response as the body should be consumed.
166167
networkResponse.close();
167-
return (io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable) result;
168+
return deserializedResult;
168169
}
169170
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package io.clientcore.annotation.processor.test;
4+
5+
import io.clientcore.core.http.models.HttpHeaderName;
6+
import io.clientcore.core.http.models.HttpMethod;
7+
import io.clientcore.core.http.models.HttpRequest;
8+
import io.clientcore.core.http.models.Response;
9+
import io.clientcore.core.http.pipeline.HttpPipeline;
10+
import io.clientcore.core.implementation.utils.UriEscapers;
11+
import io.clientcore.core.models.binarydata.BinaryData;
12+
import java.io.InputStream;
13+
import io.clientcore.annotation.processor.test.implementation.SpecialReturnBodiesService;
14+
import io.clientcore.core.instrumentation.logging.ClientLogger;
15+
import io.clientcore.core.serialization.json.JsonSerializer;
16+
import io.clientcore.core.serialization.xml.XmlSerializer;
17+
18+
/**
19+
* Initializes a new instance of the SpecialReturnBodiesServiceImpl type.
20+
*/
21+
public class SpecialReturnBodiesServiceImpl implements SpecialReturnBodiesService {
22+
23+
private static final ClientLogger LOGGER = new ClientLogger(SpecialReturnBodiesService.class);
24+
25+
private final HttpPipeline httpPipeline;
26+
27+
private final JsonSerializer jsonSerializer;
28+
29+
private final XmlSerializer xmlSerializer;
30+
31+
private SpecialReturnBodiesServiceImpl(HttpPipeline httpPipeline) {
32+
this.httpPipeline = httpPipeline;
33+
this.jsonSerializer = JsonSerializer.getInstance();
34+
this.xmlSerializer = XmlSerializer.getInstance();
35+
}
36+
37+
/**
38+
* Creates an instance of SpecialReturnBodiesService that is capable of sending requests to the service.
39+
* @param httpPipeline The HTTP pipeline to use for sending requests.
40+
* @return An instance of `SpecialReturnBodiesService`;
41+
*/
42+
public static SpecialReturnBodiesService getNewInstance(HttpPipeline httpPipeline) {
43+
return new SpecialReturnBodiesServiceImpl(httpPipeline);
44+
}
45+
46+
@SuppressWarnings("cast")
47+
@Override
48+
public BinaryData getBinaryData(String url) {
49+
String uri = url + "/bytes";
50+
// Create the HTTP request
51+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri);
52+
// Send the request through the httpPipeline
53+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
54+
int responseCode = networkResponse.getStatusCode();
55+
boolean expectedResponse = responseCode == 200;
56+
if (!expectedResponse) {
57+
throw new RuntimeException("Unexpected response code: " + responseCode);
58+
}
59+
return networkResponse.getValue();
60+
}
61+
62+
@SuppressWarnings("cast")
63+
@Override
64+
public Response<BinaryData> getBinaryDataWithResponse(String url) {
65+
String uri = url + "/bytes";
66+
// Create the HTTP request
67+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri);
68+
// Send the request through the httpPipeline
69+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
70+
int responseCode = networkResponse.getStatusCode();
71+
boolean expectedResponse = responseCode == 200;
72+
if (!expectedResponse) {
73+
throw new RuntimeException("Unexpected response code: " + responseCode);
74+
}
75+
return networkResponse;
76+
}
77+
78+
@SuppressWarnings("cast")
79+
@Override
80+
public byte[] getByteArray(String url) {
81+
String uri = url + "/bytes";
82+
// Create the HTTP request
83+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri);
84+
// Send the request through the httpPipeline
85+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
86+
int responseCode = networkResponse.getStatusCode();
87+
boolean expectedResponse = responseCode == 200;
88+
if (!expectedResponse) {
89+
throw new RuntimeException("Unexpected response code: " + responseCode);
90+
}
91+
BinaryData responseBody = networkResponse.getValue();
92+
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;
93+
// Close the network response as the body should be consumed.
94+
networkResponse.close();
95+
return responseBodyBytes;
96+
}
97+
98+
@SuppressWarnings("cast")
99+
@Override
100+
public Response<byte[]> getByteArrayWithResponse(String url) {
101+
String uri = url + "/bytes";
102+
// Create the HTTP request
103+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri);
104+
// Send the request through the httpPipeline
105+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
106+
int responseCode = networkResponse.getStatusCode();
107+
boolean expectedResponse = responseCode == 200;
108+
if (!expectedResponse) {
109+
throw new RuntimeException("Unexpected response code: " + responseCode);
110+
}
111+
BinaryData responseBody = networkResponse.getValue();
112+
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;
113+
// Close the network response as the body should be consumed.
114+
networkResponse.close();
115+
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), responseBodyBytes);
116+
}
117+
118+
@SuppressWarnings("cast")
119+
@Override
120+
public InputStream getInputStream(String url) {
121+
String uri = url + "/bytes";
122+
// Create the HTTP request
123+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri);
124+
// Send the request through the httpPipeline
125+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
126+
int responseCode = networkResponse.getStatusCode();
127+
boolean expectedResponse = responseCode == 200;
128+
if (!expectedResponse) {
129+
throw new RuntimeException("Unexpected response code: " + responseCode);
130+
}
131+
return networkResponse.getValue().toStream();
132+
}
133+
134+
@SuppressWarnings("cast")
135+
@Override
136+
public Response<InputStream> getInputStreamWithResponse(String url) {
137+
String uri = url + "/bytes";
138+
// Create the HTTP request
139+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uri);
140+
// Send the request through the httpPipeline
141+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
142+
int responseCode = networkResponse.getStatusCode();
143+
boolean expectedResponse = responseCode == 200;
144+
if (!expectedResponse) {
145+
throw new RuntimeException("Unexpected response code: " + responseCode);
146+
}
147+
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), networkResponse.getValue().toStream());
148+
}
149+
}

0 commit comments

Comments
 (0)