Search in sources :

Example 1 with AbstractApiResponse

use of org.entando.entando.aps.system.services.api.model.AbstractApiResponse in project entando-core by entando.

the class ResponseBuilder method buildApiResponseObject.

private AbstractApiResponse buildApiResponseObject(ApiMethod apiMethod) throws ApiException {
    AbstractApiResponse apiResponse = null;
    try {
        Class responseClass = Class.forName(apiMethod.getResponseClassName());
        apiResponse = (AbstractApiResponse) responseClass.newInstance();
    } catch (Exception e) {
        _logger.error("Error creating instance of response '{}'", apiMethod.getResponseClassName(), e);
    }
    return apiResponse;
}
Also used : AbstractApiResponse(org.entando.entando.aps.system.services.api.model.AbstractApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) BeansException(org.springframework.beans.BeansException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with AbstractApiResponse

use of org.entando.entando.aps.system.services.api.model.AbstractApiResponse 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)

Example 3 with AbstractApiResponse

use of org.entando.entando.aps.system.services.api.model.AbstractApiResponse in project entando-core by entando.

the class SelfRestCaller method extractResponseStatusCode.

protected Response.Status extractResponseStatusCode(Object responseObject) {
    if (responseObject instanceof AbstractApiResponse) {
        Response.Status status = Response.Status.OK;
        AbstractApiResponse mainResponse = (AbstractApiResponse) responseObject;
        if (null != mainResponse.getErrors()) {
            for (int i = 0; i < mainResponse.getErrors().size(); i++) {
                ApiError error = mainResponse.getErrors().get(i);
                Response.Status errorStatus = error.getStatus();
                if (null != errorStatus && status.getStatusCode() < errorStatus.getStatusCode()) {
                    status = errorStatus;
                }
            }
        }
        return status;
    } else {
        return Response.Status.OK;
    }
}
Also used : StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) Response(javax.ws.rs.core.Response) AbstractApiResponse(org.entando.entando.aps.system.services.api.model.AbstractApiResponse) AbstractApiResponse(org.entando.entando.aps.system.services.api.model.AbstractApiResponse) ApiError(org.entando.entando.aps.system.services.api.model.ApiError)

Aggregations

AbstractApiResponse (org.entando.entando.aps.system.services.api.model.AbstractApiResponse)3 ApiError (org.entando.entando.aps.system.services.api.model.ApiError)2 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)2 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)2 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Response (javax.ws.rs.core.Response)1 ApiMethodResult (org.entando.entando.aps.system.services.api.model.ApiMethodResult)1 BeansException (org.springframework.beans.BeansException)1