1
1
package com .microsoft .graph .content ;
2
2
3
3
import java .io .IOException ;
4
- import java .util .Arrays ;
4
+ import java .util .HashSet ;
5
5
import java .util .LinkedHashMap ;
6
6
import java .util .List ;
7
7
import java .util .Map ;
@@ -43,20 +43,14 @@ public class MSBatchRequestContent {
43
43
*
44
44
* @param batchRequestStepsArray List of batch steps for batching
45
45
*/
46
- public MSBatchRequestContent (@ Nonnull final List < MSBatchRequestStep > batchRequestStepsArray ) {
47
- if (batchRequestStepsArray .size () > MAX_NUMBER_OF_REQUESTS )
46
+ public MSBatchRequestContent (@ Nonnull final MSBatchRequestStep ... batchRequestStepsArray ) {
47
+ if (batchRequestStepsArray .length > MAX_NUMBER_OF_REQUESTS )
48
48
throw new IllegalArgumentException ("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS );
49
49
50
50
this .batchRequestStepsHashMap = new LinkedHashMap <>();
51
51
for (final MSBatchRequestStep requestStep : batchRequestStepsArray )
52
- addBatchRequestStep (requestStep );
53
- }
54
-
55
- /**
56
- * Creates empty batch request content
57
- */
58
- public MSBatchRequestContent () {
59
- this .batchRequestStepsHashMap = new LinkedHashMap <>();
52
+ if (requestStep != null )
53
+ addBatchRequestStep (requestStep );
60
54
}
61
55
62
56
/**
@@ -66,6 +60,8 @@ public MSBatchRequestContent() {
66
60
* given
67
61
*/
68
62
public boolean addBatchRequestStep (@ Nonnull final MSBatchRequestStep batchRequestStep ) {
63
+ if (batchRequestStep == null )
64
+ throw new IllegalArgumentException ("batchRequestStep parameter cannot be null" );
69
65
if (batchRequestStepsHashMap .containsKey (batchRequestStep .getRequestId ()) ||
70
66
batchRequestStepsHashMap .size () >= MAX_NUMBER_OF_REQUESTS )
71
67
return false ;
@@ -81,11 +77,13 @@ public boolean addBatchRequestStep(@Nonnull final MSBatchRequestStep batchReques
81
77
*/
82
78
@ Nonnull
83
79
public String addBatchRequestStep (@ Nonnull final Request request , @ Nullable final String ... arrayOfDependsOnIds ) {
80
+ if (request == null )
81
+ throw new IllegalArgumentException ("request parameter cannot be null" );
84
82
String requestId ;
85
83
do {
86
84
requestId = Integer .toString (ThreadLocalRandom .current ().nextInt (1 , Integer .MAX_VALUE ));
87
85
} while (batchRequestStepsHashMap .keySet ().contains (requestId ));
88
- if (addBatchRequestStep (new MSBatchRequestStep (requestId , request , Arrays . asList ( arrayOfDependsOnIds ) )))
86
+ if (addBatchRequestStep (new MSBatchRequestStep (requestId , request , arrayOfDependsOnIds )))
89
87
return requestId ;
90
88
else
91
89
throw new IllegalArgumentException ("unable to add step to batch. Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS );
@@ -103,29 +101,30 @@ public boolean removeBatchRequestStepWithId(@Nonnull final String requestId) {
103
101
batchRequestStepsHashMap .remove (requestId );
104
102
removed = true ;
105
103
for (final Map .Entry <String , MSBatchRequestStep > steps : batchRequestStepsHashMap .entrySet ()) {
106
- if (steps .getValue () != null && steps .getValue ().getArrayOfDependsOnIds () != null ) {
107
- while (steps .getValue ().getArrayOfDependsOnIds ().remove (requestId ))
104
+ if (steps .getValue () != null && steps .getValue ().getDependsOnIds () != null ) {
105
+ while (steps .getValue ().getDependsOnIds ().remove (requestId ))
108
106
;
109
107
}
110
108
}
111
109
}
112
110
return removed ;
113
111
}
114
112
115
- /**
116
- * @return Batch request content's json as String
117
- */
118
- @ Nonnull
119
- public String getBatchRequestContent () {
113
+ private JsonObject getBatchRequestContentAsJson () {
120
114
final JsonObject batchRequestContentMap = new JsonObject ();
121
115
final JsonArray batchContentArray = new JsonArray ();
122
116
for (final Map .Entry <String , MSBatchRequestStep > requestStep : batchRequestStepsHashMap .entrySet ()) {
123
117
batchContentArray .add (getBatchRequestObjectFromRequestStep (requestStep .getValue ()));
124
118
}
125
119
batchRequestContentMap .add ("requests" , batchContentArray );
126
-
127
- final String content = batchRequestContentMap .toString ();
128
- return content ;
120
+ return batchRequestContentMap ;
121
+ }
122
+ /**
123
+ * @return Batch request content's json as String
124
+ */
125
+ @ Nonnull
126
+ public String getBatchRequestContent () {
127
+ return getBatchRequestContentAsJson ().toString ();
129
128
}
130
129
131
130
/**
@@ -135,12 +134,14 @@ public String getBatchRequestContent() {
135
134
* @throws ClientException when the batch couldn't be executed because of client issues.
136
135
*/
137
136
@ Nonnull
138
- public MSBatchResponseContent execute (@ Nonnull final IBaseClient client ) throws ClientException {
139
- try {
140
- return executeAsync (client ).get ();
141
- } catch (Exception ex ) {
142
- throw new ClientException ("Batch failed to execute" , ex );
143
- }
137
+ public MSBatchResponseContent execute (@ Nonnull final IBaseClient client ) {
138
+ final JsonObject content = getBatchRequestContentAsJson ();
139
+ return new MSBatchResponseContent (client .getServiceRoot () + "/" ,
140
+ content ,
141
+ client .customRequest ("/$batch" )
142
+ .buildRequest ()
143
+ .post (content )
144
+ .getAsJsonObject ());
144
145
}
145
146
/**
146
147
* Executes the batch requests asynchronously and returns the response
@@ -152,11 +153,11 @@ public CompletableFuture<MSBatchResponseContent> executeAsync(@Nonnull final IBa
152
153
if (client == null ) {
153
154
throw new IllegalArgumentException ("client parameter cannot be null" );
154
155
}
155
- final String content = getBatchRequestContent ();
156
- return client .customRequest (client . getServiceRoot () + "/$batch" , String . class )
156
+ final JsonObject content = getBatchRequestContentAsJson ();
157
+ return client .customRequest ("/$batch" )
157
158
.buildRequest ()
158
159
.postAsync (content )
159
- .thenApply (resp -> new MSBatchResponseContent (client .getServiceRoot () + "/" , content , resp ));
160
+ .thenApply (resp -> new MSBatchResponseContent (client .getServiceRoot () + "/" , content , resp . getAsJsonObject () ));
160
161
}
161
162
162
163
private static final Pattern protocolAndHostReplacementPattern = Pattern .compile ("(?i)^http[s]?:\\ /\\ /graph\\ .microsoft\\ .com\\ /(?>v1\\ .0|beta)\\ /?" ); // (?i) case insensitive
@@ -180,9 +181,9 @@ private JsonObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep
180
181
contentmap .add ("headers" , headerMap );
181
182
}
182
183
183
- final List <String > arrayOfDependsOnIds = batchRequestStep .getArrayOfDependsOnIds ();
184
+ final HashSet <String > arrayOfDependsOnIds = batchRequestStep .getDependsOnIds ();
184
185
if (arrayOfDependsOnIds != null ) {
185
- final JsonArray array = new JsonArray ();
186
+ final JsonArray array = new JsonArray (arrayOfDependsOnIds . size () );
186
187
for (final String dependsOnId : arrayOfDependsOnIds )
187
188
array .add (dependsOnId );
188
189
contentmap .add ("dependsOn" , array );
0 commit comments