Search in sources :

Example 6 with APIException

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

the class ServletArticleRuntimeImpl method resolveArgument.

@Override
public String resolveArgument(String name) throws APIException {
    final String trimmedName = name.substring(1);
    String res = request.getParameter(trimmedName);
    if (res != null) {
        return res;
    }
    XMLElement args = article.getElementByTagName("_arguments");
    if (args == null) {
        throw new APIException("Unspecified parameter reference: " + name + ". No argument data found.", null, APIErrorCode.InternalError);
    }
    List<XMLElement> lts = args.getChildren();
    for (XMLElement xmlElement : lts) {
        if (trimmedName.equals(xmlElement.getStringAttribute("name"))) {
            boolean optional = xmlElement.getBooleanAttribute("optional", "true", "false", false);
            if (!optional) {
                // not optional + no value = fail
                throw new APIException("Missing parameter not optional: " + trimmedName, null, APIErrorCode.MissingRequiredArgument);
            }
            return xmlElement.getStringAttribute("default");
        }
    }
    throw new APIException("Unspecified parameter reference: " + name + ". No argument data found.", null, APIErrorCode.InternalError);
}
Also used : APIException(com.dexels.navajo.article.APIException) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement)

Example 7 with APIException

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

the class OAuthArticleServlet method doServiceImpl.

@Override
protected void doServiceImpl(HttpServletRequest req, HttpServletResponse resp) throws APIException {
    String token = getToken(req);
    OAuthToken oauthToken = null;
    Client client = null;
    if (token != null) {
        oauthToken = getOAuthToken(token);
        client = getClient(oauthToken);
    } else {
        client = getClient(req);
    }
    String username = client.getUsername();
    if (oauthToken != null && oauthToken.getUsername() != null && !oauthToken.getUsername().equals("")) {
        username = oauthToken.getUsername();
    }
    String pathInfo = req.getPathInfo();
    String instance = client.getInstance();
    if (pathInfo == null) {
        throw new APIException("Pathinfo is null, we cannot find an article then", null, APIErrorCode.ArticleNotFound);
    }
    String articleName = determineArticleFromRequest(req);
    File article = getContext().resolveArticle(articleName);
    if (!article.exists()) {
        throw new APIException("Article does not exist", null, APIErrorCode.ArticleNotFound);
    }
    try {
        String ip = req.getHeader("X-Forwarded-For");
        if (ip == null || ip.equals("")) {
            ip = req.getRemoteAddr();
        }
        Access access = new Access(-1, -1, username, ACCESS_PREFIX + articleName, "", "", "", null, false, null);
        access.setTenant(instance);
        access.rpcPwd = token;
        access.created = new Date();
        access.ipAddress = ip;
        access.setClientDescription("Article");
        access.setClientToken("Client id: " + client.getId());
        ArticleRuntime runtime = new ServletArticleRuntimeImpl(req, resp, "", username, article, pathInfo, req.getParameterMap(), instance, oauthToken);
        runtime.setAccess(access);
        runtime.setUsername(username);
        ArticleTmlRunnable runner = new ArticleTmlRunnable(req, resp, client, runtime, getContext(), requestTimeout);
        if (!runner.isAborted()) {
            tmlScheduler.submit(runner, false);
        }
    } catch (Throwable e) {
        throw new APIException(e.getMessage(), e, APIErrorCode.InternalError);
    }
}
Also used : OAuthToken(com.dexels.oauth.api.OAuthToken) APIException(com.dexels.navajo.article.APIException) Access(com.dexels.navajo.script.api.Access) ArticleTmlRunnable(com.dexels.navajo.article.runnable.ArticleTmlRunnable) Client(com.dexels.oauth.api.Client) File(java.io.File) Date(java.util.Date) ArticleRuntime(com.dexels.navajo.article.ArticleRuntime)

Example 8 with APIException

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

the class ServiceCommand method performCall.

protected Navajo performCall(ArticleRuntime runtime, String name, Navajo n, String instance) throws APIException {
    try {
        Navajo result = dispatcher.handle(n, instance, true);
        handleError(result);
        return result;
    } catch (UserException | AuthorizationException | FatalException e) {
        throw new APIException(e.getMessage(), e, APIErrorCode.InternalError);
    } catch (ConditionErrorException e) {
        throw new APIException(e.getMessage(), e, APIErrorCode.ConditionError);
    }
}
Also used : ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) APIException(com.dexels.navajo.article.APIException) AuthorizationException(com.dexels.navajo.script.api.AuthorizationException) FatalException(com.dexels.navajo.script.api.FatalException) Navajo(com.dexels.navajo.document.Navajo) UserException(com.dexels.navajo.script.api.UserException)

Example 9 with APIException

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

the class ArticleListServlet method doServiceImpl.

protected void doServiceImpl(HttpServletRequest request, HttpServletResponse response) throws APIException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode rootNode = mapper.createObjectNode();
    boolean extended = request.getParameter(ARGUMENT_EXTENDED) != null ? true : false;
    String requestedArticle = request.getParameter(ARGUMENT_ARTICLE);
    ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
    if (requestedArticle != null) {
        getContext().writeArticleMeta(requestedArticle, rootNode, mapper, extended);
    } else {
        List<String> articles = getContext().listArticles();
        for (String article : articles) {
            getContext().writeArticleMeta(article, rootNode, mapper, extended);
        }
    }
    try (PrintWriter pw = response.getWriter()) {
        writer.writeValue(pw, rootNode);
    } catch (IOException e) {
        throw new APIException("Autoclose on printwriter failed", e, APIErrorCode.InternalError);
    }
}
Also used : APIException(com.dexels.navajo.article.APIException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter)

Example 10 with APIException

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

the class BaseRuntimeImpl method getGroupNode.

public ObjectNode getGroupNode(ObjectNode parent, String name) throws APIException {
    JsonNode existing = parent.get(name);
    if (existing != null) {
        if (existing instanceof ObjectNode) {
            return (ObjectNode) existing;
        } else {
            throw new APIException("Error getting group node: " + name + " there is an existing node, but it is not an ObjectNode", null, APIErrorCode.InternalError);
        }
    }
    ObjectNode result = mapper.createObjectNode();
    rootNode.set(name, result);
    return result;
}
Also used : APIException(com.dexels.navajo.article.APIException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode)

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