Search in sources :

Example 21 with NavajoException

use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.

the class ForAll method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    /**
     *@todo Implement this com.dexels.navajo.parser.FunctionInterface abstract method
     */
    Message arrayMessage = null;
    String messagePath = null;
    if (getOperand(0) instanceof Message) {
        arrayMessage = (Message) getOperand(0);
    } else {
        messagePath = (String) getOperand(0);
    }
    String expression = (String) getOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = (String) getOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    // try {
    try {
        List<Message> arrayMsg = null;
        if (arrayMessage != null) {
            arrayMsg = arrayMessage.getAllMessages();
        } else {
            arrayMsg = (parent != null ? parent.getMessages(messagePath) : doc.getMessages(messagePath));
        }
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messagePath);
        }
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message current = arrayMsg.get(i);
            try {
                boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, current, getAccess()) : true);
                if (evaluate) {
                    Operand result = Expression.evaluate(expression, doc, null, current);
                    if (result == null) {
                        throw new TMLExpressionException(this, "Error evaluating expression: " + expression + " in message: " + current.getFullMessageName());
                    }
                    if (result.value == null) {
                        throw new TMLExpressionException(this, "Error evaluating expression: " + expression + " in message: " + current.getFullMessageName());
                    }
                    String res2 = "" + result.value;
                    Operand result2 = Expression.evaluate(res2, doc, null, current);
                    if (result2 == null) {
                        throw new TMLExpressionException(this, "Error evaluating expression: " + res2 + " in message: " + current.getFullMessageName());
                    }
                    if (result2.value == null) {
                        throw new TMLExpressionException(this, "Error evaluating expression: " + res2 + " in message: " + current.getFullMessageName());
                    }
                    boolean res = ((Boolean) result2.value).booleanValue();
                    if (!res) {
                        return Boolean.FALSE;
                    }
                }
            } catch (SystemException ex) {
                logger.error("Error: ", ex);
            }
        }
    } catch (NavajoException ex) {
        throw new TMLExpressionException(this, "Error evaluating message path");
    }
    return Boolean.TRUE;
}
Also used : Message(com.dexels.navajo.document.Message) SystemException(com.dexels.navajo.script.api.SystemException) Operand(com.dexels.navajo.document.Operand) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 22 with NavajoException

use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.

the class GetPropertyAttribute method getAttribute.

