| 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.depot.DepotConfig;
|
|---|
| 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 | /**
|
|---|
| 33 | * Execute an REST action through a HTTP POST query
|
|---|
| 34 | *
|
|---|
| 35 | * @param resource
|
|---|
| 36 | * Resource to run on
|
|---|
| 37 | * @param action
|
|---|
| 38 | * Action to call
|
|---|
| 39 | * @param postData
|
|---|
| 40 | * Fieldname / value pairs to include in POST data
|
|---|
| 41 | * @return JSON structure of item
|
|---|
| 42 | * @throws Exception
|
|---|
| 43 | * on HTTP error
|
|---|
| 44 | */
|
|---|
| 45 | public static JSONArray postAction(String resource, String action,
|
|---|
| 46 | HashMap<String, String> postData) throws Exception {
|
|---|
| 47 | try {
|
|---|
| 48 | List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|---|
| 49 | for (String key : postData.keySet()) {
|
|---|
| 50 | nvps.add(new BasicNameValuePair(key, postData.get(key)));
|
|---|
| 51 | }
|
|---|
| 52 | HttpEntity data = new UrlEncodedFormEntity(nvps);
|
|---|
| 53 | return executeQuery(DepotConfig.getDepotApiUrl() + resource + "/" + action
|
|---|
| 54 | + ".json", data);
|
|---|
| 55 | } catch (UnsupportedEncodingException e) {
|
|---|
| 56 | e.printStackTrace();
|
|---|
| 57 | }
|
|---|
| 58 | return null;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Execute an REST references query through a HTTP GET query
|
|---|
| 63 | *
|
|---|
| 64 | * @param resource
|
|---|
| 65 | * Resource to run on
|
|---|
| 66 | * @param index
|
|---|
| 67 | * Index of item to get the references from
|
|---|
| 68 | * @param refName
|
|---|
| 69 | * Name of references type
|
|---|
| 70 | * @return JSON structure of item
|
|---|
| 71 | * @throws Exception
|
|---|
| 72 | * on HTTP error
|
|---|
| 73 | */
|
|---|
| 74 | public static JSONArray getReferences(String resource, int index,
|
|---|
| 75 | String refName) throws Exception {
|
|---|
| 76 | return executeQuery(
|
|---|
| 77 | DepotConfig.getDepotApiUrl() + resource + "/" + Integer.toString(index) + "/"
|
|---|
| 78 | + refName + ".json", null);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Execute an REST item query through a HTTP GET query
|
|---|
| 83 | *
|
|---|
| 84 | * @param resource
|
|---|
| 85 | * Resource to run on
|
|---|
| 86 | * @param index
|
|---|
| 87 | * Index of item to get
|
|---|
| 88 | * @param parameters
|
|---|
| 89 | * Parameters to pass (must start with ampersand "&")
|
|---|
| 90 | * @return JSON structure of item
|
|---|
| 91 | * @throws Exception
|
|---|
| 92 | * on HTTP error
|
|---|
| 93 | */
|
|---|
| 94 | public static JSONArray getItem(String resource, int index,
|
|---|
| 95 | String parameters) throws Exception {
|
|---|
| 96 | return executeQuery(
|
|---|
| 97 | DepotConfig.getDepotApiUrl() + resource + "/" + Integer.toString(index)
|
|---|
| 98 | + ".json" + parameters, null);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Execute an REST index query through a HTTP GET query
|
|---|
| 103 | *
|
|---|
| 104 | * @param resource
|
|---|
| 105 | * Resource to run on
|
|---|
| 106 | * @param page
|
|---|
| 107 | * Number of page to get (for limited results, e.g. nodes), -1 to
|
|---|
| 108 | * ignore
|
|---|
| 109 | * @param pagesize
|
|---|
| 110 | * Maximum number of elements to return
|
|---|
| 111 | * @return JSON structure of item
|
|---|
| 112 | * @throws Exception
|
|---|
| 113 | * on HTTP error
|
|---|
| 114 | */
|
|---|
| 115 | public static JSONArray getIndex(String resource, int page, int pagesize)
|
|---|
| 116 | throws Exception {
|
|---|
| 117 | String pageN = "";
|
|---|
| 118 | if (page >= 0)
|
|---|
| 119 | pageN = "&page=" + Integer.toString(page);
|
|---|
| 120 | String pagesizeN = "";
|
|---|
| 121 | if (pagesize >= 0)
|
|---|
| 122 | pagesizeN = "&pagesize=" + Integer.toString(pagesize);
|
|---|
| 123 | return executeQuery(DepotConfig.getDepotApiUrl() + resource + ".json" + pageN
|
|---|
| 124 | + pagesizeN, 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 | e.printStackTrace();
|
|---|
| 173 | } catch (UnsupportedEncodingException e) {
|
|---|
| 174 | e.printStackTrace();
|
|---|
| 175 | } catch (IOException e) {
|
|---|
| 176 | e.printStackTrace();
|
|---|
| 177 | } finally {
|
|---|
| 178 | if (httpQuery != null)
|
|---|
| 179 | httpQuery.releaseConnection();
|
|---|
| 180 | if (input != null) {
|
|---|
| 181 | try {
|
|---|
| 182 | input.close();
|
|---|
| 183 | } catch (IOException e) {
|
|---|
| 184 | e.printStackTrace();
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | return null;
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|