source: AE/installer2/src/net/oni2/aeinstaller/backend/network/DrupalJSONQuery.java@ 618

Last change on this file since 618 was 605, checked in by alloc, 12 years ago

AEI2:

  • Added mod download prior to installation
  • Dependency checking (needs verification)
  • Conflicts checking basis (not implemented)
  • Run Oni through AEI
File size: 5.3 KB
Line 
1package net.oni2.aeinstaller.backend.network;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.io.UnsupportedEncodingException;
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10
11import net.oni2.aeinstaller.backend.Settings;
12
13import org.apache.http.HttpEntity;
14import org.apache.http.HttpResponse;
15import org.apache.http.NameValuePair;
16import org.apache.http.client.entity.UrlEncodedFormEntity;
17import org.apache.http.client.methods.HttpGet;
18import org.apache.http.client.methods.HttpPost;
19import org.apache.http.client.methods.HttpRequestBase;
20import org.apache.http.impl.client.DefaultHttpClient;
21import org.apache.http.message.BasicNameValuePair;
22import org.apache.http.util.EntityUtils;
23import org.json.JSONArray;
24import org.json.JSONException;
25import org.json.JSONObject;
26
27/**
28 * @author Christian Illy
29 */
30public 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 * @param pagesize
115 * Maximum number of elements to return
116 * @return JSON structure of item
117 * @throws Exception
118 * on HTTP error
119 */
120 public static JSONArray getIndex(String resource, int page, int pagesize)
121 throws Exception {
122 String pageN = "";
123 if (page >= 0)
124 pageN = "&page=" + Integer.toString(page);
125 String pagesizeN = "";
126 if (pagesize >= 0)
127 pagesizeN = "&pagesize=" + Integer.toString(pagesize);
128 return executeQuery(getDepotUrl() + resource + ".json" + pageN
129 + pagesizeN, null);
130 }
131
132 private static JSONArray executeQuery(String url, HttpEntity postData)
133 throws Exception {
134 BufferedReader input = null;
135 HttpRequestBase httpQuery = null;
136
137 try {
138 DefaultHttpClient httpclient = new DefaultHttpClient();
139 if (postData == null) {
140 httpQuery = new HttpGet(url);
141 } else {
142 httpQuery = new HttpPost(url);
143 ((HttpPost) httpQuery).setEntity(postData);
144 }
145
146 HttpResponse response = httpclient.execute(httpQuery);
147
148 int code = response.getStatusLine().getStatusCode();
149 if ((code > 299) || (code < 200)) {
150 throw new Exception(String.format(
151 "Error fetching content (HTTP status code %d).", code));
152 }
153
154 HttpEntity entity = response.getEntity();
155
156 input = new BufferedReader(new InputStreamReader(
157 entity.getContent()));
158 StringBuffer json = new StringBuffer();
159
160 char data[] = new char[1024];
161 int dataRead;
162 while ((dataRead = input.read(data, 0, 1024)) != -1) {
163 json.append(data, 0, dataRead);
164 }
165
166 EntityUtils.consume(entity);
167
168 JSONArray jA = null;
169 if (json.charAt(0) == '{') {
170 jA = new JSONArray();
171 jA.put(new JSONObject(json.toString()));
172 } else
173 jA = new JSONArray(json.toString());
174
175 return jA;
176 } catch (JSONException e) {
177 e.printStackTrace();
178 } catch (UnsupportedEncodingException e) {
179 e.printStackTrace();
180 } catch (IOException e) {
181 e.printStackTrace();
182 } finally {
183 if (httpQuery != null)
184 httpQuery.releaseConnection();
185 if (input != null) {
186 try {
187 input.close();
188 } catch (IOException e) {
189 e.printStackTrace();
190 }
191 }
192 }
193 return null;
194 }
195}
Note: See TracBrowser for help on using the repository browser.