This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from bboko/java_example
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.PHONY:all clean | ||
|
||
all:ReferenceData.class | ||
|
||
ReferenceData.class:ReferenceData.java bloomberg.jks client.p12 | ||
javac ReferenceData.java | ||
|
||
client.p12:client.crt bloomberg.crt | ||
openssl pkcs12 -export -password pass:secure -in client.crt -CAfile bloomberg.crt -inkey client.key -out client.p12 | ||
|
||
bloomberg.jks:bloomberg.crt | ||
$(RM) bloomberg.jks | ||
keytool -import -keystore bloomberg.jks -storepass secure2 -trustcacerts -file bloomberg.crt -noprompt | ||
|
||
clean: | ||
$(RM) ReferenceData.class client.p12 bloomberg.jks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Java examples | ||
|
||
This example is a Java https client focusing on two points: | ||
* converting and loading PEM certificates | ||
* sending json in a POST request over the https connection | ||
|
||
## Build | ||
|
||
This has been tested on Linux and Cygwin. | ||
|
||
1. Required files: | ||
* client.crt: PEM certificate, the public key | ||
* client.key: PEM RSA private key | ||
* bloomberg.crt: CA certificate | ||
|
||
2. Make sure your PATH has got these: | ||
* openssl | ||
* keytool: part of JDK | ||
* javac, java | ||
|
||
3. Build: | ||
|
||
``` | ||
$ make all | ||
``` | ||
4. Output: | ||
* client.p12: PKCS12 certificate holding the `client.key` and `client.crt` | ||
protected by password: `secure`. This is used by the KeyManager. | ||
* bloomberg.jks: java key store holding the CA certificate, used by the | ||
TrustManager. Password protected: `secure2`. | ||
* ReferenceData.class | ||
|
||
## Run | ||
|
||
``` | ||
$ java ReferenceData ../examples/ReferenceData/IBMHolders.json | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// ReferenceData.java | ||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.FileInputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.security.KeyStore; | ||
import java.security.SecureRandom; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.net.ssl.KeyManager; | ||
import javax.net.ssl.KeyManagerFactory; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.TrustManagerFactory; | ||
|
||
public class ReferenceData { | ||
public static final String apiUrl = "https://http-api.openbloomberg.com" | ||
+ "/request/blp/refdata/ReferenceData"; | ||
public static final String keyStorePW = "secure"; | ||
public static final String trustStorePW = "secure2"; | ||
public static final String clientCert = "client.p12"; | ||
public static final String bbCert = "bloomberg.jks"; | ||
|
||
public static void main(String[] args) { | ||
|
||
if (1 > args.length) { | ||
System.out.println("Usage: ReferenceData <json file>"); | ||
return; | ||
} | ||
String jsonFile = args[0]; | ||
try { | ||
// load the client public/private key from PKCS12 | ||
KeyStore clientStore = KeyStore.getInstance("PKCS12"); | ||
clientStore.load(new FileInputStream(clientCert), keyStorePW.toCharArray()); | ||
|
||
KeyManagerFactory kmf = KeyManagerFactory.getInstance( | ||
KeyManagerFactory.getDefaultAlgorithm()); | ||
kmf.init(clientStore, keyStorePW.toCharArray()); | ||
KeyManager[] kms = kmf.getKeyManagers(); | ||
|
||
// load the public key of the CA from JKS, | ||
// so we can verify the server certificate. | ||
KeyStore trustStore = KeyStore.getInstance("JKS"); | ||
trustStore.load(new FileInputStream(bbCert), trustStorePW.toCharArray()); | ||
|
||
TrustManagerFactory tmf = TrustManagerFactory.getInstance( | ||
TrustManagerFactory.getDefaultAlgorithm()); | ||
tmf.init(trustStore); | ||
TrustManager[] tms = tmf.getTrustManagers(); | ||
|
||
// initialize the SSLContext with the keys, | ||
// KeyManager: client public/private key, TrustManager: server public key | ||
SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
sslContext.init(kms, tms, new SecureRandom()); | ||
|
||
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); | ||
URL url = new URL(apiUrl); | ||
|
||
// open connection to the server | ||
HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection(); | ||
|
||
urlConn.setRequestMethod("POST"); | ||
urlConn.setRequestProperty("User-Agent", "blpapi-http-java-example"); | ||
urlConn.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); | ||
urlConn.setDoOutput(true); | ||
urlConn.setRequestProperty("Content-Type", "application/json; charset=utf8"); | ||
|
||
// write the json request to the output stream | ||
DataOutputStream wr = new DataOutputStream(urlConn.getOutputStream()); | ||
FileInputStream fis = new FileInputStream(jsonFile); | ||
byte[] buffer = new byte[1024]; | ||
int len = fis.read(buffer); | ||
while (-1 < len) { | ||
wr.write(buffer, 0, len); | ||
len = fis.read(buffer); | ||
} | ||
wr.flush(); | ||
wr.close(); | ||
|
||
// read the whatever we get back | ||
int responseCode = urlConn.getResponseCode(); | ||
System.out.println("\nSending 'POST' request to URL : " + url); | ||
System.out.println("Response Code : " + responseCode); | ||
|
||
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); | ||
String inputLine; | ||
StringBuffer response = new StringBuffer(); | ||
|
||
while ((inputLine = in.readLine()) != null) { | ||
response.append(inputLine); | ||
} | ||
in.close(); | ||
|
||
System.out.println(response.toString()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |