Search in sources :

Example 1 with APIException

use of com.dexels.navajo.article.APIException in project navajo by Dexels.

the class SetValueCommand method execute.

@Override
public JsonNode execute(ArticleRuntime runtime, ArticleContext context, Map<String, String> parameters, XMLElement xmlElement) throws APIException {
    String service = parameters.get("service");
    if (service == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". setvalue requires a service, which is not supplied.", null, APIErrorCode.InternalError);
    }
    String element = parameters.get("element");
    if (element == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". setvalue requires a element, which is not supplied.", null, APIErrorCode.InternalError);
    }
    Navajo n = runtime.getNavajo(service);
    if (n == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". Requested service: " + service + " is not loaded.", null, APIErrorCode.InternalError);
    }
    Property p = n.getProperty(element);
    if (p == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". Requested element: " + element + " in service " + service + " is not found.", null, APIErrorCode.InternalError);
    }
    String value = parameters.get("value");
    if (value == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". setvalue requires a value, which is not supplied.", null, APIErrorCode.InternalError);
    }
    if (value.startsWith("@")) {
        String resolved = runtime.resolveArgument(value);
        if (resolved != null) {
            p.setValue(resolved);
        }
    } else if (value.startsWith("$")) {
        p.setAnyValue(runtime.resolveScope(value));
    } else {
        p.setValue(value);
    }
    return null;
}
Also used : APIException(com.dexels.navajo.article.APIException) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Example 2 with APIException

use of com.dexels.navajo.article.APIException in project navajo by Dexels.

the class TableCommand method replaceTokens.

private String replaceTokens(String text, Message msg) throws APIException {
    Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}");
    Matcher matcher = pattern.matcher(text);
    StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        final String group = matcher.group(1);
        Property p = msg.getProperty(group);
        if (p == null) {
            throw new APIException("Error resolving target. Referenced property: " + group + " not found in message", null, APIErrorCode.InternalError);
        }
        String replacement = p.getValue();
        if (replacement != null) {
            matcher.appendReplacement(buffer, "");
            buffer.append(replacement);
        }
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}
Also used : Pattern(java.util.regex.Pattern) APIException(com.dexels.navajo.article.APIException) Matcher(java.util.regex.Matcher) Property(com.dexels.navajo.document.Property)

Example 3 with APIException

use of com.dexels.navajo.article.APIException in project navajo by Dexels.

the class TableCommand method execute.

@Override
public JsonNode execute(ArticleRuntime runtime, ArticleContext context, Map<String, String> parameters, XMLElement element) throws APIException {
    String service = parameters.get("service");
    if (service == null) {
        throw new APIException("No service parameter supplied for table. We need to know which navajo you want to use.", null, APIErrorCode.InternalError);
    }
    Navajo navajo = runtime.getNavajo(service);
    if (navajo == null) {
        throw new APIException("Navajo: " + service + " was not found in the current runtime.", null, APIErrorCode.InternalError);
    }
    String path = parameters.get("path");
    if (path == null) {
        throw new APIException("No path parameter supplied. Which message do you want to listen to.", null, APIErrorCode.InternalError);
    }
    Message message = navajo.getMessage(path);
    if (message == null) {
        throw new APIException("Message: " + path + " not found", null, APIErrorCode.InternalError);
    }
    runtime.setMimeType("application/json; charset=utf-8");
    ArrayNode nodes = runtime.getObjectMapper().createArrayNode();
    for (Message data : message.getElements()) {
        ObjectNode node = runtime.getObjectMapper().createObjectNode();
        for (XMLElement XMLElement : element.getChildren()) {
            final String id = XMLElement.getStringAttribute("id");
            final String type = XMLElement.getStringAttribute("type");
            final String target = XMLElement.getStringAttribute("target");
            if (target != null) {
                // A target is a link, they do not have navajo value.
                node.put(id, resolveTarget(target, runtime, data));
            } else {
                // We default back to the id for the propertyName if not explicit set.
                final String propertyName = XMLElement.getStringAttribute("propertyName", id);
                Property property = data.getProperty(propertyName);
                APIValue.setValueOnNodeForType(node, id, type, property, runtime);
            }
        }
        nodes.add(node);
    }
    return nodes;
}
Also used : APIException(com.dexels.navajo.article.APIException) Message(com.dexels.navajo.document.Message) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Navajo(com.dexels.navajo.document.Navajo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) Property(com.dexels.navajo.document.Property)

Example 4 with APIException

use of com.dexels.navajo.article.APIException in project navajo by Dexels.

the class BaseContextImpl method writeArticleMeta.

@Override
public void writeArticleMeta(String name, ObjectNode w, ObjectMapper mapper, boolean extended) throws APIException {
    File in = resolveArticle(name);
    try (FileReader fr = new FileReader(in)) {
        ObjectNode article = mapper.createObjectNode();
        w.set(name, article);
        XMLElement x = new CaseSensitiveXMLElement();
        x.parseFromReader(fr);
        article.put("name", name);
        interpretMeta(x, mapper, article, extended);
    } catch (FileNotFoundException e) {
        throw new APIException("Article " + name + " not found", e, APIErrorCode.ArticleNotFound);
    } catch (XMLParseException e) {
        throw new APIException("Article " + name + " is not valid XML", e, APIErrorCode.InternalError);
    } catch (IOException e) {
        throw new APIException("Autoclose on filereader failed", e, APIErrorCode.InternalError);
    }
}
Also used : CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) APIException(com.dexels.navajo.article.APIException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) XMLParseException(com.dexels.navajo.document.nanoimpl.XMLParseException) File(java.io.File)

Example 5 with APIException

use of com.dexels.navajo.article.APIException in project navajo by Dexels.

the class BaseContextImpl method interpretArticle.

@Override
public void interpretArticle(File article, ArticleRuntime ac) throws APIException, NoJSONOutputException {
    XMLElement articleXml = new CaseSensitiveXMLElement();
    Reader r = null;
    try {
        r = new FileReader(article);
        articleXml.parseFromReader(r);
        ac.execute(this);
    } catch (IOException e) {
        throw new APIException(e.getMessage(), e, APIErrorCode.InternalError);
    } finally {
        if (r != null) {
            try {
                r.close();
            } catch (IOException e) {
                logger.error("Error: ", e);
            }
        }
    }
}
Also used : CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) APIException(com.dexels.navajo.article.APIException) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) IOException(java.io.IOException) CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement)

Aggregations

APIException (com.dexels.navajo.article.APIException)17 XMLElement (com.dexels.navajo.document.nanoimpl.XMLElement)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 IOException (java.io.IOException)6 Navajo (com.dexels.navajo.document.Navajo)5 Property (com.dexels.navajo.document.Property)4 CaseSensitiveXMLElement (com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 File (java.io.File)3 FileReader (java.io.FileReader)3 NoJSONOutputException (com.dexels.navajo.article.NoJSONOutputException)2 ArticleCommand (com.dexels.navajo.article.command.ArticleCommand)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 FileNotFoundException (java.io.FileNotFoundException)2 HashMap (java.util.HashMap)2 APIErrorCode (com.dexels.navajo.article.APIErrorCode)1 ArticleRuntime (com.dexels.navajo.article.ArticleRuntime)1 ArticleTmlRunnable (com.dexels.navajo.article.runnable.ArticleTmlRunnable)1 Header (com.dexels.navajo.document.Header)1 Message (com.dexels.navajo.document.Message)1