Skip to content

Commit 45a2ba2

Browse files
author
a-brandt
committed
added HTTPS support
1 parent 16c95c3 commit 45a2ba2

File tree

10 files changed

+373
-45
lines changed

10 files changed

+373
-45
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ v2.7.0 (????-??-??)
22
---------------------------
33
* added document examples in src/test/java/com/arangodb/example/document/
44
* added graph examples in src/test/java/com/arangodb/example/document/
5+
* new function executeAqlQueryJSON(): Executes an AQL query and returns the raw JSON response as a String
6+
* initial support of HTTPS connections. Examples src/test/java/com/arangodb/example/ssl/
57

68

79
v2.6.9 (2015-10-16)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ The driver is configured with some default values:
7676
<tr><th>retryCount</th><td>http retry count</td><td>3</td></tr>
7777
<tr><th>defaultDatabase</th><td>default database</td><td></td></tr>
7878
<tr><th>enableCURLLogger</th><td>logging flag by curl format for debug</td><td>false</td></tr>
79+
<tr><th>useSsl</th><td>use HTTPS connection</td><td>false</td></tr>
80+
<tr><th>sslTrustStore</th><td>path to a trust-store</td><td>false</td></tr>
81+
7982
</table>
8083

8184
Since 2.5.4 you can configure a default and a fallback database:

src/main/java/com/arangodb/ArangoConfigure.java

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.List;
2323
import java.util.Properties;
2424

25+
import javax.net.ssl.SSLContext;
26+
2527
import org.slf4j.Logger;
2628
import org.slf4j.LoggerFactory;
2729

