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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations