Skip to main content

Hi,



I’m new using Box.com’s Java SDK. The application I’m working is trying to use Java to connect to Box.com in order to download a file. The application is a Spring Boot project, version 2.5.9, compiled using Java 8.



The code below “fails” in method: BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig, tokenCache). The method does not throw an error and never comes back from the call. The thread this process is in Stops.



         try { 

Reader reader = new FileReader("D:/Box/xxxxxxx_config.json");

BoxConfig boxConfig = BoxConfig.readFrom(reader);

IAccessTokenCache tokenCache = new InMemoryLRUAccessTokenCache(100);



BoxDeveloperEditionAPIConnection api = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig, tokenCache);



BoxFile file = new BoxFile(api, "1081XXXXXXXXX");

BoxFile.Info info = file.getInfo();



FileOutputStream stream = new FileOutputStream(info.getName());

file.download(stream);



try {

stream.close();

} catch (IOException e) {

System.out.println("Failed to close output stream" + e);

}

} catch (BoxAPIException e) {

System.out.println("Error creating Box enterprise client");

e.printStackTrace();

} catch (IOException e1) {

System.out.println("Error creating Box enterprise client:" + e1);

e1.printStackTrace();

} catch (Exception e2) {

System.out.println("Error - Run time exception" + e2);

e2.printStackTrace();

}





Debugging indicates that the call to new OkHttpClient.Builder(this.httpClient) in method BoxAPIConnection.buildHttpClients() fails.



  private void buildHttpClients() {

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();

if (this.trustManager != null) {

try {

SSLContext sslContext = SSLContext.getInstance("SSL");

sslContext.init((KeyManagerK])null, new TrustManageru]{this.trustManager}, new SecureRandom());

httpClientBuilder.sslSocketFactory(sslContext.getSocketFactory(), this.trustManager);

} catch (KeyManagementException | NoSuchAlgorithmException var3) {

throw new RuntimeException(var3);

}

}



OkHttpClient.Builder builder = httpClientBuilder.followSslRedirects(true).followRedirects(true).connectTimeout(Duration.ofMillis((long)this.connectTimeout)).readTimeout(Duration.ofMillis((long)this.readTimeout)).connectionSpecs(Collections.singletonList(ConnectionSpec.MODERN_TLS));

if (this.hostnameVerifier != null) {

httpClientBuilder.hostnameVerifier(this.hostnameVerifier);

}



if (this.proxy != null) {

builder.proxy(this.proxy);

if (this.proxyUsername != null && this.proxyPassword != null) {

builder.proxyAuthenticator((route, response) -> {

String credential = Credentials.basic(this.proxyUsername, this.proxyPassword);

return response.request().newBuilder().header("Proxy-Authorization", credential).build();

});

}



if (this.authenticator != null) {

builder.proxyAuthenticator(this.authenticator);

}

}



builder = this.modifyHttpClientBuilder(builder);

this.httpClient = builder.build();

this.noRedirectsHttpClient = (**new OkHttpClient.Builder(this.httpClient)**).followSslRedirects(false).followRedirects(false).build();

}



I’ve tried SDK versions 4.0.0, 4.6.1 as well as 4.7.0, with the same result.



Any advice on how to solve this issue would be helpful.



Thanks in advance,



Bob

Hi @Bob_B, welcome to the forum.



I’m not versed in Java, let me see if I can get someone from the Java SDK team to help.



Cheers


I think we’ve may have gotten this solved. We upgrade the Spring Boot version to 2.7.18.



Let me get back to you if that is not the issue.


Reply