-
Notifications
You must be signed in to change notification settings - Fork 70
/
UnifiedOpts.java
2550 lines (2156 loc) · 81.9 KB
/
UnifiedOpts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.storage;
import static com.google.cloud.storage.Utils.projectNameCodec;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Predicates.not;
import static java.util.Objects.requireNonNull;
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.cloud.storage.Conversions.Decoder;
import com.google.cloud.storage.Storage.BlobField;
import com.google.cloud.storage.Storage.BucketField;
import com.google.cloud.storage.spi.v1.StorageRpc;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.protobuf.ByteString;
import com.google.protobuf.FieldMask;
import com.google.storage.v2.CommonObjectRequestParams;
import com.google.storage.v2.ComposeObjectRequest;
import com.google.storage.v2.CreateBucketRequest;
import com.google.storage.v2.CreateHmacKeyRequest;
import com.google.storage.v2.DeleteBucketRequest;
import com.google.storage.v2.DeleteHmacKeyRequest;
import com.google.storage.v2.DeleteObjectRequest;
import com.google.storage.v2.GetBucketRequest;
import com.google.storage.v2.GetHmacKeyRequest;
import com.google.storage.v2.GetObjectRequest;
import com.google.storage.v2.ListBucketsRequest;
import com.google.storage.v2.ListHmacKeysRequest;
import com.google.storage.v2.ListObjectsRequest;
import com.google.storage.v2.LockBucketRetentionPolicyRequest;
import com.google.storage.v2.ReadObjectRequest;
import com.google.storage.v2.RewriteObjectRequest;
import com.google.storage.v2.UpdateBucketRequest;
import com.google.storage.v2.UpdateHmacKeyRequest;
import com.google.storage.v2.UpdateObjectRequest;
import com.google.storage.v2.WriteObjectRequest;
import java.io.Serializable;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.security.Key;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.crypto.spec.SecretKeySpec;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.RequiresNonNull;
/**
* The set of all "Options" we currently support for per-call parameters.
*
* <p>Most often, each of the respective types correspond to one of the parameters from <a
* target="_blank" rel="noopener noreferrer"
* href="https://cloud.google.com/storage/docs/json_api/v1/parameters">HTTP headers and common query
* string parameters for JSON</a>. In the case of gRPC, sometimes the parameters are in the specific
* request message or in grpc metadata.
*/
@SuppressWarnings({"deprecation", "DeprecatedIsStillUsed"})
final class UnifiedOpts {
/** Base interface type for each of the new options we're supporting. */
interface Opt extends Serializable {}
/**
* A specialization of {@link java.util.function.UnaryOperator} which maintains its lower type for
* {@link #identity()} and {@link #andThen(Mapper)}.
*/
@FunctionalInterface
interface Mapper<T> {
T apply(T t);
default Mapper<T> andThen(Mapper<T> then) {
return t -> then.apply(apply(t));
}
static <T> Mapper<T> identity() {
return t -> t;
}
}
/** Base, marker interface of those Opts which represent a get/read/origin type relationship. */
private interface SourceOpt extends Opt {}
/**
* Base, marker interface of those Opts which represent a set/write/destination type relationship.
*/
private interface TargetOpt extends Opt {}
/** Base, marker interface of those Opts which apply to listing operations. */
private interface ListOpt extends Opt {}
/** Marker interface of those Opts which are applicable to Bucket operations. */
private interface ApplicableBucket {}
/** Marker interface of those Opts which are applicable to Object/Blob operations. */
private interface ApplicableObject {}
/** Marker interface of those Opts which are applicable to HmacKey operations. */
private interface ApplicableHmacKey {}
/** Base interface for those Opts which may expose their values via gRPC Metadata */
private interface GrpcMetadataMapper {
default Mapper<GrpcCallContext> getGrpcMetadataMapper() {
return Mapper.identity();
}
}
/** Base interface for those Opts which are applicable to Object List operations */
interface ObjectListOpt extends GrpcMetadataMapper, ListOpt, ApplicableObject {
default Mapper<ListObjectsRequest.Builder> listObjects() {
return Mapper.identity();
}
}
/**
* Base interface for those Opts which are applicable to Object Source (get/read/origin
* relationship) operations
*/
interface ObjectSourceOpt extends GrpcMetadataMapper, SourceOpt, ApplicableObject {
default Mapper<ReadObjectRequest.Builder> readObject() {
return Mapper.identity();
}
default Mapper<GetObjectRequest.Builder> getObject() {
return Mapper.identity();
}
default Mapper<RewriteObjectRequest.Builder> rewriteObject() {
return Mapper.identity();
}
}
/**
* Base interface for those Opts which are applicable to Object Target (set/write/destination
* relationship) operations
*/
interface ObjectTargetOpt extends GrpcMetadataMapper, TargetOpt, ApplicableObject {
default Mapper<BlobInfo.Builder> blobInfo() {
return Mapper.identity();
}
default Mapper<WriteObjectRequest.Builder> writeObject() {
return Mapper.identity();
}
default Mapper<UpdateObjectRequest.Builder> updateObject() {
return Mapper.identity();
}
default Mapper<DeleteObjectRequest.Builder> deleteObject() {
return Mapper.identity();
}
default Mapper<ComposeObjectRequest.Builder> composeObject() {
return Mapper.identity();
}
default Mapper<RewriteObjectRequest.Builder> rewriteObject() {
return Mapper.identity();
}
}
/** Base interface for those Opts which are applicable to Bucket List operations */
interface BucketListOpt extends GrpcMetadataMapper, ListOpt, ApplicableBucket {
default Mapper<ListBucketsRequest.Builder> listBuckets() {
return Mapper.identity();
}
}
/**
* Base interface for those Opts which are applicable to Bucket Source (get/read/origin
* relationship) operations
*/
interface BucketSourceOpt extends GrpcMetadataMapper, SourceOpt, ApplicableBucket {
default Mapper<GetBucketRequest.Builder> getBucket() {
return Mapper.identity();
}
default Mapper<GetIamPolicyRequest.Builder> getIamPolicy() {
return Mapper.identity();
}
}
/**
* Base interface for those Opts which are applicable to Bucket Target (set/write/destination
* relationship) operations
*/
interface BucketTargetOpt extends GrpcMetadataMapper, TargetOpt, ApplicableBucket {
default Mapper<CreateBucketRequest.Builder> createBucket() {
return Mapper.identity();
}
default Mapper<UpdateBucketRequest.Builder> updateBucket() {
return Mapper.identity();
}
default Mapper<DeleteBucketRequest.Builder> deleteBucket() {
return Mapper.identity();
}
default Mapper<LockBucketRetentionPolicyRequest.Builder> lockBucketRetentionPolicy() {
return Mapper.identity();
}
}
/** Base interface for those Opts which are applicable to HmacKey List operations */
interface HmacKeyListOpt extends GrpcMetadataMapper, ListOpt, ApplicableHmacKey {
default Mapper<ListHmacKeysRequest.Builder> listHmacKeys() {
return Mapper.identity();
}
}
/**
* Base interface for those Opts which are applicable to HmacKey Source (get/read/origin
* relationship) operations
*/
interface HmacKeySourceOpt extends GrpcMetadataMapper, SourceOpt, ApplicableHmacKey {
default Mapper<GetHmacKeyRequest.Builder> getHmacKey() {
return Mapper.identity();
}
}
/**
* Base interface for those Opts which are applicable to HmacKey Target (set/write/destination
* relationship) operations
*/
interface HmacKeyTargetOpt extends GrpcMetadataMapper, TargetOpt, ApplicableHmacKey {
default Mapper<CreateHmacKeyRequest.Builder> createHmacKey() {
return Mapper.identity();
}
default Mapper<UpdateHmacKeyRequest.Builder> updateHmacKey() {
return Mapper.identity();
}
default Mapper<DeleteHmacKeyRequest.Builder> deleteHmacKey() {
return Mapper.identity();
}
}
/**
* Some Options have a corresponding "SOURCE" version, this interface provide a construct for
* accessing an projecting those Opts which can be turned into a "SOURCE" version.
*/
interface ProjectAsSource<O extends Opt> {
O asSource();
}
/**
* This class extends off {@link ObjectSourceOpt} and {@link ObjectTargetOpt} in order to satisfy
* some the shimming constraints of the subclasses of {@link OptionShim}.
*
* <p>All the methods from these parent interfaces will NEVER be called, and are stubbed simply to
* satisfy the need for them to be declared. They are stubbed to use identity methods so that if
* they somehow do ever leak through and are called they won't cause issue for customers.
*
* <p>If/when we're able to remove all the {@link Option} classes, this interface should be
* refactored to remove the inheritance, instead providing an explicit pre-processing phase to opt
* resolution.
*/
interface ObjectOptExtractor<O extends Opt> extends Opt, ObjectSourceOpt, ObjectTargetOpt {
O extractFromBlobInfo(BlobInfo info);
O extractFromBlobId(BlobId id);
@Override
default Mapper<GrpcCallContext> getGrpcMetadataMapper() {
return Mapper.identity();
}
@Override
default Mapper<GetObjectRequest.Builder> getObject() {
return Mapper.identity();
}
@Override
default Mapper<RewriteObjectRequest.Builder> rewriteObject() {
return Mapper.identity();
}
}
/**
* This class extends off {@link ObjectSourceOpt} and {@link ObjectTargetOpt} in order to satisfy
* some the shimming constraints of the subclasses of {@link OptionShim}.
*
* <p>All the methods from these parent interfaces will NEVER be called, and are stubbed simply to
* satisfy the need for them to be declared. They are stubbed to use identity methods so that if
* they somehow do ever leak through and are called they won't cause issue for customers.
*
* <p>If/when we're able to remove all the {@link Option} classes, this interface should be
* refactored to remove the inheritance, instead providing an explicit pre-processing phase to opt
* resolution.
*/
interface BucketOptExtractor<O extends Opt> extends Opt, BucketSourceOpt, BucketTargetOpt {
O extractFromBucketInfo(BucketInfo info);
@Override
default Mapper<GrpcCallContext> getGrpcMetadataMapper() {
return Mapper.identity();
}
}
/* --
Factory methods for each of the supported Opts, along with some of their requisite
compatibility overloads
-- */
static Crc32cMatch crc32cMatch(int i) {
return new Crc32cMatch(i);
}
static Crc32cMatch crc32cMatch(@NonNull String crc32c) {
requireNonNull(crc32c, "crc32c must be non null");
return new Crc32cMatch(Utils.crc32cCodec.decode(crc32c));
}
static Delimiter currentDirectory() {
return new Delimiter("/");
}
static DecryptionKey decryptionKey(@NonNull String decryptionKey) {
requireNonNull(decryptionKey, "decryptionKey must be non null");
return new DecryptionKey(
new SecretKeySpec(BaseEncoding.base64().decode(decryptionKey), "AES256"));
}
@RequiresNonNull({"decryptionKey", "#1.getEncoded()", "#1.getAlgorithm()"})
static DecryptionKey decryptionKey(@NonNull Key decryptionKey) {
requireNonNull(decryptionKey, "decryptionKey must be non null");
requireNonNull(decryptionKey.getEncoded(), "decryptionKey.getEncoded() must be non null");
requireNonNull(decryptionKey.getAlgorithm(), "decryptionKey.getAlgorithm() must be non null");
return new DecryptionKey(decryptionKey);
}
static Delimiter delimiter(@NonNull String delimiter) {
requireNonNull(delimiter, "delimiter must be non null");
return new Delimiter(delimiter);
}
@Deprecated
static DetectContentType detectContentType() {
return DetectContentType.INSTANCE;
}
static DisableGzipContent disableGzipContent() {
return new DisableGzipContent(true);
}
static GenerationMatch doesNotExist() {
return new GenerationMatch(0);
}
static EncryptionKey encryptionKey(@NonNull String encryptionKey) {
requireNonNull(encryptionKey, "encryptionKey must be non null");
return new EncryptionKey(
new SecretKeySpec(BaseEncoding.base64().decode(encryptionKey), "AES256"));
}
static EncryptionKey encryptionKey(@NonNull Key encryptionKey) {
requireNonNull(encryptionKey, "encryptionKey must be non null");
return new EncryptionKey(encryptionKey);
}
static EndOffset endOffset(@NonNull String endOffset) {
requireNonNull(endOffset, "endOffset must be non null");
return new EndOffset(endOffset);
}
static Fields fields(@NonNull ImmutableSet<NamedField> fields) {
requireNonNull(fields, "fields must be non null");
return new Fields(fields);
}
static GenerationMatch generationMatch(long l) {
return new GenerationMatch(l);
}
static GenerationNotMatch generationNotMatch(long l) {
return new GenerationNotMatch(l);
}
static KmsKeyName kmsKeyName(@NonNull String kmsKeyName) {
requireNonNull(kmsKeyName, "kmsKeyName must be non null");
return new KmsKeyName(kmsKeyName);
}
static MatchGlob matchGlob(@NonNull String glob) {
requireNonNull(glob, "glob must be non null");
return new MatchGlob(glob);
}
static Md5Match md5Match(@NonNull String md5) {
requireNonNull(md5, "md5 must be non null");
return new Md5Match(md5);
}
static MetagenerationMatch metagenerationMatch(long l) {
return new MetagenerationMatch(l);
}
static MetagenerationNotMatch metagenerationNotMatch(long l) {
return new MetagenerationNotMatch(l);
}
static PageSize pageSize(long l) {
return new PageSize(l);
}
static PageToken pageToken(@NonNull String pageToken) {
requireNonNull(pageToken, "pageToken must be non null");
return new PageToken(pageToken);
}
static PredefinedAcl predefinedAcl(Storage.@NonNull PredefinedAcl predefinedAcl) {
requireNonNull(predefinedAcl, "predefinedAcl must be non null");
return new PredefinedAcl(predefinedAcl.getEntry());
}
static PredefinedDefaultObjectAcl predefinedDefaultObjectAcl(
Storage.@NonNull PredefinedAcl predefinedAcl) {
requireNonNull(predefinedAcl, "predefinedAcl must be non null");
return new PredefinedDefaultObjectAcl(predefinedAcl.getEntry());
}
static Prefix prefix(@NonNull String prefix) {
requireNonNull(prefix, "prefix must be non null");
return new Prefix(prefix);
}
static ProjectId projectId(@NonNull String projectId) {
requireNonNull(projectId, "projectId must be non null");
return new ProjectId(projectId);
}
static Projection projection(@NonNull String projection) {
requireNonNull(projection, "projection must be non null");
return new Projection(projection);
}
static RequestedPolicyVersion requestedPolicyVersion(long l) {
return new RequestedPolicyVersion(l);
}
static ReturnRawInputStream returnRawInputStream(boolean b) {
return new ReturnRawInputStream(b);
}
@RequiresNonNull({"serviceAccount", "#1.getEmail()"})
static ServiceAccount serviceAccount(
com.google.cloud.storage.@NonNull ServiceAccount serviceAccount) {
requireNonNull(serviceAccount, "serviceAccount must be non null");
requireNonNull(serviceAccount.getEmail(), "serviceAccount.getEmail() must be non null");
return new ServiceAccount(serviceAccount.getEmail());
}
@VisibleForTesting
static SetContentType setContentType(@NonNull String s) {
requireNonNull(s, "s must be non null");
return new SetContentType(s);
}
static ShowDeletedKeys showDeletedKeys(boolean b) {
return new ShowDeletedKeys(b);
}
static StartOffset startOffset(@NonNull String startOffset) {
requireNonNull(startOffset, "startOffset must be non null");
return new StartOffset(startOffset);
}
static UserProject userProject(@NonNull String userProject) {
requireNonNull(userProject, "userProject must be non null");
return new UserProject(userProject);
}
static VersionsFilter versionsFilter(boolean b) {
return new VersionsFilter(b);
}
@Deprecated
static GenerationMatchExtractor generationMatchExtractor() {
return GenerationMatchExtractor.INSTANCE;
}
@Deprecated
static GenerationNotMatchExtractor generationNotMatchExtractor() {
return GenerationNotMatchExtractor.INSTANCE;
}
@Deprecated
static MetagenerationMatchExtractor metagenerationMatchExtractor() {
return MetagenerationMatchExtractor.INSTANCE;
}
@Deprecated
static MetagenerationNotMatchExtractor metagenerationNotMatchExtractor() {
return MetagenerationNotMatchExtractor.INSTANCE;
}
@Deprecated
static Crc32cMatchExtractor crc32cMatchExtractor() {
return Crc32cMatchExtractor.INSTANCE;
}
@Deprecated
static Md5MatchExtractor md5MatchExtractor() {
return Md5MatchExtractor.INSTANCE;
}
static final class Crc32cMatch implements ObjectTargetOpt {
private static final long serialVersionUID = 8172282701777561769L;
private final int val;
private Crc32cMatch(int val) {
this.val = val;
}
@Override
public Mapper<BlobInfo.Builder> blobInfo() {
return b -> b.setCrc32c(Utils.crc32cCodec.encode(val));
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Crc32cMatch)) {
return false;
}
Crc32cMatch that = (Crc32cMatch) o;
return Objects.equals(val, that.val);
}
@Override
public Mapper<WriteObjectRequest.Builder> writeObject() {
return b -> {
b.getObjectChecksumsBuilder().setCrc32C(val);
return b;
};
}
@Override
public int hashCode() {
return Objects.hash(val);
}
@Override
public String toString() {
return "Crc32cMatch{val='" + val + "'}";
}
}
/** @see EncryptionKey */
static final class DecryptionKey extends RpcOptVal<Key> implements ObjectSourceOpt {
private static final long serialVersionUID = -2198422155991275316L;
private DecryptionKey(Key val) {
super(StorageRpc.Option.CUSTOMER_SUPPLIED_KEY, val);
}
@Override
public Mapper<ImmutableMap.Builder<StorageRpc.Option, Object>> mapper() {
return b ->
b.put(
StorageRpc.Option.CUSTOMER_SUPPLIED_KEY,
BaseEncoding.base64().encode(val.getEncoded()));
}
@Override
public Mapper<ReadObjectRequest.Builder> readObject() {
return b -> {
customerSuppliedKey(b.getCommonObjectRequestParamsBuilder(), val);
return b;
};
}
@Override
public Mapper<GetObjectRequest.Builder> getObject() {
return b -> {
customerSuppliedKey(b.getCommonObjectRequestParamsBuilder(), val);
return b;
};
}
@Override
public Mapper<RewriteObjectRequest.Builder> rewriteObject() {
return b -> {
CommonObjectRequestParams.Builder builder =
customerSuppliedKey(CommonObjectRequestParams.newBuilder(), val);
return b.setCopySourceEncryptionAlgorithm(builder.getEncryptionAlgorithm())
.setCopySourceEncryptionKeyBytes(builder.getEncryptionKeyBytes())
.setCopySourceEncryptionKeySha256Bytes(builder.getEncryptionKeySha256Bytes());
};
}
}
static final class Delimiter extends RpcOptVal<String> implements ObjectListOpt {
private static final long serialVersionUID = -3789556789947615714L;
private Delimiter(String val) {
super(StorageRpc.Option.DELIMITER, val);
}
@Override
public Mapper<ListObjectsRequest.Builder> listObjects() {
return b -> b.setDelimiter(val);
}
}
static final class DisableGzipContent extends RpcOptVal<@NonNull Boolean>
implements ObjectTargetOpt {
private static final long serialVersionUID = 7445066765944965549L;
private DisableGzipContent(boolean val) {
super(StorageRpc.Option.IF_DISABLE_GZIP_CONTENT, val);
}
}
/** @see DecryptionKey */
static final class EncryptionKey extends RpcOptVal<Key> implements ObjectTargetOpt {
private static final long serialVersionUID = -7335988656032764620L;
private EncryptionKey(Key val) {
super(StorageRpc.Option.CUSTOMER_SUPPLIED_KEY, val);
}
@Override
public Mapper<ImmutableMap.Builder<StorageRpc.Option, Object>> mapper() {
return b ->
b.put(
StorageRpc.Option.CUSTOMER_SUPPLIED_KEY,
BaseEncoding.base64().encode(val.getEncoded()));
}
@Override
public Mapper<WriteObjectRequest.Builder> writeObject() {
return b -> {
customerSuppliedKey(b.getCommonObjectRequestParamsBuilder(), val);
return b;
};
}
@Override
public Mapper<UpdateObjectRequest.Builder> updateObject() {
return b -> {
customerSuppliedKey(b.getCommonObjectRequestParamsBuilder(), val);
return b;
};
}
@Override
public Mapper<DeleteObjectRequest.Builder> deleteObject() {
return b -> {
customerSuppliedKey(b.getCommonObjectRequestParamsBuilder(), val);
return b;
};
}
@Override
public Mapper<ComposeObjectRequest.Builder> composeObject() {
return b -> {
customerSuppliedKey(b.getCommonObjectRequestParamsBuilder(), val);
return b;
};
}
@Override
public Mapper<RewriteObjectRequest.Builder> rewriteObject() {
return b -> {
customerSuppliedKey(b.getCommonObjectRequestParamsBuilder(), val);
return b;
};
}
}
/** @see StartOffset */
static final class EndOffset extends RpcOptVal<String> implements ObjectListOpt {
private static final long serialVersionUID = 7446382028145458833L;
private EndOffset(String val) {
super(StorageRpc.Option.END_OFF_SET, val);
}
@Override
public Mapper<ListObjectsRequest.Builder> listObjects() {
return b -> b.setLexicographicEnd(val);
}
}
static final class Fields extends RpcOptVal<ImmutableSet<NamedField>>
implements ObjectSourceOpt,
ObjectListOpt,
ObjectTargetOpt,
BucketSourceOpt,
BucketTargetOpt,
BucketListOpt {
/**
* Apiary and gRPC have differing handling of where the field selector is evaluated relative to
* the request. For Apiary, it's from the root response document; for gRPC it's from the
* collection of results.
*
* <p>In our current case, this means we exclude some fields when we know it is being consumed
* for a gRPC message. Unfortunately, we don't know if the constructed Fields instance is for
* use with gRPC when it is instantiated so we must define here.
*/
private static final ImmutableSet<String> grpcExcludedFields =
ImmutableSet.of("nextPageToken", "prefixes", "selfLink", "mediaLink", "kind", "id");
private static final long serialVersionUID = 3286889410148272195L;
private Fields(ImmutableSet<NamedField> val) {
super(StorageRpc.Option.FIELDS, val);
}
@Override
public Mapper<ImmutableMap.Builder<StorageRpc.Option, Object>> mapper() {
return b -> {
String collect =
val.stream().map(NamedField::getApiaryName).collect(Collectors.joining(","));
return b.put(StorageRpc.Option.FIELDS, collect);
};
}
@Override
public Mapper<GetBucketRequest.Builder> getBucket() {
return b -> b.setReadMask(FieldMask.newBuilder().addAllPaths(getPaths()).build());
}
@Override
public Mapper<ListBucketsRequest.Builder> listBuckets() {
return b -> b.setReadMask(FieldMask.newBuilder().addAllPaths(getPaths()).build());
}
@Override
public Mapper<UpdateBucketRequest.Builder> updateBucket() {
return b -> b.setUpdateMask(FieldMask.newBuilder().addAllPaths(getPaths()).build());
}
@Override
public Mapper<GetObjectRequest.Builder> getObject() {
return b -> b.setReadMask(FieldMask.newBuilder().addAllPaths(getPaths()).build());
}
@Override
public Mapper<ListObjectsRequest.Builder> listObjects() {
return b -> b.setReadMask(FieldMask.newBuilder().addAllPaths(getPaths()).build());
}
@Override
public Mapper<ReadObjectRequest.Builder> readObject() {
return b -> b.setReadMask(FieldMask.newBuilder().addAllPaths(getPaths()).build());
}
@Override
public Mapper<UpdateObjectRequest.Builder> updateObject() {
return b -> b.setUpdateMask(FieldMask.newBuilder().addAllPaths(getPaths()).build());
}
@Override
public Mapper<RewriteObjectRequest.Builder> rewriteObject() {
return Mapper.identity();
}
/**
* Define a decoder which can clear out any fields which may have not been selected.
*
* <p>This approach, isn't ideal at the backside after decoding has already taken place.
* However, refactoring the whole model pipeline for both json and grpc is going to be a large
* change.
*/
Decoder<Blob, Blob> clearUnselectedBlobFields() {
return b -> {
if (val.isEmpty()) {
return b;
} else {
Set<String> names = getPaths();
Blob.Builder bldr = b.toBuilder();
blobInfoFieldClearers.entrySet().stream()
.filter(e -> !names.contains(e.getKey()))
.map(Entry::getValue)
.forEach(m -> m.apply(bldr));
return bldr.build();
}
};
}
/**
* Define a decoder which can clear out any fields which may have not been selected.
*
* <p>This approach, isn't ideal at the backside after decoding has already taken place.
* However, refactoring the whole model pipeline for both json and grpc is going to be a large
* change.
*/
Decoder<Bucket, Bucket> clearUnselectedBucketFields() {
return b -> {
if (val.isEmpty()) {
return b;
} else {
Set<String> names = getPaths();
Bucket.Builder bldr = b.toBuilder();
bucketInfoFieldClearers.entrySet().stream()
.filter(e -> !names.contains(e.getKey()))
.map(Entry::getValue)
.forEach(m -> m.apply(bldr));
return bldr.build();
}
};
}
private Set<String> getPaths() {
//noinspection Guava
return val.stream()
.map(NamedField::stripPrefix)
.map(NamedField::getGrpcName)
.filter(not(grpcExcludedFields::contains))
.collect(Collectors.toSet());
}
// It'd be preferable to define these clearing mappers in the fields themselves, however today
// the fields are enums and require interfaces in order to extend anything which in turn makes
// things public.
//
// To avoid putting more things on the public api that will hopefully take a different form
// in the medium term, we define them here.
private static final ImmutableMap<String, Mapper<BlobInfo.Builder>> blobInfoFieldClearers =
ImmutableMap.<String, Mapper<BlobInfo.Builder>>builder()
.put(BlobField.ACL.getGrpcName(), BlobInfo.Builder::clearAcl)
.put(BlobField.CACHE_CONTROL.getGrpcName(), BlobInfo.Builder::clearCacheControl)
.put(BlobField.COMPONENT_COUNT.getGrpcName(), BlobInfo.Builder::clearComponentCount)
.put(
BlobField.CONTENT_DISPOSITION.getGrpcName(),
BlobInfo.Builder::clearContentDisposition)
.put(BlobField.CONTENT_ENCODING.getGrpcName(), BlobInfo.Builder::clearContentEncoding)
.put(BlobField.CONTENT_LANGUAGE.getGrpcName(), BlobInfo.Builder::clearContentLanguage)
.put(BlobField.CONTENT_TYPE.getGrpcName(), BlobInfo.Builder::clearContentType)
.put(BlobField.CRC32C.getGrpcName(), BlobInfo.Builder::clearCrc32c)
.put(
BlobField.CUSTOMER_ENCRYPTION.getGrpcName(),
BlobInfo.Builder::clearCustomerEncryption)
.put(BlobField.CUSTOM_TIME.getGrpcName(), BlobInfo.Builder::clearCustomTime)
.put(BlobField.ETAG.getGrpcName(), BlobInfo.Builder::clearEtag)
.put(BlobField.EVENT_BASED_HOLD.getGrpcName(), BlobInfo.Builder::clearEventBasedHold)
.put(
BlobField.GENERATION.getGrpcName(),
b -> {
BlobId current = b.getBlobId();
return b.setBlobId(BlobId.of(current.getBucket(), current.getName()));
})
.put(BlobField.ID.getGrpcName(), BlobInfo.Builder::clearGeneratedId)
.put(BlobField.KMS_KEY_NAME.getGrpcName(), BlobInfo.Builder::clearKmsKeyName)
.put(BlobField.MD5HASH.getGrpcName(), BlobInfo.Builder::clearMd5)
.put(BlobField.MEDIA_LINK.getGrpcName(), BlobInfo.Builder::clearMediaLink)
.put(BlobField.METADATA.getGrpcName(), BlobInfo.Builder::clearMetadata)
.put(BlobField.METAGENERATION.getGrpcName(), BlobInfo.Builder::clearMetageneration)
.put(BlobField.OWNER.getGrpcName(), BlobInfo.Builder::clearOwner)
.put(
BlobField.RETENTION_EXPIRATION_TIME.getGrpcName(),
BlobInfo.Builder::clearRetentionExpirationTime)
.put(BlobField.SELF_LINK.getGrpcName(), BlobInfo.Builder::clearSelfLink)
.put(BlobField.SIZE.getGrpcName(), BlobInfo.Builder::clearSize)
.put(BlobField.STORAGE_CLASS.getGrpcName(), BlobInfo.Builder::clearStorageClass)
.put(BlobField.TEMPORARY_HOLD.getGrpcName(), BlobInfo.Builder::clearTemporaryHold)
.put(BlobField.TIME_CREATED.getGrpcName(), BlobInfo.Builder::clearCreateTime)
.put(BlobField.TIME_DELETED.getGrpcName(), BlobInfo.Builder::clearDeleteTime)
.put(
BlobField.TIME_STORAGE_CLASS_UPDATED.getGrpcName(),
BlobInfo.Builder::clearTimeStorageClassUpdated)
.put(BlobField.UPDATED.getGrpcName(), BlobInfo.Builder::clearUpdateTime)
.build();
private static final ImmutableMap<String, Mapper<BucketInfo.Builder>> bucketInfoFieldClearers =
ImmutableMap.<String, Mapper<BucketInfo.Builder>>builder()
.put(BucketField.ACL.getGrpcName(), BucketInfo.Builder::clearAcl)
// .put(BucketField.AUTOCLASS.getGrpcName(), b -> b.clearAutoclass())
.put(BucketField.BILLING.getGrpcName(), BucketInfo.Builder::clearRequesterPays)
.put(BucketField.CORS.getGrpcName(), BucketInfo.Builder::clearCors)
.put(
BucketField.CUSTOM_PLACEMENT_CONFIG.getGrpcName(),
BucketInfo.Builder::clearCustomPlacementConfig)
.put(
BucketField.DEFAULT_EVENT_BASED_HOLD.getGrpcName(),
BucketInfo.Builder::clearDefaultEventBasedHold)
.put(BucketField.DEFAULT_OBJECT_ACL.getGrpcName(), BucketInfo.Builder::clearDefaultAcl)
.put(BucketField.ENCRYPTION.getGrpcName(), BucketInfo.Builder::clearDefaultKmsKeyName)
.put(BucketField.ETAG.getGrpcName(), BucketInfo.Builder::clearEtag)
.put(
BucketField.IAMCONFIGURATION.getGrpcName(),
BucketInfo.Builder::clearIamConfiguration)
.put(BucketField.ID.getGrpcName(), BucketInfo.Builder::clearGeneratedId)
.put(BucketField.LABELS.getGrpcName(), BucketInfo.Builder::clearLabels)
.put(BucketField.LIFECYCLE.getGrpcName(), BucketInfo.Builder::clearLifecycleRules)
.put(BucketField.LOCATION.getGrpcName(), BucketInfo.Builder::clearLocation)
.put(BucketField.LOCATION_TYPE.getGrpcName(), BucketInfo.Builder::clearLocationType)
.put(BucketField.LOGGING.getGrpcName(), BucketInfo.Builder::clearLogging)
.put(BucketField.METAGENERATION.getGrpcName(), BucketInfo.Builder::clearMetageneration)
.put(BucketField.NAME.getGrpcName(), BucketInfo.Builder::clearName)
.put(BucketField.OWNER.getGrpcName(), BucketInfo.Builder::clearOwner)
.put(
BucketField.RETENTION_POLICY.getGrpcName(),
b ->
b.clearRetentionEffectiveTime()
.clearRetentionPolicyIsLocked()
.clearRetentionPeriod())
.put(BucketField.RPO.getGrpcName(), BucketInfo.Builder::clearRpo)
.put(BucketField.STORAGE_CLASS.getGrpcName(), BucketInfo.Builder::clearStorageClass)
.put(BucketField.TIME_CREATED.getGrpcName(), BucketInfo.Builder::clearCreateTime)
.put(BucketField.UPDATED.getGrpcName(), BucketInfo.Builder::clearUpdateTime)
.put(BucketField.VERSIONING.getGrpcName(), BucketInfo.Builder::clearVersioningEnabled)
.put(BucketField.WEBSITE.getGrpcName(), b -> b.clearIndexPage().clearNotFoundPage())
.put("project", BucketInfo.Builder::clearProject)
.build();
}
/**
* @see GenerationNotMatch
* @see SourceGenerationMatch
*/
static final class GenerationMatch extends RpcOptVal<@NonNull Long>
implements ObjectSourceOpt, ObjectTargetOpt, ProjectAsSource<SourceGenerationMatch> {
private static final long serialVersionUID = 2645517179434741007L;
private GenerationMatch(long val) {
super(StorageRpc.Option.IF_GENERATION_MATCH, val);
}
@Override
public Mapper<WriteObjectRequest.Builder> writeObject() {
return b -> {
b.getWriteObjectSpecBuilder().setIfGenerationMatch(val);
return b;
};
}
@Override
public Mapper<ReadObjectRequest.Builder> readObject() {
return b -> b.setIfGenerationMatch(val);
}
@Override
public Mapper<GetObjectRequest.Builder> getObject() {
return b -> b.setIfGenerationMatch(val);
}
@Override
public Mapper<UpdateObjectRequest.Builder> updateObject() {
return b -> b.setIfGenerationMatch(val);
}
@Override
public Mapper<DeleteObjectRequest.Builder> deleteObject() {
return b -> b.setIfGenerationMatch(val);
}
@Override
public Mapper<ComposeObjectRequest.Builder> composeObject() {
return b -> b.setIfGenerationMatch(val);
}
@Override
public Mapper<RewriteObjectRequest.Builder> rewriteObject() {
return b -> b.setIfGenerationMatch(val);
}
@Override
public SourceGenerationMatch asSource() {
return new SourceGenerationMatch(val);
}
}