16
16
17
17
package com .arangodb .example .ssl ;
18
18
19
+ import java .io .File ;
19
20
import java .io .IOException ;
20
21
import java .net .URISyntaxException ;
21
22
import java .net .URL ;
22
- import java .nio .file .Paths ;
23
23
import java .security .KeyManagementException ;
24
24
import java .security .KeyStoreException ;
25
25
import java .security .NoSuchAlgorithmException ;
37
37
import com .arangodb .ArangoHost ;
38
38
import com .arangodb .entity .ArangoVersion ;
39
39
import com .arangodb .http .HttpResponseEntity ;
40
+ import com .arangodb .util .TestUtils ;
40
41
41
42
/*-
42
43
* Example for using a HTTPS connection
@@ -75,67 +76,74 @@ public class SslExample {
75
76
@ Test
76
77
public void httpTest () throws ArangoException {
77
78
78
- ArangoConfigure configuration = new ArangoConfigure ();
79
+ final ArangoConfigure configuration = new ArangoConfigure ();
79
80
// get host and port from arangodb.properties
80
81
// configuration.setArangoHost(new ArangoHost("localhost", 8529));
81
82
configuration .init ();
82
83
83
- ArangoDriver arangoDriver = new ArangoDriver (configuration );
84
+ final ArangoDriver arangoDriver = new ArangoDriver (configuration );
84
85
85
- ArangoVersion version = arangoDriver .getVersion ();
86
+ final ArangoVersion version = arangoDriver .getVersion ();
86
87
Assert .assertNotNull (version );
87
88
88
89
}
89
90
90
91
@ Test
91
92
public void sslConnectionTest () throws ArangoException {
92
- // use HTTPS with java default trust store
93
-
94
- ArangoConfigure configuration = new ArangoConfigure ();
95
- configuration .setArangoHost (new ArangoHost ("www.arangodb.com" , 443 ));
96
- configuration .setUseSsl (true );
97
- configuration .init ();
98
-
99
- ArangoDriver arangoDriver = new ArangoDriver (configuration );
100
-
101
- HttpResponseEntity response = arangoDriver .getHttpManager ().doGet ("/" );
102
- Assert .assertEquals (200 , response .getStatusCode ());
93
+ final String javaVersion = System .getProperty ("java.version" );
94
+ if (TestUtils .compareVersion (javaVersion , "1.7" ) > -1 ) {
95
+ // use HTTPS with java default trust store
96
+ ArangoConfigure configuration = null ;
97
+ try {
98
+ configuration = new ArangoConfigure ();
99
+ configuration .setArangoHost (new ArangoHost ("www.arangodb.com" , 443 ));
100
+ configuration .setUseSsl (true );
101
+ configuration .init ();
102
+ final ArangoDriver arangoDriver = new ArangoDriver (configuration );
103
+ final HttpResponseEntity response = arangoDriver .getHttpManager ().doGet ("/" );
104
+ Assert .assertEquals (200 , response .getStatusCode ());
105
+ } finally {
106
+ if (configuration != null ) {
107
+ configuration .shutdown ();
108
+ }
109
+ }
110
+ }
103
111
}
104
112
105
113
@ Test
106
114
public void sslWithSelfSignedCertificateTest () throws ArangoException , KeyManagementException ,
107
115
NoSuchAlgorithmException , KeyStoreException , CertificateException , IOException , URISyntaxException {
108
116
109
117
// create a sslContext for the self signed certificate
110
- URL resource = this .getClass ().getResource (SSL_TRUSTSTORE );
111
- SSLContext sslContext = SSLContexts .custom ()
112
- .loadTrustMaterial (Paths . get (resource .toURI ()). toFile ( ), SSL_TRUSTSTORE_PASSWORD .toCharArray ()).build ();
118
+ final URL resource = this .getClass ().getResource (SSL_TRUSTSTORE );
119
+ final SSLContext sslContext = SSLContexts .custom ()
120
+ .loadTrustMaterial (new File (resource .toURI ()), SSL_TRUSTSTORE_PASSWORD .toCharArray ()).build ();
113
121
114
- ArangoConfigure configuration = new ArangoConfigure ("/ssl-arangodb.properties" );
122
+ final ArangoConfigure configuration = new ArangoConfigure ("/ssl-arangodb.properties" );
115
123
configuration .setSslContext (sslContext );
116
124
configuration .init ();
117
125
118
- ArangoDriver arangoDriver = new ArangoDriver (configuration );
126
+ final ArangoDriver arangoDriver = new ArangoDriver (configuration );
119
127
120
- ArangoVersion version = arangoDriver .getVersion ();
128
+ final ArangoVersion version = arangoDriver .getVersion ();
121
129
Assert .assertNotNull (version );
122
130
}
123
131
124
132
@ Test
125
133
public void sslHandshakeExceptionTest () {
126
- ArangoConfigure configuration = new ArangoConfigure ("/ssl-arangodb.properties" );
134
+ final ArangoConfigure configuration = new ArangoConfigure ("/ssl-arangodb.properties" );
127
135
configuration .init ();
128
136
129
- ArangoDriver arangoDriver = new ArangoDriver (configuration );
137
+ final ArangoDriver arangoDriver = new ArangoDriver (configuration );
130
138
131
139
try {
132
140
// java do not trust self signed certificates
133
141
134
142
arangoDriver .getVersion ();
135
143
Assert .fail ("this should fail" );
136
144
137
- } catch (ArangoException e ) {
138
- Throwable cause = e .getCause ();
145
+ } catch (final ArangoException e ) {
146
+ final Throwable cause = e .getCause ();
139
147
Assert .assertTrue (cause instanceof javax .net .ssl .SSLHandshakeException );
140
148
}
141
149
}
@@ -145,23 +153,23 @@ public void sslPeerUnverifiedExceptionTest() throws ArangoException, KeyManageme
145
153
NoSuchAlgorithmException , KeyStoreException , CertificateException , IOException , URISyntaxException {
146
154
147
155
// create a sslContext for the self signed certificate
148
- URL resource = this .getClass ().getResource (SSL_TRUSTSTORE );
149
- SSLContext sslContext = SSLContexts .custom ()
150
- .loadTrustMaterial (Paths . get (resource .toURI ()). toFile ( ), SSL_TRUSTSTORE_PASSWORD .toCharArray ()).build ();
156
+ final URL resource = this .getClass ().getResource (SSL_TRUSTSTORE );
157
+ final SSLContext sslContext = SSLContexts .custom ()
158
+ .loadTrustMaterial (new File (resource .toURI ()), SSL_TRUSTSTORE_PASSWORD .toCharArray ()).build ();
151
159
152
- ArangoConfigure configuration = new ArangoConfigure ("/ssl-arangodb.properties" );
160
+ final ArangoConfigure configuration = new ArangoConfigure ("/ssl-arangodb.properties" );
153
161
// 127.0.0.1 is the wrong name
154
162
configuration .getArangoHost ().setHost ("127.0.0.1" );
155
163
configuration .setSslContext (sslContext );
156
164
configuration .init ();
157
165
158
- ArangoDriver arangoDriver = new ArangoDriver (configuration );
166
+ final ArangoDriver arangoDriver = new ArangoDriver (configuration );
159
167
160
168
try {
161
169
arangoDriver .getVersion ();
162
170
Assert .fail ("this should fail" );
163
- } catch (ArangoException e ) {
164
- Throwable cause = e .getCause ();
171
+ } catch (final ArangoException e ) {
172
+ final Throwable cause = e .getCause ();
165
173
Assert .assertTrue (cause instanceof javax .net .ssl .SSLPeerUnverifiedException );
166
174
}
167
175
0 commit comments