Search in sources :

Example 1 with ApiMethodResult

use of org.entando.entando.aps.system.services.api.model.ApiMethodResult 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;
}
Also used : ApiMethodResult(org.entando.entando.aps.system.services.api.model.ApiMethodResult) AbstractApiResponse(org.entando.entando.aps.system.services.api.model.AbstractApiResponse) ApiError(org.entando.entando.aps.system.services.api.model.ApiError) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Aggregations

AbstractApiResponse (org.entando.entando.aps.system.services.api.model.AbstractApiResponse)1 ApiError (org.entando.entando.aps.system.services.api.model.ApiError)1 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)1 ApiMethodResult (org.entando.entando.aps.system.services.api.model.ApiMethodResult)1 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)1