Search in sources :

Example 16 with ApiError

use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.

the class ApiContentModelInterface method getModels.

public StringListApiResponse getModels(Properties properties) throws ApiException, Throwable {
    StringListApiResponse response = new StringListApiResponse();
    try {
        List<ContentModel> models = null;
        String contentTypeParam = properties.getProperty("contentType");
        String contentType = (null != contentTypeParam && contentTypeParam.trim().length() > 0) ? contentTypeParam.trim() : null;
        if (null != contentType && null == this.getContentManager().getSmallContentTypesMap().get(contentType)) {
            ApiError error = new ApiError(IApiErrorCodes.API_PARAMETER_VALIDATION_ERROR, "Content Type " + contentType + " does not exist", Response.Status.CONFLICT);
            response.addError(error);
            contentType = null;
        }
        if (null != contentType) {
            models = this.getContentModelManager().getModelsForContentType(contentType);
        } else {
            models = this.getContentModelManager().getContentModels();
        }
        List<String> list = new ArrayList<String>();
        if (null != models) {
            for (int i = 0; i < models.size(); i++) {
                ContentModel model = models.get(i);
                list.add(String.valueOf(model.getId()));
            }
        }
        response.setResult(list, null);
    } catch (Throwable t) {
        _logger.error("Error loading models", t);
        // ApsSystemUtils.logThrowable(t, this, "getModels");
        throw new ApsSystemException("Error loading models", t);
    }
    return response;
}
Also used : ContentModel(com.agiletec.plugins.jacms.aps.system.services.contentmodel.ContentModel) StringListApiResponse(org.entando.entando.aps.system.services.api.model.StringListApiResponse) ArrayList(java.util.ArrayList) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError)

Example 17 with ApiError

use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.

the class ApiResourceInterface method deleteResource.

private StringApiResponse deleteResource(Properties properties, ResourceInterface resource) throws ApiException, Throwable {
    StringApiResponse response = new StringApiResponse();
    try {
        String id = properties.getProperty("id");
        if (null == resource) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Resource with code '" + id + "' does not exist", Response.Status.CONFLICT);
        }
        UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
        if (null == user) {
            user = this.getUserManager().getGuestUser();
        }
        if (!this.getAuthorizationManager().isAuthOnGroup(user, resource.getMainGroup())) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Resource not allowed for user '" + user.getUsername() + "' - resource group '" + resource.getMainGroup() + "'", Response.Status.FORBIDDEN);
        }
        List<String> references = ((ResourceUtilizer) this.getContentManager()).getResourceUtilizers(id);
        if (references != null && references.size() > 0) {
            boolean found = false;
            for (int i = 0; i < references.size(); i++) {
                String reference = references.get(i);
                ContentRecordVO record = this.getContentManager().loadContentVO(reference);
                if (null != record) {
                    found = true;
                    response.addError(new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, "Resource " + id + " referenced to content " + record.getId() + " - '" + record.getDescr() + "'", Response.Status.CONFLICT));
                }
            }
            if (found) {
                response.setResult(IResponseBuilder.FAILURE, null);
                return response;
            }
        }
        this.getResourceManager().deleteResource(resource);
        response.setResult(IResponseBuilder.SUCCESS, null);
    } catch (ApiException ae) {
        response.addErrors(ae.getErrors());
        response.setResult(IResponseBuilder.FAILURE, null);
    } catch (Throwable t) {
        _logger.error("Error deleting resource", t);
        // ApsSystemUtils.logThrowable(t, this, "deleteResource");
        throw new ApsSystemException("Error deleting resource", t);
    }
    return response;
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ContentRecordVO(com.agiletec.plugins.jacms.aps.system.services.content.model.ContentRecordVO) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException) ResourceUtilizer(com.agiletec.plugins.jacms.aps.system.services.resource.ResourceUtilizer)

Example 18 with ApiError

use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.

the class ApiResourceInterface method addValidationError.

/*
	private boolean isDuplicateFile(JAXBResource jaxbResource, ResourceInterface resourcePrototype, boolean add) {
		if (null == jaxbResource.getBase64()) return false;
		boolean addError = true;
		String formFileName = jaxbResource.getFileName();
		try {
			resourcePrototype.setMainGroup(jaxbResource.getMainGroup());
			if (resourcePrototype.exists(formFileName)) {
				if (!add) {
					ResourceInterface masterResource = this.getResourceManager().loadResource(jaxbResource.getId());
					String masterFileName = (null != masterResource) ? masterResource.getMasterFileName() : null;
					if (null != masterFileName && masterFileName.equalsIgnoreCase(formFileName)) {
						addError = false;
					}
				}
			} else {
				addError = false;
			}
		} catch (Throwable t) {
			_logger.error("Error while check duplicate file - master file name '{}'", formFileName, t);
		}
		return addError;
	}
	*/
private void addValidationError(String message, StringApiResponse response) {
    ApiError error = new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, message, Response.Status.FORBIDDEN);
    response.addError(error);
}
Also used : ApiError(org.entando.entando.aps.system.services.api.model.ApiError)

