Search in sources :

Example 76 with Message

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

the class MappingUtils method setProperty.

public static final Property setProperty(boolean parameter, Message msg, String name, Object value, String type, String subtype, String direction, String description, int length, Navajo outputDoc, Navajo tmlDoc, boolean remove) throws MappingException {
    Message ref = null;
    if (parameter) {
        if (msg == null) {
            msg = tmlDoc.getMessage("__parms__");
            if (msg == null) {
                // Create __parms__ message.
                msg = NavajoFactory.getInstance().createMessage(tmlDoc, "__parms__");
                tmlDoc.addMessage(msg);
            }
        }
        ref = getMessageObject(name, msg, false, tmlDoc, false, "", -1);
        if (ref == null) {
            // Can be null due to absolute param name (starting with '/'). In this case use
            // __parms__ as parent.
            ref = tmlDoc.getMessage("__parms__");
        }
    } else {
        ref = getMessageObject(name, msg, false, outputDoc, false, "", -1);
    }
    if (ref == null) {
        ref = msg;
    }
    String actualName = getStrippedPropertyName(name);
    if (ref == null) {
        throw new MappingException("Property can only be created under a message");
    }
    // with ../ constructions in the name of the new property, it is possible to create a property at the rootMessage of a NavajoDoc but that will never be serialized. Logger message to see how often it happens
    if (ref.equals(ref.getRootDoc().getRootMessage())) {
        logger.warn("WARNING - Adding property to rootMessage of NavajoDoc - property will not be findable");
    }
    Property prop = ref.getProperty(actualName);
    // Remove a parameter if remove flag is set.
    if (remove && prop != null && parameter) {
        ref.removeProperty(prop);
        return null;
    }
    if (prop == null && remove && parameter) {
        return null;
    }
    if (prop == null) {
        // Property does not exist.
        if (!parameter) {
            if (value instanceof Property) {
                // Value is a property itself!
                prop = (Property) ((Property) value).clone(name);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                // Legacy mode hack, many scripts do not expect null valued string properties.
                if (Property.STRING_PROPERTY.equals(type) && value == null) {
                    value = "";
                }
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        } else {
            if (Property.EXPRESSION_LITERAL_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                prop.setValue(new NavajoExpression(value.toString()));
                prop.setType(type);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        }
        ref.addProperty(prop);
    } else {
        // Existing property.
        prop.clearValue();
        if (value instanceof Property) {
            // Value is a property itself!
            prop = (Property) ((Property) value).clone(name);
        } else if (Property.BINARY_PROPERTY.equals(type)) {
            if (value instanceof Binary) {
                prop.setValue((Binary) value);
            } else {
                prop.clearValue();
            }
        } else if (Property.SELECTION_PROPERTY.equals(type) && value != null && value instanceof Selection[]) {
            prop.setCardinality("+");
            prop.setValue((Selection[]) value);
        } else if (!Property.SELECTION_PROPERTY.equals(type)) {
            if (value != null) {
                if ((value instanceof StringLiteral)) {
                    prop.setUnCheckedStringAsValue(((StringLiteral) value).toString());
                } else {
                    prop.setAnyValue(value);
                }
            } else {
                prop.clearValue();
            }
        }
        if (Property.DIR_IN.equals(direction) || Property.DIR_OUT.equals(direction)) {
            prop.setDirection(direction);
        }
        prop.setType(type);
        // Should not matter ;)
        prop.setName(actualName);
    }
    // Set subtype if not empty.
    if (subtype != null && !subtype.equals("")) {
        prop.setSubType(subtype);
    }
    // Set description if not empty.
    if (description != null && !description.equals("")) {
        prop.setDescription(description);
    }
    // Set length if not empty ( = -1) .
    if (length != -1) {
        prop.setLength(length);
    }
    return prop;
}
Also used : NavajoExpression(com.dexels.navajo.document.types.NavajoExpression) Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property) MappingException(com.dexels.navajo.script.api.MappingException)

Example 77 with Message

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

the class CompiledScript method run.

/*
     * (non-Javadoc)
     * 
     * @see
     * com.dexels.navajo.script.api.CompiledScriptInterface#run(com.dexels.navajo
     * .api.Access)
     */
@Override
public final void run(Access access) throws Exception {
    myAccess = access;
    long start = System.currentTimeMillis();
    try {
        dumpRequest();
        setValidations();
        currentParamMsg = access.getInDoc().getMessage("__parms__");
        ConditionData[] conditions = getValidationRules(access);
        boolean conditionsFailed = false;
        if (conditions != null && conditions.length > 0) {
            Navajo outMessage = access.getOutputDoc();
            Message[] failed = checkValidationRules(conditions, access.getInDoc(), outMessage, access);
            if (failed != null) {
                conditionsFailed = true;
                access.setExitCode(Access.EXIT_VALIDATION_ERR);
                Message msg = NavajoFactory.getInstance().createMessage(outMessage, "ConditionErrors");
                outMessage.addMessage(msg);
                msg.setType(Message.MSG_TYPE_ARRAY);
                for (int i = 0; i < failed.length; i++) {
                    msg.addMessage(failed[i]);
                }
            }
        }
        if (!conditionsFailed) {
            try {
                execute(access);
                access.setExitCode(Access.EXIT_OK);
            } catch (com.dexels.navajo.mapping.BreakEvent be) {
                access.setExitCode(Access.EXIT_BREAK);
                throw be;
            } catch (UserException e) {
                access.setExitCode(Access.EXIT_USEREXCEPTION);
                throw e;
            } catch (ConditionErrorException e) {
                access.setExitCode(Access.EXIT_VALIDATION_ERR);
                throw e;
            } catch (Exception e) {
                access.setExitCode(Access.EXIT_EXCEPTION);
                throw e;
            } finally {
                finalBlock(access);
            }
        }
    } finally {
        dumpResponse();
        // Release acquired locks.
        for (Lock l : acquiredLocks) {
            try {
                l.unlock();
            } catch (Throwable t) {
            }
        }
        acquiredLocks.clear();
        access.processingTime = (int) (System.currentTimeMillis() - start);
    }
}
Also used : ConditionData(com.dexels.navajo.server.ConditionData) Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) NavajoException(com.dexels.navajo.document.NavajoException) 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) CompilationException(com.dexels.navajo.script.api.CompilationException) Lock(java.util.concurrent.locks.Lock) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) UserException(com.dexels.navajo.script.api.UserException)

