package placeholder.check;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import placeholder.http.HttpRequest;
import placeholder.http.HttpRequestException;
/**
* @author Heiko Bornholdt
*/
public class CheckServices {
protected static final PrintStream out = System.out;
protected static HashMap<String, Pattern> services = null;
public static String[] check(String content) {
ArrayList<String> matches = new ArrayList<String>();
for (Map.Entry<String, Pattern> service : services.entrySet()) {
if (service.getValue().matcher(content).find()) {
matches.add(service.getKey());
}
}
return matches.toArray(new String[matches.size()]);
}
public static void load() throws HttpRequestException, IOException {
if (services == null) {
out.print("Get services...");
services = new HashMap<String, Pattern>();
// request
HttpRequest request = new HttpRequest("http://62.75.144.112/~heiko/placeholder/services.php");
request.send();
String response = request.getResponseBody();
if (request.getConnection().getResponseCode() != 200) {
throw new HttpRequestException("Error while getting services from server.", request, request.getConnection().getResponseMessage(), request.getConnection().getResponseCode());
}
// read from json
for(String service: response.split("\t\t")) {
String[] data = service.split("\t", 2);
if (data.length == 2) {
services.put(data[0], Pattern.compile(data[1]));
}
}
out.println(services.size() + " loaded!");
}
}
}