Search in sources :

Example 6 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)

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