Search in sources :

Example 1 with UrlPictureEntity

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();
}
Also used : UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity)

Example 2 with UrlPictureEntity

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;
    }
}
Also used : UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity)

Example 3 with UrlPictureEntity

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);
}
Also used : UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) Theme(io.gravitee.repository.management.model.Theme) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) PictureEntity(io.gravitee.rest.api.model.PictureEntity) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) Test(org.junit.Test)

Example 4 with 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());
}
Also used : Response(javax.ws.rs.core.Response) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 5 with UrlPictureEntity

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());
}
Also used : Response(javax.ws.rs.core.Response) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) Test(org.junit.Test)

Aggregations

UrlPictureEntity (io.gravitee.rest.api.model.UrlPictureEntity)6 InlinePictureEntity (io.gravitee.rest.api.model.InlinePictureEntity)4 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Response (javax.ws.rs.core.Response)2 Theme (io.gravitee.repository.management.model.Theme)1 PictureEntity (io.gravitee.rest.api.model.PictureEntity)1 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)1 UserEntity (io.gravitee.rest.api.model.UserEntity)1