use of org.entando.entando.aps.system.services.api.model.ApiException in project entando-core by entando.
the class ResponseBuilder method extractApiMethod.
@Override
public ApiMethod extractApiMethod(ApiMethod.HttpMethod httpMethod, String namespace, String resourceName) throws ApiException {
ApiMethod api = null;
String signature = this.buildApiSignature(httpMethod, namespace, resourceName);
try {
api = this.getApiCatalogManager().getMethod(httpMethod, namespace, resourceName);
if (null == api) {
ApiError error = new ApiError(IApiErrorCodes.API_INVALID, signature + " does not exists", Response.Status.NOT_FOUND);
throw new ApiException(error);
}
if (!api.isActive()) {
ApiError error = new ApiError(IApiErrorCodes.API_INVALID, signature + " does not exists", Response.Status.NOT_FOUND);
throw new ApiException(error);
}
} catch (ApiException ae) {
_logger.error("Error extracting api method {}", this.buildApiSignature(httpMethod, namespace, resourceName), ae);
throw ae;
} catch (Throwable t) {
_logger.error("Error extracting api method {}", this.buildApiSignature(httpMethod, namespace, resourceName), t);
throw new ApiException(IApiErrorCodes.SERVER_ERROR, signature + " is not supported", Response.Status.INTERNAL_SERVER_ERROR);
}
return api;
}
use of org.entando.entando.aps.system.services.api.model.ApiException 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.ApiException in project entando-core by entando.
the class ResponseBuilder method invokeGetMethod.
protected Object invokeGetMethod(ApiMethod apiMethod, Object bean, String methodSuffix, Properties parameters, boolean throwException) throws ApiException, Throwable {
String methodName = null;
Object result = null;
try {
Class[] parameterTypes = new Class[] { Properties.class };
Class beanClass = bean.getClass();
methodName = (null != methodSuffix) ? apiMethod.getSpringBeanMethod() + methodSuffix.trim() : apiMethod.getSpringBeanMethod();
Method method = beanClass.getDeclaredMethod(methodName, parameterTypes);
result = method.invoke(bean, parameters);
} catch (NoSuchMethodException e) {
if (throwException) {
_logger.error("No such method '{}' of class '{}'", methodName, bean.getClass(), e);
throw new ApiException(IApiErrorCodes.API_METHOD_ERROR, "Method not supported - " + this.buildApiSignature(apiMethod), Response.Status.INTERNAL_SERVER_ERROR);
}
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof ApiException) {
throw (ApiException) e.getTargetException();
} else if (throwException) {
_logger.error("Error invoking method '{}' of class '{}'", methodName, bean.getClass());
throw new ApiException(IApiErrorCodes.API_METHOD_ERROR, "Error invoking Method - " + this.buildApiSignature(apiMethod), Response.Status.INTERNAL_SERVER_ERROR);
}
} catch (Throwable t) {
if (throwException) {
_logger.error("Error invoking method - {} {} of class '{}'", this.buildApiSignature(apiMethod), methodName, bean.getClass(), t);
throw t;
}
}
return result;
}
use of org.entando.entando.aps.system.services.api.model.ApiException 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.ApiException in project entando-core by entando.
the class ApiDataObjectInterface method addDataObject.
public StringApiResponse addDataObject(JAXBDataObject jaxbDataObject, Properties properties) throws Throwable {
StringApiResponse response = new StringApiResponse();
try {
String typeCode = jaxbDataObject.getTypeCode();
DataObject prototype = (DataObject) this.getDataObjectManager().getEntityPrototype(typeCode);
if (null == prototype) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "DataObject type with code '" + typeCode + "' does not exist", Response.Status.CONFLICT);
}
DataObject dataObject = (DataObject) jaxbDataObject.buildEntity(prototype, this.getCategoryManager());
if (null != dataObject.getId()) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "You cannot specify DataObject Id", Response.Status.CONFLICT);
}
UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
dataObject.setFirstEditor((null != user) ? user.getUsername() : SystemConstants.GUEST_USER_NAME);
response = this.validateAndSaveDataObject(dataObject, properties);
} catch (ApiException ae) {
response.addErrors(ae.getErrors());
response.setResult(IResponseBuilder.FAILURE, null);
} catch (Throwable t) {
_logger.error("Error adding DataObject", t);
throw new ApsSystemException("Error adding DataObject", t);
}
return response;
}
Aggregations