diff --git a/src/main/java/connect/src/TLSClient.java b/src/main/java/connect/src/TLSClient.java new file mode 100644 index 0000000..744c40f --- /dev/null +++ b/src/main/java/connect/src/TLSClient.java @@ -0,0 +1,81 @@ +package connect.src; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.UnknownHostException; +import java.security.KeyStore; + +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; + +public final class TLSClient implements Client { + + private ClientInformation clientInformation; + // Path to keystore file + private String keyStoreFile = ""; + // Password to access the keystore file + private String keyStoreFilePassword = ""; + // Alias password + private String keyStoreAliasPassword = ""; + + private KeyStore keyStore; + + private SSLSocket sslSocket; + private BufferedReader reader; + private PrintWriter writer; + + public void setClientData(ClientInformation clientInformation) { + this.clientInformation = clientInformation; + } + + public void createConnection() { + try { + SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); + sslSocket = (SSLSocket) sslSocketFactory.createSocket("127.0.0.1", 5000); + reader = new BufferedReader(new InputStreamReader(sslSocket.getInputStream())); + writer = new PrintWriter(sslSocket.getOutputStream()); + } catch (UnknownHostException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void sendData(Data data) { + if (sslSocket != null && sslSocket.isConnected() && reader != null && writer != null) { + if (null != data) { + writer.println("Hello Server!"); + writer.flush(); + } + } else { + System.out.println("Connection is not established."); + } + } + + public void closeConnection() { + try { + if (null != reader) { + reader.close(); + } + if (null != writer) { + writer.close(); + } + } catch (IOException e) { + // TODO + e.printStackTrace(); + } finally { + try { + if (null != sslSocket) { + sslSocket.close(); + } + } catch (IOException e) { + // TODO + e.printStackTrace(); + } + } + } +}