From 605df5dca58a07f149407f4cf6676f6443b4fa43 Mon Sep 17 00:00:00 2001 From: Safa Topal Date: Fri, 16 Jun 2017 11:58:58 +0100 Subject: [PATCH] LOG-8797 fix for silently ignoring IOException on openConnection() (#60) * LOG-8798 fixed catching IOException logic in opening tcp connection * LOG-8797 formatting and cleanup --- .../java/com/logentries/net/AsyncLogger.java | 26 +-- .../com/logentries/net/LogentriesClient.java | 196 ++++++++---------- 2 files changed, 103 insertions(+), 119 deletions(-) diff --git a/src/main/java/com/logentries/net/AsyncLogger.java b/src/main/java/com/logentries/net/AsyncLogger.java index 616caec..0ce90b6 100644 --- a/src/main/java/com/logentries/net/AsyncLogger.java +++ b/src/main/java/com/logentries/net/AsyncLogger.java @@ -505,22 +505,17 @@ class SocketAppender extends Thread { * @throws IOException */ void openConnection() throws IOException { - try{ - if(this.le_client == null){ - this.le_client = new LogentriesClient(httpPut, ssl, useDataHub, dataHubAddr, dataHubPort); - } - - this.le_client.connect(); - - if(httpPut){ - final String f = "PUT /%s/hosts/%s/?realtime=1 HTTP/1.1\r\n\r\n"; - final String header = String.format( f, key, location); - byte[] temp = header.getBytes( ASCII); - this.le_client.write( temp, 0, temp.length); - } + if (this.le_client == null) { + this.le_client = new LogentriesClient(httpPut, ssl, useDataHub, dataHubAddr, dataHubPort); + } - }catch(Exception e){ + this.le_client.connect(); + if (httpPut) { + final String f = "PUT /%s/hosts/%s/?realtime=1 HTTP/1.1\r\n\r\n"; + final String header = String.format(f, key, location); + byte[] temp = header.getBytes(ASCII); + this.le_client.write(temp, 0, temp.length); } } @@ -657,7 +652,8 @@ public void run() { try { this.le_client.write( finalLine, 0, finalLine.length); } catch (IOException e) { - // Reopen the lost connection + dbg("Existing connection is lost, will try to reestablish."); + // Reopen the lost connection reopenConnection(); continue; } diff --git a/src/main/java/com/logentries/net/LogentriesClient.java b/src/main/java/com/logentries/net/LogentriesClient.java index 66da7ed..9533a94 100644 --- a/src/main/java/com/logentries/net/LogentriesClient.java +++ b/src/main/java/com/logentries/net/LogentriesClient.java @@ -1,120 +1,108 @@ package com.logentries.net; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; -import java.net.UnknownHostException; - -import javax.net.ssl.SSLSocket; -import javax.net.ssl.SSLSocketFactory; /** * Client for sending messages to Logentries via HTTP PUT or Token-Based Logging * Supports SSL/TLS - * + * * @author Mark Lacomber - * */ -public class LogentriesClient -{ - /* - * Constants - */ - - /** Logentries API server address for Token-based input. */ - private static final String LE_TOKEN_API = "data.logentries.com"; - /** Logentries API server address for HTTP PUT input. */ - private static final String LE_HTTP_API = "api.logentries.com"; - /** Port number for HTTP PUT/Token TCP logging on Logentries server. */ - private static final int LE_PORT = 80; - /** Port number for SSL HTTP PUT/TLS Token TCP logging on Logentries server. */ - private static final int LE_SSL_PORT = 443; +public class LogentriesClient { + /** + * Logentries API server address for Token-based input. + */ + private static final String LE_TOKEN_API = "data.logentries.com"; + /** + * Logentries API server address for HTTP PUT input. + */ + private static final String LE_HTTP_API = "api.logentries.com"; + /** + * Port number for HTTP PUT/Token TCP logging on Logentries server. + */ + private static final int LE_PORT = 80; + /** + * Port number for SSL HTTP PUT/TLS Token TCP logging on Logentries server. + */ + private static final int LE_SSL_PORT = 443; + + private final SSLSocketFactory ssl_factory; + private boolean ssl_choice = false; + private boolean http_choice = false; + private Socket socket; + private OutputStream outputStream; + private String dataHubServer = LE_TOKEN_API; + private int dataHubPort = LE_PORT; + private boolean useDataHub = false; + + public LogentriesClient(boolean httpPut, boolean ssl, boolean isUsingDataHub, String server, int port) { + if (isUsingDataHub) { + ssl_factory = null; // DataHub does not support input over SSL for now, + ssl_choice = false; // so SSL flag is ignored + useDataHub = true; + dataHubServer = server; + dataHubPort = port; + } else { + ssl_factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); + ssl_choice = ssl; + http_choice = httpPut; + } + } + + public int getPort() { + if (useDataHub) { + return dataHubPort; + } else { + return ssl_choice ? LE_SSL_PORT : LE_PORT; + } + } + + public String getAddress() { + if (useDataHub) { + return dataHubServer; + } else { + return http_choice ? LE_HTTP_API : LE_TOKEN_API; + } + } + + public void connect() throws IOException { + if (ssl_choice) { + if (http_choice) { + SSLSocket s = (SSLSocket) ssl_factory.createSocket(getAddress(), getPort()); + s.startHandshake(); + socket = s; + } else { + socket = ssl_factory.createSocket(getAddress(), getPort()); + } + } else { + socket = new Socket(getAddress(), getPort()); + } + + this.socket.setKeepAlive(true); + this.outputStream = socket.getOutputStream(); + } - final SSLSocketFactory ssl_factory; - private boolean ssl_choice = false; - private boolean http_choice = false; - private Socket socket; - private OutputStream stream; - private String dataHubServer = LE_TOKEN_API; - private int dataHubPort = LE_PORT; - private boolean useDataHub = false; - - public LogentriesClient(boolean httpPut, boolean ssl, boolean isUsingDataHub, String server, int port) - { - if(isUsingDataHub){ - ssl_factory = null; // DataHub does not support input over SSL for now, - ssl_choice = false; // so SSL flag is ignored - useDataHub = isUsingDataHub; - dataHubServer = server; - dataHubPort = port; - } - else{ - ssl_factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); - ssl_choice = ssl; - http_choice = httpPut; - } - } + public void write(byte[] buffer, int offset, int length) throws IOException { + if (this.outputStream == null) { + throw new IOException(); + } - public int getPort() - { - if(useDataHub) - { - return dataHubPort; - } - else{ - return ssl_choice ? LE_SSL_PORT : LE_PORT; - } - } + this.outputStream.write(buffer, offset, length); + this.outputStream.flush(); + } - public String getAddress() - { - if(useDataHub) - { - return dataHubServer; - } - else{ - return http_choice ? LE_HTTP_API : LE_TOKEN_API; - } - } - - public void connect() throws UnknownHostException, IOException - { - if(ssl_choice) { - if(http_choice) - { - SSLSocket s = (SSLSocket) ssl_factory.createSocket( getAddress(), getPort() ); - s.setTcpNoDelay( true); - s.startHandshake(); - socket = s; - }else{ - socket = SSLSocketFactory.getDefault().createSocket( getAddress(), getPort() ); - } - }else{ - socket = new Socket( getAddress(), getPort() ); - } + public void close() { + try { + if (this.socket != null) { + this.socket.close(); + this.socket = null; + } + } catch (Exception e) { - this.stream = socket.getOutputStream(); - } - - public void write(byte[] buffer, int offset, int length) throws IOException - { - if(this.stream == null){ - throw new IOException(); - } - this.stream.write(buffer, offset, length); - this.stream.flush(); - } - - public void close() - { - try{ - if (this.socket != null) - { - this.socket.close(); - this.socket = null; - } - }catch(Exception e){ - - } - } + } + } }