Skip to content

Commit 61e1f91

Browse files
committed
Fix code
1 parent 1bd342f commit 61e1f91

File tree

5 files changed

+97
-11
lines changed

5 files changed

+97
-11
lines changed

src/intTest/java/com/box/sdk/BoxFileIT.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,50 @@ public void uploadAndDownloadFileSucceeds() throws IOException {
225225

226226
}
227227

228+
@Test
229+
public void uploadAndDownloadFileUseZstdSucceeds() throws IOException {
230+
BoxAPIConnection api = jwtApiForServiceAccount();
231+
api.setUseZstdCompression(true);
232+
233+
String fileName = "smalltest.pdf";
234+
URL fileURL = this.getClass().getResource("/sample-files/" + fileName);
235+
String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8");
236+
byte[] fileContent = readAllBytes(filePath);
237+
BoxFile file = null;
238+
try {
239+
file = uploadSampleFileToUniqueFolder(api, fileName);
240+
ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
241+
ProgressListener mockDownloadListener = mock(ProgressListener.class);
242+
file.download(downloadStream, mockDownloadListener);
243+
byte[] downloadedFileContent = downloadStream.toByteArray();
244+
assertThat(downloadedFileContent, is(equalTo(fileContent)));
245+
} finally {
246+
deleteFile(file);
247+
}
248+
}
249+
250+
@Test
251+
public void uploadAndDownloadFileDisabledZstdSucceeds() throws IOException {
252+
BoxAPIConnection api = jwtApiForServiceAccount();
253+
api.setUseZstdCompression(false);
254+
255+
String fileName = "smalltest.pdf";
256+
URL fileURL = this.getClass().getResource("/sample-files/" + fileName);
257+
String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8");
258+
byte[] fileContent = readAllBytes(filePath);
259+
BoxFile file = null;
260+
try {
261+
file = uploadSampleFileToUniqueFolder(api, fileName);
262+
ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
263+
ProgressListener mockDownloadListener = mock(ProgressListener.class);
264+
file.download(downloadStream, mockDownloadListener);
265+
byte[] downloadedFileContent = downloadStream.toByteArray();
266+
assertThat(downloadedFileContent, is(equalTo(fileContent)));
267+
} finally {
268+
deleteFile(file);
269+
}
270+
}
271+
228272
@Test
229273
public void downloadFileRangeSucceeds() throws IOException {
230274
BoxAPIConnection api = jwtApiForServiceAccount();

src/main/java/com/box/sdk/BoxAPIConnection.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public class BoxAPIConnection {
122122
private int maxRetryAttempts;
123123
private int connectTimeout;
124124
private int readTimeout;
125+
private Boolean useZstdCompression;
125126
private final List<BoxAPIConnectionListener> listeners;
126127
private RequestInterceptor interceptor;
127128
private final Map<String, String> customHeaders;
@@ -160,6 +161,7 @@ public BoxAPIConnection(String clientID, String clientSecret, String accessToken
160161
this.maxRetryAttempts = BoxGlobalSettings.getMaxRetryAttempts();
161162
this.connectTimeout = BoxGlobalSettings.getConnectTimeout();
162163
this.readTimeout = BoxGlobalSettings.getReadTimeout();
164+
this.useZstdCompression = BoxGlobalSettings.getUseZstdCompression();
163165
this.refreshLock = new ReentrantReadWriteLock();
164166
this.userAgent = "Box Java SDK v" + SDK_VERSION + " (Java " + JAVA_VERSION + ")";
165167
this.listeners = new ArrayList<>();
@@ -237,7 +239,9 @@ private void buildHttpClients() {
237239
}
238240
}
239241
builder = modifyHttpClientBuilder(builder);
240-
builder.addInterceptor(new ZstdInterceptor());
242+
if (this.useZstdCompression) {
243+
builder.addNetworkInterceptor(new ZstdInterceptor());
244+
}
241245

242246
this.httpClient = builder.build();
243247
this.noRedirectsHttpClient = new OkHttpClient.Builder(httpClient)
@@ -658,6 +662,23 @@ public void setConnectTimeout(int connectTimeout) {
658662
buildHttpClients();
659663
}
660664

665+
/*
666+
* Gets if request use zstd encoding when possible
667+
* @return true if request use zstd encoding when possible
668+
*/
669+
public Boolean getUseZstdCompression() {
670+
return this.useZstdCompression;
671+
}
672+
673+
/*
674+
* Sets if request use zstd encoding when possible
675+
* @param useZstdCompression true if request use zstd encoding when possible
676+
*/
677+
public void setUseZstdCompression(Boolean useZstdCompression) {
678+
this.useZstdCompression = useZstdCompression;
679+
buildHttpClients();
680+
}
681+
661682
/**
662683
* Gets the read timeout for this connection in milliseconds.
663684
*

src/main/java/com/box/sdk/BoxGlobalSettings.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public final class BoxGlobalSettings {
77
private static int connectTimeout = 0;
88
private static int readTimeout = 0;
99
private static int maxRetryAttempts = BoxAPIConnection.DEFAULT_MAX_RETRIES;
10+
private static boolean useZstdCompression = true;
1011

1112
private BoxGlobalSettings() {
1213
}
@@ -67,4 +68,20 @@ public static int getMaxRetryAttempts() {
6768
public static void setMaxRetryAttempts(int attempts) {
6869
BoxGlobalSettings.maxRetryAttempts = attempts;
6970
}
71+
72+
/*
73+
* Returns the global settings for using Zstd compression.
74+
* @return true if Zstd compression is enabled, false otherwise
75+
*/
76+
public static boolean getUseZstdCompression() {
77+
return useZstdCompression;
78+
}
79+
80+
/*
81+
* Sets the global settings for using Zstd compression.
82+
* @param useZstdCompression true to enable Zstd compression, false otherwise
83+
*/
84+
public static void setUseZstdCompression(boolean useZstdCompression) {
85+
BoxGlobalSettings.useZstdCompression = useZstdCompression;
86+
}
7087
}

src/main/java/com/box/sdk/ZstdInterceptor.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
package com.box.sdk;
22

33
import com.github.luben.zstd.ZstdInputStream;
4-
import java.util.List;
5-
import okhttp3.*;
64
import java.io.ByteArrayOutputStream;
7-
85
import java.io.IOException;
6+
import okhttp3.Interceptor;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
import okhttp3.ResponseBody;
10+
import org.jetbrains.annotations.NotNull;
911

1012
/**
1113
* Interceptor that adds zstd compression support to API requests.
1214
* This interceptor adds zstd to the Accept-Encoding header and handles decompression of zstd responses.
1315
*/
1416
public class ZstdInterceptor implements Interceptor {
17+
@NotNull
1518
@Override
1619
public Response intercept(Chain chain) throws IOException {
1720
Request request = chain.request();
1821

1922
// Add zstd to the Accept-Encoding header
2023
String acceptEncoding;
21-
List<String> acceptEncodingHeaders = request.headers("Accept-Encoding");
22-
if (acceptEncodingHeaders.isEmpty()) {
24+
String acceptEncodingHeader = request.header("Accept-Encoding");
25+
if (acceptEncodingHeader == null) {
2326
acceptEncoding = "zstd";
2427
} else {
25-
acceptEncoding = acceptEncodingHeaders.get(0) + ", zstd";
28+
acceptEncoding = acceptEncodingHeader + ", zstd";
2629
}
2730

2831
Request compressedRequest = request.newBuilder()
32+
.removeHeader("Accept-Encoding")
2933
.addHeader("Accept-Encoding", acceptEncoding)
3034
.build();
3135

@@ -55,13 +59,13 @@ public Response intercept(Chain chain) throws IOException {
5559

5660
// Create a new response body that serves the buffered content
5761
ResponseBody decompressedBody = ResponseBody.create(
58-
originalBody.contentType(),
59-
decompressedBytes
62+
decompressedBytes,
63+
originalBody.contentType()
6064
);
6165

6266
return response.newBuilder()
6367
.body(decompressedBody)
64-
.removeHeader("Content-Encoding")
68+
.removeHeader("X-Content-Encoding")
6569
.removeHeader("Content-Length")
6670
.build();
6771
}

src/test/java/com/box/sdk/BoxAPIRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public void handlesZstdResponse() throws IOException {
356356
BoxAPIConnection api = createConnectionWith(boxMockUrl().toString());
357357
BoxAPIRequest request = new BoxAPIRequest(api, boxMockUrl(), "GET");
358358
String jsonString = "{\"foo\":\"bar\"}";
359-
359+
360360
stubFor(get(urlEqualTo("/")).willReturn(
361361
aResponse()
362362
.withStatus(200)

0 commit comments

Comments
 (0)