Search in sources :

Example 16 with ResourceNotFoundException

use of org.entando.entando.aps.system.exception.ResourceNotFoundException in project entando-core by entando.

the class FileBrowserService method checkResource.

protected void checkResource(String currentPath, String objectName, Boolean protectedFolder) {
    if (null == protectedFolder && !StringUtils.isEmpty(currentPath)) {
        BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(objectName, objectName);
        bindingResult.reject(FileBrowserValidator.ERRCODE_REQUIRED_FOLDER_TYPE, "fileBrowser.browser.protectedFolder.required");
        throw new ValidationGenericException(bindingResult);
    }
    if (null == protectedFolder) {
        return;
    }
    try {
        boolean exists = this.getStorageManager().exists(currentPath, protectedFolder);
        if (!exists) {
            logger.warn("no resource found for path {} - type {}", currentPath, protectedFolder);
            throw new ResourceNotFoundException(FileBrowserValidator.ERRCODE_RESOURCE_DOES_NOT_EXIST, objectName, currentPath);
        }
    } catch (ResourceNotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Error checking resource {} , protected {} ", currentPath, protectedFolder, e);
        throw new RestServerError("error checking resource", e);
    }
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RestServerError(org.entando.entando.aps.system.exception.RestServerError) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationConflictException(org.entando.entando.web.common.exceptions.ValidationConflictException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException)

Example 17 with ResourceNotFoundException

use of org.entando.entando.aps.system.exception.ResourceNotFoundException in project entando-core by entando.

the class WidgetService method updateWidget.

@Override
public WidgetDto updateWidget(String widgetCode, WidgetRequest widgetRequest) {
    WidgetType type = this.getWidgetManager().getWidgetType(widgetCode);
    if (type == null) {
        throw new ResourceNotFoundException(WidgetValidator.ERRCODE_WIDGET_DOES_NOT_EXISTS, "widget", widgetCode);
    }
    WidgetDto widgetDto = null;
    try {
        if (null == this.getGroupManager().getGroup(widgetRequest.getGroup())) {
            BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(type, "widget");
            bindingResult.reject(WidgetValidator.ERRCODE_WIDGET_GROUP_INVALID, new String[] { widgetRequest.getGroup() }, "widgettype.group.invalid");
            throw new ValidationGenericException(bindingResult);
        }
        if (type.isUserType() && StringUtils.isBlank(widgetRequest.getCustomUi()) && !WidgetType.existsJsp(this.srvCtx, widgetCode, widgetCode)) {
            BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(type, "widget");
            bindingResult.reject(WidgetValidator.ERRCODE_NOT_BLANK, new String[] { type.getCode() }, "widgettype.customUi.notBlank");
            throw new ValidationGenericException(bindingResult);
        }
        this.processWidgetType(type, widgetRequest);
        widgetDto = dtoBuilder.convert(type);
        this.getWidgetManager().updateWidgetType(widgetCode, type.getTitles(), type.getConfig(), type.getMainGroup(), type.getConfigUi(), type.getBundleId());
        if (!StringUtils.isEmpty(widgetCode)) {
            GuiFragment guiFragment = this.getGuiFragmentManager().getUniqueGuiFragmentByWidgetType(widgetCode);
            if (null == guiFragment) {
                this.createAndAddFragment(type, widgetRequest);
            } else {
                guiFragment.setGui(widgetRequest.getCustomUi());
                this.getGuiFragmentManager().updateGuiFragment(guiFragment);
            }
        }
        this.addFragments(widgetDto);
    } catch (ValidationGenericException vge) {
        logger.error("Found an error on validation, throwing original exception", vge);
        throw vge;
    } catch (Throwable e) {
        logger.error("failed to update widget type", e);
        throw new RestServerError("Failed to update widget", e);
    }
    return widgetDto;
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) GuiFragment(org.entando.entando.aps.system.services.guifragment.GuiFragment) RestServerError(org.entando.entando.aps.system.exception.RestServerError) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) WidgetDto(org.entando.entando.aps.system.services.widgettype.model.WidgetDto)

Example 18 with ResourceNotFoundException

use of org.entando.entando.aps.system.exception.ResourceNotFoundException in project entando-core by entando.

