Search in sources :

Example 31 with NavajoException

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

the class Expression method replacePropertyValues.

public static final String replacePropertyValues(String clause, Navajo inMessage) {
    // Find all property references in clause.
    StringBuilder result = new StringBuilder();
    int begin = clause.indexOf('[');
    if (// Clause does not contain properties.
    begin == -1)
        return clause;
    result.append(clause.substring(0, begin));
    while (begin >= 0) {
        int end = clause.indexOf(']');
        String propertyRef = clause.substring(begin + 1, end);
        Property prop = inMessage.getProperty(propertyRef);
        String value = "null";
        if (prop != null) {
            String type = prop.getType();
            if (type.equals(Property.SELECTION_PROPERTY)) {
                if (!prop.getCardinality().equals("+")) {
                    // Uni-selection property.
                    try {
                        List<Selection> list = prop.getAllSelectedSelections();
                        if (!list.isEmpty()) {
                            Selection sel = list.get(0);
                            value = sel.getValue();
                        }
                    } catch (com.dexels.navajo.document.NavajoException te) {
                        throw new TMLExpressionException(te.getMessage());
                    }
                } else {
                    // Multi-selection property.
                    try {
                        List<Selection> list = prop.getAllSelectedSelections();
                        List<String> selectedValues = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            Selection sel = list.get(i);
                            String o = sel.getValue();
                            selectedValues.add(o);
                        }
                        value = String.join(";", selectedValues);
                    } catch (NavajoException te) {
                        throw new TMLExpressionException(te.getMessage(), te);
                    }
                }
            } else if (type.equals(Property.STRING_PROPERTY)) {
                value = "\"" + prop.getValue() + "\"";
            } else {
                value = prop.getValue();
            }
        }
        result.append("{" + value + "}");
        clause = clause.substring(end + 1, clause.length());
        begin = clause.indexOf('[');
        if (begin >= 0)
            result.append(clause.substring(0, begin));
        else
            result.append(clause.substring(0, clause.length()));
    }
    return result.toString();
}
Also used : Selection(com.dexels.navajo.document.Selection) ArrayList(java.util.ArrayList) NavajoException(com.dexels.navajo.document.NavajoException) NavajoException(com.dexels.navajo.document.NavajoException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 32 with NavajoException

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

the class BaseMessageImpl method merge.

@Override
public void merge(Message incoming, boolean preferThis, boolean applySubType) {
    if (isArrayMessage() != incoming.isArrayMessage()) {
        throw new NavajoException("Incompatible message types in merge: " + getFullMessageName() + " (" + getType() + "), " + incoming.getFullMessageName() + " (" + incoming.getType() + ")");
    }
    if (this.isArrayMessage() && incoming.isArrayMessage() && incoming.getDefinitionMessage() != null) {
        // Perform merge for all my children with the definition message
        for (Message child : this.getElements()) {
            child.merge(incoming.getDefinitionMessage(), preferThis);
        }
    }
    if (incoming.getScope() != null && (this.getScope() == null || this.getScope().equals(""))) {
        this.setScope(incoming.getScope());
    }
    if (incoming.getSubType() != null && (this.getSubType() == null || this.getSubType().equals(""))) {
        this.setSubType(incoming.getSubType());
    }
    // Check if message with incoming name exists.
    if (!getName().equals(incoming.getName())) {
        incoming.setName(getName());
    }
    List<Property> properties = incoming.getAllProperties();
    for (int i = 0; i < properties.size(); i++) {
        Property p = (Property) properties.get(i).clone();
        Property otherProperty = null;
        if (preferThis) {
            otherProperty = getProperty(p.getName());
        }
        if (!preferThis || otherProperty == null) {
            addProperty(p);
        }
        // If we don't have a method set, use the incoming method
        if (otherProperty != null && otherProperty.getMethod().equals("")) {
            otherProperty.setMethod(p.getMethod());
        }
    }
    List<Message> incomingSubMessages = incoming.getAllMessages();
    for (int i = 0; i < incomingSubMessages.size(); i++) {
        Message incomingSubMessage = incomingSubMessages.get(i);
        // check if we have a sub message with the same name
        Message existingSubMessage = getMessage(incomingSubMessage.getName());
        if (existingSubMessage != null) {
            // incoming message, if not, we cannot use it
            if (existingSubMessage.isArrayMessage() != incomingSubMessage.isArrayMessage()) {
                throw new NavajoException("Incompatible message types in merge: " + existingSubMessage.getFullMessageName() + " (" + existingSubMessage.getType() + "), " + incomingSubMessage.getFullMessageName() + " (" + incomingSubMessage.getType() + ")");
            }
        }
        if (existingSubMessage == null) {
            // if we don't have the sub message ourselves and if the incoming sub message is marked
            // as nullable, then we should NOT add it (because we explicitly allow the message
            // to not exist)
            String nullableString = incomingSubMessage.getSubType("nullable");
            boolean nullable = nullableString != null && Boolean.parseBoolean(nullableString);
            if (nullable && applySubType) {
                continue;
            } else {
                addMessage(incomingSubMessage.copy());
            }
        } else {
            existingSubMessage.merge(incomingSubMessage, preferThis);
        }
    }
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) Property(com.dexels.navajo.document.Property)

Example 33 with NavajoException

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

the class CheckUniqueness method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            logger.debug("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(operand(0).value instanceof String && operand(1).value instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + operand(0).value.getClass() + " and " + operand(1).value.getClass());
    }
    String messageName = getStringOperand(0);
    String propertyName = getStringOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = getStringOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    boolean isUnique = true;
    HashSet<Object> values = new HashSet<Object>();
    try {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            Property p = m.getProperty(propertyName);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                if (p != null) {
                    Object o = null;
                    if (p.getType().equals(Property.SELECTION_PROPERTY)) {
                        o = p.getSelected().getValue();
                    } else {
                        o = p.getTypedValue();
                    }
                    if (values.contains(o)) {
                        return Boolean.FALSE;
                    } else {
                        values.add(o);
                    }
                }
            }
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
    return (isUnique ? Boolean.TRUE : Boolean.FALSE);
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) SystemException(com.dexels.navajo.script.api.SystemException) Property(com.dexels.navajo.document.Property) HashSet(java.util.HashSet)

