use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ResponseBuilder method checkParameter.
private void checkParameter(ApiMethod apiMethod, Properties parameters) throws ApiException, Throwable {
try {
List<ApiMethodParameter> apiParameters = apiMethod.getParameters();
if (null == apiParameters || apiParameters.isEmpty()) {
return;
}
List<ApiError> errors = new ArrayList<ApiError>();
for (int i = 0; i < apiParameters.size(); i++) {
ApiMethodParameter apiParam = apiParameters.get(i);
String paramName = apiParam.getKey();
Object value = parameters.get(paramName);
if (apiParam.isRequired() && (null == value || value.toString().trim().length() == 0)) {
errors.add(new ApiError(IApiErrorCodes.API_PARAMETER_REQUIRED, "Parameter '" + paramName + "' is required", Response.Status.BAD_REQUEST));
}
}
if (!errors.isEmpty()) {
throw new ApiException(errors);
}
} catch (ApiException t) {
throw t;
} catch (Throwable t) {
_logger.error("Error checking api parameters", t);
throw new ApsSystemException("Internal Error", t);
}
}
use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ResponseBuilder method createResponse.
@Override
public Object createResponse(ApiMethod method, Object bodyObject, Properties parameters) throws ApsSystemException {
AbstractApiResponse response = null;
try {
this.checkParameter(method, parameters);
Object bean = this.extractBean(method);
Object masterResult = null;
if (method.getHttpMethod().equals(ApiMethod.HttpMethod.GET)) {
masterResult = this.invokeGetMethod(method, bean, null, parameters, true);
if (null == masterResult) {
ApiError error = new ApiError(IApiErrorCodes.API_INVALID_RESPONSE, "Invalid or null Response", Response.Status.SERVICE_UNAVAILABLE);
throw new ApiException(error);
}
} else {
masterResult = this.invokePutPostDeleteMethod(method, bean, parameters, bodyObject);
}
if (null == method.getResponseClassName()) {
return masterResult;
}
response = this.buildApiResponseObject(method);
if (null == response && (masterResult instanceof String)) {
return masterResult;
}
String htmlResult = this.extractHtmlResult(masterResult, response, method, parameters, bean);
if (masterResult instanceof ApiMethodResult) {
response.addErrors(((ApiMethodResult) masterResult).getErrors());
response.setResult(((ApiMethodResult) masterResult).getResult(), htmlResult);
} else {
response.setResult(masterResult, htmlResult);
}
} catch (ApiException e) {
if (response == null) {
response = new StringApiResponse();
}
response.addErrors(e.getErrors());
response.setResult(FAILURE, null);
} catch (Throwable t) {
_logger.error("Error creating response - {}", this.buildApiSignature(method), t);
String message = "Error creating response - " + this.buildApiSignature(method);
if (response == null) {
response = new StringApiResponse();
}
ApiError error = new ApiError(IApiErrorCodes.API_METHOD_ERROR, message, Response.Status.INTERNAL_SERVER_ERROR);
response.addError(error);
response.setResult(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 validate.
private List<ApiError> validate(DataObject dataObject) throws ApsSystemException {
List<ApiError> errors = new ArrayList<ApiError>();
try {
if (null == dataObject.getMainGroup()) {
errors.add(new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, "Main group null", Response.Status.CONFLICT));
}
List<FieldError> fieldErrors = dataObject.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 DataObject", t);
throw new ApsSystemException("Error validating DataObject", t);
}
return errors;
}
use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ApiDataObjectTypeInterface method checkDataObjectModel.
private boolean checkDataObjectModel(Integer modelId, DataObject dataObjectType, StringApiResponse response) {
if (null == modelId) {
return true;
}
DataObjectModel contentModel = this.getDataObjectModelManager().getDataObjectModel(modelId);
if (null == contentModel) {
ApiError error = new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, "DataObject model with id '" + modelId + "' does not exist", Response.Status.ACCEPTED);
response.addError(error);
return false;
}
if (!dataObjectType.getTypeCode().equals(contentModel.getDataType())) {
ApiError error = new ApiError(IApiErrorCodes.API_VALIDATION_ERROR, "DataObject model with id '" + modelId + "' is for DataObjects of type '" + contentModel.getDataType() + "'", Response.Status.ACCEPTED);
response.addError(error);
return false;
}
return true;
}
use of org.entando.entando.aps.system.services.api.model.ApiError in project entando-core by entando.
the class ApiDataObjectModelInterface method getModels.
public StringListApiResponse getModels(Properties properties) throws ApiException, Throwable {
StringListApiResponse response = new StringListApiResponse();
try {
List<DataObjectModel> models = null;
String dataTypeParam = properties.getProperty("dataType");
String dataType = (null != dataTypeParam && dataTypeParam.trim().length() > 0) ? dataTypeParam.trim() : null;
if (null != dataType && null == this.getDataObjectManager().getSmallDataTypesMap().get(dataType)) {
ApiError error = new ApiError(IApiErrorCodes.API_PARAMETER_VALIDATION_ERROR, "Content Type " + dataType + " does not exist", Response.Status.CONFLICT);
response.addError(error);
dataType = null;
}
if (null != dataType) {
models = this.getDataObjectModelManager().getModelsForDataObjectType(dataType);
} else {
models = this.getDataObjectModelManager().getDataObjectModels();
}
List<String> list = new ArrayList<String>();
if (null != models) {
for (int i = 0; i < models.size(); i++) {
DataObjectModel model = models.get(i);
list.add(String.valueOf(model.getId()));
}
}
response.setResult(list, null);
} catch (Throwable t) {
_logger.error("Error loading models", t);
throw new ApsSystemException("Error loading models", t);
}
return response;
}
Aggregations