public Object getAttribute(Object operand, String attribute) throws com.dexels.navajo.expression.api.TMLExpressionException {
    Property p = null;
    if (operand instanceof Property) {
        p = (Property) operand;
    } else {
        String propertyName = operand.toString();
        p = (getCurrentMessage() != null ? getCurrentMessage().getProperty(propertyName) : this.getNavajo().getProperty(propertyName));
    }
    if (p == null) {
        throw new TMLExpressionException(this, "Property " + operand.toString() + " not found");
    }
    if (attribute.equals("direction")) {
        return p.getDirection();
    }
    if (attribute.equals("type")) {
        return p.getType();
    }
    if (attribute.equals("cardinality")) {
        return p.getCardinality();
    }
    try {
        throw new TMLExpressionException(this, "attribute " + attribute + " not found for property " + p.getFullPropertyName());
    } catch (NavajoException e) {
        throw new TMLExpressionException(this, "attribute " + attribute + " not found for unknown property ");
    }
}
Also used : NavajoException(com.dexels.navajo.document.NavajoException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 23 with NavajoException

use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.

the class NavajoMap method setAppendParms.

/**
 * @param b
 * @throws UserException
 *
 *             if messageOffset is '/', the messages of the received Doc will be appended to the root param block or to the current param message.
 */
public final void setAppendParms(String messageOffset) throws UserException {
    waitForResult();
    try {
        Message parm = (access.getCompiledScript().getCurrentParamMsg() == null ? access.getInDoc().getMessage("__parms__") : access.getCompiledScript().getCurrentParamMsg());
        List<Message> list = null;
        // If append message equals '/'.
        if (messageOffset.equals(Navajo.MESSAGE_SEPARATOR)) {
            list = inDoc.getAllMessages();
        } else if (inDoc.getMessage(messageOffset) == null) {
            return;
        } else if (inDoc.getMessage(messageOffset).getType().equals(Message.MSG_TYPE_ARRAY)) {
            list = new ArrayList<>();
            list.add(inDoc.getMessage(messageOffset));
        } else {
            list = inDoc.getMessages(messageOffset);
        }
        for (int i = 0; i < list.size(); i++) {
            Message inMsg = list.get(i);
            // Clone message and append it to currentMsg if it exists, else
            // directly under currentDoc.
            // currentDoc.importMessage(inMsg)
            Message clone = inDoc.copyMessage(inMsg, parm.getRootDoc());
            parm.addMessage(clone, true);
        }
    } catch (NavajoException ne) {
        throw new UserException(-1, ne.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException)

Example 24 with NavajoException

use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.

the class AuditLogEvent method getEventNavajo.

@Override
public Navajo getEventNavajo() {
    Navajo input = NavajoFactory.getInstance().createNavajo();
    Message event = NavajoFactory.getInstance().createMessage(input, "__event__");
    try {
        input.addMessage(event);
        Property eventMessage = NavajoFactory.getInstance().createProperty(input, "Message", Property.STRING_PROPERTY, getMessage(), 0, "", Property.DIR_OUT);
        Property eventLevel = NavajoFactory.getInstance().createProperty(input, "Level", Property.STRING_PROPERTY, getLevel().getName(), 0, "", Property.DIR_OUT);
        Property eventSubsystem = NavajoFactory.getInstance().createProperty(input, "Subsystem", Property.STRING_PROPERTY, getSubSystem(), 0, "", Property.DIR_OUT);
        event.addProperty(eventMessage);
        event.addProperty(eventLevel);
        event.addProperty(eventSubsystem);
    } catch (NavajoException e) {
        logger.error("Error: ", e);
    }
    return input;
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Example 25 with NavajoException

use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.

the class Dispatcher method errorHandler.

/**
 * Handles errors.
 *
 * @param access
 * @param e
 * @param inMessage
 * @return the response error Navajo document.
 *
 * @throws FatalException
 */
private final Navajo errorHandler(Access access, Throwable e, Navajo inMessage) throws FatalException {
    String message = "";
    if (access != null) {
        try {
            message = e.getClass().toString() + ": " + e.getMessage() + ", " + e.toString() + ", " + e.getLocalizedMessage();
            if (message.equalsIgnoreCase("")) {
                message = "Undefined Error";
            }
            StringWriter swriter = new StringWriter();
            PrintWriter writer = new PrintWriter(swriter);
            e.printStackTrace(writer);
            message += swriter.getBuffer().toString();
            message += "\n";
            swriter = new StringWriter();
            writer = new PrintWriter(swriter);
            // Remove some messages that might contain sensitive info.
            if (inMessage != null) {
                inMessage.removeInternalMessages();
            }
            inMessage.write(writer);
            message += swriter.getBuffer().toString();
        } catch (NavajoException tbe) {
            throw new FatalException(tbe.getMessage());
        }
    } else {
        // if no access was created, use the throwable message to be able to
        // say *anything*
        message += e.getMessage();
    }
    try {
        Navajo out = generateErrorMessage(access, message, SystemException.SYSTEM_ERROR, 1, e);
        if (access != null) {
            access.setOutputDoc(out);
        }
        return out;
    } catch (Exception ne) {
        throw new FatalException(ne.getMessage(), ne);
    }
}
Also used : StringWriter(java.io.StringWriter) FatalException(com.dexels.navajo.script.api.FatalException) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) FatalException(com.dexels.navajo.script.api.FatalException) NavajoException(com.dexels.navajo.document.NavajoException) AuthorizationException(com.dexels.navajo.script.api.AuthorizationException) UserException(com.dexels.navajo.script.api.UserException) TriggerException(com.dexels.navajo.server.enterprise.scheduler.TriggerException) IOException(java.io.IOException) SystemException(com.dexels.navajo.script.api.SystemException) PrintWriter(java.io.PrintWriter)

Aggregations

NavajoException (com.dexels.navajo.document.NavajoException)46 Message (com.dexels.navajo.document.Message)28 Property (com.dexels.navajo.document.Property)25 Navajo (com.dexels.navajo.document.Navajo)21 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)12 SystemException (com.dexels.navajo.script.api.SystemException)9 Operand (com.dexels.navajo.document.Operand)8 UserException (com.dexels.navajo.script.api.UserException)8 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 Selection (com.dexels.navajo.document.Selection)5 MappableException (com.dexels.navajo.script.api.MappableException)5 ArrayList (java.util.ArrayList)4 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)3 ManualAsyncClient (com.dexels.navajo.client.async.ManualAsyncClient)3 FatalException (com.dexels.navajo.script.api.FatalException)3 File (java.io.File)3 ClientException (com.dexels.navajo.client.ClientException)2 NavajoResponseHandler (com.dexels.navajo.client.NavajoResponseHandler)2 Header (com.dexels.navajo.document.Header)2