Search in sources :

Example 1 with CategoryEntity

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();
}
Also used : InvalidImageException(io.gravitee.rest.api.exception.InvalidImageException) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 2 with CategoryEntity

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;
}
Also used : UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 3 with CategoryEntity

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();
}
Also used : UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 4 with CategoryEntity

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;
}
Also used : UuidString(io.gravitee.rest.api.service.common.UuidString) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity)

Example 5 with CategoryEntity

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);
    }
}
Also used : Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Aggregations

CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)29 Test (org.junit.Test)14 UpdateCategoryEntity (io.gravitee.rest.api.model.UpdateCategoryEntity)13 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)12 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)12 Before (org.junit.Before)11 Assert (org.junit.Assert)9 Mockito (org.mockito.Mockito)9 Category (io.gravitee.repository.management.model.Category)7 NewCategoryEntity (io.gravitee.rest.api.model.NewCategoryEntity)7 java.util (java.util)7 Collectors (java.util.stream.Collectors)7 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)6 ApiLifecycleState (io.gravitee.rest.api.model.api.ApiLifecycleState)6 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)6 FilteredEntities (io.gravitee.rest.api.model.filtering.FilteredEntities)6 io.gravitee.rest.api.portal.rest.model (io.gravitee.rest.api.portal.rest.model)6 Error (io.gravitee.rest.api.portal.rest.model.Error)6 Entity (javax.ws.rs.client.Entity)6 Response (javax.ws.rs.core.Response)6