Skip to content

Commit 119ae1a

Browse files
committed
Begin Work for Async->Sync revert
1 parent be071b1 commit 119ae1a

16 files changed

+176
-347
lines changed

spotBugsExcludeFilter.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
7777
<Class name="com.microsoft.graph.requests.middleware.GraphTelemetryHandler" />
7878
<Class name="com.microsoft.graph.requests.upload.UploadSessionRequestBuilder" />
7979
<Class name="com.microsoft.graph.requests.upload.UploadSliceRequestBuilder" />
80-
<Class name="com.microsoft.graph.tasks.PageIterator$BuilderWithAsyncProcess" />
81-
<Class name="com.microsoft.graph.tasks.PageIterator$BuilderWithSyncProcess" />
80+
<Class name="com.microsoft.graph.tasks.PageIterator$Builder" />
8281
</Or>
8382
</Match>
8483

src/main/java/com/microsoft/graph/content/BatchRequestContent.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.*;
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.*;
23-
import java.util.concurrent.CompletableFuture;
2423

2524
/**
2625
* A class representing the content of a batch request.
@@ -111,16 +110,15 @@ public String addBatchRequestStep(@Nonnull Request request) {
111110
* @return The request id of the added request.
112111
*/
113112
@Nonnull
114-
public CompletableFuture<String> addBatchRequestStep(@Nonnull RequestInformation requestInformation) {
113+
public String addBatchRequestStep(@Nonnull RequestInformation requestInformation) {
115114
if(this.batchRequestSteps.size() >= CoreConstants.BatchRequest.MAX_REQUESTS) {
116115
throw new IllegalArgumentException(maxStepsExceededMessage);
117116
}
118117
String requestId = java.util.UUID.randomUUID().toString();
119-
return this.requestAdapter.convertToNativeRequestAsync(requestInformation).thenCompose(request -> {
120-
BatchRequestStep requestStep = new BatchRequestStep(requestId, (Request) request);
121-
this.batchRequestSteps.put(requestId, requestStep);
122-
return CompletableFuture.completedFuture(requestId);
123-
});
118+
Request request = this.requestAdapter.convertToNativeRequest(requestInformation);
119+
BatchRequestStep requestStep = new BatchRequestStep(requestId, request);
120+
this.batchRequestSteps.put(requestId, requestStep);
121+
return requestId;
124122
}
125123
/**
126124
* Removes a batch request step from the batch request.
@@ -162,7 +160,7 @@ public BatchRequestContent createNewBatchFromFailedRequests (@Nonnull Map<String
162160
* @return The json content of the batch request as an InputStream.
163161
*/
164162
@Nonnull
165-
public CompletableFuture<InputStream> getBatchRequestContentAsync() {
163+
public InputStream getBatchRequestContentAsync() {
166164
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
167165
try (JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
168166
writer.beginObject();
@@ -177,12 +175,10 @@ public CompletableFuture<InputStream> getBatchRequestContentAsync() {
177175
PipedInputStream in = new PipedInputStream();
178176
try(final PipedOutputStream out = new PipedOutputStream(in)) {
179177
outputStream.writeTo(out);
180-
return CompletableFuture.completedFuture(in);
178+
return in;
181179
}
182180
} catch(IOException e) {
183-
CompletableFuture<InputStream> exception = new CompletableFuture<>();
184-
exception.completeExceptionally(e);
185-
return exception;
181+
throw new RuntimeException(e);
186182
}
187183
}
188184
private void writeBatchRequestStepAsync(BatchRequestStep requestStep, JsonWriter writer) {
@@ -209,10 +205,10 @@ private void writeBatchRequestStepAsync(BatchRequestStep requestStep, JsonWriter
209205
headers = headers.newBuilder().add("Content-Type", contentType).build();
210206
writer.name(CoreConstants.BatchRequest.BODY);
211207
if(contentType.toLowerCase(Locale.US).contains(CoreConstants.MimeTypeNames.APPLICATION_JSON)){
212-
JsonObject bodyObject = getJsonRequestContent(requestBody).join();
208+
JsonObject bodyObject = getJsonRequestContent(requestBody);
213209
writer.jsonValue(bodyObject.toString());
214210
} else {
215-
String rawBodyContent = getRawRequestContent(requestBody).join();
211+
String rawBodyContent = getRawRequestContent(requestBody);
216212
writer.value(rawBodyContent);
217213
}
218214
}
@@ -226,33 +222,27 @@ private void writeBatchRequestStepAsync(BatchRequestStep requestStep, JsonWriter
226222
}
227223
writer.endObject();
228224
} catch (IOException e) {
229-
CompletableFuture<Void> exception = new CompletableFuture<>();
230-
exception.completeExceptionally(e);
225+
throw new RuntimeException(e);
231226
}
232227
}
233-
private CompletableFuture<JsonObject> getJsonRequestContent(RequestBody requestBody) {
228+
private JsonObject getJsonRequestContent(RequestBody requestBody) {
234229
try {
235230
Buffer buffer = new Buffer();
236231
requestBody.writeTo(buffer);
237-
JsonObject jsonObject = JsonParser.parseString(buffer.readUtf8()).getAsJsonObject();
238-
return CompletableFuture.completedFuture(jsonObject);
232+
return JsonParser.parseString(buffer.readUtf8()).getAsJsonObject();
239233
} catch(IOException e) {
240234
ClientException clientException = new ClientException(ErrorConstants.Messages.UNABLE_TO_DESERIALIZE_CONTENT, e);
241-
CompletableFuture<JsonObject> exception = new CompletableFuture<>();
242-
exception.completeExceptionally(clientException);
243-
return exception;
235+
throw new RuntimeException(clientException);
244236
}
245237
}
246-
private CompletableFuture<String> getRawRequestContent(RequestBody requestBody) {
238+
private String getRawRequestContent(RequestBody requestBody) {
247239
try{
248240
Buffer buffer = new Buffer();
249241
requestBody.writeTo(buffer);
250-
return CompletableFuture.completedFuture(buffer.readUtf8());
242+
return buffer.readUtf8();
251243
} catch(IOException e) {
252244
ClientException clientException = new ClientException(ErrorConstants.Messages.UNABLE_TO_DESERIALIZE_CONTENT, e);
253-
CompletableFuture<String> exception = new CompletableFuture<>();
254-
exception.completeExceptionally(clientException);
255-
return exception;
245+
throw new RuntimeException(clientException);
256246
}
257247
}
258248
private boolean containsCorrespondingRequestId(List<String> dependsOn) {

src/main/java/com/microsoft/graph/content/BatchRequestContentCollection.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import jakarta.annotation.Nonnull;
1111
import java.util.*;
12-
import java.util.concurrent.CompletableFuture;
1312

1413
/**
1514
* A collection of batch requests
@@ -58,7 +57,7 @@ public String addBatchRequestStep(@Nonnull Request request) {
5857
* @return the id of the request in the batch.
5958
*/
6059
@Nonnull
61-
public CompletableFuture<String> addBatchRequestStepAsync(@Nonnull RequestInformation requestInformation) {
60+
public String addBatchRequestStep(@Nonnull RequestInformation requestInformation) {
6261
setupCurrentRequest();
6362
return currentBatchRequest.addBatchRequestStep(requestInformation);
6463
}

src/main/java/com/microsoft/graph/content/BatchResponseContent.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.io.*;
1616
import java.nio.charset.StandardCharsets;
1717
import java.util.*;
18-
import java.util.concurrent.CompletableFuture;
18+
1919

2020
/**
2121
* A class representing the content of a batch request response.
@@ -49,9 +49,9 @@ public BatchResponseContent(@Nonnull Response batchResponse, @Nullable Map<Strin
4949
* @return The responses of the batch request.
5050
*/
5151
@Nonnull
52-
public CompletableFuture<HashMap<String, Response>> getResponses() {
52+
public HashMap<String, Response> getResponses() {
5353
HashMap<String, Response> responses = new HashMap<>();
54-
jsonBatchResponseObject = jsonBatchResponseObject != null ? jsonBatchResponseObject : getBatchResponseContent().join();
54+
jsonBatchResponseObject = jsonBatchResponseObject != null ? jsonBatchResponseObject : getBatchResponseContent();
5555
if (jsonBatchResponseObject != null) {
5656
JsonElement responsesElement = jsonBatchResponseObject.get(CoreConstants.BatchRequest.RESPONSES);
5757
if (responsesElement != null && responsesElement.isJsonArray()) { //ensure "responses" is not null and is an array.
@@ -61,16 +61,16 @@ public CompletableFuture<HashMap<String, Response>> getResponses() {
6161
}
6262
}
6363
}
64-
return CompletableFuture.completedFuture(responses);
64+
return responses;
6565
}
6666
/**
6767
* Gets the status codes of the responses of the batch request.
6868
* @return The status codes of the responses of the batch request.
6969
*/
7070
@Nonnull
71-
public CompletableFuture<HashMap<String, Integer>> getResponsesStatusCode() {
71+
public HashMap<String, Integer> getResponsesStatusCode() {
7272
HashMap<String, Integer> statusCodes = new HashMap<>();
73-
jsonBatchResponseObject = jsonBatchResponseObject != null ? jsonBatchResponseObject : getBatchResponseContent().join();
73+
jsonBatchResponseObject = jsonBatchResponseObject != null ? jsonBatchResponseObject : getBatchResponseContent();
7474
if (jsonBatchResponseObject != null) {
7575
JsonElement responsesElement = jsonBatchResponseObject.get(CoreConstants.BatchRequest.RESPONSES);
7676
if (responsesElement != null && responsesElement.isJsonArray()) { //ensure "responses" is not null and is an array.
@@ -80,31 +80,31 @@ public CompletableFuture<HashMap<String, Integer>> getResponsesStatusCode() {
8080
}
8181
}
8282
}
83-
return CompletableFuture.completedFuture(statusCodes);
83+
return statusCodes;
8484
}
8585
/**
8686
* Gets the response within the batch response via specified id.
8787
* @param requestId The id of the request.
8888
* @return The response within the batch response via specified id, null if not found.
8989
*/
9090
@Nullable
91-
public CompletableFuture<Response> getResponseById(@Nonnull String requestId) {
91+
public Response getResponseById(@Nonnull String requestId) {
9292
Objects.requireNonNull(requestId);
9393
if(!requestId.isEmpty()) {
94-
jsonBatchResponseObject = jsonBatchResponseObject != null ? jsonBatchResponseObject : getBatchResponseContent().join();
94+
jsonBatchResponseObject = jsonBatchResponseObject != null ? jsonBatchResponseObject : getBatchResponseContent();
9595
if (jsonBatchResponseObject != null) {
9696
JsonElement responsesElement = jsonBatchResponseObject.get(CoreConstants.BatchRequest.RESPONSES);
9797
if (responsesElement != null && responsesElement.isJsonArray()) { //ensure "responses" is not null and is an array.
9898
JsonArray responsesArray = responsesElement.getAsJsonArray();
9999
for (JsonElement responseElement : responsesArray) {
100100
if (responseElement.getAsJsonObject().get("id").getAsString().equals(requestId)) {
101-
return CompletableFuture.completedFuture(getResponseFromJsonObject(responseElement));
101+
return getResponseFromJsonObject(responseElement);
102102
}
103103
}
104104
}
105105
}
106106
}
107-
return CompletableFuture.completedFuture(null);
107+
return null;
108108
}
109109
/**
110110
* Gets the response within the batch response via specified id.
@@ -114,12 +114,12 @@ public CompletableFuture<Response> getResponseById(@Nonnull String requestId) {
114114
* @param <T> The type of the response body.
115115
*/
116116
@Nullable
117-
public <T extends Parsable> CompletableFuture<T> getResponseById(@Nonnull String requestId, @Nonnull ResponseHandler responseHandler) {
118-
Response response = getResponseById(requestId).join();
117+
public <T extends Parsable> T getResponseById(@Nonnull String requestId, @Nonnull ResponseHandler responseHandler) {
118+
Response response = getResponseById(requestId);
119119
if(response == null) {
120-
return CompletableFuture.completedFuture(null);
120+
return null;
121121
}
122-
return responseHandler.handleResponseAsync(response, apiErrorMappings);
122+
return responseHandler.handleResponse(response, apiErrorMappings);
123123
}
124124
/**
125125
* Gets the response within the batch response via specified id.
@@ -129,7 +129,7 @@ public <T extends Parsable> CompletableFuture<T> getResponseById(@Nonnull String
129129
* @param <T> The type of the response body.
130130
*/
131131
@Nullable
132-
public <T extends Parsable> CompletableFuture<T> getResponseById(@Nonnull String requestId, @Nonnull ParsableFactory<T> factory) {
132+
public <T extends Parsable> T getResponseById(@Nonnull String requestId, @Nonnull ParsableFactory<T> factory) {
133133
return this.getResponseById(requestId, new ResponseBodyHandler<>(factory));
134134
}
135135
/**
@@ -138,39 +138,39 @@ public <T extends Parsable> CompletableFuture<T> getResponseById(@Nonnull String
138138
* @return The response within the batch response via specified id as an InputStream, null if not found.
139139
*/
140140
@Nullable
141-
public CompletableFuture<InputStream> getResponseStreamById(@Nonnull String requestId) {
142-
Response response = getResponseById(requestId).join();
141+
public InputStream getResponseStreamById(@Nonnull String requestId) {
142+
Response response = getResponseById(requestId);
143143
if(response != null && response.body() != null) {
144144
InputStream in = response.body().byteStream();
145-
return CompletableFuture.completedFuture(in);
145+
return in;
146146
}
147-
return CompletableFuture.completedFuture(null);
147+
return null;
148148
}
149149
/**
150150
* Get the next link of the batch response.
151151
* @return The next link of the batch response.
152152
*/
153153
@Nullable
154-
public CompletableFuture<String> getNextLink() {
155-
jsonBatchResponseObject = jsonBatchResponseObject != null ? jsonBatchResponseObject : getBatchResponseContent().join();
154+
public String getNextLink() {
155+
jsonBatchResponseObject = jsonBatchResponseObject != null ? jsonBatchResponseObject : getBatchResponseContent();
156156
if(jsonBatchResponseObject != null) {
157157
JsonElement nextLinkElement = jsonBatchResponseObject.get(CoreConstants.Serialization.ODATA_NEXT_LINK);
158158
if (nextLinkElement != null && nextLinkElement.isJsonPrimitive()) {
159-
return CompletableFuture.completedFuture(nextLinkElement.getAsString());
159+
return nextLinkElement.getAsString();
160160
}
161161
}
162-
return CompletableFuture.completedFuture(null);
162+
return null;
163163
}
164-
private CompletableFuture<JsonObject> getBatchResponseContent() {
164+
private JsonObject getBatchResponseContent() {
165165
if (this.batchResponse.body() != null && this.batchResponse.body().contentType() != null) {
166166
InputStream in = this.batchResponse.body().byteStream();
167167
try(InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
168-
return CompletableFuture.completedFuture(JsonParser.parseReader(reader).getAsJsonObject());
168+
return JsonParser.parseReader(reader).getAsJsonObject();
169169
} catch (IOException e) {
170-
return CompletableFuture.completedFuture(null);
170+
return null;
171171
}
172172
}
173-
return CompletableFuture.completedFuture(null);
173+
return null;
174174
}
175175
private Response getResponseFromJsonObject(JsonElement responseElement) {
176176
Response.Builder response = new Response.Builder();

src/main/java/com/microsoft/graph/content/BatchResponseContentCollection.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import jakarta.annotation.Nullable;
1010
import java.io.InputStream;
1111
import java.util.*;
12-
import java.util.concurrent.CompletableFuture;
1312

1413
/**
1514
* A collection of BatchResponseContent objects.
@@ -52,10 +51,10 @@ private BatchResponseContent getBatchResponseContaining(@Nonnull String requestI
5251
* @return the response for the request with the given id, null if no response was found.
5352
*/
5453
@Nullable
55-
public CompletableFuture<Response> getResponseByIdAsync(@Nonnull String requestId) {
54+
public Response getResponseByIdAsync(@Nonnull String requestId) {
5655
Objects.requireNonNull(requestId);
5756
BatchResponseContent response = getBatchResponseContaining(requestId);
58-
return response == null ? CompletableFuture.completedFuture(null) : response.getResponseById(requestId);
57+
return response == null ? null : response.getResponseById(requestId);
5958
}
6059
/**
6160
* Gets the response for the request with the given id.
@@ -65,10 +64,10 @@ public CompletableFuture<Response> getResponseByIdAsync(@Nonnull String requestI
6564
* @param <T> the type of the response.
6665
*/
6766
@Nullable
68-
public <T extends Parsable> CompletableFuture<T> getResponseByIdAsync(@Nonnull String requestId, @Nonnull ResponseHandler handler) {
67+
public <T extends Parsable> T getResponseByIdAsync(@Nonnull String requestId, @Nonnull ResponseHandler handler) {
6968
Objects.requireNonNull(requestId);
7069
BatchResponseContent response = getBatchResponseContaining(requestId);
71-
return response == null ? CompletableFuture.completedFuture(null) : response.getResponseById(requestId, handler);
70+
return response == null ? null : response.getResponseById(requestId, handler);
7271
}
7372
/**
7473
* Gets the response for the request with the given id.
@@ -78,32 +77,32 @@ public <T extends Parsable> CompletableFuture<T> getResponseByIdAsync(@Nonnull S
7877
* @param <T> the type of the response.
7978
*/
8079
@Nullable
81-
public <T extends Parsable> CompletableFuture<T> getResponseByIdAsync(@Nonnull String requestId, @Nonnull ParsableFactory<T> factory) {
80+
public <T extends Parsable> T getResponseByIdAsync(@Nonnull String requestId, @Nonnull ParsableFactory<T> factory) {
8281
Objects.requireNonNull(requestId);
8382
BatchResponseContent response = getBatchResponseContaining(requestId);
84-
return response == null ? CompletableFuture.completedFuture(null) : response.getResponseById(requestId, factory);
83+
return response == null ? null : response.getResponseById(requestId, factory);
8584
}
8685
/**
8786
* Gets the response for the request with the given id as a stream.
8887
* @param requestId the id of the request to get the response for.
8988
* @return the response for the request with the given id, null if no response was found.
9089
*/
9190
@Nullable
92-
public CompletableFuture<InputStream> getResponseStreamByIdAsync(@Nonnull String requestId) {
91+
public InputStream getResponseStreamByIdAsync(@Nonnull String requestId) {
9392
BatchResponseContent response = getBatchResponseContaining(requestId);
94-
return response == null ? CompletableFuture.completedFuture(null) : response.getResponseStreamById(requestId);
93+
return response == null ? null : response.getResponseStreamById(requestId);
9594
}
9695
/**
9796
* Gets the response codes for all the requests in the batch.
9897
* @return the response codes for all the requests in the batch.
9998
*/
10099
@Nonnull
101-
public CompletableFuture<HashMap<String, Integer>> getResponsesStatusCodesAsync() {
100+
public HashMap<String, Integer> getResponsesStatusCodesAsync() {
102101
HashMap<String, Integer> statusCodes = new HashMap<>();
103102
for(KeyedBatchResponseContent keyedResponse : batchResponses) {
104-
HashMap<String, Integer> responseStatusCodes = keyedResponse.response.getResponsesStatusCode().join();
103+
HashMap<String, Integer> responseStatusCodes = keyedResponse.response.getResponsesStatusCode();
105104
statusCodes.putAll(responseStatusCodes);
106105
}
107-
return CompletableFuture.completedFuture(statusCodes);
106+
return statusCodes;
108107
}
109108
}

0 commit comments

Comments
 (0)