Search in sources :

Example 16 with SystemException

use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.

the class EvaluateExpression method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() != 1 && getOperands().size() != 3) {
        throw new TMLExpressionException("Wrong number of arguments");
    }
    Navajo currentNavajo = this.getNavajo();
    Message currentMessage = this.getCurrentMessage();
    Operand result = null;
    boolean conditional = (this.getOperands().size() == 3);
    if (!conditional) {
        String expression = (String) getOperand(0);
        try {
            result = Expression.evaluate(expression, currentNavajo, null, currentMessage, null, null, null, null);
        } catch (TMLExpressionException ex) {
            logger.error("Error: ", ex);
        }
    } else {
        String condition = (String) getOperand(0);
        String exp1 = (String) getOperand(1);
        String exp2 = (String) getOperand(2);
        try {
            if (Condition.evaluate(condition, currentNavajo, null, currentMessage, getAccess())) {
                result = Expression.evaluate(exp1, currentNavajo, null, currentMessage, null, null, null, null);
            } else {
                result = Expression.evaluate(exp2, currentNavajo, null, currentMessage, null, null, null, null);
            }
        } catch (SystemException ex1) {
            logger.error("Error: ", ex1);
            throw new TMLExpressionException(this, ex1.getMessage());
        } catch (TMLExpressionException ex1) {
            logger.error("Error: ", ex1);
            throw new TMLExpressionException(this, ex1.getMessage());
        }
    }
    if (result != null) {
        return result.value;
    } else {
        return null;
    }
}
Also used : Message(com.dexels.navajo.document.Message) SystemException(com.dexels.navajo.script.api.SystemException) Operand(com.dexels.navajo.document.Operand) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 17 with SystemException

use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.

the class RESTAdapter method setDoSend.

