use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ApiRestStatusServer method buildErrorResponse.
private StringApiResponse buildErrorResponse(ApiMethod.HttpMethod httpMethod, String namespace, String resourceName, Throwable t) {
StringBuilder buffer = new StringBuilder();
buffer.append("Method '").append(httpMethod).append("' Namespace '").append(namespace).append("' Resource '").append(resourceName).append("'");
_logger.error("Error building api response - {}", buffer.toString(), t);
StringApiResponse response = new StringApiResponse();
ApiError error = new ApiError(IApiErrorCodes.SERVER_ERROR, "Error building response - " + buffer.toString(), Response.Status.INTERNAL_SERVER_ERROR);
response.addError(error);
response.setResult(IResponseBuilder.FAILURE, null);
return response;
}
use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ApiDataObjectInterface method validateAndSaveDataObject.
protected StringApiResponse validateAndSaveDataObject(DataObject dataObject, Properties properties) throws ApiException, Throwable {
StringApiResponse response = new StringApiResponse();
try {
UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
if (null == user) {
user = this.getUserManager().getGuestUser();
}
if (!this.getDataObjectAuthorizationHelper().isAuth(user, dataObject)) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "DataObject groups makes the new content not allowed for user " + user.getUsername(), Response.Status.FORBIDDEN);
}
List<ApiError> errors = this.validate(dataObject);
if (errors.size() > 0) {
response.addErrors(errors);
response.setResult(IResponseBuilder.FAILURE, null);
return response;
}
String insertString = properties.getProperty("insert");
boolean insert = (null != insertString) ? Boolean.parseBoolean(insertString) : false;
if (!insert) {
this.getDataObjectManager().saveDataObject(dataObject);
} else {
this.getDataObjectManager().insertDataObject(dataObject);
}
response.setResult(IResponseBuilder.SUCCESS, null);
} catch (ApiException ae) {
response.addErrors(ae.getErrors());
response.setResult(IResponseBuilder.FAILURE, null);
} catch (Throwable t) {
_logger.error("error in validateAndSaveDataObject", t);
throw new ApsSystemException("Error adding dataObject", t);
}
return response;
}
use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ApiContentInterface method validateAndSaveContent.
protected StringApiResponse validateAndSaveContent(Content content, Properties properties) throws ApiException, Throwable {
StringApiResponse response = new StringApiResponse();
try {
UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
if (null == user) {
user = this.getUserManager().getGuestUser();
}
if (!this.getContentAuthorizationHelper().isAuth(user, content)) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Content groups makes the new content not allowed for user " + user.getUsername(), Response.Status.FORBIDDEN);
}
List<ApiError> errors = this.validate(content);
if (errors.size() > 0) {
response.addErrors(errors);
response.setResult(IResponseBuilder.FAILURE, null);
return response;
}
String insertOnLineString = properties.getProperty("insertOnLine");
boolean insertOnLine = (null != insertOnLineString) ? Boolean.parseBoolean(insertOnLineString) : false;
if (!insertOnLine) {
this.getContentManager().saveContent(content);
} else {
this.getContentManager().insertOnLineContent(content);
}
response.setResult(IResponseBuilder.SUCCESS, null);
} catch (ApiException ae) {
response.addErrors(ae.getErrors());
response.setResult(IResponseBuilder.FAILURE, null);
} catch (Throwable t) {
_logger.error("error in validateAndSaveContent", t);
throw new ApsSystemException("Error adding content", t);
}
return response;
}
use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ApiContentInterface method validate.
private List<ApiError> validate(Content content) throws ApsSystemException {
List<ApiError> errors = new ArrayList<ApiError>();
try {
if (null == content.getMainGroup()) {
errors.add(new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, "Main group null", Response.Status.CONFLICT));
}
List<FieldError> fieldErrors = content.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 content", t);
throw new ApsSystemException("Error validating content", t);
}
return errors;
}
use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ApiContentInterface method deleteContent.
public StringApiResponse deleteContent(Properties properties) throws Throwable {
StringApiResponse response = new StringApiResponse();
try {
String id = properties.getProperty("id");
Content masterContent = this.getContentManager().loadContent(id, false);
if (null == masterContent) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Content 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.getContentAuthorizationHelper().isAuth(user, masterContent)) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Content groups makes the new content not allowed for user " + user.getUsername(), Response.Status.FORBIDDEN);
}
List<String> references = ((ContentUtilizer) this.getContentManager()).getContentUtilizers(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, "Content " + id + " referenced to content " + record.getId() + " - '" + record.getDescr() + "'", Response.Status.CONFLICT));
}
}
if (found) {
response.setResult(IResponseBuilder.FAILURE, null);
return response;
}
}
if (masterContent.isOnLine()) {
this.getContentManager().removeOnLineContent(masterContent);
}
String removeWorkVersionString = properties.getProperty("removeWorkVersion");
boolean removeWorkVersion = (null != removeWorkVersionString) ? Boolean.parseBoolean(removeWorkVersionString) : false;
if (removeWorkVersion) {
this.getContentManager().deleteContent(masterContent);
}
response.setResult(IResponseBuilder.SUCCESS, null);
} catch (ApiException ae) {
response.addErrors(ae.getErrors());
response.setResult(IResponseBuilder.FAILURE, null);
} catch (Throwable t) {
_logger.error("Error deleting content", t);
throw new ApsSystemException("Error deleting content", t);
}
return response;
}
Aggregations