Skip to content

Commit 71a3017

Browse files
authored
Merge pull request #201 from nbehrens/mergePrivateToPublic
Add maxEventsPerBatch and fix for `deleteAllFilesInBucket`
2 parents 51c7ac0 + 34e04d2 commit 71a3017

File tree

11 files changed

+125
-13
lines changed

11 files changed

+125
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22
## [Unreleased] - TBD
3+
### Added
4+
* Fixed `B2StorageClient.deleteAllFilesInBucket` so it uses `fileVersions` instead of `fileNames`.
5+
* Added `maxEventsPerBatch` field to `B2WebhookConfiguration`.
6+
7+
### Changed
8+
* Validate idle connections after 2 seconds of inactivity
39

410
## [6.2.1] - 2024-07-16
511
### Added
@@ -62,7 +68,7 @@
6268
* Fixed B2ListFilesIterableBase assuming a response with 0 results was the end. It now looks for
6369
`nextFileName` being null to indicate the end.
6470

65-
### [6.1.0] - 2022-09-19
71+
## [6.1.0] - 2022-09-19
6672
### Added
6773
* Added support for custom upload timestamps
6874
* Added support for Java 8's `-parameters` option so constructor parameters do not need to be reiterated in `B2Json.constructor#params`

core/src/main/java/com/backblaze/b2/client/B2StorageClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ default void deleteFileVersion(String fileName,
775775
* file versions have been deleted (if any) and which haven't (if any).
776776
*/
777777
default void deleteAllFilesInBucket(String bucketId) throws B2Exception {
778-
for (B2FileVersion fileVersion: fileNames(bucketId)) {
778+
for (B2FileVersion fileVersion: fileVersions(bucketId)) {
779779
deleteFileVersion(fileVersion);
780780
}
781781
}

core/src/main/java/com/backblaze/b2/client/structures/B2WebhookConfiguration.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,42 @@ public class B2WebhookConfiguration extends B2EventNotificationTargetConfigurati
2727
@B2Json.optional
2828
private final String hmacSha256SigningSecret;
2929

30+
/**
31+
* An optional maximum number of events to batch into a single webhook request.
32+
*/
33+
@B2Json.optional(omitNull = true)
34+
private final Integer maxEventsPerBatch;
35+
3036
@B2Json.constructor
3137
public B2WebhookConfiguration(String url,
3238
TreeSet<B2WebhookCustomHeader> customHeaders,
33-
String hmacSha256SigningSecret) {
39+
String hmacSha256SigningSecret,
40+
Integer maxEventsPerBatch) {
3441
B2Preconditions.checkArgument(
3542
url != null && url.startsWith("https://"),
3643
"The protocol for the url must be https://"
3744
);
45+
B2Preconditions.checkArgument(
46+
maxEventsPerBatch == null || (maxEventsPerBatch > 0 && maxEventsPerBatch <= 50),
47+
"The events per batch must be between 1 and 50"
48+
);
3849

3950
this.url = url;
4051
this.customHeaders = customHeaders;
4152
this.hmacSha256SigningSecret = hmacSha256SigningSecret;
53+
this.maxEventsPerBatch = maxEventsPerBatch;
4254
}
4355

4456
public B2WebhookConfiguration(String url) {
45-
this(url, null, null);
57+
this(url, null, null, null);
4658
}
4759

4860
public B2WebhookConfiguration(String url, TreeSet<B2WebhookCustomHeader> customHeaders) {
49-
this(url, customHeaders, null);
61+
this(url, customHeaders, null, null);
5062
}
5163

5264
public B2WebhookConfiguration(String url, String hmacSha256SigningSecret) {
53-
this(url, null, hmacSha256SigningSecret);
65+
this(url, null, hmacSha256SigningSecret, null);
5466
}
5567

5668
public String getUrl() {
@@ -68,14 +80,19 @@ public String getHmacSha256SigningSecret() {
6880
return hmacSha256SigningSecret;
6981
}
7082

83+
public Integer getMaxEventsPerBatch() {
84+
return maxEventsPerBatch;
85+
}
86+
7187
@Override
7288
public boolean equals(Object o) {
7389
if (this == o) return true;
7490
if (o == null || getClass() != o.getClass()) return false;
7591
final B2WebhookConfiguration that = (B2WebhookConfiguration) o;
7692
return url.equals(that.url) &&
7793
Objects.equals(customHeaders, that.customHeaders) &&
78-
Objects.equals(hmacSha256SigningSecret, that.hmacSha256SigningSecret);
94+
Objects.equals(hmacSha256SigningSecret, that.hmacSha256SigningSecret) &&
95+
Objects.equals(maxEventsPerBatch, that.maxEventsPerBatch);
7996
}
8097

8198
@Override

core/src/test/java/com/backblaze/b2/client/B2StorageClientImplTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,24 @@ public void testDeleteFileVersion() throws B2Exception {
521521

522522
}
523523

524+
@Test
525+
public void testDeleteAllFilesInBucket() throws B2Exception {
526+
final B2ListFileVersionsRequest request = B2ListFileVersionsRequest.builder(bucketId(1)).setMaxFileCount(1000).build();
527+
final List<B2FileVersion> versions = new ArrayList<>();
528+
for (int i = 0; i < 10; i++) {
529+
versions.add(makeVersion(i, i));
530+
}
531+
final B2ListFileVersionsResponse response = new B2ListFileVersionsResponse(versions, null, null);
532+
when(webifier.listFileVersions(anyObject(), eq(request))).thenReturn(response);
533+
534+
client.deleteAllFilesInBucket(bucketId(1));
535+
536+
for (B2FileVersion version : versions) {
537+
final B2DeleteFileVersionRequest deleteRequest = B2DeleteFileVersionRequest.builder(version.getFileName(), version.getFileId()).build();
538+
verify(webifier, times(1)).deleteFileVersion(anyObject(), eq(deleteRequest));
539+
}
540+
}
541+
524542
@Test
525543
public void testGetDownloadAuthorization() throws B2Exception {
526544
final B2DownloadAuthorization downloadAuth = new B2DownloadAuthorization(bucketId(1), FILE_PREFIX, "downloadAuthToken");

core/src/test/java/com/backblaze/b2/client/B2StorageClientWebifierImplTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,8 @@ public void testSetBucketNotificationRules() throws B2Exception {
19751975
new B2WebhookCustomHeader("name2", "val2")
19761976
)
19771977
),
1978-
"3XDfkdQte2OgA78qCtSD17LAzpj6ay9H"
1978+
"3XDfkdQte2OgA78qCtSD17LAzpj6ay9H",
1979+
20
19791980
),
19801981
true
19811982
),
@@ -2030,6 +2031,7 @@ public void testSetBucketNotificationRules() throws B2Exception {
20302031
" }\n" +
20312032
" ],\n" +
20322033
" \"hmacSha256SigningSecret\": \"3XDfkdQte2OgA78qCtSD17LAzpj6ay9H\",\n" +
2034+
" \"maxEventsPerBatch\": 20,\n" +
20332035
" \"targetType\": \"webhook\",\n" +
20342036
" \"url\": \"https://www.example.com\"\n" +
20352037
" }\n" +

core/src/test/java/com/backblaze/b2/client/structures/B2EventNotificationRuleTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void testToJsonAndBack() {
4040
" }\n" +
4141
" ],\n" +
4242
" \"hmacSha256SigningSecret\": \"3XDfkdQte2OgA78qCtSD17LAzpj6ay9H\",\n" +
43+
" \"maxEventsPerBatch\": 20,\n" +
4344
" \"targetType\": \"webhook\",\n" +
4445
" \"url\": \"https://www.example.com\"\n" +
4546
" }\n" +
@@ -66,7 +67,8 @@ public void testToJsonAndBack() {
6667
new B2WebhookCustomHeader("name2", "val2")
6768
)
6869
),
69-
"3XDfkdQte2OgA78qCtSD17LAzpj6ay9H"
70+
"3XDfkdQte2OgA78qCtSD17LAzpj6ay9H",
71+
20
7072
),
7173
true,
7274
false,

core/src/test/java/com/backblaze/b2/client/structures/B2GetBucketNotificationRulesResponseTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public void testFullGetBucketNotificationRulesResponse() {
3535
new B2WebhookCustomHeader("name2", "val2")
3636
)
3737
),
38-
"dummySigningSecret"),
38+
"dummySigningSecret",
39+
20),
3940
true,
4041
false,
4142
null
@@ -73,6 +74,7 @@ public void testFullGetBucketNotificationRulesResponse() {
7374
" }\n" +
7475
" ],\n" +
7576
" \"hmacSha256SigningSecret\": \"dummySigningSecret\",\n" +
77+
" \"maxEventsPerBatch\": 20,\n" +
7678
" \"targetType\": \"webhook\",\n" +
7779
" \"url\": \"https://www.example.com\"\n" +
7880
" }\n" +

core/src/test/java/com/backblaze/b2/client/structures/B2SetBucketNotificationRulesRequestTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public void testFullSetBucketNotificationRulesRequest() {
4242
new B2WebhookCustomHeader("name2", "val2")
4343
)
4444
),
45-
"rrzaVL6BqYt83s2Q5R2I79AilaxVBJUS"
45+
"rrzaVL6BqYt83s2Q5R2I79AilaxVBJUS",
46+
20
4647
),
4748
true
4849
)
@@ -95,6 +96,7 @@ public void testFullSetBucketNotificationRulesRequest() {
9596
" }\n" +
9697
" ],\n" +
9798
" \"hmacSha256SigningSecret\": \"rrzaVL6BqYt83s2Q5R2I79AilaxVBJUS\",\n" +
99+
" \"maxEventsPerBatch\": 20,\n" +
98100
" \"targetType\": \"webhook\",\n" +
99101
" \"url\": \"https://www.example.com\"\n" +
100102
" }\n" +

core/src/test/java/com/backblaze/b2/client/structures/B2SetBucketNotificationRulesResponseTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public void testFullSetBucketNotificationRulesResponse() {
4444
new B2WebhookCustomHeader("name2", "val2")
4545
)
4646
),
47-
"dummySigningSecret"),
47+
"dummySigningSecret",
48+
20),
4849
true,
4950
false,
5051
null
@@ -98,6 +99,7 @@ public void testFullSetBucketNotificationRulesResponse() {
9899
" }\n" +
99100
" ],\n" +
100101
" \"hmacSha256SigningSecret\": \"dummySigningSecret\",\n" +
102+
" \"maxEventsPerBatch\": 20,\n" +
101103
" \"targetType\": \"webhook\",\n" +
102104
" \"url\": \"https://www.example.com\"\n" +
103105
" }\n" +

core/src/test/java/com/backblaze/b2/client/structures/B2WebhookConfigurationTest.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void testUrlWithIncorrectProtocolThrows() {
3030
new B2WebhookCustomHeader("name2", "val2")
3131
)
3232
),
33+
null,
3334
null
3435
);
3536
fail("should have thrown");
@@ -39,6 +40,63 @@ public void testUrlWithIncorrectProtocolThrows() {
3940
}
4041
}
4142

