33
33
import org .neo4j .driver .v1 .exceptions .ClientException ;
34
34
import org .neo4j .driver .v1 .exceptions .ServiceUnavailableException ;
35
35
36
+ import static java .lang .String .format ;
36
37
import static javax .net .ssl .SSLEngineResult .HandshakeStatus .FINISHED ;
37
38
import static javax .net .ssl .SSLEngineResult .HandshakeStatus .NOT_HANDSHAKING ;
38
39
@@ -80,8 +81,7 @@ public static TLSSocketChannel create( ByteChannel channel, Logger logger, SSLEn
80
81
}
81
82
catch ( SSLHandshakeException e )
82
83
{
83
- throw new ClientException ( "Failed to establish secured connection with the server: " + e .getMessage (),
84
- e .getCause () );
84
+ throw new ClientException ( "Failed to establish secured connection with the server: " + e .getMessage (), e );
85
85
}
86
86
return tlsChannel ;
87
87
}
@@ -153,12 +153,10 @@ private HandshakeStatus runDelegatedTasks()
153
153
* @param toBuffer the destination where the data read from the socket channel are saved
154
154
* @throws IOException when failed to read from channel
155
155
*/
156
- void channelRead ( ByteBuffer toBuffer ) throws IOException
156
+ int channelRead ( ByteBuffer toBuffer ) throws IOException
157
157
{
158
- /**
159
- * This is the only place to read from the underlying channel
160
- */
161
- if ( channel .read ( toBuffer ) < 0 )
158
+ int read = channel .read ( toBuffer );
159
+ if ( read < 0 )
162
160
{
163
161
try
164
162
{
@@ -172,16 +170,18 @@ void channelRead( ByteBuffer toBuffer ) throws IOException
172
170
"SSL Connection terminated while receiving data. " +
173
171
"This can happen due to network instabilities, or due to restarts of the database." );
174
172
}
173
+ return read ;
175
174
}
176
175
177
176
/**
178
177
* Write the data saved in the buffer to the socket channel
179
178
* @param fromBuffer the source where the data written to the socket channel are saved
180
179
* @throws IOException when failed to write to channel
181
180
*/
182
- void channelWrite ( ByteBuffer fromBuffer ) throws IOException
181
+ int channelWrite ( ByteBuffer fromBuffer ) throws IOException
183
182
{
184
- if ( channel .write ( fromBuffer ) < 0 )
183
+ int written = channel .write ( fromBuffer );
184
+ if ( written < 0 )
185
185
{
186
186
try
187
187
{
@@ -195,6 +195,7 @@ void channelWrite( ByteBuffer fromBuffer ) throws IOException
195
195
"SSL Connection terminated while writing data. " +
196
196
"This can happen due to network instabilities, or due to restarts of the database." );
197
197
}
198
+ return written ;
198
199
}
199
200
200
201
/**
@@ -259,7 +260,7 @@ private HandshakeStatus unwrap( ByteBuffer buffer ) throws IOException
259
260
if ( newAppSize > appSize * 2 )
260
261
{
261
262
throw new ClientException (
262
- String . format ( "Failed ro enlarge application input buffer from %s to %s, as the maximum " +
263
+ format ( "Failed ro enlarge application input buffer from %s to %s, as the maximum " +
263
264
"buffer size allowed is %s. The content in the buffer is: %s\n " ,
264
265
curAppSize , newAppSize , appSize * 2 , BytePrinter .hex ( plainIn ) ) );
265
266
}
@@ -311,7 +312,7 @@ private HandshakeStatus unwrap( ByteBuffer buffer ) throws IOException
311
312
* @return The status of the current handshake
312
313
* @throws IOException
313
314
*/
314
- private HandshakeStatus wrap ( ByteBuffer buffer ) throws IOException
315
+ private HandshakeStatus wrap ( ByteBuffer buffer ) throws IOException , ClientException
315
316
{
316
317
HandshakeStatus handshakeStatus = sslEngine .getHandshakeStatus ();
317
318
Status status = sslEngine .wrap ( buffer , cipherOut ).getStatus ();
@@ -345,7 +346,14 @@ private HandshakeStatus wrap( ByteBuffer buffer ) throws IOException
345
346
{
346
347
// flush as much data as possible
347
348
cipherOut .flip ();
348
- channelWrite ( cipherOut );
349
+ if ( channelWrite ( cipherOut ) == 0 )
350
+ {
351
+ throw new ClientException ( format (
352
+ "Failed to enlarge network buffer from %s to %s. This is either because the " +
353
+ "new size is however less than the old size, or because the application " +
354
+ "buffer size %s is so big that the application data still cannot fit into the " +
355
+ "new network buffer." , curNetSize , netSize , buffer .capacity () ) );
356
+ }
349
357
cipherOut .compact ();
350
358
logger .debug ( "Network output buffer couldn't be enlarged, flushing data to the channel instead." );
351
359
}
0 commit comments