Search in sources :

Example 1 with Access

use of com.dexels.navajo.script.api.Access in project navajo by Dexels.

the class ASTReactiveScriptNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String originalExpression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    int start = hasHeader ? headers : 0;
    if (hasHeader) {
        for (int i = 0; i < headers; i++) {
            ASTKeyValueNode hdr = (ASTKeyValueNode) jjtGetChild(i);
            NamedExpression ne = (NamedExpression) hdr.interpretToLambda(problems, originalExpression, functionClassifier, mapResolver);
            String key = ne.name;
            headerMap.put(key, ne.apply());
        }
    }
    int count = jjtGetNumChildren();
    List<Node> unnamedPipes = new ArrayList<>();
    Map<String, Node> namedPipes = new HashMap<>();
    for (int i = start; i < count; i++) {
        Node child = jjtGetChild(i);
        // ASTReactivePipe pipe = null;
        if (child instanceof ASTPipeDefinition) {
            unnamedPipes.add((ASTPipeDefinition) child);
        } else if (child instanceof ASTKeyValueNode) {
            ASTKeyValueNode kvNode = (ASTKeyValueNode) child;
            String streamName = kvNode.val;
            Node namedPipe = kvNode.jjtGetChild(0);
            // assert value types perhaps? TODO
            namedPipes.put(streamName, namedPipe);
        }
    }
    List<ReactivePipeNode> pipes = unnamedPipes.stream().map(p -> (ReactivePipeNode) p.interpretToLambda(problems, originalExpression, functionClassifier, name -> {
        Optional<Node> initial = Optional.ofNullable(namedPipes.get(name));
        if (initial.isPresent()) {
            return initial;
        } else {
            return mapResolver.apply(name);
        }
    })).collect(Collectors.toList());
    return new ContextExpression() {

        @Override
        public Optional<String> returnType() {
            return Optional.of(Reactive.ReactiveItemType.REACTIVE_SCRIPT.toString());
        }

        @Override
        public boolean isLiteral() {
            return true;
        }

        @Override
        public String expression() {
            return "";
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            return Operand.ofCustom(pipes, Reactive.ReactiveItemType.REACTIVE_SCRIPT.toString());
        }
    };
}
Also used : TipiLink(com.dexels.navajo.expression.api.TipiLink) Arrays(java.util.Arrays) Access(com.dexels.navajo.script.api.Access) Message(com.dexels.navajo.document.Message) Reactive(com.dexels.navajo.reactive.api.Reactive) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) List(java.util.List) Operand(com.dexels.navajo.document.Operand) Selection(com.dexels.navajo.document.Selection) FunctionClassification(com.dexels.navajo.expression.api.FunctionClassification) NamedExpression(com.dexels.navajo.parser.NamedExpression) ReactivePipeNode(com.dexels.navajo.parser.compiled.api.ReactivePipeNode) Map(java.util.Map) Optional(java.util.Optional) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Collections(java.util.Collections) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) Navajo(com.dexels.navajo.document.Navajo) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) Optional(java.util.Optional) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) HashMap(java.util.HashMap) Selection(com.dexels.navajo.document.Selection) ReactivePipeNode(com.dexels.navajo.parser.compiled.api.ReactivePipeNode) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ArrayList(java.util.ArrayList) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) ReactivePipeNode(com.dexels.navajo.parser.compiled.api.ReactivePipeNode) TipiLink(com.dexels.navajo.expression.api.TipiLink) NamedExpression(com.dexels.navajo.parser.NamedExpression)

Example 2 with Access

use of com.dexels.navajo.script.api.Access in project navajo by Dexels.

