1
- /*
2
- * Licensed to the Apache Software Foundation (ASF) under one
3
- * or more contributor license agreements. See the NOTICE file
4
- * distributed with this work for additional information
5
- * regarding copyright ownership. The ASF licenses this file
6
- * to you under the Apache License, Version 2.0 (the
7
- * "License"); you may not use this file except in compliance
8
- * with the License. You may obtain a copy of the License at
9
- *
10
- * http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing,
13
- * software distributed under the License is distributed on an
14
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
- * KIND, either express or implied. See the License for the
16
- * specific language governing permissions and limitations
17
- * under the License.
18
- */
19
-
20
1
package org .apache .jackrabbit .oak .blob .cloud .azure .blobstorage ;
21
2
22
- import java .io .File ;
23
- import java .io .FileInputStream ;
24
- import java .io .IOException ;
25
- import java .io .InputStream ;
26
- import java .net .InetSocketAddress ;
27
- import java .net .Proxy ;
28
- import java .net .SocketAddress ;
29
- import java .net .URISyntaxException ;
30
- import java .security .InvalidKeyException ;
31
- import java .util .Properties ;
32
-
33
- import org .apache .jackrabbit .guava .common .base .Strings ;
34
- import com .microsoft .azure .storage .CloudStorageAccount ;
35
- import com .microsoft .azure .storage .OperationContext ;
36
- import com .microsoft .azure .storage .RetryExponentialRetry ;
37
- import com .microsoft .azure .storage .RetryNoRetry ;
38
- import com .microsoft .azure .storage .RetryPolicy ;
39
- import com .microsoft .azure .storage .StorageException ;
40
- import com .microsoft .azure .storage .blob .BlobRequestOptions ;
41
- import com .microsoft .azure .storage .blob .CloudBlobClient ;
42
- import com .microsoft .azure .storage .blob .CloudBlobContainer ;
3
+ import com .azure .core .http .HttpClient ;
4
+ import com .azure .core .http .ProxyOptions ;
5
+ import com .azure .core .http .netty .NettyAsyncHttpClientBuilder ;
6
+ import com .azure .storage .blob .BlobContainerClient ;
7
+ import com .azure .storage .blob .BlobServiceClient ;
8
+ import com .azure .storage .blob .BlobServiceClientBuilder ;
9
+ import com .azure .storage .common .policy .RequestRetryOptions ;
10
+ import com .azure .storage .common .policy .RetryPolicyType ;
11
+ import com .google .common .base .Strings ;
43
12
import org .apache .commons .lang3 .StringUtils ;
44
13
import org .apache .jackrabbit .core .data .DataStoreException ;
45
14
import org .apache .jackrabbit .oak .commons .PropertiesUtil ;
46
15
import org .jetbrains .annotations .NotNull ;
47
16
import org .jetbrains .annotations .Nullable ;
48
17
49
- public final class Utils {
50
-
51
- public static final String DEFAULT_CONFIG_FILE = "azure.properties" ;
18
+ import java .io .File ;
19
+ import java .io .FileInputStream ;
20
+ import java .io .IOException ;
21
+ import java .io .InputStream ;
22
+ import java .net .InetSocketAddress ;
23
+ import java .time .Duration ;
24
+ import java .util .Properties ;
52
25
26
+ public class Utils {
53
27
public static final String DASH = "-" ;
28
+ public static final String DEFAULT_CONFIG_FILE = "azure.properties" ;
54
29
55
- /**
56
- * private constructor so that class cannot initialized from outside.
57
- */
58
- private Utils () {
59
- }
60
-
61
- /**
62
- * Create CloudBlobClient from properties.
63
- *
64
- * @param connectionString connectionString to configure @link {@link CloudBlobClient}
65
- * @return {@link CloudBlobClient}
66
- */
67
- public static CloudBlobClient getBlobClient (@ NotNull final String connectionString ) throws URISyntaxException , InvalidKeyException {
68
- return getBlobClient (connectionString , null );
69
- }
30
+ public Utils () {}
70
31
71
- public static CloudBlobClient getBlobClient (@ NotNull final String connectionString ,
72
- @ Nullable final BlobRequestOptions requestOptions ) throws URISyntaxException , InvalidKeyException {
73
- CloudStorageAccount account = CloudStorageAccount .parse (connectionString );
74
- CloudBlobClient client = account .createCloudBlobClient ();
75
- if (null != requestOptions ) {
76
- client .setDefaultRequestOptions (requestOptions );
77
- }
78
- return client ;
79
- }
32
+ public static BlobContainerClient getBlobContainer (String accountName , String accountKey , @ NotNull final String connectionString ,
33
+ @ NotNull final String containerName ,
34
+ @ Nullable final RequestRetryOptions retryOptions ,
35
+ boolean isProxyNeeded , final Properties properties ) throws DataStoreException {
36
+ BlobContainerClient blobContainerClient ;
37
+ try {
38
+ if (isProxyNeeded ) {
39
+ String proxyHost = properties .getProperty (AzureConstants .PROXY_HOST );
40
+ String proxyPort = properties .getProperty (AzureConstants .PROXY_PORT );
41
+
42
+ ProxyOptions proxyOptions = new ProxyOptions (ProxyOptions .Type .HTTP ,
43
+ new InetSocketAddress (proxyHost , Integer .parseInt (proxyPort )));
44
+
45
+ HttpClient httpClient = new NettyAsyncHttpClientBuilder ()
46
+ .proxy (proxyOptions )
47
+ .build ();
48
+
49
+ BlobServiceClient blobServiceClient = new BlobServiceClientBuilder ()
50
+ .connectionString (connectionString )
51
+ .httpClient (httpClient )
52
+ .retryOptions (retryOptions )
53
+ .buildClient ();
54
+ blobContainerClient = blobServiceClient .getBlobContainerClient (containerName );
55
+ return blobContainerClient ;
56
+ }
80
57
81
- public static CloudBlobContainer getBlobContainer (@ NotNull final String connectionString ,
82
- @ NotNull final String containerName ) throws DataStoreException {
83
- return getBlobContainer (connectionString , containerName , null );
84
- }
58
+ BlobServiceClient blobServiceClient = new BlobServiceClientBuilder ()
59
+ .connectionString (connectionString )
60
+ .retryOptions (retryOptions )
61
+ .buildClient ();
62
+ blobContainerClient = blobServiceClient .getBlobContainerClient (containerName );
63
+ return blobContainerClient ;
85
64
86
- public static CloudBlobContainer getBlobContainer (@ NotNull final String connectionString ,
87
- @ NotNull final String containerName ,
88
- @ Nullable final BlobRequestOptions requestOptions ) throws DataStoreException {
89
- try {
90
- CloudBlobClient client = (
91
- (null == requestOptions )
92
- ? Utils .getBlobClient (connectionString )
93
- : Utils .getBlobClient (connectionString , requestOptions )
94
- );
95
- return client .getContainerReference (containerName );
96
- } catch (InvalidKeyException | URISyntaxException | StorageException e ) {
65
+ } catch (Exception e ) {
97
66
throw new DataStoreException (e );
98
67
}
99
68
}
100
69
101
- public static void setProxyIfNeeded (final Properties properties ) {
70
+ public static boolean isProxyNeeded (final Properties properties ) {
102
71
String proxyHost = properties .getProperty (AzureConstants .PROXY_HOST );
103
72
String proxyPort = properties .getProperty (AzureConstants .PROXY_PORT );
104
73
105
- if (!Strings .isNullOrEmpty (proxyHost ) &&
106
- Strings .isNullOrEmpty (proxyPort )) {
107
- int port = Integer .parseInt (proxyPort );
108
- SocketAddress proxyAddr = new InetSocketAddress (proxyHost , port );
109
- Proxy proxy = new Proxy (Proxy .Type .HTTP , proxyAddr );
110
- OperationContext .setDefaultProxy (proxy );
111
- }
74
+ return !Strings .isNullOrEmpty (proxyHost ) && Strings .isNullOrEmpty (proxyPort );
112
75
}
113
76
114
- public static RetryPolicy getRetryPolicy (final String maxRequestRetry ) {
115
- int retries = PropertiesUtil .toInteger (maxRequestRetry , -1 );
116
- if (retries < 0 ) {
77
+ public static RequestRetryOptions getRetryOptions (final String maxRequestRetryCount ) {
78
+ int retries = PropertiesUtil .toInteger (maxRequestRetryCount , -1 );
79
+ if (retries < 0 ) {
117
80
return null ;
118
81
}
82
+
119
83
if (retries == 0 ) {
120
- return new RetryNoRetry ( );
84
+ return new RequestRetryOptions ( RetryPolicyType . FIXED , 1 , 1 , null , null , null );
121
85
}
122
- return new RetryExponentialRetry ( RetryPolicy . DEFAULT_CLIENT_BACKOFF , retries );
86
+ return new RequestRetryOptions ( RetryPolicyType . EXPONENTIAL , retries , ( int ) Duration . ofSeconds ( 3 ). toMillis (), null , null , null );
123
87
}
124
88
125
-
126
89
public static String getConnectionStringFromProperties (Properties properties ) {
127
-
128
90
String sasUri = properties .getProperty (AzureConstants .AZURE_SAS , "" );
129
91
String blobEndpoint = properties .getProperty (AzureConstants .AZURE_BLOB_ENDPOINT , "" );
130
92
String connectionString = properties .getProperty (AzureConstants .AZURE_CONNECTION_STRING , "" );
@@ -141,7 +103,7 @@ public static String getConnectionStringFromProperties(Properties properties) {
141
103
142
104
return getConnectionString (
143
105
accountName ,
144
- accountKey ,
106
+ accountKey ,
145
107
blobEndpoint );
146
108
}
147
109
@@ -153,15 +115,11 @@ public static String getConnectionStringForSas(String sasUri, String blobEndpoin
153
115
}
154
116
}
155
117
156
- public static String getConnectionString (final String accountName , final String accountKey ) {
157
- return getConnectionString (accountName , accountKey , null );
158
- }
159
-
160
118
public static String getConnectionString (final String accountName , final String accountKey , String blobEndpoint ) {
161
119
StringBuilder connString = new StringBuilder ("DefaultEndpointsProtocol=https" );
162
120
connString .append (";AccountName=" ).append (accountName );
163
121
connString .append (";AccountKey=" ).append (accountKey );
164
-
122
+
165
123
if (!Strings .isNullOrEmpty (blobEndpoint )) {
166
124
connString .append (";BlobEndpoint=" ).append (blobEndpoint );
167
125
}
0 commit comments