@@ -161,7 +161,7 @@ else if (value instanceof JSONArray) {
161161 }
162162 }
163163
164- protected static String getContent (String method , String urlString , JSONObject parameters ) {
164+ protected static HttpURLConnection setupConnection (String method , String urlString , JSONObject parameters ) {
165165 try {
166166 String appender = urlString .contains ("?" ) ? "&" : "?" ;
167167 String parameterString = JSONToQueryString (parameters );
@@ -181,26 +181,51 @@ protected static String getContent(String method, String urlString, JSONObject p
181181
182182 // Add the session cookie.
183183 connection .setRequestProperty ("Cookie" , "session=" + sessionKey );
184- connection .setRequestProperty ("Accept-Encoding" , "gzip, deflate" );
184+ if (!debug ) {
185+ connection .setRequestProperty ("Accept-Encoding" , "gzip, deflate" );
186+ }
185187
186188 if (method .equals ("POST" ) && parameterString .length () > 0 ) {
187189 connection .setRequestProperty ("Content-Length" , Integer .toString (parameterString .length ()));
188190 connection .addRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
189191 connection .setRequestProperty ("charset" , "utf-8" );
190192 connection .getOutputStream ().write (parameterString .getBytes ("UTF8" ));
191193 }
194+ return connection ;
195+ } catch (Exception e ) {
196+ e .printStackTrace ();
197+ return null ;
198+ }
199+ }
192200
193- connection .connect ();
201+ protected static InputStream getInputStream (HttpURLConnection connection ) {
202+ try {
194203 InputStream inputStream = connection .getInputStream ();
195-
204+ connection . connect ();
196205 // If the output was gzipped, change the input stream to the ungzipped version.
197206 if ("gzip" .equals (connection .getContentEncoding ())) {
198207 inputStream = new GZIPInputStream (inputStream );
199208 }
209+ return inputStream ;
210+ } catch (Exception e ) {
211+ return null ;
212+ }
213+ }
214+
215+ protected static String getContentString (String method , String urlString , JSONObject parameters ) {
216+ HttpURLConnection connection = setupConnection (method , urlString , parameters );
217+ if (connection == null ) {
218+ return null ;
219+ }
220+ try {
221+ InputStream inputStream = getInputStream (connection );
222+ if (inputStream == null ) {
223+ return null ;
224+ }
200225
201226 String output = IOUtils .toString (inputStream , "UTF-8" );
202- inputStream .close ();
203227
228+ inputStream .close ();
204229 connection .disconnect ();
205230
206231 return output ;
@@ -211,8 +236,37 @@ protected static String getContent(String method, String urlString, JSONObject p
211236 }
212237 }
213238
239+ protected static byte [] getContentBytes (String method , String urlString , JSONObject parameters , String requiredContentTypePrefix ) {
240+ HttpURLConnection connection = setupConnection (method , urlString , parameters );
241+ if (connection == null ) {
242+ return null ;
243+ }
244+ try {
245+ InputStream inputStream = getInputStream (connection );
246+ if (inputStream == null ) {
247+ return null ;
248+ }
249+
250+ // Require the content type header.
251+ if (requiredContentTypePrefix != null && !connection .getContentType ().startsWith (requiredContentTypePrefix )) {
252+ return null ;
253+ }
254+
255+ byte [] bytes = IOUtils .toByteArray (inputStream );
256+
257+ inputStream .close ();
258+ connection .disconnect ();
259+
260+ return bytes ;
261+ } catch (Exception e ) {
262+ // Nothing loaded.
263+ e .printStackTrace ();
264+ return null ;
265+ }
266+ }
267+
214268 protected static JSONObject sendAndReturnObject (String method , String urlString , JSONObject parameters ) {
215- String content = getContent (method , urlString , parameters );
269+ String content = getContentString (method , urlString , parameters );
216270 try {
217271 JSONObject response = new JSONObject (content );
218272 handleJSONError (response );
@@ -225,7 +279,7 @@ protected static JSONObject sendAndReturnObject(String method, String urlString,
225279
226280 protected static JSONArray sendAndReturnArray (String method , String urlString , JSONObject parameters ) {
227281 // TODO: If there is an error, it will come back as an object.
228- String content = getContent (method , urlString , parameters );
282+ String content = getContentString (method , urlString , parameters );
229283 try {
230284 return new JSONArray (content );
231285 } catch (Exception e ) {
@@ -241,8 +295,20 @@ protected static JSONArray sendAndReturnArray(String method, String urlString, J
241295 }
242296
243297 protected static byte [] sendAndReturnImage (String method , String urlString , JSONObject parameters ) {
244- // TODO: return a byte array pointer.
245- return null ;
298+ byte [] data = getContentBytes (method , urlString , parameters , "image/" );
299+ // If this is json, it could be an error.
300+ if (data [0 ] == '{' ) {
301+ try {
302+ JSONObject response = new JSONObject (data .toString ());
303+ if (response != null ) {
304+ handleJSONError (response );
305+ }
306+ } catch (Exception e ) {
307+ // This could handle under normal circumstances.
308+ e .printStackTrace ();
309+ }
310+ }
311+ return data ;
246312 }
247313
248314 public static JSONObject GET (String url , JSONObject parameters ) {
@@ -264,4 +330,8 @@ public static JSONObject POST(String url) {
264330 public static JSONArray GETArray (String url , JSONObject parameters ) {
265331 return sendAndReturnArray ("GET" , url , parameters );
266332 }
333+
334+ public static byte [] GETImage (String url , JSONObject parameters ) {
335+ return sendAndReturnImage ("GET" , url , parameters );
336+ }
267337}
0 commit comments