Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
LOG-8797 fix for silently ignoring IOException on openConnection() (#60)
Browse files Browse the repository at this point in the history
* LOG-8798 fixed catching IOException logic in opening tcp connection

* LOG-8797 formatting and cleanup
  • Loading branch information
stopal-r7 authored Jun 16, 2017
1 parent aa249d9 commit 605df5d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 119 deletions.
26 changes: 11 additions & 15 deletions src/main/java/com/logentries/net/AsyncLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
196 changes: 92 additions & 104 deletions src/main/java/com/logentries/net/LogentriesClient.java
Original file line number Diff line number Diff line change
@@ -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){

}
}
}
}
}

0 comments on commit 605df5d

Please sign in to comment.