Skip to content

Commit bc35ef3

Browse files
authored
fix: buffered body write and fixed SDK logging (#1079)
1 parent 17aa75c commit bc35ef3

9 files changed

Lines changed: 148 additions & 91 deletions

File tree

src/example/java/com/box/sdk/example/AccessAsAppUser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import com.box.sdk.BoxDeveloperEditionAPIConnection;
55
import com.box.sdk.BoxFolder;
66
import com.box.sdk.BoxItem;
7+
import com.box.sdk.BoxLogger;
78
import com.box.sdk.BoxUser;
89
import com.box.sdk.IAccessTokenCache;
910
import com.box.sdk.InMemoryLRUAccessTokenCache;
1011
import java.io.FileReader;
1112
import java.io.IOException;
1213
import java.io.Reader;
13-
import java.util.logging.Level;
14-
import java.util.logging.Logger;
1514

1615

1716
public final class AccessAsAppUser {
@@ -25,7 +24,7 @@ private AccessAsAppUser() {
2524

2625
public static void main(String[] args) throws IOException {
2726
// Limit logging messages to prevent polluting the output.
28-
Logger.getLogger("com.box.sdk").setLevel(Level.WARNING);
27+
BoxLogger.defaultLogger().setLevelToWarning();
2928

3029
//It is a best practice to use an access token cache to prevent unneeded requests to Box for access tokens.
3130
//For production applications it is recommended to use a distributed cache like Memcached or Redis, and to

src/example/java/com/box/sdk/example/BoxDeveloperEditionAPIConnectionAsEnterpriseUser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import com.box.sdk.BoxConfig;
44
import com.box.sdk.BoxDeveloperEditionAPIConnection;
5+
import com.box.sdk.BoxLogger;
56
import com.box.sdk.BoxUser;
67
import com.box.sdk.DeveloperEditionEntityType;
78
import com.box.sdk.IAccessTokenCache;
89
import com.box.sdk.InMemoryLRUAccessTokenCache;
910
import java.io.FileReader;
1011
import java.io.IOException;
1112
import java.io.Reader;
12-
import java.util.logging.Level;
13-
import java.util.logging.Logger;
1413

1514
public final class BoxDeveloperEditionAPIConnectionAsEnterpriseUser {
1615

@@ -25,7 +24,7 @@ private BoxDeveloperEditionAPIConnectionAsEnterpriseUser() {
2524

2625
public static void main(String[] args) throws IOException {
2726
// Limit logging messages to prevent polluting the output.
28-
Logger.getLogger("com.box.sdk").setLevel(Level.WARNING);
27+
BoxLogger.defaultLogger().setLevelToWarning();
2928

3029
//It is a best practice to use an access token cache to prevent unneeded requests to Box for access tokens.
3130
//For production applications it is recommended to use a distributed cache like Memcached or Redis, and to

src/example/java/com/box/sdk/example/CreateAppUser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import com.box.sdk.BoxConfig;
44
import com.box.sdk.BoxDeveloperEditionAPIConnection;
5+
import com.box.sdk.BoxLogger;
56
import com.box.sdk.BoxUser;
67
import com.box.sdk.CreateUserParams;
78
import com.box.sdk.IAccessTokenCache;
89
import com.box.sdk.InMemoryLRUAccessTokenCache;
910
import java.io.FileReader;
1011
import java.io.IOException;
1112
import java.io.Reader;
12-
import java.util.logging.Level;
13-
import java.util.logging.Logger;
1413

1514

1615
public final class CreateAppUser {
@@ -24,7 +23,7 @@ private CreateAppUser() {
2423

2524
public static void main(String[] args) throws IOException {
2625
// Limit logging messages to prevent polluting the output.
27-
Logger.getLogger("com.box.sdk").setLevel(Level.WARNING);
26+
BoxLogger.defaultLogger().setLevelToWarning();
2827

2928

3029
//It is a best practice to use an access token cache to prevent unneeded requests to Box for access tokens.

src/example/java/com/box/sdk/example/Main.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import com.box.sdk.BoxAPIConnection;
44
import com.box.sdk.BoxFolder;
55
import com.box.sdk.BoxItem;
6+
import com.box.sdk.BoxLogger;
67
import com.box.sdk.BoxUser;
7-
import java.util.logging.Level;
8-
import java.util.logging.Logger;
98

109
public final class Main {
1110
private static final String DEVELOPER_TOKEN = "";
@@ -16,7 +15,7 @@ private Main() {
1615

1716
public static void main(String[] args) {
1817
// Limit logging messages to prevent polluting the output.
19-
Logger.getLogger("com.box.sdk").setLevel(Level.WARNING);
18+
BoxLogger.defaultLogger().setLevelToWarning();
2019

2120
BoxAPIConnection api = new BoxAPIConnection(DEVELOPER_TOKEN);
2221

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
import java.nio.file.Files;
88
import java.nio.file.Paths;
99
import java.util.Properties;
10-
import java.util.logging.ConsoleHandler;
11-
import java.util.logging.Handler;
12-
import java.util.logging.Level;
13-
import java.util.logging.Logger;
1410

1511

1612
final class TestConfig {
@@ -31,35 +27,6 @@ final class TestConfig {
3127
private TestConfig() {
3228
}
3329

34-
public static Logger enableLogger(String levelString) {
35-
Level level = Level.parse(levelString);
36-
Logger logger = Logger.getLogger("com.box.sdk");
37-
logger.setLevel(level);
38-
39-
boolean hasConsoleHandler = false;
40-
for (Handler handler : logger.getHandlers()) {
41-
handler.setLevel(level);
42-
if (handler instanceof ConsoleHandler) {
43-
hasConsoleHandler = true;
44-
}
45-
}
46-
47-
if (!hasConsoleHandler) {
48-
Handler handler = new ConsoleHandler();
49-
handler.setLevel(level);
50-
logger.addHandler(handler);
51-
}
52-
return logger;
53-
}
54-
55-
public static BoxAPIConnection getAPIConnection() {
56-
BoxAPIConnection api = new BoxAPIConnection("");
57-
api.setBaseURL("http://localhost:53621/");
58-
api.setBaseUploadURL("http://localhost:53621/");
59-
60-
return api;
61-
}
62-
6330
public static String getAccessToken() {
6431
if (accessToken == null || accessToken.equals("")) {
6532
accessToken = getProperty("accessToken");

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.box.sdk;
22

3+
import static java.lang.String.format;
4+
35
import com.box.sdk.http.HttpHeaders;
46
import com.box.sdk.http.HttpMethod;
57
import com.eclipsesource.json.Json;
@@ -44,6 +46,7 @@ public class BoxAPIRequest {
4446
private static final BoxLogger LOGGER = BoxLogger.defaultLogger();
4547
private static final int MAX_REDIRECTS = 3;
4648
private static final String ERROR_CREATING_REQUEST_BODY = "Error creating request body";
49+
private static final int BUFFER_SIZE = 8192;
4750
private static SSLSocketFactory sslSocketFactory;
4851

4952
static {
@@ -439,7 +442,7 @@ public BoxAPIResponse send(ProgressListener listener) {
439442
}
440443

441444
LOGGER.warn(
442-
String.format("Retrying request due to transient error status=%d body=%s",
445+
format("Retrying request due to transient error status=%d body=%s",
443446
apiException.getResponseCode(),
444447
apiException.getResponse())
445448
);
@@ -510,7 +513,7 @@ BoxFileUploadSessionPart sendForUploadPart(BoxFileUploadSession session, long of
510513
} catch (BoxAPIException e) {
511514
}
512515
}
513-
LOGGER.warn(String.format(
516+
LOGGER.warn(format(
514517
"Retrying request due to transient error status=%d body=%s",
515518
apiException.getResponseCode(),
516519
apiException.getResponse()
@@ -618,17 +621,22 @@ protected void writeBody(HttpURLConnection connection, ProgressListener listener
618621
if (listener != null) {
619622
output = new ProgressOutputStream(output, listener, this.bodyLength);
620623
}
621-
int b = this.body.read();
622-
while (b != -1) {
623-
output.write(b);
624-
b = this.body.read();
625-
}
624+
writeWithBuffer(output);
626625
output.close();
627626
} catch (IOException e) {
628627
throw new BoxAPIException(ERROR_CREATING_REQUEST_BODY, e);
629628
}
630629
}
631630

631+
private void writeWithBuffer(OutputStream output) throws IOException {
632+
byte[] buffer = new byte[BUFFER_SIZE];
633+
int b = this.body.read(buffer);
634+
while (b != -1) {
635+
output.write(buffer, 0, b);
636+
b = this.body.read(buffer);
637+
}
638+
}
639+
632640
/**
633641
* Resets the InputStream containing this request's body.
634642
*
@@ -702,11 +710,14 @@ private BoxAPIResponse trySend(ProgressListener listener) {
702710

703711
int responseCode;
704712
try {
713+
long writeStart = System.currentTimeMillis();
705714
this.writeBody(connection, listener);
706-
715+
logDebug(format("[trySend] Body write took %dms%n", (System.currentTimeMillis() - writeStart)));
707716
// Ensure that we're connected in case writeBody() didn't write anything.
708717
try {
718+
long start = System.currentTimeMillis();
709719
connection.connect();
720+
logDebug(format("[trySend] connection.connect() took %dms%n", (System.currentTimeMillis() - start)));
710721
} catch (IOException e) {
711722
throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
712723
}
@@ -717,7 +728,11 @@ private BoxAPIResponse trySend(ProgressListener listener) {
717728
// happens correctly. There seems to be a bug in Oracle's Java implementation where automatically handled
718729
// redirects will not keep the connection alive.
719730
try {
731+
long getResponseStart = System.currentTimeMillis();
720732
responseCode = connection.getResponseCode();
733+
logDebug(format(
734+
"[trySend] Get Response (read network) took %dms%n", System.currentTimeMillis() - getResponseStart
735+
));
721736
} catch (IOException e) {
722737
throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
723738
}
@@ -780,12 +795,16 @@ private BoxAPIResponse handleRedirect(HttpURLConnection connection, ProgressList
780795
}
781796
}
782797

783-
private void logRequest() {
798+
private void logDebug(String message) {
784799
if (LOGGER.isDebugEnabled()) {
785-
LOGGER.debug(this.toString());
800+
LOGGER.debug(message);
786801
}
787802
}
788803

804+
private void logRequest() {
805+
logDebug(this.toString());
806+
}
807+
789808
private HttpURLConnection createConnection() {
790809
HttpURLConnection connection;
791810

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ public JsonValue getBodyAsJsonValue() {
9595

9696
@Override
9797
protected String bodyToString() {
98-
return this.jsonValue.toString();
98+
return this.jsonValue != null ? this.jsonValue.toString() : null;
9999
}
100100
}

0 commit comments

Comments
 (0)