Example 78 with Message

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

the class CompiledScript method checkValidationRules.

/**
 * Deprecated method to check validation errors. Use <validations> block
 * inside webservice script instead.
 *
 * @param conditions
 * @param inMessage
 * @param outMessage
 * @return
 * @throws NavajoException
 * @throws SystemException
 * @throws UserException
 */
private final Message[] checkValidationRules(ConditionData[] conditions, Navajo inMessage, Navajo outMessage, Access a) throws SystemException, UserException {
    if (conditions == null) {
        return null;
    }
    List<Message> messages = new ArrayList<>();
    int index = 0;
    for (int i = 0; i < conditions.length; i++) {
        ConditionData condition = conditions[i];
        boolean valid = false;
        try {
            valid = com.dexels.navajo.parser.Condition.evaluate(condition.condition, inMessage, a);
        } catch (com.dexels.navajo.expression.api.TMLExpressionException ee) {
            throw new UserException(-1, "Invalid condition: " + ee.getMessage(), ee);
        }
        if (!valid) {
            String eval = com.dexels.navajo.parser.Expression.replacePropertyValues(condition.condition, inMessage);
            Message msg = NavajoFactory.getInstance().createMessage(outMessage, "failed" + (index++));
            Property prop0 = NavajoFactory.getInstance().createProperty(outMessage, "Id", Property.STRING_PROPERTY, condition.id + "", 0, "", Property.DIR_OUT);
            // Evaluate comment as expression.
            String description = "";
            try {
                Operand o = Expression.evaluate(condition.comment, inMessage);
                description = o.value + "";
            } catch (Exception e) {
                description = condition.comment;
            }
            Property prop1 = NavajoFactory.getInstance().createProperty(outMessage, "Description", Property.STRING_PROPERTY, description, 0, "", Property.DIR_OUT);
            msg.addProperty(prop0);
            msg.addProperty(prop1);
            if (System.getenv("DEBUG_SCRIPTS") != null && System.getenv("DEBUG_SCRIPTS").equals("true")) {
                Property prop2 = NavajoFactory.getInstance().createProperty(outMessage, "FailedExpression", Property.STRING_PROPERTY, condition.condition, 0, "", Property.DIR_OUT);
                Property prop3 = NavajoFactory.getInstance().createProperty(outMessage, "EvaluatedExpression", Property.STRING_PROPERTY, eval, 0, "", Property.DIR_OUT);
                msg.addProperty(prop2);
                msg.addProperty(prop3);
            }
            messages.add(msg);
        }
    }
    if (!messages.isEmpty()) {
        Message[] msgArray = new Message[messages.size()];
        messages.toArray(msgArray);
        return msgArray;
    } else {
        return null;
    }
}
Also used : ConditionData(com.dexels.navajo.server.ConditionData) Message(com.dexels.navajo.document.Message) Operand(com.dexels.navajo.document.Operand) ArrayList(java.util.ArrayList) NavajoException(com.dexels.navajo.document.NavajoException) 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) CompilationException(com.dexels.navajo.script.api.CompilationException) UserException(com.dexels.navajo.script.api.UserException) Property(com.dexels.navajo.document.Property)

