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.complexityintelligence.ws.txp.markupcleanerclient;
public class Main {
public static void main(String[] args) {
new MarkupCleanerClient.execute();
}
}
package com.complexityintelligence.ws.nlp.nerclient;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.*;
import org.apache.commons.codec.binary.*;
import java.net.*;
import java.io.*;
public class MarkupCleanerClient {
public void execute() {
String rService = "txp";
String rType = "mkc";
String pText = "<br/>This is an example<br/>";
String pType = "plain";
String publicAccessID = "0123456789012345678901234567890...78901234567890123";
String secretAuthKey = "9876543210987654321098765432109...10987654321098765";
String strToHash = rService + rType + pText;
System.out.println("strToHash: [" + strToHash + "]");
String authWSControlKey = getHmacSHA256(strToHash, secretAuthKey);
System.out.println(authWSControlKey);
// US-Datacenter-Endpoint
String endPoint = "http://us.ws.complexityintelligence.com/rest/txp/markupcleaner/v1";
// EU-Datacenter-Endpoint
// String endPoint = "http://eu.ws.complexityintelligence.com/rest/txp/markupcleaner/v1";
String response = sendPost(endPoint, rService, rType, pText, pTest, pLang,
authWSControlKey, publicAccessID);
System.out.println(response);
}
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);
}
private String sendPost(String endPoint, String rService, String rType,
String pText, String pType,
String authWSControlKey, String publicAccessID) {
String reply = "";
try {
// Construct data
String data = URLEncoder.encode("rService", "UTF-8") + "="
+ URLEncoder.encode(rService, "UTF-8");
data += "&" + URLEncoder.encode("rType", "UTF-8") + "="
+ URLEncoder.encode(rType, "UTF-8");
data += "&" + URLEncoder.encode("pText", "UTF-8") + "="
+ URLEncoder.encode(pText, "UTF-8");
data += "&" + URLEncoder.encode("pType", "UTF-8") + "="
+ URLEncoder.encode(pType, "UTF-8");
data += "&" + URLEncoder.encode("authWSControlKey", "UTF-8") + "="
+ URLEncoder.encode(authWSControlKey, "UTF-8");
data += "&" + URLEncoder.encode("publicAccessID", "UTF-8") + "="
+ URLEncoder.encode(publicAccessID, "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 {
// Here you can raise an exception if you want, e.g. with
// the HTTP error code.
inputStream = conn.getErrorStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line;
while ((line = rd.readLine()) != null) {
reply += line;
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return (reply);
}
}