Skip to content

Commit efecb2f

Browse files
committed
Get Secure bundle binary content
1 parent cec7b98 commit efecb2f

File tree

5 files changed

+88
-103
lines changed

5 files changed

+88
-103
lines changed

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/db/DatabaseClient.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ public boolean isActive() {
111111
// ---- SECURE BUNDLE ----
112112
// ---------------------------------
113113

114+
/**
115+
* Download SecureBundle for a specific data center.
116+
* @return
117+
* binary content.
118+
*/
119+
public byte[] downloadDefaultSecureConnectBundle() {
120+
return Utils.downloadFile(getDefaultSecureConnectBundleUrl());
121+
}
122+
114123
/**
115124
* Download SecureBundle for a specific data center
116125
*
@@ -120,14 +129,39 @@ public boolean isActive() {
120129
public void downloadDefaultSecureConnectBundle(String destination) {
121130
// Parameters Validation
122131
Assert.hasLength(destination, "destination");
132+
// Download binary in target folder
133+
Utils.downloadFile(getDefaultSecureConnectBundleUrl(), destination);
134+
}
135+
136+
/**
137+
* This utility method retrieve the binary content for the bundle.
138+
*
139+
* @return
140+
* secure connect bundle binary content.
141+
*/
142+
private String getDefaultSecureConnectBundleUrl() {
123143
if (!isActive())
124144
throw new IllegalStateException("Database '" + databaseId + "' is not available.");
125145
// Get list of urls
126146
ApiResponseHttp res = POST(getEndpointDatabase() + "/secureBundleURL");
127147
// Mapping
128-
String url = (String) JsonUtils.unmarshallBean(res.getBody(), Map.class).get("downloadURL");
129-
// Download binary in target folder
130-
Utils.downloadFile(url, destination);
148+
return (String) JsonUtils.unmarshallBean(res.getBody(), Map.class).get("downloadURL");
149+
}
150+
151+
/**
152+
* Download SecureBundle for a specific data center.
153+
* @return
154+
* binary content.
155+
*/
156+
public byte[] downloadSecureConnectBundle(String region) {
157+
Assert.hasLength(region, "region");
158+
Database db = get();
159+
return downloadSecureConnectBundle(db.getInfo()
160+
.getDatacenters()
161+
.stream()
162+
.filter(d -> region.equalsIgnoreCase(d.getRegion()))
163+
.findFirst()
164+
.orElseThrow(() -> new RegionNotFoundException(region, databaseId)));
131165
}
132166

133167
/**
@@ -150,6 +184,16 @@ public void downloadSecureConnectBundle(String region, String destination) {
150184
.orElseThrow(() -> new RegionNotFoundException(region, databaseId)), destination);
151185
}
152186

187+
/**
188+
* Download SCB for a database and a datacenter in target location.
189+
*
190+
* @param dc
191+
* current region
192+
*/
193+
private byte[] downloadSecureConnectBundle(Datacenter dc) {
194+
return Utils.downloadFile(dc.getSecureBundleUrl());
195+
}
196+
153197
/**
154198
* Download SCB for a database and a datacenter in target location.
155199
*

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/utils/Utils.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package com.dtsx.astra.sdk.utils;
1818

1919
import java.io.BufferedInputStream;
20+
import java.io.ByteArrayOutputStream;
2021
import java.io.FileOutputStream;
2122
import java.io.IOException;
23+
import java.io.InputStream;
2224
import java.net.HttpURLConnection;
2325
import java.net.MalformedURLException;
2426
import java.net.URL;
@@ -57,7 +59,32 @@ public static boolean hasAllLength(String... lStr) {
5759
if (null == lStr) return false;
5860
return Arrays.stream(lStr).allMatch(Utils::hasLength);
5961
}
60-
62+
63+
/**
64+
* Download File Content.
65+
*
66+
* @param fileUrl
67+
* current file URL
68+
* @return
69+
* the file content
70+
*/
71+
public static byte[] downloadFile(String fileUrl) {
72+
try {
73+
URL url = new URL(fileUrl);
74+
try (InputStream in = new BufferedInputStream(url.openStream());
75+
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
76+
byte[] buffer = new byte[1024];
77+
int bytesRead;
78+
while ((bytesRead = in.read(buffer, 0, buffer.length)) != -1) {
79+
out.write(buffer, 0, bytesRead);
80+
}
81+
return out.toByteArray();
82+
}
83+
} catch(IOException ioe) {
84+
throw new IllegalArgumentException("Cannot download file",ioe);
85+
}
86+
}
87+
6188
/**
6289
* downloadFile
6390
*

astra-sdk-devops/src/test/java/com/dtsx/astra/sdk/db/DatabaseClientTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public void shouldDownloadDefaultScbTest() {
7878
// Then
7979
Assertions.assertTrue(new File(randomFile).exists());
8080
getSdkTestDatabaseClient().downloadSecureConnectBundle(SDK_TEST_DB_REGION, randomFile);
81+
// When
82+
byte[] data = getSdkTestDatabaseClient().downloadDefaultSecureConnectBundle();
83+
// Then
84+
Assertions.assertTrue(data != null && data.length > 1000);
8185
}
8286

8387
@Test
@@ -93,8 +97,13 @@ public void shouldDownloadRegionScbTest() {
9397
// When
9498
getSdkTestDatabaseClient().downloadSecureConnectBundle(SDK_TEST_DB_REGION, randomFile);
9599
Assertions.assertTrue(new File(randomFile).exists());
100+
// When
101+
byte[] data = getSdkTestDatabaseClient().downloadSecureConnectBundle(SDK_TEST_DB_REGION);
102+
// Then
103+
Assertions.assertTrue(data != null && data.length > 1000);
96104
}
97105

106+
98107
@Test
99108
@Order(5)
100109
@DisplayName("05. Download All Cloud Secured Bundle")

astra-spring-boot-3x-autoconfigure/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</parent>
1313

1414
<properties>
15-
<spring-boot.version>3.1.0</spring-boot.version>
15+
<spring-boot.version>3.1.1</spring-boot.version>
1616
</properties>
1717

1818
<dependencies>

0 commit comments

Comments
 (0)