Search in sources :

Example 6 with ApiError

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);
    }
}
Also used : ArrayList(java.util.ArrayList) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError) ApiMethodParameter(org.entando.entando.aps.system.services.api.model.ApiMethodParameter) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 7 with ApiError

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;
}
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 8 with ApiError

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;
}
Also used : ArrayList(java.util.ArrayList) AttributeFieldError(com.agiletec.aps.system.common.entity.model.AttributeFieldError) FieldError(com.agiletec.aps.system.common.entity.model.FieldError) AttributeFieldError(com.agiletec.aps.system.common.entity.model.AttributeFieldError) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError)

Example 9 with ApiError

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;
}
Also used : DataObjectModel(org.entando.entando.aps.system.services.dataobjectmodel.DataObjectModel) ApiError(org.entando.entando.aps.system.services.api.model.ApiError)

Example 10 with ApiError

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;
}
Also used : StringListApiResponse(org.entando.entando.aps.system.services.api.model.StringListApiResponse) ArrayList(java.util.ArrayList) DataObjectModel(org.entando.entando.aps.system.services.dataobjectmodel.DataObjectModel) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiError(org.entando.entando.aps.system.services.api.model.ApiError)

Aggregations

ApiError (org.entando.entando.aps.system.services.api.model.ApiError)21 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)13 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)10 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)9 ArrayList (java.util.ArrayList)8 UserDetails (com.agiletec.aps.system.services.user.UserDetails)4 AttributeFieldError (com.agiletec.aps.system.common.entity.model.AttributeFieldError)3 FieldError (com.agiletec.aps.system.common.entity.model.FieldError)3 IApsEntity (com.agiletec.aps.system.common.entity.model.IApsEntity)3 ContentRecordVO (com.agiletec.plugins.jacms.aps.system.services.content.model.ContentRecordVO)2 ContentModel (com.agiletec.plugins.jacms.aps.system.services.contentmodel.ContentModel)2 AbstractApiResponse (org.entando.entando.aps.system.services.api.model.AbstractApiResponse)2 StringListApiResponse (org.entando.entando.aps.system.services.api.model.StringListApiResponse)2 DataObjectModel (org.entando.entando.aps.system.services.dataobjectmodel.DataObjectModel)2 IUserProfile (org.entando.entando.aps.system.services.userprofile.model.IUserProfile)2 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)1 DefaultJAXBAttributeType (com.agiletec.aps.system.common.entity.model.attribute.DefaultJAXBAttributeType)1 ContentUtilizer (com.agiletec.plugins.jacms.aps.system.services.content.ContentUtilizer)1 Content (com.agiletec.plugins.jacms.aps.system.services.content.model.Content)1 ResourceUtilizer (com.agiletec.plugins.jacms.aps.system.services.resource.ResourceUtilizer)1