From deefd69924eebde513091fe938d868a0c087b2a9 Mon Sep 17 00:00:00 2001 From: Rachit Sachdeva Date: Thu, 19 Mar 2015 15:10:01 +0530 Subject: [PATCH] Adding a downloadMedia function that return an ImputStream --- java/org/webservice/fotolia/FotoliaApi.java | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/java/org/webservice/fotolia/FotoliaApi.java b/java/org/webservice/fotolia/FotoliaApi.java index 9a86807..9748ba5 100644 --- a/java/org/webservice/fotolia/FotoliaApi.java +++ b/java/org/webservice/fotolia/FotoliaApi.java @@ -223,6 +223,49 @@ public void downloadMedia(final String download_url, final String output_file) t if (output_file != null) { stream.close(); } + } + + /** + * Download a media and get the InputStream with its binary + * + * @param download_url URL as returned by getMedia() + */ + public InputStream getMediaInputStream(final String download_url) throws FileNotFoundException, IOException, FotoliaApiException + { + DefaultHttpClient client; + HttpResponse response; + StatusLine statusLine; + HttpEntity entity; + JSONObject obj; + InputStream inputStream = null; + String error_msg; + int error_code; + + client = this._getHttpClient(true); + response = client.execute(new HttpGet(download_url)); + + statusLine = response.getStatusLine(); + entity = response.getEntity(); + if (statusLine.getStatusCode() != 200) { + if (entity == null) { + throw new FotoliaApiException(statusLine.getStatusCode(), + statusLine.getReasonPhrase()); + } else { + obj = (JSONObject) JSONValue.parse(EntityUtils.toString(entity)); + error_msg = (String) obj.get("error"); + if (obj.get("code") != null) { + error_code = Integer.parseInt((String) obj.get("code")); + } else { + error_code = statusLine.getStatusCode(); + } + + throw new FotoliaApiException(error_code, error_msg); + } + + } + + inputStream = new ByteArrayInputStream(EntityUtils.toByteArray(entity)); + return inputStream; } /**