package util.configuration;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import external.JSON.JSONArray;
import external.JSON.JSONObject;

/**
 * RemoteResourceLoader loads remotely-stored resources. It can load resources
 * as raw strings, JSON objects, or JSON arrays.
 * 
 * TODO: This doesn't really belong in "util.configuration" because it's not
 * related to the project's local configuration, but it seems like a reasonable
 * place to put it for now because it's near LocalResourceLoader.
 * 
 * @author Sam
 */
public class RemoteResourceLoader {
    public static JSONObject loadJSON(String theURL) throws IOException {
        try {
            return new JSONObject(loadRaw(theURL));
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
    
    public static JSONArray loadJSONArray(String theURL) throws IOException {
        try {
            return new JSONArray(loadRaw(theURL));
        } catch (Exception e) {
            throw new IOException(e);
        }                
    }
    
    public static String loadRaw(String theURL) throws IOException {
        URL url = new URL(theURL);
        URLConnection urlConnection = url.openConnection();                
        if (urlConnection.getContentLength() == 0)
            throw new IOException("Could not load URL: " + theURL);
        StringBuilder theJSON = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        do {
            String nextLine = br.readLine();
            if (nextLine == null) break;
            theJSON.append(nextLine);
        } while (true);
        return theJSON.toString();
    }
}
@@ -112,32 +112,68 @@
 tStreamReader;%0D%0A
+import java.io.OutputStreamWriter;%0D%0A
 import java.net.
@@ -1877,10 +1877,843 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    public static String postRawWithTimeout(String theURL, String toPost, int nTimeout) throws IOException %7B%0D%0A        URL url = new URL(theURL);%0D%0A        URLConnection urlConnection = url.openConnection();%0D%0A        urlConnection.setDoOutput(true);%0D%0A        urlConnection.setConnectTimeout(nTimeout);%0D%0A        OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());%0D%0A        out.write(toPost);%0D%0A        out.close();%0D%0A        %0D%0A        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));%0D%0A        StringBuilder decodedString = new StringBuilder();%0D%0A        String decodedLine;%0D%0A        while ((decodedLine = in.readLine()) != null) %7B%0D%0A            decodedString.append(decodedLine);%0D%0A        %7D%0D%0A        in.close();%0D%0A        return decodedString.toString();%0D%0A    %7D%0D
 %0A%7D

Add functionality to the ResourceLoader to load resources via POST requests that have content. This is useful e.g. for issuing quick ping requests to players.