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 | e.printStackTrace();
|
---|
62 | }
|
---|
63 | return null;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Execute an REST references query through a HTTP GET query
|
---|
68 | *
|
---|
69 | * @param resource
|
---|
70 | * Resource to run on
|
---|
71 | * @param index
|
---|
72 | * Index of item to get the references from
|
---|
73 | * @param refName
|
---|
74 | * Name of references type
|
---|
75 | * @return JSON structure of item
|
---|
76 | * @throws Exception
|
---|
77 | * on HTTP error
|
---|
78 | */
|
---|
79 | public static JSONArray getReferences(String resource, int index,
|
---|
80 | String refName) throws Exception {
|
---|
81 | return executeQuery(
|
---|
82 | getDepotUrl() + resource + "/" + Integer.toString(index) + "/"
|
---|
83 | + refName + ".json", null);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Execute an REST item query through a HTTP GET query
|
---|
88 | *
|
---|
89 | * @param resource
|
---|
90 | * Resource to run on
|
---|
91 | * @param index
|
---|
92 | * Index of item to get
|
---|
93 | * @param parameters
|
---|
94 | * Parameters to pass (must start with ampersand "&")
|
---|
95 | * @return JSON structure of item
|
---|
96 | * @throws Exception
|
---|
97 | * on HTTP error
|
---|
98 | */
|
---|
99 | public static JSONArray getItem(String resource, int index,
|
---|
100 | String parameters) throws Exception {
|
---|
101 | return executeQuery(
|
---|
102 | getDepotUrl() + resource + "/" + Integer.toString(index)
|
---|
103 | + ".json" + parameters, null);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Execute an REST index query through a HTTP GET query
|
---|
108 | *
|
---|
109 | * @param resource
|
---|
110 | * Resource to run on
|
---|
111 | * @param page
|
---|
112 | * Number of page to get (for limited results, e.g. nodes), -1 to
|
---|
113 | * ignore
|
---|
114 | * @return JSON structure of item
|
---|
115 | * @throws Exception
|
---|
116 | * on HTTP error
|
---|
117 | */
|
---|
118 | public static JSONArray getIndex(String resource, int page)
|
---|
119 | throws Exception {
|
---|
120 | String pageN = "";
|
---|
121 | if (page >= 0)
|
---|
122 | pageN = "&page=" + Integer.toString(page);
|
---|
123 | return executeQuery(getDepotUrl() + resource + ".json" + pageN, null);
|
---|
124 | }
|
---|
125 |
|
---|
126 | private static JSONArray executeQuery(String url, HttpEntity postData)
|
---|
127 | throws Exception {
|
---|
128 | BufferedReader input = null;
|
---|
129 | HttpRequestBase httpQuery = null;
|
---|
130 |
|
---|
131 | try {
|
---|
132 | DefaultHttpClient httpclient = new DefaultHttpClient();
|
---|
133 | if (postData == null) {
|
---|
134 | httpQuery = new HttpGet(url);
|
---|
135 | } else {
|
---|
136 | httpQuery = new HttpPost(url);
|
---|
137 | ((HttpPost) httpQuery).setEntity(postData);
|
---|
138 | }
|
---|
139 |
|
---|
140 | HttpResponse response = httpclient.execute(httpQuery);
|
---|
141 |
|
---|
142 | int code = response.getStatusLine().getStatusCode();
|
---|
143 | if ((code > 299) || (code < 200)) {
|
---|
144 | throw new Exception(String.format(
|
---|
145 | "Error fetching content (HTTP status code %d).", code));
|
---|
146 | }
|
---|
147 |
|
---|
148 | HttpEntity entity = response.getEntity();
|
---|
149 |
|
---|
150 | input = new BufferedReader(new InputStreamReader(
|
---|
151 | entity.getContent()));
|
---|
152 | StringBuffer json = new StringBuffer();
|
---|
153 |
|
---|
154 | char data[] = new char[1024];
|
---|
155 | int dataRead;
|
---|
156 | while ((dataRead = input.read(data, 0, 1024)) != -1) {
|
---|
157 | json.append(data, 0, dataRead);
|
---|
158 | }
|
---|
159 |
|
---|
160 | EntityUtils.consume(entity);
|
---|
161 |
|
---|
162 | JSONArray jA = null;
|
---|
163 | if (json.charAt(0) == '{') {
|
---|
164 | jA = new JSONArray();
|
---|
165 | jA.put(new JSONObject(json.toString()));
|
---|
166 | } else
|
---|
167 | jA = new JSONArray(json.toString());
|
---|
168 |
|
---|
169 | return jA;
|
---|
170 | } catch (JSONException e) {
|
---|
171 | e.printStackTrace();
|
---|
172 | } catch (UnsupportedEncodingException e) {
|
---|
173 | e.printStackTrace();
|
---|
174 | } catch (IOException e) {
|
---|
175 | e.printStackTrace();
|
---|
176 | } finally {
|
---|
177 | if (httpQuery != null)
|
---|
178 | httpQuery.releaseConnection();
|
---|
179 | if (input != null) {
|
---|
180 | try {
|
---|
181 | input.close();
|
---|
182 | } catch (IOException e) {
|
---|
183 | e.printStackTrace();
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | return null;
|
---|
188 | }
|
---|
189 | }
|
---|