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) {