-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiExample.java
More file actions
163 lines (136 loc) · 5.12 KB
/
ApiExample.java
File metadata and controls
163 lines (136 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
This is an example of using the Ohloh API from Java.
Detailed information can be found at the Ohloh website:
http://meta.ohloh.net/getting_started
This examples retrieves a account and simply shows the name associated.
Pass your Ohloh API key as the first parameter to this example.
Ohloh API keys are free. If you do not have one, you can obtain one
at the Ohloh website:
http://www.ohloh.net/accounts/<your_login>/api_keys/new
Pass the email address of the account as the second parameter to this script.
*/
import java.net.*;
import java.security.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class ApiExample
{
private static final char[] hexChars ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
public static void usage()
{
System.out.println("Usage:");
System.out.println("java ApiExample [api_key] [user_email]");
}
public ApiExample(String apiKey, String userEmail)
{
initiate(apiKey, userEmail);
}
public void initiate(String apiKey, String userEmail)
{
System.out.println("Initialising request.");
// Calculate MD5 digest from the email address.
String emailDigest = calculateDigest(userEmail);
try
{
// Request XML file.
URL url = new URL("http://www.ohloh.net/accounts/" + emailDigest + ".xml?api_key=" + apiKey + "&v=1");
URLConnection con = url.openConnection();
// Check for status OK.
if (con.getHeaderField("Status").startsWith("200"))
{
System.out.println("Request succeeded.");
}
else
{
System.out.println("Request failed. Possibly wrong API key?");
return;
}
System.out.println("Looking up name..");
// Create a document from the URL's input stream, and parse.
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(con.getInputStream());
NodeList responseNodes = doc.getElementsByTagName("response");
for (int i = 0; i < responseNodes.getLength(); i++)
{
Element element = (Element)responseNodes.item(i);
// First check for the status code inside the XML file. It is
// most likely, though, that if the request would have failed,
// it is already returned earlier.
NodeList statusList = element.getElementsByTagName("status");
if (statusList.getLength() == 1)
{
Node statusNode = statusList.item(0);
// Check if the text inidicates that the request was
// successful.
if (!statusNode.getTextContent().equals("success"))
{
System.out.println("Failed. " + statusNode.getTextContent());
return;
}
}
Element resultElement = (Element)element.getElementsByTagName("result").item(0);
// We assume we only have one account result here.
Element accountElement = (Element)resultElement.getElementsByTagName("account").item(0);
if (accountElement != null)
{
// Lookup name.
String realName = accountElement.getElementsByTagName("name").item(0).getTextContent();
System.out.println("Located the real name: " + realName);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String calculateDigest(String email)
{
return hexStringFromBytes(calculateHash(email.getBytes()));
}
private byte[] calculateHash(byte[] dataToHash)
{
try
{
// Calculate MD5 digest.
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(dataToHash, 0, dataToHash.length);
return md.digest();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return null;
}
public String hexStringFromBytes(byte[] b)
{
// Conversion from bytes to String.
String hex = "";
int msb;
int lsb = 0;
int i;
for (i = 0; i < b.length; i++)
{
msb = ((int)b[i] & 0x000000FF) / 16;
lsb = ((int)b[i] & 0x000000FF) % 16;
hex = hex + hexChars[msb] + hexChars[lsb];
}
return hex;
}
public static void main(String[] args)
{
if (args.length == 2)
{
// Simply pass arguments.
new ApiExample(args[0], args[1]);
}
else
{
// Show usage information.
usage();
}
}
}