In 'Your Active Web Service list' click on the application name to
have access to subscription details, including the PublicAccessID
and the SecretAuthKey
Replace the PublicAccessID and SecretAuthKey in the code snippet below
with yours data.
The PublicAccessID key is sent as clear text, and is used to identify
both user and application. Our platform uses an hash based verification system
to check the data integrity and the user identity.
The SecretAuthKey is only used locally to generate the hash, and it's
never transmitted over the network. You never have to send it. Keep it secret!
package com.intellexere.ws.demo.sentanalysis;
/**
*
* @author Complexity Intelligence, LLC
*/
public class SentAnalysisClientJavaDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Client().execute();
}
}
package com.intellexere.ws.demo.sentanalysis;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.*;
import org.apache.commons.codec.binary.*;
import java.net.*;
import java.io.*;
/**
*
* @author Complexity Intelligence, LLC
*/
public class Client {
public void execute() {
String wsServiceCode = "nlp:sen:v1";
String pText = "What a fabulous hotel, the dinner was excellent.";
String pLangID = "en";
String publicAccessID = "0123456789012345678901234567890123456789012345678901234567890123";
String secretAuthKey = "0123456789012345678901234567890123456789012345678901234567890123";
String strToHash = wsServiceCode + pText + pLangID;
String authWSControlKey = getHmacSHA256(strToHash, secretAuthKey);
// US-Location Endpoint
String endPoint = "http://us.ws.intellexere.com/core/nlp/sen/v1";
// EU-Location Endpoint
// String endPoint = "http://eu.ws.intellexere.com/core/nlp/sen/v1";
String response = sendPost(endPoint, pText, pLangID,
authWSControlKey, publicAccessID);
// Now you can do whatever you want with the response ("response")
System.out.println(response);
}
private String sendPost(String endPoint, String pText, String pLangID,
String authWSControlKey, String publicAccessID) {
StringBuilder reply = new StringBuilder();
try {
String data = "";
data += "&" + URLEncoder.encode("authWSControlKey", "UTF-8") + "="
+ URLEncoder.encode(authWSControlKey, "UTF-8");
data += "&" + URLEncoder.encode("publicAccessID", "UTF-8") + "="
+ URLEncoder.encode(publicAccessID, "UTF-8");
data += "&" + URLEncoder.encode("pText", "UTF-8") + "="
+ URLEncoder.encode(pText, "UTF-8");
data += "&" + URLEncoder.encode("pLangID", "UTF-8") + "="
+ URLEncoder.encode(pLangID, "UTF-8");
// Send data
URL url = new URL(endPoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Retrieve the output
int responseCode = conn.getResponseCode();
InputStream inputStream;
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = conn.getInputStream();
} else {
inputStream = conn.getErrorStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line;
while ((line = rd.readLine()) != null) {
reply.append(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return (reply.toString());
}
private String getHmacSHA256(String strToHash, String secretAuthKey) {
String hexBytes = null;
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretAuthKey.getBytes(), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretKeySpec);
byte[] rawHmacBytes = mac.doFinal(strToHash.getBytes());
hexBytes = new String(Hex.encodeHex(rawHmacBytes));
} catch (Exception e) {
e.printStackTrace();
}
return (hexBytes);
}
}