the class AbstractPaginationValidator method validateRestListRequest.

public void validateRestListRequest(RestListRequest listRequest, Class<?> type) {
    this.checkDefaultSortField(listRequest);
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(listRequest, "listRequest");
    if (listRequest.getPage() < 1) {
        bindingResult.reject(ERRCODE_PAGE_INVALID, new Object[] {}, "pagination.page.invalid");
        throw new ValidationGenericException(bindingResult);
    }
    if (listRequest.getPageSize() < 0) {
        bindingResult.reject(ERRCODE_PAGE_SIZE_INVALID, new Object[] {}, "pagination.page.size.invalid");
    } else if (listRequest.getPage() > 1 && listRequest.getPageSize() == 0) {
        bindingResult.reject(ERRCODE_NO_ITEM_ON_PAGE, new String[] { String.valueOf(listRequest.getPage()) }, "pagination.item.empty");
        throw new ResourceNotFoundException(bindingResult);
    }
    if (null == type) {
        return;
    }
    if (!isValidField(listRequest.getSort(), type)) {
        bindingResult.reject(ERRCODE_SORTING_INVALID, new Object[] { listRequest.getSort() }, "sorting.sort.invalid");
    }
    if (listRequest.getFilters() != null) {
        List<String> attributes = Arrays.asList(listRequest.getFilters()).stream().filter(filter -> filter.getEntityAttr() == null).map(filter -> filter.getAttributeName()).filter(attr -> !isValidField(attr, type)).collect(Collectors.toList());
        if (attributes.size() > 0) {
            bindingResult.reject(ERRCODE_FILTERING_ATTR_INVALID, new Object[] { attributes.get(0) }, "filtering.filter.attr.name.invalid");
        }
        if (Arrays.stream(FilterOperator.values()).map(FilterOperator::getValue).noneMatch(op -> Arrays.stream(listRequest.getFilters()).map(Filter::getOperator).anyMatch(op::equals))) {
            bindingResult.reject(ERRCODE_FILTERING_OP_INVALID, new Object[] {}, "filtering.filter.operation.invalid");
        }
        Arrays.stream(listRequest.getFilters()).filter(f -> getDateFilterKeys().contains(f.getAttribute())).forEach(f -> {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(SystemConstants.API_DATE_FORMAT);
                sdf.parse(f.getValue());
            } catch (ParseException ex) {
                bindingResult.reject(ERRCODE_FILTERING_OP_INVALID, new Object[] { f.getValue(), f.getAttribute() }, "filtering.filter.date.format.invalid");
            }
        });
    }
    if (!Arrays.asList(directions).contains(listRequest.getDirection())) {
        bindingResult.reject(ERRCODE_DIRECTION_INVALID, new Object[] {}, "sorting.direction.invalid");
    }
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
}
Also used : ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Validator(org.springframework.validation.Validator) SystemConstants(com.agiletec.aps.system.SystemConstants) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) FilterOperator(org.entando.entando.web.common.model.FilterOperator) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) RestListRequest(org.entando.entando.web.common.model.RestListRequest) Filter(org.entando.entando.web.common.model.Filter) PagedMetadata(org.entando.entando.web.common.model.PagedMetadata) Map(java.util.Map) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ParseException(java.text.ParseException) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Filter(org.entando.entando.web.common.model.Filter) ParseException(java.text.ParseException) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 19 with ResourceNotFoundException

use of org.entando.entando.aps.system.exception.ResourceNotFoundException in project entando-core by entando.

the class DataObjectModelController method updateDataObjectModel.