public void setDoSend(String url, Navajo od) throws UserException, ConditionErrorException, SystemException {
    this.url = url.trim();
    if (dateFormat != null && !dateFormat.equals("")) {
        json.setDateFormat(new SimpleDateFormat(dateFormat));
    }
    Writer w = new StringWriter();
    Binary bContent = new Binary();
    if (textContent == null) {
        // Remove globals and parms message
        if (od.getMessage("__globals__") != null)
            od.removeMessage("__globals__");
        if (od.getMessage(Message.MSG_PARAMETERS_BLOCK) != null)
            od.removeMessage(Message.MSG_PARAMETERS_BLOCK);
        if (od.getMessage(Message.MSG_TOKEN_BLOCK) != null)
            od.removeMessage(Message.MSG_TOKEN_BLOCK);
        if (od.getMessage(Message.MSG_AAA_BLOCK) != null)
            od.removeMessage(Message.MSG_AAA_BLOCK);
        try {
            json.format(od, w, true);
            bContent.getOutputStream().write(w.toString().getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            logger.error("Exception on parsing input navajo as JSON! Not performing REST call!");
            throw new UserException(e.getMessage(), e);
        }
    } else {
        try {
            bContent.getOutputStream().write(textContent.toString().getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            logger.error("IOException on writing textcontent! Not performing REST call!");
            throw new UserException(e.getMessage(), e);
        }
    }
    HTTPMap http = new HTTPMap();
    setupHttpMap(http, bContent);
    http.setDoSend(true);
    Binary result = http.getResult();
    responseCode = http.getResponseCode();
    responseMessage = http.getResponseMessage();
    try {
        if (result == null) {
            throw new UserException(-1, "Null result");
        }
        if (responseCode >= 300) {
            logger.warn("Got a non-200 response code: {}!", responseCode);
            if (breakOnException) {
                throw new UserException(responseCode, responseMessage);
            }
        }
        rawResult = new String(result.getData());
        if (jsonResponse) {
            if (http.getResponseContentType() != null && http.getResponseContentType().contains("application/json")) {
                try {
                    inDoc = json.parse(result.getDataAsStream(), topMessage);
                } catch (Throwable t) {
                    logger.warn("Unable to parse response as JSON!", t);
                    if (breakOnException) {
                        throw t;
                    }
                }
            } else if (http.getResponseContentType() == null) {
                logger.info("No response content type - creating empty navajo as response");
                inDoc = NavajoFactory.getInstance().createNavajo();
            } else {
                logger.warn("Unexpected output content type: {}", http.getResponseContentType());
                if (breakOnException) {
                    throw new UserException(-1, "Unexpected content type: " + http.getResponseContentType());
                }
            }
        } else {
            logger.debug("Non-json response - creating empty Navajo");
            inDoc = NavajoFactory.getInstance().createNavajo();
        }
    } catch (Throwable e) {
        logger.error("Exception on getting response", e);
        if (breakOnException) {
            logger.warn("Raw response data: {}", rawResult);
            throw new UserException(e.getMessage(), e);
        } else {
            logger.warn("Exception on getting response, but breakOnException was set. Continuing!");
        }
    }
    if (inDoc == null) {
        logger.warn("No indoc - creating empty one");
        inDoc = NavajoFactory.getInstance().createNavajo();
    }
}
Also used : StringWriter(java.io.StringWriter) Binary(com.dexels.navajo.document.types.Binary) UserException(com.dexels.navajo.script.api.UserException) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) StringWriter(java.io.StringWriter) Writer(java.io.Writer) IOException(java.io.IOException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SystemException(com.dexels.navajo.script.api.SystemException) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException)

Example 18 with SystemException

use of com.dexels.navajo.script.api.SystemException in project navajo by Dexels.

the class NavajoMap method setDoSend.

/**
 * Use this method to call another Navajo webservice. If server is not specified, the Navajo server that is used to handle this request is also used to
 * handle the new request.
 *
 * @param method
 * @throws UserException
 */
public void setDoSend(String method, Navajo od) throws UserException, ConditionErrorException, SystemException {
    if (serviceCalled) {
        logger.warn("DO NOT USE A NAVAJOMAP TO CALL A SECOND WEBSERVICE, USE NEW NAVAJOMAP INSTEAD");
    }
    // Reset current msgPointer when performing new doSend.
    msgPointer = null;
    setMethod(method);
    this.outDoc = od;
    this.username = (username == null) ? this.access.rpcUser : username;
    this.password = (password == null) ? this.access.rpcPwd : password;
    this.method = method;
    if (password == null)
        password = "";
    try {
        if (this.resource != null) {
            serviceCalled = true;
            AsyncClient ac = NavajoClientResourceManager.getInstance().getAsyncClient(this.resource);
            if (ac == null) {
                throw new UserException(-1, "No external resource found for: " + this.resource);
            }
            ac.callService(outDoc, method, this);
        } else if (server != null) {
            // External request.
            try {
                ManualAsyncClient ac = AsyncClientFactory.getManualInstance();
                if (ac == null) {
                    logger.warn("unable to find async client - cannot perform navajomap call!");
                    throw new UserException(-1, "AsyncClient null");
                }
                String server = this.server.startsWith("http") ? this.server : "http://" + this.server;
                Integer timeout = null;
                if (serverTimeout > -1) {
                    timeout = serverTimeout;
                }
                ac.callService(server, username, password, outDoc, method, this, timeout);
            } catch (Exception e) {
                throw new UserException(-1, e.getMessage(), e);
            }
            serviceCalled = true;
        } else // Internal request.
        {
            inDoc = null;
            serviceFinished = false;
            if (block) {
                this.run();
            } else {
                SchedulerRegistry.submit(this, lowPriority);
            }
            serviceCalled = true;
            if (getException() != null) {
                if (getException() instanceof ConditionErrorException) {
                    throw (ConditionErrorException) getException();
                } else if (getException() instanceof UserException) {
                    throw (UserException) getException();
                } else if (getException() instanceof SystemException) {
                    throw (SystemException) getException();
                } else {
                    throw new SystemException(-1, "", getException());
                }
            }
        }
    } catch (NavajoException | IOException e) {
        throw new SystemException("Error connecting to remote server", e);
    }
}
Also used : ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) ManualAsyncClient(com.dexels.navajo.client.async.ManualAsyncClient) SystemException(com.dexels.navajo.script.api.SystemException) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) IOException(java.io.IOException) AsyncClient(com.dexels.navajo.client.async.AsyncClient) ManualAsyncClient(com.dexels.navajo.client.async.ManualAsyncClient) NavajoException(com.dexels.navajo.document.NavajoException) AuthorizationException(com.dexels.navajo.script.api.AuthorizationException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) IOException(java.io.IOException) SystemException(com.dexels.navajo.script.api.SystemException) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException)

Aggregations

SystemException (com.dexels.navajo.script.api.SystemException)18 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)10 Message (com.dexels.navajo.document.Message)9 Navajo (com.dexels.navajo.document.Navajo)9 NavajoException (com.dexels.navajo.document.NavajoException)9 UserException (com.dexels.navajo.script.api.UserException)8 Operand (com.dexels.navajo.document.Operand)7 IOException (java.io.IOException)7 Property (com.dexels.navajo.document.Property)5 CompilationException (com.dexels.navajo.script.api.CompilationException)4 KeywordException (com.dexels.navajo.mapping.compiler.meta.KeywordException)3 MetaCompileException (com.dexels.navajo.mapping.compiler.meta.MetaCompileException)3 ParseException (com.dexels.navajo.parser.compiled.ParseException)3 MappableException (com.dexels.navajo.script.api.MappableException)3 MappingException (com.dexels.navajo.script.api.MappingException)3 ConditionErrorException (com.dexels.navajo.server.ConditionErrorException)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 TransformerException (javax.xml.transform.TransformerException)3 Header (com.dexels.navajo.document.Header)2