Example 34 with NavajoException

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

the class NavajoRequestToString method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    Navajo in = getNavajo().copy();
    in.removeHeader();
    in.removeInternalMessages();
    StringWriter ws = new StringWriter();
    try {
        in.write(ws);
    } catch (NavajoException e) {
        throw new TMLExpressionException(this, e.getMessage(), e);
    }
    return ws.toString();
}
Also used : StringWriter(java.io.StringWriter) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 35 with NavajoException

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

the class NqlServlet method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    String server = req.getParameter("server");
    String query = req.getParameter("query");
    String ping = req.getParameter("ping");
    String tenant = req.getParameter("tenant");
    if (tenant == null) {
        // Fall back to header
        tenant = req.getHeader("X-Navajo-Instance");
    }
    if (ping != null) {
        if (!checkPing(username, resp)) {
            throw new ServletException("ping failed.");
        }
        return;
    }
    NavajoRemoteContext nrc = new NavajoRemoteContext();
    nrc.setupClient(server, username, password, req.getServerName(), req.getServerPort(), req.getContextPath(), "/PostmanLegacy");
    // new NQLContext();
    NqlContextApi nc = getNqlContext();
    nc.setNavajoContext(getClientContext());
    try {
        nc.executeCommand(query, tenant, username, password, new OutputCallback() {

            @Override
            public void setOutputType(String mime) {
                resp.setContentType(mime);
            }

            @Override
            public void setContentLength(long l) {
                resp.setContentLength((int) l);
                resp.setHeader("Accept-Ranges", "none");
                resp.setHeader("Connection", "close");
            }

            @Override
            public OutputStream getOutputStream() {
                try {
                    return resp.getOutputStream();
                } catch (IOException e) {
                    logger.error("Error: ", e);
                    return null;
                }
            }
        });
    } catch (ClientException e) {
        logger.error("Error: ", e);
    } catch (NavajoException e) {
        logger.error("Error: ", e);
    }
    resp.getOutputStream().flush();
    resp.getOutputStream().close();
// String
}
Also used : ServletException(javax.servlet.ServletException) NqlContextApi(com.dexels.navajo.client.nql.NqlContextApi) NavajoRemoteContext(com.dexels.navajo.client.context.NavajoRemoteContext) OutputStream(java.io.OutputStream) NavajoException(com.dexels.navajo.document.NavajoException) IOException(java.io.IOException) ClientException(com.dexels.navajo.client.ClientException) OutputCallback(com.dexels.navajo.client.nql.OutputCallback)

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