use of io.gravitee.rest.api.model.UrlPictureEntity in project gravitee-management-rest-api by gravitee-io.
the class ThemeResource method buildPictureResponse.
private Response buildPictureResponse(PictureEntity picture, @Context Request request) {
if (picture == null) {
return Response.ok().build();
}
if (picture instanceof UrlPictureEntity) {
return Response.temporaryRedirect(URI.create(((UrlPictureEntity) picture).getUrl())).build();
}
CacheControl cc = new CacheControl();
cc.setNoTransform(true);
cc.setMustRevalidate(false);
cc.setNoCache(false);
cc.setMaxAge(86400);
InlinePictureEntity image = (InlinePictureEntity) picture;
EntityTag etag = new EntityTag(Integer.toString(new String(image.getContent()).hashCode()));
Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
if (builder != null) {
// Preconditions are not met, returning HTTP 304 'not-modified'
return builder.cacheControl(cc).build();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(image.getContent(), 0, image.getContent().length);
return Response.ok().entity(baos).cacheControl(cc).tag(etag).type(image.getType()).build();
}
use of io.gravitee.rest.api.model.UrlPictureEntity in project gravitee-management-rest-api by gravitee-io.
the class ThemeServiceImpl method convertToPicture.
private PictureEntity convertToPicture(String picture) {
if (picture.matches("^(http|https)://.*$")) {
return new UrlPictureEntity(picture);
} else {
InlinePictureEntity imageEntity = new InlinePictureEntity();
String[] parts = picture.split(";", 2);
imageEntity.setType(parts[0].split(":")[1]);
String base64Content = picture.split(",", 2)[1];
imageEntity.setContent(DatatypeConverter.parseBase64Binary(base64Content));
return imageEntity;
}
}
use of io.gravitee.rest.api.model.UrlPictureEntity in project gravitee-management-rest-api by gravitee-io.
the class ThemeServiceTest method shouldGetBackgroundImageUrl.
@Test
public void shouldGetBackgroundImageUrl() throws TechnicalException {
final Theme theme = mock(Theme.class);
when(theme.getBackgroundImage()).thenReturn("http://localhost/image");
when(theme.getId()).thenReturn(THEME_ID);
when(theme.getName()).thenReturn("NAME");
when(theme.isEnabled()).thenReturn(true);
when(theme.getDefinition()).thenReturn(themeServiceImpl.getDefaultDefinition());
when(themeRepository.findByReferenceIdAndReferenceType(GraviteeContext.getCurrentEnvironment(), ENVIRONMENT.name())).thenReturn(singleton(theme));
PictureEntity backgroundImage = themeService.getBackgroundImage(THEME_ID);
assertNotNull(backgroundImage);
assertTrue(backgroundImage instanceof UrlPictureEntity);
}
use of io.gravitee.rest.api.model.UrlPictureEntity in project gravitee-management-rest-api by gravitee-io.
the class UserResourceTest method shouldGetUserAvatarRedirectUrl.
@Test
public void shouldGetUserAvatarRedirectUrl() throws IOException {
doReturn(new UserEntity()).when(userService).findById(any());
doReturn(new UserEntity()).when(userService).findByIdWithRoles(any());
doReturn(new UrlPictureEntity(target().getUri().toURL().toString())).when(userService).getPicture(any());
final Response response = target().path("avatar").request().get();
assertEquals(OK_200, response.getStatus());
}
use of io.gravitee.rest.api.model.UrlPictureEntity in project gravitee-management-rest-api by gravitee-io.
the class UsersResourceTest method shouldGetUserAvatarRedirectUrl.
@Test
public void shouldGetUserAvatarRedirectUrl() throws IOException {
doReturn(new UrlPictureEntity(root().path("openapi").getUri().toURL().toString())).when(userService).getPicture(any());
final Response response = target().path("userId").path("avatar").request().get();
assertEquals(OK_200, response.getStatus());
}
Aggregations