AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> { public String computePicture(final byte[] pictureData) { String pictureContent = new String(pictureData); if(pictureContent.toUpperCase().startsWith("HTTP")) { return pictureContent; } try { String contentType = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(pictureData)); if(contentType != null) { StringBuilder sb = new StringBuilder(); sb.append("data:"); sb.append(contentType); sb.append(";base64,"); sb.append(StringUtils.newStringUtf8(Base64.encodeBase64(pictureData, false))); return sb.toString(); } else { LOGGER.warn("Unable to compute the user picture from URL."); } } catch (IOException e) { LOGGER.warn("Problem while parsing picture", e); } return null; }  @Override void onApplicationEvent(AuthenticationSuccessEvent event); String computePicture(final byte[] pictureData);  }
@Test public void shouldReturnPictureURL() { String pictureURL = "https: String picture = listener.computePicture(pictureURL.getBytes()); assertEquals(pictureURL, picture); }
@Test public void shouldReturnPictureContent() throws URISyntaxException, IOException { Path resourcePath = Paths.get(getClass().getResource("beer.jpeg").toURI()); byte[] pictureData = Files.readAllBytes(resourcePath); String picture = listener.computePicture(pictureData); String expectedPictureContent = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxENEhAPDxIOEBAPDhAPDw4NDQ8ODQ8OFhEXFhURFRYkHSsiGBopJxUTLTEhJikrLi4uFx84ODMtNyg5MCsBCgoKDg0OGhAQFzAdHSI3LSsrLS0rLS0tLS0uLys3Ky0tKy4tKys3LS0vKy0tNy0rLS0tKy0rLSsrLS0rLSstK assertEquals(expectedPictureContent, picture); }