Added some log message to better debug and remove the exception raise

This commit is contained in:
Mauro Mugnaini 2020-08-13 20:47:37 +02:00
parent da8ed52808
commit bb3728c421
1 changed files with 28 additions and 20 deletions

View File

@ -206,26 +206,34 @@ public class OpenIdConnectRESTHelper {
return false; return false;
} }
public static byte[] getUserAvatar(URL avatarURL, JWTToken token) throws Exception { public static byte[] getUserAvatar(URL avatarURL, JWTToken token) {
HttpURLConnection conn = (HttpURLConnection) avatarURL.openConnection(); logger.debug("Getting user avatar from URL: {}", avatarURL);
conn.setRequestMethod("GET"); ByteArrayOutputStream buffer;
conn.setDoOutput(false); try {
conn.setDoInput(true); HttpURLConnection conn = (HttpURLConnection) avatarURL.openConnection();
conn.setRequestProperty("Accept", "image/png, image/jpeg, image/gif"); conn.setRequestMethod("GET");
if (token != null) { conn.setDoOutput(false);
String authorization = token.getAccessTokenAsBearer(); conn.setDoInput(true);
logger.debug("Adding authorization header as: {}", authorization); conn.setRequestProperty("Accept", "image/png, image/jpeg, image/gif");
conn.setRequestProperty("Authorization", authorization); if (token != null) {
} String authorization = token.getAccessTokenAsBearer();
InputStream is = conn.getInputStream(); logger.debug("Adding authorization header as: {}", authorization);
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); conn.setRequestProperty("Authorization", authorization);
int nRead; }
byte[] data = new byte[1024]; logger.debug("Getting the resource opening the stream");
while ((nRead = is.read(data, 0, data.length)) != -1) { InputStream is = conn.getInputStream();
buffer.write(data, 0, nRead); 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(); buffer.flush();
return buffer.toByteArray(); return buffer.toByteArray();
} catch (IOException e) {
logger.debug("Can't download avatar", e);
}
return null;
} }
} }