Skip to content

Commit ad6542f

Browse files
authored
Check in generated class and script to transfer generated classes to main (Azure#44970)
1 parent 41e7411 commit ad6542f

File tree

3 files changed

+2522
-0
lines changed

3 files changed

+2522
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copy generated implementation classes to main source tree for review/versioning
2+
3+
$srcDir = "target/generated-sources//io/clientcore/annotation/processor/test/"
4+
$destDir = "src/main/java/io/clientcore/annotation/processor/test/"
5+
6+
if (!(Test-Path $destDir)) {
7+
New-Item -ItemType Directory -Path $destDir | Out-Null
8+
}
9+
Copy-Item "$srcDir\*.java" $destDir -Force
10+
Write-Host "Copied generated impl classes to $destDir"
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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.annotation.processor.test.implementation.models.SimpleXmlSerializable;
6+
import io.clientcore.core.http.models.HttpHeaderName;
7+
import io.clientcore.core.http.models.HttpMethod;
8+
import io.clientcore.core.http.models.HttpRequest;
9+
import io.clientcore.core.http.models.Response;
10+
import io.clientcore.core.http.pipeline.HttpPipeline;
11+
import io.clientcore.core.models.binarydata.BinaryData;
12+
import io.clientcore.annotation.processor.test.implementation.SimpleXmlSerializableService;
13+
import io.clientcore.core.instrumentation.logging.ClientLogger;
14+
import io.clientcore.core.serialization.json.JsonSerializer;
15+
import io.clientcore.core.serialization.xml.XmlSerializer;
16+
import io.clientcore.core.implementation.utils.UriEscapers;
17+
import java.io.IOException;
18+
import java.io.UncheckedIOException;
19+
import io.clientcore.core.utils.CoreUtils;
20+
import io.clientcore.core.serialization.SerializationFormat;
21+
import io.clientcore.core.serialization.ObjectSerializer;
22+
import java.lang.reflect.ParameterizedType;
23+
import java.lang.reflect.Type;
24+
import java.util.List;
25+
26+
/**
27+
* Initializes a new instance of the SimpleXmlSerializableServiceImpl type.
28+
*/
29+
public class SimpleXmlSerializableServiceImpl implements SimpleXmlSerializableService {
30+
31+
private static final ClientLogger LOGGER = new ClientLogger(SimpleXmlSerializableService.class);
32+
33+
private final HttpPipeline httpPipeline;
34+
35+
private final JsonSerializer jsonSerializer;
36+
37+
private final XmlSerializer xmlSerializer;
38+
39+
private SimpleXmlSerializableServiceImpl(HttpPipeline httpPipeline) {
40+
this.httpPipeline = httpPipeline;
41+
this.jsonSerializer = JsonSerializer.getInstance();
42+
this.xmlSerializer = XmlSerializer.getInstance();
43+
}
44+
45+
/**
46+
* Creates an instance of SimpleXmlSerializableService that is capable of sending requests to the service.
47+
* @param httpPipeline The HTTP pipeline to use for sending requests.
48+
* @return An instance of `SimpleXmlSerializableService`;
49+
*/
50+
public static SimpleXmlSerializableService getNewInstance(HttpPipeline httpPipeline) {
51+
return new SimpleXmlSerializableServiceImpl(httpPipeline);
52+
}
53+
54+
@SuppressWarnings({ "unchecked", "cast" })
55+
@Override
56+
public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
57+
String url = "http://localhost/sendApplicationXml";
58+
// Create the HTTP request
59+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT).setUri(url);
60+
httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, "application/xml");
61+
if (simpleXmlSerializable != null) {
62+
SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
63+
if (xmlSerializer.supportsFormat(serializationFormat)) {
64+
httpRequest.setBody(BinaryData.fromObject(simpleXmlSerializable, xmlSerializer));
65+
} else {
66+
httpRequest.setBody(BinaryData.fromObject(simpleXmlSerializable, jsonSerializer));
67+
}
68+
}
69+
// Send the request through the httpPipeline
70+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
71+
int responseCode = networkResponse.getStatusCode();
72+
boolean expectedResponse = responseCode == 200;
73+
if (!expectedResponse) {
74+
throw new RuntimeException("Unexpected response code: " + responseCode);
75+
}
76+
try {
77+
networkResponse.close();
78+
} catch (IOException e) {
79+
throw LOGGER.logThrowableAsError(new UncheckedIOException(e));
80+
}
81+
}
82+
83+
@SuppressWarnings({ "unchecked", "cast" })
84+
@Override
85+
public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
86+
String url = "http://localhost/sendTextXml";
87+
// Create the HTTP request
88+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT).setUri(url);
89+
httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, "text/xml");
90+
if (simpleXmlSerializable != null) {
91+
SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
92+
if (xmlSerializer.supportsFormat(serializationFormat)) {
93+
httpRequest.setBody(BinaryData.fromObject(simpleXmlSerializable, xmlSerializer));
94+
} else {
95+
httpRequest.setBody(BinaryData.fromObject(simpleXmlSerializable, jsonSerializer));
96+
}
97+
}
98+
// Send the request through the httpPipeline
99+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
100+
int responseCode = networkResponse.getStatusCode();
101+
boolean expectedResponse = responseCode == 200;
102+
if (!expectedResponse) {
103+
throw new RuntimeException("Unexpected response code: " + responseCode);
104+
}
105+
try {
106+
networkResponse.close();
107+
} catch (IOException e) {
108+
throw LOGGER.logThrowableAsError(new UncheckedIOException(e));
109+
}
110+
}
111+
112+
@SuppressWarnings({ "unchecked", "cast" })
113+
@Override
114+
public SimpleXmlSerializable getXml(String contentType) {
115+
String url = "http://localhost/getXml";
116+
// Create the HTTP request
117+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(url);
118+
httpRequest.getHeaders().add(HttpHeaderName.CONTENT_TYPE, contentType);
119+
// Send the request through the httpPipeline
120+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
121+
int responseCode = networkResponse.getStatusCode();
122+
boolean expectedResponse = responseCode == 200;
123+
if (!expectedResponse) {
124+
throw new RuntimeException("Unexpected response code: " + responseCode);
125+
}
126+
Object result = null;
127+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
128+
SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
129+
if (jsonSerializer.supportsFormat(serializationFormat)) {
130+
result = decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType);
131+
} else if (xmlSerializer.supportsFormat(serializationFormat)) {
132+
result = decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType);
133+
} else {
134+
throw new RuntimeException(new UnsupportedOperationException("None of the provided serializers support the format: " + serializationFormat + "."));
135+
}
136+
try {
137+
networkResponse.close();
138+
} catch (IOException e) {
139+
throw LOGGER.logThrowableAsError(new UncheckedIOException(e));
140+
}
141+
return (io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable) result;
142+
}
143+
144+
@SuppressWarnings({ "unchecked", "cast" })
145+
@Override
146+
public SimpleXmlSerializable getInvalidXml() {
147+
String url = "http://localhost/getInvalidXml";
148+
// Create the HTTP request
149+
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(url);
150+
// Send the request through the httpPipeline
151+
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
152+
int responseCode = networkResponse.getStatusCode();
153+
boolean expectedResponse = responseCode == 200;
154+
if (!expectedResponse) {
155+
throw new RuntimeException("Unexpected response code: " + responseCode);
156+
}
157+
Object result = null;
158+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
159+
SerializationFormat serializationFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders());
160+
if (jsonSerializer.supportsFormat(serializationFormat)) {
161+
result = decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType);
162+
} else if (xmlSerializer.supportsFormat(serializationFormat)) {
163+
result = decodeNetworkResponse(networkResponse.getValue(), xmlSerializer, returnType);
164+
} else {
165+
throw new RuntimeException(new UnsupportedOperationException("None of the provided serializers support the format: " + serializationFormat + "."));
166+
}
167+
try {
168+
networkResponse.close();
169+
} catch (IOException e) {
170+
throw LOGGER.logThrowableAsError(new UncheckedIOException(e));
171+
}
172+
return (io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable) result;
173+
}
174+
175+
/**
176+
* Decodes the body of an {@link Response} into the type returned by the called API.
177+
* @param data The BinaryData to decode.
178+
* @param serializer The serializer to use.
179+
* @param returnType The type of the ParameterizedType return value.
180+
* @return The decoded value.
181+
* @throws IOException If the deserialization fails.
182+
*/
183+
private static Object decodeNetworkResponse(BinaryData data, ObjectSerializer serializer, ParameterizedType returnType) {
184+
if (data == null) {
185+
return null;
186+
}
187+
try {
188+
if (List.class.isAssignableFrom((Class<?>) returnType.getRawType())) {
189+
return serializer.deserializeFromBytes(data.toBytes(), returnType);
190+
}
191+
Type token = returnType.getRawType();
192+
if (Response.class.isAssignableFrom((Class<?>) token)) {
193+
token = returnType.getActualTypeArguments()[0];
194+
}
195+
return serializer.deserializeFromBytes(data.toBytes(), token);
196+
} catch (IOException e) {
197+
throw LOGGER.logThrowableAsError(new UncheckedIOException(e));
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)