@@ -52,48 +54,54 @@ public class ArangoConfigure {
5254
/** default property file */
5355
private static final String DEFAULT_PROPERTY_FILE = "/arangodb.properties";
5456

55-
List<ArangoHost> arangoHosts;
56-
int currentArangoHost;
57+
private List<ArangoHost> arangoHosts;
58+
private int currentArangoHost;
5759

5860
/** connection timeout(ms) */
59-
int connectionTimeout = -1;
61+
private int connectionTimeout = -1;
6062
/** socket read timeout(ms) */
61-
int timeout = -1;
63+
private int timeout = -1;
6264

6365
/** max connection per configure */
64-
int maxTotalConnection;
66+
private int maxTotalConnection;
6567
/** max connection per host */
66-
int maxPerConnection;
68+
private int maxPerConnection;
6769

6870
/** Basic auth user */
69-
String user;
71+
private String user;
7072
/** Basic auth password */
71-
String password;
73+
private String password;
7274

7375
/** proxy-host */
74-
String proxyHost;
76+
private String proxyHost;
7577
/** proxy-port */
76-
int proxyPort;
78+
private int proxyPort;
7779

7880
/** http retry count */
79-
int retryCount = 3;
81+
private int retryCount = 3;
8082

8183
/**
8284
* number of connect retries (0 means infinite)
8385
*/
84-
int connectRetryCount = 3;
86+
private int connectRetryCount = 3;
8587

8688
/**
8789
* milliseconds
8890
*/
89-
int connectRetryWait = 1000;
91+
private int connectRetryWait = 1000;
9092

9193
/** Default Database */
9294
String defaultDatabase;
9395

94-
boolean enableCURLLogger = false;
96+
private boolean enableCURLLogger = false;
97+
98+
private boolean staleConnectionCheck = false;
99+
100+
private boolean useSsl = false;
101+
102+
private SSLContext sslContext = null;
95103

96-
boolean staleConnectionCheck = false;
104+
private String sslTrustStore = null;
97105

98106
/**
99107
* the default ArangoDB cursor batch size
@@ -247,6 +255,16 @@ public void loadProperties(String propertyPath) {
247255
setBatchSize(Integer.parseInt(batchSize));
248256
}
249257

258+
String useSsl = prop.getProperty("useSsl");
259+
if (useSsl != null) {
260+
setUseSsl(Boolean.parseBoolean(useSsl));
261+
}
262+
263+
String sslTrustStore = prop.getProperty("sslTrustStore");
264+
if (sslTrustStore != null) {
265+
setSslTrustStore(sslTrustStore);
266+
}
267+
250268
}
251269
} catch (IOException e) {
252270
logger.warn("load property error", e);
@@ -285,13 +303,13 @@ public void shutdown() {
285303
public String getBaseUrl() {
286304
ArangoHost currentHost = getCurrentHost();
287305

288-
return "http://" + currentHost.getHost() + ":" + currentHost.getPort();
306+
return (useSsl ? "https://" : "http://") + currentHost.getHost() + ":" + currentHost.getPort();
289307
}
290308

291309
public String getEndpoint() {
292310
ArangoHost currentHost = getCurrentHost();
293311

294-
return "tcp://" + currentHost.getHost() + ":" + currentHost.getPort();
312+
return (useSsl ? "ssl://" : "tcp://") + currentHost.getHost() + ":" + currentHost.getPort();
295313
}
296314

297315
private ArangoHost getCurrentHost() {
@@ -585,4 +603,48 @@ public void setBatchSize(int batchSize) {
585603
this.batchSize = batchSize;
586604
}
587605

606+
public boolean getUseSsl() {
607+
return useSsl;
608+
}
609+
610+
/**
611+
* Configure the client to use HTTPS or HTTP
612+
*
613+
* @param useSsl
614+
* set true to use HTTPS (default false)
615+
*/
616+
public void setUseSsl(boolean useSsl) {
617+
this.useSsl = useSsl;
618+
}
619+
620+
public SSLContext getSslContext() {
621+
return sslContext;
622+
}
623+
624+
/**
625+
* Set SSL contest for HTTPS connections.
626+
*
627+
* (do not use setSslTrustStore() together with setSslContext())
628+
*
629+
* @param sslContext
630+
*/
631+
public void setSslContext(SSLContext sslContext) {
632+
this.sslContext = sslContext;
633+
}
634+
635+
public String getSslTrustStore() {
636+
return sslTrustStore;
637+
}
638+
639+
/**
640+
* Set file name of trust store
641+
*
642+
* (do not use setSslTrustStore() together with setSslContext())
643+
*
644+
* @param sslTrustStore
645+
*/
646+
public void setSslTrustStore(String sslTrustStore) {
647+
this.sslTrustStore = sslTrustStore;
648+
}
649+
588650
}

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,25 +2471,26 @@ public <T> CursorResult<T> executeAqlQuery(
24712471

24722472
/**
24732473
* Executes an AQL query and returns the raw JSON response
2474-
* @param query an AQL query as string
2475-
* @param bindVars a map containing all bind variables,
2476-
* @param aqlQueryOptions AQL query options
2474+
*
2475+
* @param query
2476+
* an AQL query as string
2477+
* @param bindVars
2478+
* a map containing all bind variables,
2479+
* @param aqlQueryOptions
2480+
* AQL query options
24772481
* @return A JSON string with the results from server
24782482
* @throws ArangoException
24792483
*/
2480-
public String executeAqlQueryJSON(
2481-
String query,
2482-
Map<String, Object> bindVars,
2483-
AqlQueryOptions aqlQueryOptions
2484-
) throws ArangoException {
2485-
2486-
if (aqlQueryOptions == null) {
2487-
aqlQueryOptions = getDefaultAqlQueryOptions();
2488-
}
2484+
public String executeAqlQueryJSON(String query, Map<String, Object> bindVars, AqlQueryOptions aqlQueryOptions)
2485+
throws ArangoException {
24892486

2490-
return cursorDriver.executeAqlQueryJSON(getDefaultDatabase(), query, bindVars, aqlQueryOptions);
2487+
if (aqlQueryOptions == null) {
2488+
aqlQueryOptions = getDefaultAqlQueryOptions();
24912489
}
24922490

2491+
return cursorDriver.executeAqlQueryJSON(getDefaultDatabase(), query, bindVars, aqlQueryOptions);
2492+
}
2493+
24932494
/**
24942495
* This method executes an AQL query and returns a DocumentCursorResult
24952496
*
@@ -5756,4 +5757,12 @@ public DefaultEntity killQuery(String database, String id) throws ArangoExceptio
57565757
return this.cursorDriver.killQuery(database, id);
57575758
}
57585759

5760+
/**
5761+
* Returns the HTTP manager of the driver
5762+
*
5763+
* @return httpManager
5764+
*/
5765+
public HttpManager getHttpManager() {
5766+
return httpManager;
5767+
}
57595768
}

src/main/java/com/arangodb/http/HttpManager.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@
4949
import org.apache.http.client.methods.HttpPut;
5050
import org.apache.http.client.methods.HttpRequestBase;
5151
import org.apache.http.client.utils.URLEncodedUtils;
52+
import org.apache.http.config.Registry;
53+
import org.apache.http.config.RegistryBuilder;
5254
import org.apache.http.conn.ConnectionKeepAliveStrategy;
55+
import org.apache.http.conn.socket.ConnectionSocketFactory;
56+
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
57+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
5358
import org.apache.http.entity.ContentType;
5459
import org.apache.http.entity.StringEntity;
5560
import org.apache.http.impl.auth.BasicScheme;
@@ -62,6 +67,7 @@
6267
import org.apache.http.message.BasicNameValuePair;
6368
import org.apache.http.protocol.HTTP;
6469
import org.apache.http.protocol.HttpContext;
70+
import org.apache.http.ssl.SSLContexts;
6571
import org.slf4j.Logger;
6672
import org.slf4j.LoggerFactory;
6773

@@ -95,9 +101,7 @@ public class HttpManager {
95101
private Map<String, InvocationObject> jobs = new HashMap<String, InvocationObject>();
96102

97103
public static enum HttpMode {
98-
SYNC,
99-
ASYNC,
100-
FIREANDFORGET
104+
SYNC, ASYNC, FIREANDFORGET
101105
}
102106

103107
public HttpManager(ArangoConfigure configure) {
@@ -109,8 +113,23 @@ public ArangoConfigure getConfiguration() {
109113
}
110114

111115
public void init() {
116+
// socket factory for HTTP
117+
ConnectionSocketFactory plainsf = new PlainConnectionSocketFactory();
118+
119+
// socket factory for HTTPS
120+
SSLConnectionSocketFactory sslsf = null;
121+
if (configure.getSslContext() != null) {
122+
sslsf = new SSLConnectionSocketFactory(configure.getSslContext());
123+
} else {
124+
sslsf = new SSLConnectionSocketFactory(SSLContexts.createSystemDefault());
125+
}
126+
127+
// register socket factories
128+
Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory> create()
129+
.register("http", plainsf).register("https", sslsf).build();
130+
112131
// ConnectionManager
113-
cm = new PoolingHttpClientConnectionManager();
132+
cm = new PoolingHttpClientConnectionManager(r);
114133
cm.setDefaultMaxPerRoute(configure.getMaxPerConnection());
115134
cm.setMaxTotal(configure.getMaxTotalConnection());
116135

@@ -137,7 +156,8 @@ public void init() {
137156
@Override
138157
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
139158
// Honor 'keep-alive' header
140-
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
159+
HeaderElementIterator it = new BasicHeaderElementIterator(
160+
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
141161
while (it.hasNext()) {
142162
HeaderElement he = it.nextElement();
143163
String param = he.getName();
@@ -281,8 +301,11 @@ public HttpResponseEntity doPostWithHeaders(
281301
return doPostPutPatch(RequestType.POST, url, headers, params, body, entity);
282302
}
283303

284-
public HttpResponseEntity doPut(String url, Map<String, Object> headers, Map<String, Object> params, String bodyText)
285-
throws ArangoException {
304+
public HttpResponseEntity doPut(
305+
String url,
306+
Map<String, Object> headers,
307+
Map<String, Object> params,
308+
String bodyText) throws ArangoException {
286309
return doPostPutPatch(RequestType.PUT, url, headers, params, bodyText, null);
287310
}
288311

@@ -365,19 +388,19 @@ public HttpResponseEntity execute(HttpRequestEntity requestEntity) throws Arango
365388
* @return the response of the request
366389
* @throws ArangoException
367390
*/
368-
private HttpResponseEntity executeInternal(String baseUrl, HttpRequestEntity requestEntity) throws ArangoException,
369-
SocketException {
391+
private HttpResponseEntity executeInternal(String baseUrl, HttpRequestEntity requestEntity)
392+
throws ArangoException, SocketException {
370393

371394
String url = buildUrl(baseUrl, requestEntity);
372395

373396
if (logger.isDebugEnabled()) {
374397
if (requestEntity.type == RequestType.POST || requestEntity.type == RequestType.PUT
375398
|| requestEntity.type == RequestType.PATCH) {
376-
logger.debug("[REQ]http-{}: url={}, headers={}, body={}", new Object[] { requestEntity.type, url,
377-
requestEntity.headers, requestEntity.bodyText });
399+
logger.debug("[REQ]http-{}: url={}, headers={}, body={}",
400+
new Object[] { requestEntity.type, url, requestEntity.headers, requestEntity.bodyText });
378401
} else {
379-
logger.debug("[REQ]http-{}: url={}, headers={}", new Object[] { requestEntity.type, url,
380-
requestEntity.headers });
402+
logger.debug("[REQ]http-{}: url={}, headers={}",
403+
new Object[] { requestEntity.type, url, requestEntity.headers });
381404
}
382405
}
383406

0 commit comments

Comments
 (0)