@RestAccessControl(permission = Permission.SUPERUSER)
@RequestMapping(value = "/{dataModelId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<SimpleRestResponse<DataModelDto>> updateDataObjectModel(@PathVariable String dataModelId, @Valid @RequestBody DataObjectModelRequest dataObjectModelRequest, BindingResult bindingResult) throws JsonProcessingException {
    logger.debug("Updating data object model -> {}", dataObjectModelRequest.getModelId());
    // field validations
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
    this.getDataObjectModelValidator().validateBodyName(dataModelId, dataObjectModelRequest, bindingResult);
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
    int result = this.getDataObjectModelValidator().validateBody(dataObjectModelRequest, true, bindingResult);
    if (bindingResult.hasErrors()) {
        if (404 == result) {
            if (1 == bindingResult.getFieldErrorCount("type")) {
                throw new ResourceNotFoundException(DataObjectModelValidator.ERRCODE_PUT_DATAOBJECTTYPE_DOES_NOT_EXIST, "type", dataObjectModelRequest.getType());
            } else {
                throw new ResourceNotFoundException(DataObjectModelValidator.ERRCODE_DATAOBJECTMODEL_ALREADY_EXISTS, "modelId", dataObjectModelRequest.getModelId());
            }
        } else {
            throw new ValidationGenericException(bindingResult);
        }
    }
    DataModelDto dataModelDto = this.getDataObjectModelService().updateDataObjectModel(dataObjectModelRequest);
    logger.debug("Main Response -> {}", dataModelDto);
    return new ResponseEntity<>(new SimpleRestResponse<>(dataModelDto), HttpStatus.OK);
}
Also used : DataModelDto(org.entando.entando.aps.system.services.dataobjectmodel.model.DataModelDto) ResponseEntity(org.springframework.http.ResponseEntity) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) RestAccessControl(org.entando.entando.web.common.annotation.RestAccessControl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with ResourceNotFoundException

use of org.entando.entando.aps.system.exception.ResourceNotFoundException in project entando-core by entando.

the class DataObjectModelController method addDataObjectModel.

@RestAccessControl(permission = Permission.SUPERUSER)
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<SimpleRestResponse<DataModelDto>> addDataObjectModel(@Valid @RequestBody DataObjectModelRequest dataObjectModelRequest, BindingResult bindingResult) throws JsonProcessingException {
    logger.debug("Adding data object model -> {}", dataObjectModelRequest.getModelId());
    // field validations
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
    // business validations
    this.getDataObjectModelValidator().validate(dataObjectModelRequest, bindingResult);
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
    int result = this.getDataObjectModelValidator().validateBody(dataObjectModelRequest, false, bindingResult);
    if (bindingResult.hasErrors()) {
        if (404 == result) {
            throw new ResourceNotFoundException(DataObjectModelValidator.ERRCODE_POST_DATAOBJECTTYPE_DOES_NOT_EXIST, "type", dataObjectModelRequest.getType());
        } else {
            throw new ValidationGenericException(bindingResult);
        }
    }
    DataModelDto dataModelDto = this.getDataObjectModelService().addDataObjectModel(dataObjectModelRequest);
    logger.debug("Main Response -> {}", dataModelDto);
    return new ResponseEntity<>(new SimpleRestResponse<>(dataModelDto), HttpStatus.OK);
}
Also used : DataModelDto(org.entando.entando.aps.system.services.dataobjectmodel.model.DataModelDto) ResponseEntity(org.springframework.http.ResponseEntity) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) RestAccessControl(org.entando.entando.web.common.annotation.RestAccessControl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ResourceNotFoundException (org.entando.entando.aps.system.exception.ResourceNotFoundException)53 RestServerError (org.entando.entando.aps.system.exception.RestServerError)27 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)20 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)16 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)15 IPage (com.agiletec.aps.system.services.page.IPage)9 RestAccessControl (org.entando.entando.web.common.annotation.RestAccessControl)9 Category (com.agiletec.aps.system.services.category.Category)7 PagedMetadata (org.entando.entando.web.common.model.PagedMetadata)7 ResponseEntity (org.springframework.http.ResponseEntity)7 SearcherDaoPaginatedResult (com.agiletec.aps.system.common.model.dao.SearcherDaoPaginatedResult)6 List (java.util.List)6 ArrayList (java.util.ArrayList)5 ValidationConflictException (org.entando.entando.web.common.exceptions.ValidationConflictException)5 PageDto (org.entando.entando.aps.system.services.page.model.PageDto)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 CategoryUtilizer (com.agiletec.aps.system.services.category.CategoryUtilizer)3 Group (com.agiletec.aps.system.services.group.Group)3 Widget (com.agiletec.aps.system.services.page.Widget)3 Role (com.agiletec.aps.system.services.role.Role)3