I answer on my question myself.
Original problem "connection still allocated" was really actual as was assumed in the question above. Root cause was a need of
thread-safe connection (for HTTP client in Box SDK)... But as this code are in the Box SDK, the single thing that I was able to try it's use of BoxConnectionManager (which provides thread-safe).
But when I had used BoxConnectionManager
I faced with the second error mentioned: hostname in certificate didn't match. This error is due to different versions of Apache HTTP client on the classpath. My server (eXo Platform 4.0) provides Apache HTTP client 4.1.2, but Box SDK requires 4.2.5. After installation of my app, the server libs has both these JARs and 4.1.2 appears as loaded for the Box. If remove JAR of 4.1.2, then everything works as expected and those who can do this way will not face with SSL cert error as I did. But I cannot do this way as other parts of the eXo certified with HTTP client 4.1.2 and I don't want get in risk for them.
My solution is use of custom BoxRESTClient
in BoxClient
. Thanks to the Box team it's possible to create an instance of their client with low-level details such as the REST client. The BoxConnectionManager
hides this by providing an own BoxRESTClient
, but it is also possible to create this REST client from external code, without using the manager. My custom REST client adopted to HTTP client 4.1.2 and has "allow-all" hostname verifier.
So far, my solution is acceptable and does work well. Below the code snippets that I used:
How I create Box client:
BoxResourceHub hub = new BoxResourceHub();
BoxJSONParser parser = new BoxJSONParser(hub);
this.client = new BoxClient(key,
clientSecret,
hub,
parser,
new RESTClient(),
new BoxConfigBuilder().build());
And custom RESTClient
:
class RESTClient extends BoxRESTClient {
final HttpClient httpClient;
@SuppressWarnings("deprecation")
RESTClient() {
super();
SchemeRegistry schemeReg = new SchemeRegistry();
schemeReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
SSLSocketFactory socketFactory;
try {
socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS,
null,
null,
null,
null,
null,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
} catch (Exception ex) {
throw new IllegalStateException("Failure initializing default SSL context for Box REST client", ex);
}
schemeReg.register(new Scheme("https", socketFactory, 443));
ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(new BasicHttpParams(), schemeReg);
this.httpClient = new DefaultHttpClient(connectionManager);
}
@Override
public HttpClient getRawHttpClient() {
return httpClient;
}
}
This way the Box SDK works well and can upload multiple file to the user Box.