the class ASTDatePatternNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    ContextExpression y = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Year (item 0) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression m = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Month (item 1) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression d = jjtGetChild(2).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Day (item 2) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression h = jjtGetChild(3).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Hour (item 3) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression min = jjtGetChild(4).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Minute (item 4) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    ContextExpression s = jjtGetChild(5).interpretToLambda(problems, expression, functionClassifier, mapResolver);
    checkOrAdd("Second (item 5) field should be an integer", problems, y.returnType(), Property.INTEGER_PROPERTY);
    final boolean isLiteral = y.isLiteral() && m.isLiteral() && d.isLiteral() && h.isLiteral() && min.isLiteral() && s.isLiteral();
    return new ContextExpression() {

        @Override
        public boolean isLiteral() {
            return isLiteral;
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            int yearT = ((Integer) y.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int monthT = ((Integer) m.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int dayT = ((Integer) d.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int hourT = ((Integer) h.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int minT = ((Integer) min.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            int secT = ((Integer) s.apply(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage).value).intValue();
            return Operand.ofDatePattern(new DatePattern(yearT, monthT, dayT, hourT, minT, secT, true));
        }

        @Override
        public Optional<String> returnType() {
            return Optional.of(Property.DATE_PATTERN_PROPERTY);
        }

        @Override
        public String expression() {
            return expression;
        }
    };
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Optional(java.util.Optional) Selection(com.dexels.navajo.document.Selection) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) TipiLink(com.dexels.navajo.expression.api.TipiLink) DatePattern(com.dexels.navajo.document.types.DatePattern)

Example 3 with Access

use of com.dexels.navajo.script.api.Access in project navajo by Dexels.

the class ASTForAllNode method interpretToLambda.

@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    return new ContextExpression() {

        @Override
        public boolean isLiteral() {
            return false;
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            List<String> problems = new ArrayList<>();
            ContextExpression a = jjtGetChild(0).interpretToLambda(problems, expression, functionClassifier, mapResolver);
            ContextExpression b = jjtGetChild(1).interpretToLambda(problems, expression, functionClassifier, mapResolver);
            if (!problems.isEmpty()) {
                throw new TMLExpressionException(problems, expression);
            }
            return interpret(doc, parentMsg, parentParamMsg, parentSel, mapNode, tipiLink, access, immutableMessage, paramMessage, a, b);
        }

        @Override
        public Optional<String> returnType() {
            return Optional.empty();
        }

        @Override
        public String expression() {
            return expression;
        }
    };
}
Also used : MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) TipiLink(com.dexels.navajo.expression.api.TipiLink) Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Optional(java.util.Optional) Selection(com.dexels.navajo.document.Selection) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Access(com.dexels.navajo.script.api.Access) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 4 with Access

use of com.dexels.navajo.script.api.Access 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 5 with Access

use of com.dexels.navajo.script.api.Access in project navajo by Dexels.

the class DomainObjectMapperTest method testStoreWithMultipleSelectionProperty.

@Test
public void testStoreWithMultipleSelectionProperty() throws Exception {
    // The other way around, with an input Navajo with property that does
    // have an associated attribute.
    Navajo doc = NavajoFactory.getInstance().createNavajo();
    Message m = NavajoFactory.getInstance().createMessage(doc, "MyMessage");
    doc.addMessage(m);
    Property p1 = NavajoFactory.getInstance().createProperty(doc, "Id", "string", "hello", 0, "", "in");
    Property p2 = NavajoFactory.getInstance().createProperty(doc, "Selection", "+", "", "in");
    m.addProperty(p1);
    m.addProperty(p2);
    // Add selections...
    Selection s1 = NavajoFactory.getInstance().createSelection(doc, "aap", "AAP", true);
    Selection s2 = NavajoFactory.getInstance().createSelection(doc, "noot", "NOOT", true);
    Selection s3 = NavajoFactory.getInstance().createSelection(doc, "mies", "MIES", false);
    p2.addSelection(s1);
    p2.addSelection(s2);
    p2.addSelection(s3);
    Access a = new Access();
    a.setInDoc(doc);
    DomainObjectMapper dom2 = new DomainObjectMapper();
    dom2.setCurrentMessageName("MyMessage");
    dom2.load(a);
    dom2.setObjectName("com.dexels.navajo.mapping.bean.Relation");
    boolean exception = false;
    try {
        dom2.store();
    } catch (Exception e) {
        exception = true;
    }
    // Multiple cardinality is not yet supported, hence exception.
    assertTrue(exception);
// Object o = dom2.getMyObject();
// assertNotNull(o);
// assertEquals(Relation.class, o.getClass());
// assertEquals("hello", ((Relation) o).getId());
// assertEquals("NOOT", ((Relation) o).getSelection());
}
Also used : Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) Test(org.junit.Test)

Aggregations

Access (com.dexels.navajo.script.api.Access)45 Navajo (com.dexels.navajo.document.Navajo)29 Message (com.dexels.navajo.document.Message)27 Test (org.junit.Test)18 Property (com.dexels.navajo.document.Property)15 Selection (com.dexels.navajo.document.Selection)14 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)12 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)12 TipiLink (com.dexels.navajo.expression.api.TipiLink)12 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)12 Optional (java.util.Optional)12 Operand (com.dexels.navajo.document.Operand)10 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)10 ArrayList (java.util.ArrayList)8 UserException (com.dexels.navajo.script.api.UserException)6 ResultMessage (com.dexels.navajo.adapter.messagemap.ResultMessage)5 Binary (com.dexels.navajo.document.types.Binary)4 HttpResource (com.dexels.navajo.resource.http.HttpResource)4 HttpResourceFactory (com.dexels.navajo.resource.http.HttpResourceFactory)4 Dispatcher (com.dexels.navajo.server.Dispatcher)4