Search in sources :

Example 16 with APIException

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

the class ServiceCommand method execute.

@Override
public JsonNode execute(ArticleRuntime runtime, ArticleContext context, Map<String, String> parameters, XMLElement element) throws APIException {
    Long startedAt = System.currentTimeMillis();
    String name = parameters.get("name");
    if (name == null) {
        throw new APIException("Command: " + this.getName() + " can't be executed without required parameters: " + name, null, APIErrorCode.InternalError);
    }
    String refresh = parameters.get("refresh");
    if ("false".equals(refresh)) {
        Navajo res = runtime.getNavajo(name);
        if (res != null) {
            runtime.pushNavajo(name, res);
            return null;
        }
    }
    String input = parameters.get("input");
    Navajo n = null;
    if (input != null) {
        n = runtime.getNavajo(input);
        if (n == null) {
            throw new APIException("Command: " + this.getName() + " supplies an 'input' parameter: " + input + ", but that navajo object can not be found" + name, null, APIErrorCode.InternalError);
        }
    }
    if (runtime.getNavajo() != null) {
        n = runtime.getNavajo();
    } else {
        n = NavajoFactory.getInstance().createNavajo();
    }
    appendTokenAttributes(runtime, n);
    final String username = runtime.getUsername();
    Header h = NavajoFactory.getInstance().createHeader(n, name, username, "", -1);
    if (runtime.getAccess() != null) {
        h.setHeaderAttribute("parentaccessid", runtime.getAccess().accessID);
    }
    n.addHeader(h);
    h.setHeaderAttribute("application", "article");
    final Navajo result = performCall(runtime, name, n, runtime.getInstance());
    statLogger.info("Finished {} ({}) in {}ms", h.getHeaderAttribute("parentaccessid"), name, (System.currentTimeMillis() - startedAt));
    runtime.pushNavajo(name, result);
    return null;
}
Also used : APIException(com.dexels.navajo.article.APIException) Header(com.dexels.navajo.document.Header) Navajo(com.dexels.navajo.document.Navajo)

Example 17 with APIException

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

the class TestServiceCommand method performCall.

@Override
protected Navajo performCall(ArticleRuntime runtime, String name, Navajo n, String instance) throws APIException {
    File tmlFolder = new File("testresources/tml");
    File scriptFile = new File(tmlFolder, name + ".xml");
    if (scriptFile.exists()) {
        FileReader fr = null;
        try {
            fr = new FileReader(scriptFile);
            return NavajoFactory.getInstance().createNavajo(fr);
        } catch (FileNotFoundException e) {
            throw new APIException("Error reading tml stub at: " + scriptFile.getAbsolutePath(), e, APIErrorCode.ArticleNotFound);
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    logger.error("Error closing file: ", e);
                }
            }
        }
    } else {
        throw new APIException("Error reading tml stub at: " + scriptFile.getAbsolutePath(), null, APIErrorCode.ArticleNotFound);
    }
}
Also used : APIException(com.dexels.navajo.article.APIException) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

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