Skip to content

Commit

Permalink
Add examples with API key. Divide examples into 2 groups
Browse files Browse the repository at this point in the history
- Divide examples into two groups: access by passwrod and by API key
- Add java code sample with API key access example
  • Loading branch information
whoisxmlapi committed Mar 30, 2017
1 parent 71bb622 commit 02abb22
Show file tree
Hide file tree
Showing 33 changed files with 187 additions and 0 deletions.
29 changes: 29 additions & 0 deletions apikey/java/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/resources/config"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions apikey/java/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TestClient</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
21 changes: 21 additions & 0 deletions apikey/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>LLC</groupId>
<artifactId>com.whoisxmlapi</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>

</dependencies>
</project>
114 changes: 114 additions & 0 deletions apikey/java/src/main/java/com/whoisxmlapi/test/ApiKeyClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.whoisxmlapi.test;

import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONException;
import org.json.JSONObject;

public class ApiKeyClientTest {

private Logger logger = Logger.getLogger(ApiKeyClientTest.class.getName());

public static void main(String[]args) {
new ApiKeyClientTest().getSimpleDomainUsingApiKey();
}

private void getSimpleDomainUsingApiKey() {
String domainName = "test.com";

String username = "username";
String apiKey = "apiKey";
String secretKey = "secretKey";

getDomainNameUsingApiKey(domainName, username, apiKey, secretKey);
}

private String executeURL(String url) {
HttpClient c = new HttpClient();
System.out.println(url);
HttpMethod m = new GetMethod(url);
String res = null;
try {
c.executeMethod(m);
res = new String(m.getResponseBody());
} catch (Exception e) {
logger.log(Level.SEVERE, "Cannot get url", e);
} finally {
m.releaseConnection();
}
return res;
}

public void getDomainNameUsingApiKey(String domainName, String username, String apiKey, String secretKey) {
String apiKeyAuthenticationRequest = generateApiKeyAuthenticationRequest(username, apiKey, secretKey);
if (apiKeyAuthenticationRequest == null) {
return;
}

StringBuilder sb = new StringBuilder();
sb.append("http://www.whoisxmlapi.com/whoisserver/WhoisService?");
sb.append(apiKeyAuthenticationRequest);
sb.append("&domainName=");
sb.append(domainName);

String url = sb.toString();

String result = executeURL(url);
if (result != null) {
logger.log(Level.INFO, "result: " + result);
}
}

private String generateApiKeyAuthenticationRequest(String username, String apiKey, String secretKey) {
try {
long timestamp = System.currentTimeMillis();

String request = generateRequest(username, timestamp);
String digest = generateDigest(username, apiKey, secretKey, timestamp);

String requestURL = URLEncoder.encode(request, "UTF-8");
String digestURL = URLEncoder.encode(digest, "UTF-8");

String apiKeyAuthenticationRequest = "requestObject="+requestURL+"&digest="+digestURL;
return apiKeyAuthenticationRequest;
} catch (Exception e) {
logger.log(Level.SEVERE, "an error occurred", e);
}
return null;
}

private String generateRequest(String username, long timestamp) throws JSONException {
JSONObject json = new JSONObject();
json.put("u", username);
json.put("t", timestamp);
String jsonStr = json.toString();
byte[] json64 = Base64.encodeBase64(jsonStr.getBytes());
String json64Str = new String(json64);
return json64Str;
}

private String generateDigest(String username, String apiKey, String secretKey, long timestamp) throws Exception {
StringBuilder sb = new StringBuilder();
sb.append(username);
sb.append(timestamp);
sb.append(apiKey);

SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacMD5");
Mac mac = Mac.getInstance(secretKeySpec.getAlgorithm());
mac.init(secretKeySpec);

byte[] digestBytes = mac.doFinal(sb.toString().getBytes("UTF-8"));
String digest = new String(Hex.encodeHex(digestBytes));
return digest;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 02abb22

Please sign in to comment.