Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding java https client #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions java-https-client/HTTPSClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.KeyStore;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

public class HTTPSClient {
private String host = "localhost";
private int port = 8243;

public static void main(String[] args){
HTTPSClient client = new HTTPSClient();
client.run();
}

HTTPSClient(){
}

HTTPSClient(String host, int port){
this.host = host;
this.port = port;
}

// Create the and initialize the SSLContext
private SSLContext createSSLContext(){
try{
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream("/Users/apple/Documents/support/131_UVUSUB-20/testclient/wso2carbon.jks"),"wso2carbon".toCharArray());

// Create key manager
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(keyStore, "wso2carbon".toCharArray());
KeyManager[] km = keyManagerFactory.getKeyManagers();

// Create trust manager
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
trustManagerFactory.init(keyStore);
TrustManager[] tm = trustManagerFactory.getTrustManagers();

// Initialize SSLContext
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(km, tm, null);

return sslContext;
} catch (Exception ex){
ex.printStackTrace();
}

return null;
}

// Start to run the server
public void run(){
SSLContext sslContext = this.createSSLContext();

try{
// Create socket factory
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

// Create socket
SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(this.host, this.port);

System.out.println("SSL client started");
new ClientThread(sslSocket).start();
} catch (Exception ex){
ex.printStackTrace();
}
}

// Thread handling the socket to server
static class ClientThread extends Thread {
private SSLSocket sslSocket = null;

ClientThread(SSLSocket sslSocket){
this.sslSocket = sslSocket;
}

public void run(){
sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());

try{
// Start handshake
sslSocket.startHandshake();

// Get session after the connection is established
SSLSession sslSession = sslSocket.getSession();

System.out.println("SSLSession :");
System.out.println("\tProtocol : "+sslSession.getProtocol());
System.out.println("\tCipher suite : "+sslSession.getCipherSuite());

// Start handling application content
InputStream inputStream = sslSocket.getInputStream();
OutputStream outputStream = sslSocket.getOutputStream();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream));

// Write data
printWriter.print("GET /services/Version HTTP/1.0\r\n");
printWriter.print("\r\n");
printWriter.print("\r\n");
printWriter.flush();
String line = null;
while((line = bufferedReader.readLine()) != null){
System.out.println("Inut : "+line);

if(line.trim().equals("HTTP/1.1 200\r\n")){
break;
}
}

sslSocket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
4 changes: 4 additions & 0 deletions java-https-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Welcome to JAVA HTTPS client!

You can use this java https client to invoke APIs over https and constructs the requests as you want.