43+
@Test
44+
public void testUrlWithHighMaxEventsPerBatchThrows() {
45+
try {
46+
new B2WebhookConfiguration(
47+
"https://www.backblaze.com",
48+
new TreeSet<>(
49+
listOf(
50+
new B2WebhookCustomHeader("name1", "val1"),
51+
new B2WebhookCustomHeader("name2", "val2")
52+
)
53+
),
54+
null,
55+
500
56+
);
57+
fail("should have thrown");
58+
}
59+
catch (IllegalArgumentException e) {
60+
assertEquals("The events per batch must be between 1 and 50", e.getMessage());
61+
}
62+
}
63+
64+
@Test
65+
public void testUrlWith50EventsPerBatchSucceeds() {
66+
new B2WebhookConfiguration(
67+
"https://www.backblaze.com",
68+
new TreeSet<>(
69+
listOf(
70+
new B2WebhookCustomHeader("name1", "val1"),
71+
new B2WebhookCustomHeader("name2", "val2")
72+
)
73+
),
74+
null,
75+
50
76+
);
77+
}
78+
79+
@Test
80+
public void testUrlWithZeroMaxEventsPerBatchThrows() {
81+
try {
82+
new B2WebhookConfiguration(
83+
"https://www.backblaze.com",
84+
new TreeSet<>(
85+
listOf(
86+
new B2WebhookCustomHeader("name1", "val1"),
87+
new B2WebhookCustomHeader("name2", "val2")
88+
)
89+
),
90+
null,
91+
0
92+
);
93+
fail("should have thrown");
94+
}
95+
catch (IllegalArgumentException e) {
96+
assertEquals("The events per batch must be between 1 and 50", e.getMessage());
97+
}
98+
}
99+
42100
@Test
43101
public void testToJsonAndBack() {
44102
final String jsonString = "{\n" +
@@ -53,6 +111,7 @@ public void testToJsonAndBack() {
53111
" }\n" +
54112
" ],\n" +
55113
" \"hmacSha256SigningSecret\": \"rrzaVL6BqYt83s2Q5R2I79AilaxVBJUS\",\n" +
114+
" \"maxEventsPerBatch\": 20,\n" +
56115
" \"targetType\": \"webhook\",\n" +
57116
" \"url\": \"https://www.example.com\"\n" +
58117
"}";
@@ -71,7 +130,8 @@ public void testToJsonAndBack() {
71130
new B2WebhookCustomHeader("name2", "val2")
72131
)
73132
),
74-
"rrzaVL6BqYt83s2Q5R2I79AilaxVBJUS"
133+
"rrzaVL6BqYt83s2Q5R2I79AilaxVBJUS",
134+
20
75135
);
76136
final String convertedJson = B2Json.toJsonOrThrowRuntime(defaultConfig);
77137
assertEquals(defaultConfig, converted);

httpclient/src/main/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ private HttpClientConnectionManager createConnectionManager() {
207207
final PoolingHttpClientConnectionManager mgr = new PoolingHttpClientConnectionManager(registry);
208208
mgr.setMaxTotal(maxTotalConnectionsInPool);
209209
mgr.setDefaultMaxPerRoute(maxConnectionsPerRoute);
210+
mgr.setValidateAfterInactivity(2000);
210211
return mgr;
211212
}
212213
}

0 commit comments

Comments
 (0)