Search in sources :

Example 11 with StringApiResponse

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;
}
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 12 with StringApiResponse

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;
}
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)

Example 13 with StringApiResponse

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;
}
Also used : JAXBDataObject(org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject) DataObject(org.entando.entando.aps.system.services.dataobject.model.DataObject) 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)

Example 14 with StringApiResponse

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

Example 15 with StringApiResponse

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

Aggregations

StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)28 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)23 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)20 UserDetails (com.agiletec.aps.system.services.user.UserDetails)9 ApiError (org.entando.entando.aps.system.services.api.model.ApiError)8 IApsEntity (com.agiletec.aps.system.common.entity.model.IApsEntity)6 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)5 IEntityTypesConfigurer (com.agiletec.aps.system.common.entity.IEntityTypesConfigurer)4 Content (com.agiletec.plugins.jacms.aps.system.services.content.model.Content)4 ApiMethod (org.entando.entando.aps.system.services.api.model.ApiMethod)4 JAXBContent (org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent)4 JAXBDataObject (org.entando.entando.aps.system.services.dataobject.api.model.JAXBDataObject)3 DataObject (org.entando.entando.aps.system.services.dataobject.model.DataObject)3 IUserProfile (org.entando.entando.aps.system.services.userprofile.model.IUserProfile)3 ContentRecordVO (com.agiletec.plugins.jacms.aps.system.services.content.model.ContentRecordVO)2 BaseResourceDataBean (com.agiletec.plugins.jacms.aps.system.services.resource.model.BaseResourceDataBean)2 ResourceInterface (com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface)2 Properties (java.util.Properties)2 ApiResource (org.entando.entando.aps.system.services.api.model.ApiResource)2 IEntityManager (com.agiletec.aps.system.common.entity.IEntityManager)1