use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryResource method updateCategory.
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update the category", notes = "User must have the PORTAL_CATEGORY[UPDATE] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Category successfully updated", response = CategoryEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_CATEGORY, acls = RolePermissionAction.UPDATE) })
public Response updateCategory(@Valid @NotNull final UpdateCategoryEntity category) {
try {
ImageUtils.verify(category.getPicture());
ImageUtils.verify(category.getBackground());
} catch (InvalidImageException e) {
throw new BadRequestException("Invalid image format");
}
CategoryEntity categoryEntity = categoryService.update(categoryId, category);
setPictures(categoryEntity, false);
return Response.ok(categoryEntity).build();
}
use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryResource method getCategory.
@GET
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get the category", notes = "User must have the PORTAL_CATEGORY[READ] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Category's definition", response = CategoryEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
public CategoryEntity getCategory() {
boolean canShowCategory = hasPermission(RolePermission.ENVIRONMENT_CATEGORY, RolePermissionAction.READ);
CategoryEntity category = categoryService.findById(categoryId);
if (!canShowCategory && category.isHidden()) {
throw new UnauthorizedAccessException();
}
// set picture
setPictures(category, false);
return category;
}
use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryResource method getImageResponse.
private Response getImageResponse(Request request, InlinePictureEntity image) {
boolean canShowCategory = hasPermission(RolePermission.ENVIRONMENT_CATEGORY, RolePermissionAction.READ);
CategoryEntity category = categoryService.findById(categoryId);
if (!canShowCategory && category.isHidden()) {
throw new UnauthorizedAccessException();
}
CacheControl cc = new CacheControl();
cc.setNoTransform(true);
cc.setMustRevalidate(false);
cc.setNoCache(false);
cc.setMaxAge(86400);
if (image == null || image.getContent() == null) {
return Response.ok().build();
}
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(baos).cacheControl(cc).tag(etag).type(image.getType()).build();
}
use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryServiceImpl method getBackground.
@Override
public InlinePictureEntity getBackground(String categoryId) {
CategoryEntity categoryEntity = findById(categoryId);
InlinePictureEntity imageEntity = new InlinePictureEntity();
if (categoryEntity.getBackground() != null) {
String[] parts = categoryEntity.getBackground().split(";", 2);
imageEntity.setType(parts[0].split(":")[1]);
String base64Content = categoryEntity.getBackground().split(",", 2)[1];
imageEntity.setContent(DatatypeConverter.parseBase64Binary(base64Content));
}
return imageEntity;
}
use of io.gravitee.rest.api.model.CategoryEntity in project gravitee-management-rest-api by gravitee-io.
the class CategoryServiceImpl method update.
@Override
public CategoryEntity update(String categoryId, UpdateCategoryEntity categoryEntity) {
try {
LOGGER.debug("Update Category {}", categoryId);
Optional<Category> optCategoryToUpdate = categoryRepository.findById(categoryId);
if (!optCategoryToUpdate.isPresent()) {
throw new CategoryNotFoundException(categoryId);
}
final Category categoryToUpdate = optCategoryToUpdate.get();
Category category = convert(categoryEntity, categoryToUpdate.getEnvironmentId());
// If no new picture and the current picture url is not the default one, keep the current picture
if (categoryEntity.getPicture() == null && categoryEntity.getPictureUrl() != null && categoryEntity.getPictureUrl().indexOf("?hash") > 0) {
category.setPicture(categoryToUpdate.getPicture());
}
final Date updatedAt = new Date();
category.setCreatedAt(categoryToUpdate.getCreatedAt());
category.setUpdatedAt(updatedAt);
CategoryEntity updatedCategory = convert(categoryRepository.update(category));
auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_UPDATED, updatedAt, categoryToUpdate, category);
return updatedCategory;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update category {}", categoryEntity.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to update category " + categoryEntity.getName(), ex);
}
}
Aggregations