Search in sources :

Example 26 with ApiException

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

Example 27 with ApiException

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);
    }
}
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 28 with ApiException

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;
}
Also used : ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) Method(java.lang.reflect.Method) Properties(java.util.Properties) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 29 with ApiException

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;
}
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 30 with ApiException

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;
}
Also used : JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) DataObject(org.entando.entando.aps.system.services.dataobject.model.DataObject) UserDetails(com.agiletec.aps.system.services.user.UserDetails) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Aggregations

ApiException (org.entando.entando.aps.system.services.api.model.ApiException)80 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)49 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)23 UserDetails (com.agiletec.aps.system.services.user.UserDetails)13 ApsProperties (com.agiletec.aps.util.ApsProperties)12 IApsEntity (com.agiletec.aps.system.common.entity.model.IApsEntity)11 ApiError (org.entando.entando.aps.system.services.api.model.ApiError)10 ArrayList (java.util.ArrayList)9 GuiFragment (org.entando.entando.aps.system.services.guifragment.GuiFragment)9 WidgetType (org.entando.entando.aps.system.services.widgettype.WidgetType)9 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)7 Content (com.agiletec.plugins.jacms.aps.system.services.content.model.Content)7 JAXBDataObject (org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject)7 DataObject (org.entando.entando.aps.system.services.dataobject.model.DataObject)7 JAXBContent (org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent)7 IEntityTypesConfigurer (com.agiletec.aps.system.common.entity.IEntityTypesConfigurer)6 EntitySearchFilter (com.agiletec.aps.system.common.entity.model.EntitySearchFilter)5 ApiMethod (org.entando.entando.aps.system.services.api.model.ApiMethod)5 IUserProfile (org.entando.entando.aps.system.services.userprofile.model.IUserProfile)5 Properties (java.util.Properties)4