Package org.symphonyoss.symphony.jcurl
Class JCurl
- java.lang.Object
-
- org.symphonyoss.symphony.jcurl.JCurl
-
public class JCurl extends Object
JSON-aware curl (1) in Java
Usage//Get a session token JCurl jcurl = JCurl.builder() .method(JCurl.HttpMethod.POST) .keystore("bot.user1.p12") //Set user certificate for authentication .storepass("changeit") .storetype("pkcs12") .extract("skey", "token") //Extract the value of the JSON tag "token" to a map entry under "skey" .build(); HttpURLConnection connection = jcurl.connect("https://localhost.symphony.com:8444/sessionauth/v1/authenticate"); JCurl.Response response = jcurl.processResponse(connection); String sessionToken = response.getTag("skey"); //Retrieve the extracted tag saved as "skey" //Get session info (returns the requesting user ID) jcurl = JCurl.builder() .method(JCurl.HttpMethod.GET) //HTTP GET is the default; this line can be skipped .header("sessionToken", sessionToken) //Set the session token in the request header .extract("uid", "userId") //Extract the user ID from the response as "uid" .build(); connection = jcurl.connect("https://localhost.symphony.com:8443/pod/v1/sessioninfo"); response = jcurl.processResponse(connection); String userId = response.getTag("uid"); System.out.println("User ID: " + userId); //Create an IM with user 123456 jcurl = JCurl.builder() .method(JCurl.HttpMethod.POST) //Set implicitly by specifying ".data()"; this line can be skipped .header("sessionToken", sessionToken) //Set the session token in the request header .data("[123456]") //Set the JSON payload of the request .extract("sid", "id") //Extract the stream ID of the conversation as "sid" .build(); connection = jcurl.connect("https://localhost.symphony.com:8443/pod/v1/im/create"); response = jcurl.processResponse(connection); String streamId = response.getTag("sid"); System.out.println("Stream ID: " + streamId); //Print the output of the call System.out.println(response.getOutput()); //Prints '{"id": "wFwupr-KY3QW1oEkjE61x3___qsvcXdFdA"}'- Version:
- $Id: $Id
- Author:
- bruce.skingle, ldrozdz
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJCurl.Builderstatic classJCurl.HttpMethodclassJCurl.Response
-
Constructor Summary
Constructors Constructor Description JCurl()
-
Method Summary
-
-
-
Method Detail
-
main
public static void main(String[] argv) throws IOException, CertificateParsingException, KeyManagementException, NoSuchAlgorithmException
Entry point for command-line usage. Call
java -jar jcurl.jarfor usage info.- Parameters:
argv- an array ofStringobjects.- Throws:
IOException- if any.CertificateParsingException- if any.KeyManagementException- if any.NoSuchAlgorithmException- if any.
-
builder
public static JCurl.Builder builder()
Create a builder.
- Returns:
- a
JCurl.Builderobject.
-
connect
public HttpURLConnection connect() throws IOException
Perform a HTTP(s) request to the provided URL. Unless a different configuration is specified with JCurl.Builder, performs a GET request with Content-Type: application/json, expecting a HTTP 200 response. This method requires the URL to have been set with {@link JCurl.Builder.url()}.- Returns:
- a
HttpURLConnectionobject. - Throws:
IOException- if any.
-
connect
public HttpURLConnection connect(URL url) throws IOException
Perform a HTTP(s) request to the provided URL. Unless a different configuration is specified with JCurl.Builder, performs a GET request with Content-Type: application/json, expecting a HTTP 200 response.- Parameters:
url- aURLobject.- Returns:
- a
HttpURLConnectionobject. - Throws:
IOException- if any.
-
connect
public HttpURLConnection connect(String urlString) throws IOException
Connect to the provided URL.
- Parameters:
urlString- aStringobject.- Returns:
- a
HttpURLConnectionobject. - Throws:
IOException- if any.
-
processResponse
public JCurl.Response processResponse(HttpURLConnection con) throws IOException, CertificateParsingException
Process response data and, if applicable, HTTPS information. TheJCurl.Responseobject returned can be printed out with response.print().- Parameters:
con- aHttpURLConnectionobject.- Returns:
- a
JCurl.Responseobject. - Throws:
IOException- if any.CertificateParsingException- if any.
-
getUrl
public String getUrl()
-
getData
public String getData()
-
getKeyStore
public String getKeyStore()
-
getStoreType
public String getStoreType()
-
getStorePass
public String getStorePass()
-
getTrustStore
public String getTrustStore()
-
getTrustType
public String getTrustType()
-
getTrustPass
public String getTrustPass()
-
getProxyHost
public String getProxyHost()
-
getProxyPort
public String getProxyPort()
-
getNonProxyHosts
public String getNonProxyHosts()
-
getVerbosity
public int getVerbosity()
-
getConnectTimeout
public int getConnectTimeout()
-
getReadTimeout
public int getReadTimeout()
-
isTrustAllHostnames
public boolean isTrustAllHostnames()
-
isTrustAllCerts
public boolean isTrustAllCerts()
-
isExtractCookies
public boolean isExtractCookies()
-
getMethod
public JCurl.HttpMethod getMethod()
-
getContentType
public String getContentType()
-
-