Example 79 with Message

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

the class NavajoCompileScriptEvent method getEventNavajo.

/**
 * Return the event parameters as a Navajo object with a message __event__.
 */
@Override
public Navajo getEventNavajo() {
    Navajo input = NavajoFactory.getInstance().createNavajo();
    Message event = NavajoFactory.getInstance().createMessage(input, "__event__");
    try {
        input.addMessage(event);
        Property eventWebService = NavajoFactory.getInstance().createProperty(input, "Webservice", Property.STRING_PROPERTY, getWebservice(), 0, "", Property.DIR_OUT);
        event.addProperty(eventWebService);
    } 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 80 with Message

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

the class NavajoExceptionEvent method getEventNavajo.

/**
 * Return the event parameters as a Navajo object with a message __event__.
 */
@Override
public Navajo getEventNavajo() {
    Navajo input = NavajoFactory.getInstance().createNavajo();
    Message event = NavajoFactory.getInstance().createMessage(input, "__event__");
    try {
        input.addMessage(event);
        Property webserviceProperty = NavajoFactory.getInstance().createProperty(input, "Webservice", Property.STRING_PROPERTY, getWebservice(), 0, "", Property.DIR_OUT);
        Property exception = NavajoFactory.getInstance().createProperty(input, "Exception", Property.STRING_PROPERTY, getException().getMessage(), 0, "", Property.DIR_OUT);
        Property accessIdProperty = NavajoFactory.getInstance().createProperty(input, "AccessId", Property.STRING_PROPERTY, getAccessId(), 0, "", Property.DIR_OUT);
        Property userProperty = NavajoFactory.getInstance().createProperty(input, "User", Property.STRING_PROPERTY, getUser(), 0, "", Property.DIR_OUT);
        event.addProperty(webserviceProperty);
        event.addProperty(exception);
        event.addProperty(accessIdProperty);
        event.addProperty(userProperty);
    } 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)

Aggregations

Message (com.dexels.navajo.document.Message)312 Property (com.dexels.navajo.document.Property)149 Navajo (com.dexels.navajo.document.Navajo)127 Test (org.junit.Test)95 NavajoException (com.dexels.navajo.document.NavajoException)39 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)35 Access (com.dexels.navajo.script.api.Access)27 UserException (com.dexels.navajo.script.api.UserException)27 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 Selection (com.dexels.navajo.document.Selection)25 Operand (com.dexels.navajo.document.Operand)22 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)19 MappableException (com.dexels.navajo.script.api.MappableException)18 Ignore (org.junit.Ignore)17 SystemException (com.dexels.navajo.script.api.SystemException)16 Optional (java.util.Optional)15 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)13 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)13 HashMap (java.util.HashMap)13