-
Notifications
You must be signed in to change notification settings - Fork 164
/
CloudStorageAccount.java
1372 lines (1195 loc) · 56.7 KB
/
CloudStorageAccount.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 Microsoft Corporation
*
* 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.microsoft.azure.storage;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.microsoft.azure.storage.analytics.CloudAnalyticsClient;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.core.SR;
import com.microsoft.azure.storage.core.SharedAccessSignatureHelper;
import com.microsoft.azure.storage.core.StorageCredentialsHelper;
import com.microsoft.azure.storage.core.UriQueryBuilder;
import com.microsoft.azure.storage.core.Utility;
import com.microsoft.azure.storage.file.CloudFileClient;
import com.microsoft.azure.storage.queue.CloudQueue;
import com.microsoft.azure.storage.queue.CloudQueueClient;
import com.microsoft.azure.storage.table.CloudTable;
import com.microsoft.azure.storage.table.CloudTableClient;
/**
* Represents a Microsoft Azure storage account.
*/
public final class CloudStorageAccount {
/**
* Represents the setting name for the account key.
*/
protected static final String ACCOUNT_KEY_NAME = "AccountKey";
/**
* Represents the setting name for the account name.
*/
protected static final String ACCOUNT_NAME_NAME = "AccountName";
/**
* Represents the final terms of each root storage DNS name.
*/
private static final String DNS_NAME_FORMAT = "%s.%s";
/**
* Represents the root storage DNS name.
*/
private static final String DEFAULT_DNS = "core.windows.net";
/**
* The suffix appended to account in order to access secondary location for read only access.
*/
private static final String SECONDARY_LOCATION_ACCOUNT_SUFFIX = "-secondary";
/**
* Represents the setting name for a custom storage endpoint suffix.
*/
private static final String ENDPOINT_SUFFIX_NAME = "EndpointSuffix";
/**
* Represents the setting name for a custom blob storage endpoint.
*/
protected static final String BLOB_ENDPOINT_NAME = "BlobEndpoint";
/**
* Represents the setting name for a custom blob storage secondary endpoint.
*/
protected static final String BLOB_SECONDARY_ENDPOINT_NAME = "BlobSecondaryEndpoint";
/**
* The setting name for using the default storage endpoints with the specified protocol.
*/
private static final String DEFAULT_ENDPOINTS_PROTOCOL_NAME = "DefaultEndpointsProtocol";
/**
* The format string for the primary endpoint.
*/
private static final String DEVELOPMENT_STORAGE_PRIMARY_ENDPOINT_FORMAT = "%s://%s:%s/%s";
/**
* The format string for the secondary endpoint.
*/
private static final String DEVELOPMENT_STORAGE_SECONDARY_ENDPOINT_FORMAT =
DEVELOPMENT_STORAGE_PRIMARY_ENDPOINT_FORMAT + SECONDARY_LOCATION_ACCOUNT_SUFFIX;
/**
* The setting name for specifying a development storage proxy Uri.
*/
private static final String DEVELOPMENT_STORAGE_PROXY_URI_NAME = "DevelopmentStorageProxyUri";
/**
* The default account key for the development storage.
*/
private static final String DEVSTORE_ACCOUNT_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
/**
* The default account name for the development storage.
*/
private static final String DEVSTORE_ACCOUNT_NAME = "devstoreaccount1";
/**
* Represents the setting name for a custom file endpoint.
*/
private static final String FILE_ENDPOINT_NAME = "FileEndpoint";
/**
* Represents the setting name for a custom file secondary endpoint.
*/
private static final String FILE_SECONDARY_ENDPOINT_NAME = "FileSecondaryEndpoint";
/**
* The format string for the primary endpoint.
*/
private static final String PRIMARY_ENDPOINT_FORMAT = "%s://%s.%s";
/**
* The format string for the secondary endpoint
*/
private static final String SECONDARY_ENDPOINT_FORMAT = "%s://%s%s.%s";
/**
* Represents the setting name for a custom queue endpoint.
*/
protected static final String QUEUE_ENDPOINT_NAME = "QueueEndpoint";
/**
* Represents the setting name for a custom queue secondary endpoint.
*/
protected static final String QUEUE_SECONDARY_ENDPOINT_NAME = "QueueSecondaryEndpoint";
/**
* Represents the setting name for a shared access key.
*/
protected static final String SHARED_ACCESS_SIGNATURE_NAME = "SharedAccessSignature";
/**
* Represents the setting name for a custom table storage endpoint.
*/
protected static final String TABLE_ENDPOINT_NAME = "TableEndpoint";
/**
* Represents the setting name for a custom table storage secondary endpoint.
*/
protected static final String TABLE_SECONDARY_ENDPOINT_NAME = "TableSecondaryEndpoint";
/**
* The setting name for using the development storage.
*/
private static final String USE_DEVELOPMENT_STORAGE_NAME = "UseDevelopmentStorage";
/**
* Returns a {@link CloudStorageAccount} object that represents the development storage credentials. Secondary
* endpoints are enabled by default.
*
* @return A {@link CloudStorageAccount} object for the development storage credentials.
*/
public static CloudStorageAccount getDevelopmentStorageAccount() {
try {
return getDevelopmentStorageAccount(null);
}
catch (final URISyntaxException e) {
// this won't happen since we know the standard development stororage uri is valid.
return null;
}
}
/**
* Returns a {@link CloudStorageAccount} object that represents the development storage credentials, using the
* specified proxy URI. Secondary endpoints are enabled by default.
*
* @param proxyUri
* A <code>java.net.URI</code> object that represents the proxy endpoint to use. Specifying
* <code>null</code> will use the default <code>http://127.0.0.1</code>.
*
* @return A {@link CloudStorageAccount} object for the development storage credentials.
*
* @throws URISyntaxException
* If the resource URI is invalid.
*/
public static CloudStorageAccount getDevelopmentStorageAccount(final URI proxyUri) throws URISyntaxException {
String scheme;
String host;
if (proxyUri == null) {
scheme = "http";
host = "127.0.0.1";
}
else {
scheme = proxyUri.getScheme();
host = proxyUri.getHost();
}
StorageCredentials credentials = new StorageCredentialsAccountAndKey(DEVSTORE_ACCOUNT_NAME,
DEVSTORE_ACCOUNT_KEY);
URI blobPrimaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_PRIMARY_ENDPOINT_FORMAT, scheme, host,
"10000", DEVSTORE_ACCOUNT_NAME));
URI queuePrimaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_PRIMARY_ENDPOINT_FORMAT, scheme, host,
"10001", DEVSTORE_ACCOUNT_NAME));
URI tablePrimaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_PRIMARY_ENDPOINT_FORMAT, scheme, host,
"10002", DEVSTORE_ACCOUNT_NAME));
URI blobSecondaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_SECONDARY_ENDPOINT_FORMAT, scheme, host,
"10000", DEVSTORE_ACCOUNT_NAME));
URI queueSecondaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_SECONDARY_ENDPOINT_FORMAT, scheme, host,
"10001", DEVSTORE_ACCOUNT_NAME));
URI tableSecondaryEndpoint = new URI(String.format(DEVELOPMENT_STORAGE_SECONDARY_ENDPOINT_FORMAT, scheme, host,
"10002", DEVSTORE_ACCOUNT_NAME));
CloudStorageAccount account = new CloudStorageAccount(credentials, new StorageUri(blobPrimaryEndpoint,
blobSecondaryEndpoint), new StorageUri(queuePrimaryEndpoint, queueSecondaryEndpoint), new StorageUri(
tablePrimaryEndpoint, tableSecondaryEndpoint), null /* fileStorageUri */);
account.isDevStoreAccount = true;
return account;
}
/**
* Parses a connection string and returns a cloud storage account created from the connection string.
* <p>
* The connection string should be in the <a href="http://msdn.microsoft.com/library/azure/ee758697.aspx">Azure
* connection string</a> format.
* <p>
* Note that while a connection string may include a SAS token, it is often easier to use the
* {@link CloudBlobContainer#CloudBlobContainer(URI)}, {@link CloudQueue#CloudQueue(URI)},
* {@link CloudTable#CloudTable(URI)} constructors directly. To do this, create a
* {@link StorageCredentialsSharedAccessSignature#StorageCredentialsSharedAccessSignature(String)} object with your
* SAS token, use the {@link StorageCredentialsSharedAccessSignature#transformUri(URI)} method on the container,
* queue, or table URI, and then use that URI to construct the object.
*
* @param connectionString
* A <code>String</code> that represents the connection string to parse.
*
* @return A {@link CloudStorageAccount} object that represents the cloud storage account constructed from the
* values provided in the connection string.
*
* @throws InvalidKeyException
* If credentials in the connection string contain an invalid key.
* @throws URISyntaxException
* If the connection string specifies an invalid URI.
*/
public static CloudStorageAccount parse(final String connectionString) throws URISyntaxException,
InvalidKeyException {
if (connectionString == null || connectionString.length() == 0) {
throw new IllegalArgumentException(SR.INVALID_CONNECTION_STRING);
}
// 1. Parse connection string in to key / value pairs
final Map<String, String> settings = Utility.parseAccountString(connectionString);
// 2 Validate General Settings rules,
// - only setting value per key
// - setting must have value.
for (final Entry<String, String> entry : settings.entrySet()) {
if (entry.getValue() == null || entry.getValue().equals(Constants.EMPTY_STRING)) {
throw new IllegalArgumentException(SR.INVALID_CONNECTION_STRING);
}
}
// 3. Validate scenario specific constraints
CloudStorageAccount account = tryConfigureDevStore(settings);
if (account != null) {
return account;
}
account = tryConfigureServiceAccount(settings);
if (account != null) {
return account;
}
throw new IllegalArgumentException(SR.INVALID_CONNECTION_STRING);
}
/**
* Gets the {@link StorageUri} using specified service, settings, and endpoint.
*
* @param settings
* A <code>java.util.Map</code> of key/value pairs which represents
* the connection settings.
* @param service
* A String that represents the service's base DNS name.
* @param serviceEndpointName
* The service endpoint name to check in settings.
* @param serviceSecondaryEndpointName
* The service secondary endpoint name to check in settings.
* @param matchesAutomaticEndpointsSpec
* Whether the settings match the automatic endpoints specification.
* @return The {@link StorageUri}.
* @throws URISyntaxException
*/
private static StorageUri getStorageUri(
final Map<String, String> settings, final String service, final String serviceEndpointName, final String serviceSecondaryEndpointName, final Boolean matchesAutomaticEndpointsSpec)
throws URISyntaxException {
String serviceEndpoint = settingOrDefault(settings, serviceEndpointName);
String serviceSecondaryEndpoint = settingOrDefault(settings, serviceSecondaryEndpointName);
if (serviceSecondaryEndpoint != null && serviceEndpoint != null) {
return new StorageUri(new URI(serviceEndpoint), new URI(serviceSecondaryEndpoint));
} else if (serviceEndpoint != null) {
return new StorageUri(new URI(serviceEndpoint));
} else if (matchesAutomaticEndpointsSpec) {
final String scheme = settings.get(CloudStorageAccount.DEFAULT_ENDPOINTS_PROTOCOL_NAME);
final String accountName = settings.get(CloudStorageAccount.ACCOUNT_NAME_NAME);
final String endpointSuffix = settings.get(CloudStorageAccount.ENDPOINT_SUFFIX_NAME);
return getDefaultStorageUri(scheme, accountName, getDNS(service, endpointSuffix));
} else {
return null;
}
}
/**
* Gets the default {@link StorageUri} using the specified service, protocol and account name.
*
* @param scheme
* The protocol to use.
* @param accountName
* The name of the storage account.
* @param service
* A String that represents the service's base DNS name.
* @return The default {@link StorageUri}.
*/
private static StorageUri getDefaultStorageUri(final String scheme, final String accountName, final String service)
throws URISyntaxException {
if (Utility.isNullOrEmpty(scheme)) {
throw new IllegalArgumentException(SR.SCHEME_NULL_OR_EMPTY);
}
if (Utility.isNullOrEmpty(accountName)) {
throw new IllegalArgumentException(SR.ACCOUNT_NAME_NULL_OR_EMPTY);
}
URI primaryUri = new URI(String.format(PRIMARY_ENDPOINT_FORMAT, scheme, accountName, service));
URI secondaryUri = new URI(
String.format(SECONDARY_ENDPOINT_FORMAT, scheme, accountName, SECONDARY_LOCATION_ACCOUNT_SUFFIX, service));
return new StorageUri(primaryUri, secondaryUri);
}
/**
* This generates a domain name for the given service.
*
* @param service
* the service to connect to
* @param base
* the suffix to use
* @return the domain name
*/
private static String getDNS(String service, String base) {
if (base == null) {
base = DEFAULT_DNS;
}
return String.format(DNS_NAME_FORMAT, service, base);
}
/**
* Evaluates connection settings and returns a CloudStorageAccount representing Development Storage.
*
* @param settings
* A <code>java.util.Map</code> of key/value pairs which represents the connection settings.
* @return A {@link CloudStorageAccount} object constructed from the values provided in the connection settings, or
* null if
* one cannot be constructed.
* @throws URISyntaxException
* if the connection settings contains an invalid URI
*/
private static CloudStorageAccount tryConfigureDevStore(final Map<String, String> settings)
throws URISyntaxException {
if (matchesSpecification(
settings,
allRequired(USE_DEVELOPMENT_STORAGE_NAME),
optional(DEVELOPMENT_STORAGE_PROXY_URI_NAME))) {
if (!Boolean.parseBoolean(settings.get(USE_DEVELOPMENT_STORAGE_NAME))) {
throw new IllegalArgumentException(SR.INVALID_CONNECTION_STRING_DEV_STORE_NOT_TRUE);
}
URI devStoreProxyUri = null;
if (settings.containsKey(DEVELOPMENT_STORAGE_PROXY_URI_NAME)) {
devStoreProxyUri = new URI(settings.get(DEVELOPMENT_STORAGE_PROXY_URI_NAME));
}
return getDevelopmentStorageAccount(devStoreProxyUri);
} else {
return null;
}
}
private interface ConnectionStringFilter {
Map<String, String> apply(Map<String, String> settings);
}
private static ConnectionStringFilter allRequired(final String... settingNames) {
return new ConnectionStringFilter() {
@Override
public Map<String, String> apply(Map<String, String> settings) {
final Map<String, String> result = new HashMap<String, String>(settings);
for (final String settingName : settingNames) {
if (result.containsKey(settingName)) {
result.remove(settingName);
} else {
return null;
}
}
return result;
}
};
}
private static ConnectionStringFilter optional(final String... settingNames) {
return new ConnectionStringFilter() {
@Override
public Map<String, String> apply(Map<String, String> settings) {
final Map<String, String> result = new HashMap<String, String>(settings);
for (final String settingName : settingNames) {
if (result.containsKey(settingName)) {
result.remove(settingName);
}
}
return result;
}
};
}
private static ConnectionStringFilter atLeastOne(final String... settingNames) {
return new ConnectionStringFilter() {
@Override
public Map<String, String> apply(Map<String, String> settings) {
final Map<String, String> result = new HashMap<String, String>(settings);
Boolean foundOne = false;
for (final String settingName : settingNames) {
if (result.containsKey(settingName)) {
result.remove(settingName);
foundOne = true;
}
}
return foundOne ? result : null;
}
};
}
private static ConnectionStringFilter none(final String... settingNames) {
return new ConnectionStringFilter() {
@Override
public Map<String, String> apply(Map<String, String> settings) {
final Map<String, String> result = new HashMap<String, String>(settings);
Boolean foundOne = false;
for (final String settingName : settingNames) {
if (result.containsKey(settingName)) {
result.remove(settingName);
foundOne = true;
}
}
return foundOne ? null : result;
}
};
}
private static ConnectionStringFilter matchesAll(final ConnectionStringFilter... filters) {
return new ConnectionStringFilter() {
@Override
public Map<String, String> apply(Map<String, String> settings) {
Map<String, String> result = new HashMap<String, String>(settings);
for (final ConnectionStringFilter filter : filters) {
if (result == null) {
break;
}
result = filter.apply(result);
}
return result;
}
};
}
private static ConnectionStringFilter matchesOne(final ConnectionStringFilter... filters) {
return new ConnectionStringFilter() {
@Override
public Map<String, String> apply(Map<String, String> settings) {
Map<String, String> matchResult = null;
for (final ConnectionStringFilter filter : filters) {
Map<String, String> result = filter.apply(new HashMap<String, String>(settings));
if (result != null) {
if (matchResult == null) {
matchResult = result;
} else {
return null;
}
}
}
return matchResult;
}
};
}
private static ConnectionStringFilter matchesExactly(final ConnectionStringFilter filter) {
return new ConnectionStringFilter() {
@Override
public Map<String, String> apply(Map<String, String> settings) {
Map<String, String> result = new HashMap<String, String>(settings);
result = filter.apply(result);
if (result == null || !result.isEmpty()) {
return null;
} else {
return result;
}
}
};
}
private static ConnectionStringFilter validCredentials =
matchesOne(
matchesAll(allRequired(ACCOUNT_NAME_NAME, ACCOUNT_KEY_NAME), none(SHARED_ACCESS_SIGNATURE_NAME)),
matchesAll(allRequired(SHARED_ACCESS_SIGNATURE_NAME), optional(ACCOUNT_NAME_NAME), none(ACCOUNT_KEY_NAME)),
none(ACCOUNT_NAME_NAME, ACCOUNT_KEY_NAME, SHARED_ACCESS_SIGNATURE_NAME)
);
private static Boolean matchesSpecification(
Map<String, String> settings,
ConnectionStringFilter... constraints) {
for (ConnectionStringFilter constraint: constraints) {
Map<String, String> remainingSettings = constraint.apply(settings);
if (remainingSettings == null) {
return false;
} else {
settings = remainingSettings;
}
}
if (settings.isEmpty()) {
return true;
}
return false;
}
private static Boolean isValidEndpointPair(String primary, String secondary) {
return (primary != null) || (/* primary is null, and... */ secondary == null);
}
private static String settingOrDefault(Map<String, String> settings, String key) {
return settings.containsKey(key) ? settings.get(key) : null;
}
/**
* Evaluates connection settings and configures a CloudStorageAccount accordingly.
*
* @param settings
* A <code>java.util.Map</code> of key/value pairs which represents
* the connection settings.
* @return A {@link CloudStorageAccount} represented by the settings.
* @throws URISyntaxException
* if the connectionString specifies an invalid URI.
* @throws InvalidKeyException
* if credentials in the connection settings contain an invalid key.
*/
private static CloudStorageAccount tryConfigureServiceAccount(final Map<String, String> settings)
throws URISyntaxException, InvalidKeyException {
ConnectionStringFilter endpointsOptional =
optional(
BLOB_ENDPOINT_NAME, BLOB_SECONDARY_ENDPOINT_NAME,
QUEUE_ENDPOINT_NAME, QUEUE_SECONDARY_ENDPOINT_NAME,
TABLE_ENDPOINT_NAME, TABLE_SECONDARY_ENDPOINT_NAME,
FILE_ENDPOINT_NAME, FILE_SECONDARY_ENDPOINT_NAME
);
ConnectionStringFilter primaryEndpointRequired =
atLeastOne(
BLOB_ENDPOINT_NAME,
QUEUE_ENDPOINT_NAME,
TABLE_ENDPOINT_NAME,
FILE_ENDPOINT_NAME
);
ConnectionStringFilter secondaryEndpointsOptional =
optional(
BLOB_SECONDARY_ENDPOINT_NAME,
QUEUE_SECONDARY_ENDPOINT_NAME,
TABLE_SECONDARY_ENDPOINT_NAME,
FILE_SECONDARY_ENDPOINT_NAME
);
ConnectionStringFilter automaticEndpointsMatchSpec =
matchesExactly(matchesAll(
matchesOne(
matchesAll(allRequired(ACCOUNT_KEY_NAME)), // Key + Name, Endpoints optional
allRequired(SHARED_ACCESS_SIGNATURE_NAME) // SAS + Name, Endpoints optional
),
allRequired(ACCOUNT_NAME_NAME), // Name required to automatically create URIs
endpointsOptional,
optional(DEFAULT_ENDPOINTS_PROTOCOL_NAME, ENDPOINT_SUFFIX_NAME)
));
ConnectionStringFilter explicitEndpointsMatchSpec =
matchesExactly(matchesAll( // Any Credentials, Endpoints must be explicitly declared
validCredentials,
primaryEndpointRequired,
secondaryEndpointsOptional
));
Boolean matchesAutomaticEndpointsSpec = matchesSpecification(settings, automaticEndpointsMatchSpec);
Boolean matchesExplicitEndpointsSpec = matchesSpecification(settings, explicitEndpointsMatchSpec);
if (matchesAutomaticEndpointsSpec || matchesExplicitEndpointsSpec) {
if (matchesAutomaticEndpointsSpec && !settings.containsKey(DEFAULT_ENDPOINTS_PROTOCOL_NAME)) {
settings.put(DEFAULT_ENDPOINTS_PROTOCOL_NAME, "https");
}
String blobEndpoint = settingOrDefault(settings, BLOB_ENDPOINT_NAME);
String queueEndpoint = settingOrDefault(settings, QUEUE_ENDPOINT_NAME);
String tableEndpoint = settingOrDefault(settings, TABLE_ENDPOINT_NAME);
String fileEndpoint = settingOrDefault(settings, FILE_ENDPOINT_NAME);
String blobSecondaryEndpoint = settingOrDefault(settings, BLOB_SECONDARY_ENDPOINT_NAME);
String queueSecondaryEndpoint = settingOrDefault(settings, QUEUE_SECONDARY_ENDPOINT_NAME);
String tableSecondaryEndpoint = settingOrDefault(settings, TABLE_SECONDARY_ENDPOINT_NAME);
String fileSecondaryEndpoint = settingOrDefault(settings, FILE_SECONDARY_ENDPOINT_NAME);
// if secondary is specified, primary must also be specified
if (
isValidEndpointPair(blobEndpoint, blobSecondaryEndpoint)
&& isValidEndpointPair(queueEndpoint, queueSecondaryEndpoint)
&& isValidEndpointPair(tableEndpoint, tableSecondaryEndpoint)
&& isValidEndpointPair(fileEndpoint, fileSecondaryEndpoint)
) {
CloudStorageAccount accountInformation =
new CloudStorageAccount(
StorageCredentials.tryParseCredentials(settings),
getStorageUri(settings, SR.BLOB, BLOB_ENDPOINT_NAME, BLOB_SECONDARY_ENDPOINT_NAME, matchesAutomaticEndpointsSpec),
getStorageUri(settings, SR.QUEUE, QUEUE_ENDPOINT_NAME, QUEUE_SECONDARY_ENDPOINT_NAME, matchesAutomaticEndpointsSpec),
getStorageUri(settings, SR.TABLE, TABLE_ENDPOINT_NAME, TABLE_SECONDARY_ENDPOINT_NAME, matchesAutomaticEndpointsSpec),
getStorageUri(settings, SR.FILE, FILE_ENDPOINT_NAME, FILE_SECONDARY_ENDPOINT_NAME, matchesAutomaticEndpointsSpec)
);
accountInformation.isBlobEndpointDefault = blobEndpoint == null;
accountInformation.isFileEndpointDefault = fileEndpoint == null;
accountInformation.isQueueEndpointDefault = queueEndpoint == null;
accountInformation.isTableEndpointDefault = tableEndpoint == null;
accountInformation.endpointSuffix = settingOrDefault(settings, ENDPOINT_SUFFIX_NAME);
accountInformation.accountName = settingOrDefault(settings, ACCOUNT_NAME_NAME);
return accountInformation;
}
}
return null;
}
/**
* The explicit endpoint suffix if one other than the default is needed. Null otherwise.
*/
private String endpointSuffix;
/**
* The internal Blob StorageUri.
*/
private final StorageUri blobStorageUri;
/**
* The internal file StorageUri.
*/
private final StorageUri fileStorageUri;
/**
* The internal queue StorageUri.
*/
private final StorageUri queueStorageUri;
/**
* The internal table StorageUri.
*/
private final StorageUri tableStorageUri;
/**
* The internal Storage Credentials.
*/
private StorageCredentials credentials;
/**
* The internal account name.
*/
private String accountName;
/**
* Internal flag storing true if the blob endpoint was created using default settings.
* False if the caller specified the blob endpoint explicitly.
*/
private boolean isBlobEndpointDefault = false;
/**
* Internal flag storing true if the file endpoint was created using default settings.
* False if the caller specified the file endpoint explicitly.
*/
private boolean isFileEndpointDefault = false;
/**
* Internal flag storing true if the queue endpoint was created using default settings.
* False if the caller specified the queue endpoint explicitly.
*/
private boolean isQueueEndpointDefault = false;
/**
* Internal flag storing true if the table endpoint was created using default settings.
* False if the caller specified the table endpoint explicitly.
*/
private boolean isTableEndpointDefault = false;
/**
* Internal flag storing true if this is a dev store account created by one of the
* getDevelopmentStorageAccount methods, either called directly or by parsing a
* connection string with the UseDevelopmentStorage flag. False otherwise.
*/
private boolean isDevStoreAccount = false;
/**
* Creates an instance of the <code>CloudStorageAccount</code> class using the specified
* account credentials.
* <p>
* With this constructor, the <code>CloudStorageAccount</code> object is constructed using the
* default HTTP storage service endpoints. The default HTTP storage service endpoints are
* <code>http://<i>myaccount</i>.blob.core.windows.net</code>,
* <code>http://<i>myaccount</i>.queue.core.windows.net</code>,
* <code>http://<i>myaccount</i>.table.core.windows.net</code>, and
* <code>http://<i>myaccount</i>.file.core.windows.net</code>, where
* <code><i>myaccount</i></code> is the name of your storage account.
* <p>
* The credentials provided when constructing the <code>CloudStorageAccount</code> object
* are used to authenticate all further requests against resources that are accessed via
* the <code>CloudStorageAccount</code> object or a client object created from it.
* A client object may be a {@link CloudBlobClient} object.
*
* @param storageCredentials
* A {@link StorageCredentials} object that represents the storage credentials
* to use to authenticate this account.
*
* @throws URISyntaxException
* If <code>storageCredentials</code> specify an invalid account name.
*/
public CloudStorageAccount(final StorageCredentials storageCredentials) throws URISyntaxException {
// Protocol defaults to HTTP unless otherwise specified
this(storageCredentials, false, null);
}
/**
* Creates an instance of the <code>CloudStorageAccount</code> class using the specified
* account credentials and the default service endpoints, using HTTP or HTTPS as specified.
* <p>
* With this constructor, the <code>CloudStorageAccount</code> object is constructed using
* the default storage service endpoints. The default storage service endpoints are:
* <code>[http|https]://<i>myaccount</i>.blob.core.windows.net</code>;
* <code>[http|https]://<i>myaccount</i>.queue.core.windows.net</code>;
* <code>[http|https]://<i>myaccount</i>.table.core.windows.net</code>; and
* <code>[http|https]://<i>myaccount</i>.file.core.windows.net</code>,
* where <code><i>myaccount</i></code> is the name of your storage account. Access to the cloud
* storage account may be via HTTP or HTTPS, as specified by the <code>useHttps</code> parameter.
* <p>
* The credentials provided when constructing the <code>CloudStorageAccount</code> object
* are used to authenticate all further requests against resources that are accessed via
* the <code>CloudStorageAccount</code> object or a client object created from it. A client
* object may be a {@link CloudBlobClient} object.
*
* @param storageCredentials
* A {@link StorageCredentials} object that represents the storage credentials
* to use to authenticate this account.
* @param useHttps
* <code>true</code> to use HTTPS to connect to the storage service endpoints;
* otherwise, <code>false</code>.
*
* @throws URISyntaxException
* If <code>storageCredentials</code> specify an invalid account name.
*/
public CloudStorageAccount(final StorageCredentials storageCredentials, final boolean useHttps)
throws URISyntaxException {
this (storageCredentials, useHttps, null);
}
/**
* Creates an instance of the <code>CloudStorageAccount</code> class using the specified
* account credentials.
* <p>
* With this constructor, the <code>CloudStorageAccount</code> object is constructed using the
* given HTTP storage service endpoint suffix (if any, otherwise the default is used).
*
* The credentials provided when constructing the <code>CloudStorageAccount</code> object
* are used to authenticate all further requests against resources that are accessed via
* the <code>CloudStorageAccount</code> object or a client object created from it.
* A client object may be a {@link CloudBlobClient} object.
*
* @param storageCredentials
* A {@link StorageCredentials} object that represents the storage credentials
* to use to authenticate this account.
* @param useHttps
* <code>true</code> to use HTTPS to connect to the storage service endpoints;
* otherwise, <code>false</code>.
* @param endpointSuffix
* A String that represents the endpointSuffix to use, if any.
*
* @throws URISyntaxException
* If <code>storageCredentials</code> specify an invalid account name.
*/
public CloudStorageAccount(
final StorageCredentials storageCredentials, final boolean useHttps, final String endpointSuffix)
throws URISyntaxException {
this(storageCredentials, useHttps, endpointSuffix, null);
}
/**
* Creates an instance of the <code>CloudStorageAccount</code> class using the specified
* account credentials.
* <p>
* With this constructor, the <code>CloudStorageAccount</code> object is constructed using the
* given HTTP storage service endpoint suffix (if any, otherwise the default is used).
*
* The credentials provided when constructing the <code>CloudStorageAccount</code> object
* are used to authenticate all further requests against resources that are accessed via
* the <code>CloudStorageAccount</code> object or a client object created from it.
* A client object may be a {@link CloudBlobClient} object.
*
* @param storageCredentials
* A {@link StorageCredentials} object that represents the storage credentials
* to use to authenticate this account.
* @param useHttps
* <code>true</code> to use HTTPS to connect to the storage service endpoints;
* otherwise, <code>false</code>.
* @param endpointSuffix
* A String that represents the endpointSuffix to use, if any.
* @param accountName
* A <code>String</code> that contains the account name. This will be used in place of a
* <code>null</code> {@link StorageCredentials#getAccountName()}, but the two must match if
* both are not <code>null</code>.
*
* @throws URISyntaxException
* If <code>storageCredentials</code> specify an invalid account name.
*/
public CloudStorageAccount(
final StorageCredentials storageCredentials, final boolean useHttps, final String endpointSuffix, String accountName)
throws URISyntaxException {
Utility.assertNotNull("storageCredentials", storageCredentials);
if (Utility.isNullOrEmpty(accountName)) {
accountName = storageCredentials.getAccountName();
}
else if (!Utility.isNullOrEmpty(storageCredentials.getAccountName()) &&
!accountName.equals(storageCredentials.getAccountName())) {
throw new IllegalArgumentException(SR.ACCOUNT_NAME_MISMATCH);
}
String protocol = useHttps ? Constants.HTTPS : Constants.HTTP;
this.credentials = storageCredentials;
this.blobStorageUri = getDefaultStorageUri(protocol, accountName, getDNS(SR.BLOB, endpointSuffix));
this.fileStorageUri = getDefaultStorageUri(protocol, accountName, getDNS(SR.FILE, endpointSuffix));
this.queueStorageUri = getDefaultStorageUri(protocol, accountName, getDNS(SR.QUEUE, endpointSuffix));
this.tableStorageUri = getDefaultStorageUri(protocol, accountName, getDNS(SR.TABLE, endpointSuffix));
this.endpointSuffix = endpointSuffix;
this.isBlobEndpointDefault = true;
this.isFileEndpointDefault = true;
this.isQueueEndpointDefault = true;
this.isTableEndpointDefault = true;
}
/**
* Creates an instance of the <code>CloudStorageAccount</code> class using the specified
* account credentials and service endpoints.
* <p>
* Use this constructor to construct a <code>CloudStorageAccount</code> object using custom
* endpoints, in the case where you've configured a custom domain name for your storage account.
* <p>
* The credentials provided when constructing the <code>CloudStorageAccount</code> object
* are used to authenticate all further requests against resources that are accessed via
* the <code>CloudStorageAccount</code> object or a client object created from it. A
* client object may be a {@link CloudBlobClient} object.
*
* @param storageCredentials
* A {@link StorageCredentials} object that represents the storage credentials
* to use to authenticate this account.
* @param blobEndpoint
* A <code>java.net.URI</code> object that represents the Blob service endpoint.
* @param queueEndpoint
* A <code>java.net.URI</code> object that represents the Queue service endpoint.
* @param tableEndpoint
* A <code>java.net.URI</code> object that represents the Table service endpoint.
*/
public CloudStorageAccount(final StorageCredentials storageCredentials, final URI blobEndpoint,
final URI queueEndpoint, final URI tableEndpoint) {
this(storageCredentials, new StorageUri(blobEndpoint), new StorageUri(queueEndpoint),
new StorageUri(tableEndpoint), null);
}
/**
* Creates an instance of the <code>CloudStorageAccount</code> class using the specified
* account credentials and service endpoints.
* <p>
* Use this constructor to construct a <code>CloudStorageAccount</code> object using custom
* endpoints, in the case where you've configured a custom domain name for your storage account.
* <p>
* The credentials provided when constructing the <code>CloudStorageAccount</code> object
* are used to authenticate all further requests against resources that are accessed via
* the <code>CloudStorageAccount</code> object or a client object created from it. A client
* object may be a {@link CloudBlobClient} object.
*
* @param storageCredentials
* A {@link StorageCredentials} object that represents the storage credentials
* to use to authenticate this account.
* @param blobEndpoint
* A <code>java.net.URI</code> object that represents the Blob service endpoint.
* @param queueEndpoint
* A <code>java.net.URI</code> object that represents the Queue service endpoint.
* @param tableEndpoint
* A <code>java.net.URI</code> object that represents the Table service endpoint.
* @param fileEndpoint
* A <code>java.net.URI</code> object that represents the File service endpoint.
*/
public CloudStorageAccount(final StorageCredentials storageCredentials, final URI blobEndpoint,
final URI queueEndpoint, final URI tableEndpoint, final URI fileEndpoint) {
this(storageCredentials, new StorageUri(blobEndpoint), new StorageUri(queueEndpoint),
new StorageUri(tableEndpoint), new StorageUri(fileEndpoint));
}
/**
* Creates an instance of the <code>CloudStorageAccount</code> class using the specified
* account credentials and service endpoints.
* <p>
* Use this constructor to construct a <code>CloudStorageAccount</code> object using custom
* endpoints, in the case where you've configured a custom domain name for your storage account.
* <p>
* The credentials provided when constructing the <code>CloudStorageAccount</code> object
* are used to authenticate all further requests against resources that are accessed via
* the <code>CloudStorageAccount</code> object or a client object created from it. A client
* object may be a {@link CloudBlobClient} object.
*
* @param storageCredentials
* A {@link StorageCredentials} object that represents the storage credentials
* to use to authenticate this account.
* @param blobStorageUri
* A {@link StorageUri} object that represents the Blob service endpoint.
* @param queueStorageUri
* A {@link StorageUri} object that represents the Queue service endpoint.
* @param tableStorageUri
* A {@link StorageUri} object that represents the Table service endpoint.
*/
public CloudStorageAccount(final StorageCredentials storageCredentials,
final StorageUri blobStorageUri,
final StorageUri queueStorageUri,
final StorageUri tableStorageUri) {
this(storageCredentials, blobStorageUri, queueStorageUri, tableStorageUri, null);
}
/**
* Creates an instance of the <code>CloudStorageAccount</code> class using the specified
* account credentials and service endpoints.
* <p>
* Use this constructor to construct a <code>CloudStorageAccount</code> object using custom
* endpoints, in the case where you've configured a custom domain name for your storage account.
* <p>
* The credentials provided when constructing the <code>CloudStorageAccount</code> object are
* used to authenticate all further requests against resources that are accessed via the
* <code>CloudStorageAccount</code> object or a client object created from it.
* A client object may be a {@link CloudBlobClient} object.
*
* @param storageCredentials
* A {@link StorageCredentials} object that represents the storage credentials
* to use to authenticate this account.
* @param blobStorageUri
* A {@link StorageUri} object that represents the Blob service endpoint.