use of org.entando.entando.aps.system.services.api.model.StringApiResponse 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.StringApiResponse 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;
}
use of org.entando.entando.aps.system.services.api.model.StringApiResponse in project entando-core by entando.
the class ApiDataObjectInterface method updateDataObject.
public StringApiResponse updateDataObject(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 content = (DataObject) jaxbDataObject.buildEntity(prototype, this.getCategoryManager());
DataObject masterData = this.getDataObjectManager().loadDataObject(content.getId(), false);
if (null == masterData) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "DataObject with code '" + content.getId() + "' does not exist", Response.Status.CONFLICT);
} else if (!masterData.getMainGroup().equals(content.getMainGroup())) {
throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid main group " + content.getMainGroup() + " not equals then master " + masterData.getMainGroup(), Response.Status.CONFLICT);
}
response = this.validateAndSaveDataObject(content, properties);
} catch (ApiException ae) {
response.addErrors(ae.getErrors());
response.setResult(IResponseBuilder.FAILURE, null);
} catch (Throwable t) {
_logger.error("Error updating DataObject", t);
throw new ApsSystemException("Error updating DataObject", t);
}
return response;
}
use of org.entando.entando.aps.system.services.api.model.StringApiResponse 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.StringApiResponse in project entando-core by entando.
the class ResponseBuilder method createResponse.
@Override
@Deprecated
public Object createResponse(String resourceName, Properties parameters) throws ApsSystemException {
Object apiResponse = null;
try {
ApiMethod method = this.extractApiMethod(ApiMethod.HttpMethod.GET, null, resourceName);
apiResponse = this.createResponse(method, parameters);
} catch (ApiException e) {
_logger.error("Error creating response for method GET, resource '{}'", resourceName, e);
if (apiResponse == null) {
apiResponse = new StringApiResponse();
}
((AbstractApiResponse) apiResponse).addErrors(e.getErrors());
}
return apiResponse;
}
Aggregations