package placeholder.website.queue.http;
import java.io.PrintStream;
import org.json.JSONArray;
import placeholder.http.HttpRequest;
import placeholder.website.queue.AbstractWebsiteQueue;
import placeholder.website.queue.WebsiteQueueFillException;
/**
* @author Heiko Bornholdt
*/
public class HttpWebsiteQueue extends AbstractWebsiteQueue {
public HttpWebsiteQueue(PrintStream out) {
super(out);
}
@Override
synchronized protected void fill() throws WebsiteQueueFillException {
this.out.print("Fill WebsiteQueue...");
HttpRequest request = null;
try {
// request
request = new HttpRequest("http://62.75.144.112/~heiko/placeholder/get.php");
request.send();
String response = request.getResponseBody();
if (request.getConnection().getResponseCode() != 200) {
throw new HttpWebsiteQueueFillException("Error while getting new websites from server.", request, request.getConnection().getResponseMessage(), request.getConnection().getResponseCode());
}
// read from json
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
this.queue.add(array.getString(i));
}
} catch (Exception e) {
throw new WebsiteQueueFillException(e);
}
this.out.println("finished!");
}
}