Skip to content

Instantly share code, notes, and snippets.

@seansabour
Last active June 3, 2017 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seansabour/e541bafe67ffd2f5f5785183f6936538 to your computer and use it in GitHub Desktop.
Save seansabour/e541bafe67ffd2f5f5785183f6936538 to your computer and use it in GitHub Desktop.
TextRecruit Challenge
import com.google.gson.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/* Part 2: Processing Application
* -------
*
* Requirement:
*
* - Utilize Java 6 or later
*
* Features:
*
* - Read the data from the following URL and prase the JSON
*
* URL: https://gist.githubusercontent.com/anonymous/8f60e8f49158efdd2e8fed6fa97373a4/raw/01add7ea44ed12f5d90180dc1367915af331492e/java-data2.json
*
* - Using threads, for each item in the JSON 'items' array process the MD5 checksum of the 'uid' value and present the MD5 result and the current thread name
*
*
* Notes:
*
* - Console Application or Web App (your call!)
* - Use appropriate amounts of error handling
* - You can use external libraries to support implementing the features (if desired or needed)
* - Create a repo/gist on GitHub for your results
*
*/
public class App {
public static String getData(String urlString) throws IOException {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
public static void main(String args[]) throws IOException {
final String URL = "https://gist.githubusercontent.com/anonymous/8f60e8f49158efdd2e8fed6fa97373a4/raw/01add7ea44ed12f5d90180dc1367915af331492e/java-data2.json";
parseJSONWithThread(URL);
}
public static void parseJSONWithThread(String URL) throws IOException {
// Get Data from URL
String jsonString = getData(URL);
// Parse JSON string and conver to JSON Object
JsonParser parser = new JsonParser();
JsonObject object = (JsonObject) parser.parse(jsonString);
// Parse the items array
JsonArray arr = object.get("items").getAsJsonArray();
// Iterate through each element and start a thread to process md5 hash and display results with thread name.
for(JsonElement el: arr) {
String index = el.getAsJsonObject().get("index").getAsString();
String uid = el.getAsJsonObject().get("uid").getAsString();
new Thread(){
public void run(){
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(uid.getBytes("UTF-8"));
byte[] digest = md.digest();
System.out.println(getName() + " running. " + " MD5: " + digest );
Thread.sleep(1000);
} catch(InterruptedException err) {
System.out.println(err);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment