44
44
import com .google .api .client .util .Data ;
45
45
import com .google .api .services .storage .Storage ;
46
46
import com .google .api .services .storage .Storage .Objects .Compose ;
47
+ import com .google .api .services .storage .Storage .Objects .Delete ;
47
48
import com .google .api .services .storage .Storage .Objects .Get ;
48
49
import com .google .api .services .storage .Storage .Objects .Insert ;
49
50
import com .google .api .services .storage .Storage .Objects .Move ;
51
+ import com .google .api .services .storage .Storage .Objects .Patch ;
52
+ import com .google .api .services .storage .StorageRequest ;
50
53
import com .google .api .services .storage .model .Bucket ;
51
54
import com .google .api .services .storage .model .Bucket .RetentionPolicy ;
52
55
import com .google .api .services .storage .model .BucketAccessControl ;
@@ -109,6 +112,7 @@ public class HttpStorageRpc implements StorageRpc {
109
112
// declare this HttpStatus code here as it's not included in java.net.HttpURLConnection
110
113
private static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416 ;
111
114
private static final boolean IS_RECORD_EVENTS = true ;
115
+ private static final String X_GOOG_GCS_IDEMPOTENCY_TOKEN = "x-goog-gcs-idempotency-token" ;
112
116
113
117
private final StorageOptions options ;
114
118
private final Storage storage ;
@@ -208,7 +212,7 @@ public void intercept(HttpRequest request) throws IOException {
208
212
.filter (java .util .Objects ::nonNull )
209
213
.collect (JOINER );
210
214
headers .set ("x-goog-api-client" , newValue );
211
- headers .set ("x-goog-gcs-idempotency-token" , invocationId );
215
+ headers .set (X_GOOG_GCS_IDEMPOTENCY_TOKEN , invocationId );
212
216
213
217
String userAgent = headers .getUserAgent ();
214
218
if ((userAgent == null
@@ -247,7 +251,9 @@ public void addDelete(
247
251
batches .add (storage .batch ());
248
252
currentBatchSize = 0 ;
249
253
}
250
- deleteCall (storageObject , options ).queue (batches .getLast (), toJsonCallback (callback ));
254
+ Delete call = deleteCall (storageObject , options );
255
+ addIdempotencyTokenToCall (call );
256
+ call .queue (batches .getLast (), toJsonCallback (callback ));
251
257
currentBatchSize ++;
252
258
} catch (IOException ex ) {
253
259
throw translate (ex );
@@ -264,7 +270,9 @@ public void addPatch(
264
270
batches .add (storage .batch ());
265
271
currentBatchSize = 0 ;
266
272
}
267
- patchCall (storageObject , options ).queue (batches .getLast (), toJsonCallback (callback ));
273
+ Patch call = patchCall (storageObject , options );
274
+ addIdempotencyTokenToCall (call );
275
+ call .queue (batches .getLast (), toJsonCallback (callback ));
268
276
currentBatchSize ++;
269
277
} catch (IOException ex ) {
270
278
throw translate (ex );
@@ -281,7 +289,9 @@ public void addGet(
281
289
batches .add (storage .batch ());
282
290
currentBatchSize = 0 ;
283
291
}
284
- getCall (storageObject , options ).queue (batches .getLast (), toJsonCallback (callback ));
292
+ Get call = getCall (storageObject , options );
293
+ addIdempotencyTokenToCall (call );
294
+ call .queue (batches .getLast (), toJsonCallback (callback ));
285
295
currentBatchSize ++;
286
296
} catch (IOException ex ) {
287
297
throw translate (ex );
@@ -310,6 +320,12 @@ public void submit() {
310
320
span .end (HttpStorageRpcSpans .END_SPAN_OPTIONS );
311
321
}
312
322
}
323
+
324
+ private void addIdempotencyTokenToCall (StorageRequest <?> call ) {
325
+ HttpRpcContext instance = HttpRpcContext .getInstance ();
326
+ call .getRequestHeaders ().set (X_GOOG_GCS_IDEMPOTENCY_TOKEN , instance .newInvocationId ());
327
+ instance .clearInvocationId ();
328
+ }
313
329
}
314
330
315
331
private static <T > JsonBatchCallback <T > toJsonCallback (final RpcBatch .Callback <T > callback ) {
0 commit comments