Example 19 with ApiError

use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.

the class ApiUserProfileInterface method validate.

private List<ApiError> validate(IUserProfile userProfile) throws ApsSystemException {
    List<ApiError> errors = new ArrayList<ApiError>();
    try {
        List<FieldError> fieldErrors = userProfile.validate(this.getGroupManager());
        if (null != fieldErrors) {
            for (int i = 0; i < fieldErrors.size(); i++) {
                FieldError fieldError = fieldErrors.get(i);
                if (fieldError instanceof AttributeFieldError) {
                    AttributeFieldError attributeError = (AttributeFieldError) fieldError;
                    errors.add(new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, attributeError.getFullMessage(), Response.Status.CONFLICT));
                } else {
                    errors.add(new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, fieldError.getMessage(), Response.Status.CONFLICT));
                }
            }
        }
    } catch (Throwable t) {
        _logger.error("Error validating profile", t);
        // ApsSystemUtils.logThrowable(t, this, "validate");
        throw new ApsSystemException("Error validating profile", t);
    }
    return errors;
}
Also used : ArrayList(java.util.ArrayList) AttributeFieldError(com.agiletec.aps.system.common.entity.model.AttributeFieldError) FieldError(com.agiletec.aps.system.common.entity.model.FieldError) AttributeFieldError(com.agiletec.aps.system.common.entity.model.AttributeFieldError) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError)

Example 20 with ApiError

use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.

the class ApiUserProfileInterface method updateUserProfile.

public StringApiResponse updateUserProfile(JAXBUserProfile jaxbUserProfile) throws Throwable {
    StringApiResponse response = new StringApiResponse();
    try {
        String username = jaxbUserProfile.getId();
        if (null == this.getUserProfileManager().getProfile(username)) {
            throw new ApiException(IApiErrorCodes.API_PARAMETER_VALIDATION_ERROR, "Profile of user '" + username + "' does not exist", Response.Status.CONFLICT);
        }
        IApsEntity profilePrototype = this.getUserProfileManager().getEntityPrototype(jaxbUserProfile.getTypeCode());
        if (null == profilePrototype) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "User Profile type with code '" + jaxbUserProfile.getTypeCode() + "' does not exist", Response.Status.CONFLICT);
        }
        IUserProfile userProfile = (IUserProfile) jaxbUserProfile.buildEntity(profilePrototype, null);
        List<ApiError> errors = this.validate(userProfile);
        if (errors.size() > 0) {
            response.addErrors(errors);
            response.setResult(IResponseBuilder.FAILURE, null);
            return response;
        }
        this.getUserProfileManager().updateProfile(username, userProfile);
        response.setResult(IResponseBuilder.SUCCESS, null);
    } catch (ApiException ae) {
        response.addErrors(ae.getErrors());
        response.setResult(IResponseBuilder.FAILURE, null);
    } catch (Throwable t) {
        _logger.error("Error updating user profile", t);
        // ApsSystemUtils.logThrowable(t, this, "updateUserProfile");
        throw new ApsSystemException("Error updating user profile", t);
    }
    return response;
}
Also used : IUserProfile(org.entando.entando.aps.system.services.userprofile.model.IUserProfile) IApsEntity(com.agiletec.aps.system.common.entity.model.IApsEntity) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Aggregations

ApiError (org.entando.entando.aps.system.services.api.model.ApiError)21 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)13 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)10 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)9 ArrayList (java.util.ArrayList)8 UserDetails (com.agiletec.aps.system.services.user.UserDetails)4 AttributeFieldError (com.agiletec.aps.system.common.entity.model.AttributeFieldError)3 FieldError (com.agiletec.aps.system.common.entity.model.FieldError)3 IApsEntity (com.agiletec.aps.system.common.entity.model.IApsEntity)3 ContentRecordVO (com.agiletec.plugins.jacms.aps.system.services.content.model.ContentRecordVO)2 ContentModel (com.agiletec.plugins.jacms.aps.system.services.contentmodel.ContentModel)2 AbstractApiResponse (org.entando.entando.aps.system.services.api.model.AbstractApiResponse)2 StringListApiResponse (org.entando.entando.aps.system.services.api.model.StringListApiResponse)2 DataObjectModel (org.entando.entando.aps.system.services.dataobjectmodel.DataObjectModel)2 IUserProfile (org.entando.entando.aps.system.services.userprofile.model.IUserProfile)2 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)1 DefaultJAXBAttributeType (com.agiletec.aps.system.common.entity.model.attribute.DefaultJAXBAttributeType)1 ContentUtilizer (com.agiletec.plugins.jacms.aps.system.services.content.ContentUtilizer)1 Content (com.agiletec.plugins.jacms.aps.system.services.content.model.Content)1 ResourceUtilizer (com.agiletec.plugins.jacms.aps.system.services.resource.ResourceUtilizer)1