From 35459bec6bc99e1a1230b07bddb3cea01d83ec9a Mon Sep 17 00:00:00 2001 From: Mauro Mugnaini Date: Wed, 20 Jan 2021 00:58:02 +0100 Subject: [PATCH] Revised get avatar helper method --- .../oidc/rest/OpenIdConnectRESTHelper.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/gcube/oidc/rest/OpenIdConnectRESTHelper.java b/src/main/java/org/gcube/oidc/rest/OpenIdConnectRESTHelper.java index b95d125..8c2ba3d 100644 --- a/src/main/java/org/gcube/oidc/rest/OpenIdConnectRESTHelper.java +++ b/src/main/java/org/gcube/oidc/rest/OpenIdConnectRESTHelper.java @@ -274,21 +274,24 @@ public class OpenIdConnectRESTHelper { logger.debug("Adding authorization header as: {}", authorization); conn.setRequestProperty("Authorization", authorization); } - String contentType = conn.getContentType(); - int contentLength = conn.getContentLength(); - - logger.debug("Getting the stream to a {} bytes lenght resource with MIME: {}", contentLength, contentType); - - InputStream is = conn.getInputStream(); - buffer = new ByteArrayOutputStream(); - int nRead; - byte[] data = new byte[1024]; - while ((nRead = is.read(data, 0, data.length)) != -1) { - buffer.write(data, 0, nRead); + if (conn.getResponseCode() == 200) { + String contentType = conn.getContentType(); + + logger.debug("Getting the stream to the avatar resource with MIME: {}", contentType); + + InputStream is = conn.getInputStream(); + buffer = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[1024]; + while ((nRead = is.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + + buffer.flush(); + return buffer.toByteArray(); + } else { + logger.debug("Something wrong on the avatar server. Response code: {}", conn.getResponseCode()); } - - buffer.flush(); - return buffer.toByteArray(); } catch (FileNotFoundException e) { logger.debug("User's avatar not found"); } catch (IOException e) {