| 1 | package net.oni2.aeinstaller.backend.network; | 
|---|
| 2 |  | 
|---|
| 3 | import java.io.BufferedReader; | 
|---|
| 4 | import java.io.IOException; | 
|---|
| 5 | import java.io.InputStreamReader; | 
|---|
| 6 | import java.io.UnsupportedEncodingException; | 
|---|
| 7 | import java.util.ArrayList; | 
|---|
| 8 | import java.util.HashMap; | 
|---|
| 9 | import java.util.List; | 
|---|
| 10 |  | 
|---|
| 11 | import net.oni2.aeinstaller.backend.Settings; | 
|---|
| 12 |  | 
|---|
| 13 | import org.apache.http.HttpEntity; | 
|---|
| 14 | import org.apache.http.HttpResponse; | 
|---|
| 15 | import org.apache.http.NameValuePair; | 
|---|
| 16 | import org.apache.http.client.entity.UrlEncodedFormEntity; | 
|---|
| 17 | import org.apache.http.client.methods.HttpGet; | 
|---|
| 18 | import org.apache.http.client.methods.HttpPost; | 
|---|
| 19 | import org.apache.http.client.methods.HttpRequestBase; | 
|---|
| 20 | import org.apache.http.impl.client.DefaultHttpClient; | 
|---|
| 21 | import org.apache.http.message.BasicNameValuePair; | 
|---|
| 22 | import org.apache.http.util.EntityUtils; | 
|---|
| 23 | import org.json.JSONArray; | 
|---|
| 24 | import org.json.JSONException; | 
|---|
| 25 | import org.json.JSONObject; | 
|---|
| 26 |  | 
|---|
| 27 | /** | 
|---|
| 28 | * @author Christian Illy | 
|---|
| 29 | */ | 
|---|
| 30 | public class DrupalJSONQuery { | 
|---|
| 31 |  | 
|---|
| 32 | private static String getDepotUrl() { | 
|---|
| 33 | return Settings.getInstance().get("depot_api_url", | 
|---|
| 34 | "http://mods.oni2.net/?q=api/"); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | /** | 
|---|
| 38 | * Execute an REST action through a HTTP POST query | 
|---|
| 39 | * | 
|---|
| 40 | * @param resource | 
|---|
| 41 | *            Resource to run on | 
|---|
| 42 | * @param action | 
|---|
| 43 | *            Action to call | 
|---|
| 44 | * @param postData | 
|---|
| 45 | *            Fieldname / value pairs to include in POST data | 
|---|
| 46 | * @return JSON structure of item | 
|---|
| 47 | * @throws Exception | 
|---|
| 48 | *             on HTTP error | 
|---|
| 49 | */ | 
|---|
| 50 | public static JSONArray postAction(String resource, String action, | 
|---|
| 51 | HashMap<String, String> postData) throws Exception { | 
|---|
| 52 | try { | 
|---|
| 53 | List<NameValuePair> nvps = new ArrayList<NameValuePair>(); | 
|---|
| 54 | for (String key : postData.keySet()) { | 
|---|
| 55 | nvps.add(new BasicNameValuePair(key, postData.get(key))); | 
|---|
| 56 | } | 
|---|
| 57 | HttpEntity data = new UrlEncodedFormEntity(nvps); | 
|---|
| 58 | return executeQuery(getDepotUrl() + resource + "/" + action | 
|---|
| 59 | + ".json", data); | 
|---|
| 60 | } catch (UnsupportedEncodingException e) { | 
|---|
| 61 | // TODO Auto-generated catch block | 
|---|
| 62 | e.printStackTrace(); | 
|---|
| 63 | } | 
|---|
| 64 | return null; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | /** | 
|---|
| 68 | * Execute an REST references query through a HTTP GET query | 
|---|
| 69 | * | 
|---|
| 70 | * @param resource | 
|---|
| 71 | *            Resource to run on | 
|---|
| 72 | * @param index | 
|---|
| 73 | *            Index of item to get the references from | 
|---|
| 74 | * @param refName | 
|---|
| 75 | *            Name of references type | 
|---|
| 76 | * @return JSON structure of item | 
|---|
| 77 | * @throws Exception | 
|---|
| 78 | *             on HTTP error | 
|---|
| 79 | */ | 
|---|
| 80 | public static JSONArray getReferences(String resource, int index, | 
|---|
| 81 | String refName) throws Exception { | 
|---|
| 82 | return executeQuery( | 
|---|
| 83 | getDepotUrl() + resource + "/" + Integer.toString(index) + "/" | 
|---|
| 84 | + refName + ".json", null); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** | 
|---|
| 88 | * Execute an REST item query through a HTTP GET query | 
|---|
| 89 | * | 
|---|
| 90 | * @param resource | 
|---|
| 91 | *            Resource to run on | 
|---|
| 92 | * @param index | 
|---|
| 93 | *            Index of item to get | 
|---|
| 94 | * @param parameters | 
|---|
| 95 | *            Parameters to pass (must start with ampersand "&") | 
|---|
| 96 | * @return JSON structure of item | 
|---|
| 97 | * @throws Exception | 
|---|
| 98 | *             on HTTP error | 
|---|
| 99 | */ | 
|---|
| 100 | public static JSONArray getItem(String resource, int index, | 
|---|
| 101 | String parameters) throws Exception { | 
|---|
| 102 | return executeQuery( | 
|---|
| 103 | getDepotUrl() + resource + "/" + Integer.toString(index) | 
|---|
| 104 | + ".json" + parameters, null); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | /** | 
|---|
| 108 | * Execute an REST index query through a HTTP GET query | 
|---|
| 109 | * | 
|---|
| 110 | * @param resource | 
|---|
| 111 | *            Resource to run on | 
|---|
| 112 | * @param page | 
|---|
| 113 | *            Number of page to get (for limited results, e.g. nodes), -1 to | 
|---|
| 114 | *            ignore | 
|---|
| 115 | * @return JSON structure of item | 
|---|
| 116 | * @throws Exception | 
|---|
| 117 | *             on HTTP error | 
|---|
| 118 | */ | 
|---|
| 119 | public static JSONArray getIndex(String resource, int page) | 
|---|
| 120 | throws Exception { | 
|---|
| 121 | String pageN = ""; | 
|---|
| 122 | if (page >= 0) | 
|---|
| 123 | pageN = "&page=" + Integer.toString(page); | 
|---|
| 124 | return executeQuery(getDepotUrl() + resource + ".json" + pageN, null); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | private static JSONArray executeQuery(String url, HttpEntity postData) | 
|---|
| 128 | throws Exception { | 
|---|
| 129 | BufferedReader input = null; | 
|---|
| 130 | HttpRequestBase httpQuery = null; | 
|---|
| 131 |  | 
|---|
| 132 | try { | 
|---|
| 133 | DefaultHttpClient httpclient = new DefaultHttpClient(); | 
|---|
| 134 | if (postData == null) { | 
|---|
| 135 | httpQuery = new HttpGet(url); | 
|---|
| 136 | } else { | 
|---|
| 137 | httpQuery = new HttpPost(url); | 
|---|
| 138 | ((HttpPost) httpQuery).setEntity(postData); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | HttpResponse response = httpclient.execute(httpQuery); | 
|---|
| 142 |  | 
|---|
| 143 | int code = response.getStatusLine().getStatusCode(); | 
|---|
| 144 | if ((code > 299) || (code < 200)) { | 
|---|
| 145 | throw new Exception(String.format( | 
|---|
| 146 | "Error fetching content (HTTP status code %d).", code)); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | HttpEntity entity = response.getEntity(); | 
|---|
| 150 |  | 
|---|
| 151 | input = new BufferedReader(new InputStreamReader( | 
|---|
| 152 | entity.getContent())); | 
|---|
| 153 | StringBuffer json = new StringBuffer(); | 
|---|
| 154 |  | 
|---|
| 155 | char data[] = new char[1024]; | 
|---|
| 156 | int dataRead; | 
|---|
| 157 | while ((dataRead = input.read(data, 0, 1024)) != -1) { | 
|---|
| 158 | json.append(data, 0, dataRead); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | EntityUtils.consume(entity); | 
|---|
| 162 |  | 
|---|
| 163 | JSONArray jA = null; | 
|---|
| 164 | if (json.charAt(0) == '{') { | 
|---|
| 165 | jA = new JSONArray(); | 
|---|
| 166 | jA.put(new JSONObject(json.toString())); | 
|---|
| 167 | } else | 
|---|
| 168 | jA = new JSONArray(json.toString()); | 
|---|
| 169 |  | 
|---|
| 170 | return jA; | 
|---|
| 171 | } catch (JSONException e) { | 
|---|
| 172 | // TODO Auto-generated catch block | 
|---|
| 173 | e.printStackTrace(); | 
|---|
| 174 | } catch (UnsupportedEncodingException e) { | 
|---|
| 175 | // TODO Auto-generated catch block | 
|---|
| 176 | e.printStackTrace(); | 
|---|
| 177 | } catch (IOException e) { | 
|---|
| 178 | // TODO Auto-generated catch block | 
|---|
| 179 | e.printStackTrace(); | 
|---|
| 180 | } finally { | 
|---|
| 181 | if (httpQuery != null) | 
|---|
| 182 | httpQuery.releaseConnection(); | 
|---|
| 183 | if (input != null) { | 
|---|
| 184 | try { | 
|---|
| 185 | input.close(); | 
|---|
| 186 | } catch (IOException e) { | 
|---|
| 187 | // TODO Auto-generated catch block | 
|---|
| 188 | e.printStackTrace(); | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|
| 192 | return null; | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|