Search in sources :

Example 1 with InvalidPostProcessResultException

use of org.entando.entando.aps.system.init.model.InvalidPostProcessResultException in project entando-core by entando.

the class SelfRestCaller method printResponse.

private void printResponse(SelfRestCallPostProcess selfRestCall, Object result, Response.Status responseStatus, ApiMethod method, Properties properties) throws InvalidPostProcessResultException, Throwable {
    String responseClassName = method.getResponseClassName();
    Class responseClass = (null != responseClassName) ? Class.forName(responseClassName) : StringApiResponse.class;
    JAXBContext context = JAXBContext.newInstance(responseClass);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    boolean validResponse = (responseStatus.getStatusCode() == selfRestCall.getExpectedResult());
    StringBuilder path = new StringBuilder();
    path.append("/api/rs/");
    path.append(properties.get(SystemConstants.API_LANG_CODE_PARAMETER)).append("/");
    if (null != method.getNamespace()) {
        path.append(method.getNamespace()).append("/");
    }
    path.append(method.getResourceName());
    StringBuilder log = new StringBuilder();
    StringWriter writer = new StringWriter();
    try {
        marshaller.marshal(result, writer);
    } catch (Exception e) {
        _logger.info("Error extracting body response : " + e.getMessage());
        writer.append(".......");
    }
    log.append("*************** Self Rest Call - response ***************\n");
    log.append(method.getHttpMethod().toString()).append(" ");
    log.append(path.toString()).append("\n");
    log.append("Result   ").append(responseStatus.getStatusCode()).append("\n");
    log.append("Expected ").append(selfRestCall.getExpectedResult()).append("\n");
    if (!validResponse) {
        log.append("******** INVALID RESPONSE STATUS ********\n");
    }
    log.append("---------------------------------------------------------\n");
    log.append(writer.toString()).append("\n");
    log.append("*********************************************************\n");
    if (!validResponse) {
        if (!selfRestCall.isFailOnError()) {
            log.append("failonerror was set to false: continuing anyway.\n");
        } else {
            log.append("the post processes will be stopped.\n");
        }
    }
    _logger.info(log.toString());
    System.out.println(log.toString());
    if (!validResponse && selfRestCall.isFailOnError()) {
        throw new InvalidPostProcessResultException(responseStatus.getStatusCode(), selfRestCall.getExpectedResult(), path.toString(), method.getHttpMethod());
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) InvalidPostProcessResultException(org.entando.entando.aps.system.init.model.InvalidPostProcessResultException) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) BeansException(org.springframework.beans.BeansException) InvalidPostProcessResultException(org.entando.entando.aps.system.init.model.InvalidPostProcessResultException)

Example 2 with InvalidPostProcessResultException

use of org.entando.entando.aps.system.init.model.InvalidPostProcessResultException in project entando-core by entando.

the class SelfRestCaller method executePostProcess.

@Override
public int executePostProcess(IPostProcess postProcess) throws InvalidPostProcessResultException, ApsSystemException {
    if (!(postProcess instanceof SelfRestCallPostProcess)) {
        return 0;
    }
    SelfRestCallPostProcess selfRestCall = (SelfRestCallPostProcess) postProcess;
    IResponseBuilder responseBuilder = this.getResponseBuilder();
    try {
        Object result = null;
        ApiMethod method = responseBuilder.extractApiMethod(selfRestCall.getMethod(), selfRestCall.getNamespace(), selfRestCall.getResourceName());
        Properties properties = this.extractParameters(selfRestCall);
        if (method.getHttpMethod().equals(ApiMethod.HttpMethod.GET) || method.getHttpMethod().equals(ApiMethod.HttpMethod.DELETE)) {
            result = responseBuilder.createResponse(method, properties);
        } else {
            String contentBody = this.getContentBody(selfRestCall);
            Object bodyObject = UnmarshalUtils.unmarshal(method, contentBody, selfRestCall.getContentType());
            result = responseBuilder.createResponse(method, bodyObject, properties);
        }
        Response.Status responseStatus = this.extractResponseStatusCode(result);
        if (selfRestCall.isPrintResponse()) {
            this.printResponse(selfRestCall, result, responseStatus, method, properties);
        }
    } catch (InvalidPostProcessResultException t) {
        _logger.error("error in executePostProcess", t);
        // ApsSystemUtils.logThrowable(t, this, "executePostProcess", t.getMessage());
        throw t;
    } catch (Throwable t) {
        _logger.error("Error invoking api method", t);
        // ApsSystemUtils.logThrowable(t, this, "executePostProcess", "Error invoking api method");
        throw new ApsSystemException("Error invoking api method", t);
    }
    return 1;
}
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) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) InvalidPostProcessResultException(org.entando.entando.aps.system.init.model.InvalidPostProcessResultException) SelfRestCallPostProcess(org.entando.entando.aps.system.init.model.SelfRestCallPostProcess) Properties(java.util.Properties) IResponseBuilder(org.entando.entando.aps.system.services.api.server.IResponseBuilder)

Aggregations

ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)2 InvalidPostProcessResultException (org.entando.entando.aps.system.init.model.InvalidPostProcessResultException)2 StringWriter (java.io.StringWriter)1 Properties (java.util.Properties)1 Response (javax.ws.rs.core.Response)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 SelfRestCallPostProcess (org.entando.entando.aps.system.init.model.SelfRestCallPostProcess)1 AbstractApiResponse (org.entando.entando.aps.system.services.api.model.AbstractApiResponse)1 ApiMethod (org.entando.entando.aps.system.services.api.model.ApiMethod)1 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)1 IResponseBuilder (org.entando.entando.aps.system.services.api.server.IResponseBuilder)1 BeansException (org.springframework.beans.BeansException)1