io.undertow.predicate.PredicateBuilder Java Examples

The following examples show how to use io.undertow.predicate.PredicateBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public static List<PredicatedHandler> parse(final String contents, final ClassLoader classLoader) {
    Deque<Token> tokens = tokenize(contents);

    Node node = parse(contents, tokens);
    Map<String, PredicateBuilder> predicateBuilders = loadPredicateBuilders(classLoader);
    Map<String, HandlerBuilder> handlerBuilders = loadHandlerBuilders(classLoader);

    final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
    return handleNode(contents, node, predicateBuilders, handlerBuilders, attributeParser);
}
 
Example #2
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static Map<String, PredicateBuilder> loadPredicateBuilders(final ClassLoader classLoader) {
    ServiceLoader<PredicateBuilder> loader = ServiceLoader.load(PredicateBuilder.class, classLoader);
    final Map<String, PredicateBuilder> ret = new HashMap<>();
    for (PredicateBuilder builder : loader) {
        if (ret.containsKey(builder.name())) {
            if (ret.get(builder.name()).getClass() != builder.getClass()) {
                throw UndertowMessages.MESSAGES.moreThanOnePredicateWithName(builder.name(), builder.getClass(), ret.get(builder.name()).getClass());
            }
        } else {
            ret.put(builder.name(), builder);
        }
    }
    return ret;
}
 
Example #3
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static Predicate handlePredicateExpressionNode(String contents, ExpressionNode node, Map<String, PredicateBuilder> handlerBuilders, ExchangeAttributeParser parser) {
    Token token = node.getToken();
    PredicateBuilder builder = handlerBuilders.get(token.getToken());
    if (builder == null) {
        throw error(contents, token.getPosition(), "no predicate named " + token.getToken() + " known predicates are " + handlerBuilders.keySet());
    }
    Map<String, Object> parameters = new HashMap<>();

    for(Map.Entry<String, Node> val : node.getValues().entrySet()) {
        String name = val.getKey();
        if(name == null) {
            if(builder.defaultParameter() == null) {
                throw error(contents, token.getPosition(), "default parameter not supported");
            }
            name = builder.defaultParameter();
        }
        Class<?> type = builder.parameters().get(name);
        if(type == null) {
            throw error(contents, val.getValue().getToken().getPosition(), "unknown parameter " + name);
        }
        if(val.getValue() instanceof ValueNode) {
            parameters.put(name, coerceToType(contents, val.getValue().getToken(), type, parser));
        } else if(val.getValue() instanceof ArrayNode) {
            parameters.put(name, readArrayType(contents, name, (ArrayNode)val.getValue(), parser, type));
        } else {
            throw error(contents, val.getValue().getToken().getPosition(), "unexpected node " + val.getValue());
        }
    }
    return builder.build(parameters);
}
 
Example #4
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static List<PredicatedHandler> handleBlockNode(String contents, BlockNode node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
    List<PredicatedHandler> ret = new ArrayList<>();
    for(Node line : node.getBlock()) {
        ret.addAll(handleNode(contents, line, predicateBuilders, handlerBuilders, parser));
    }
    return ret;

}
 
Example #5
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static HandlerWrapper handlePredicatedAction(String contents, Node node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
    if(node instanceof ExpressionNode) {
        return handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, parser);
    } else if(node instanceof BlockNode) {
        List<PredicatedHandler> handlers = handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, parser);
        return  new PredicatesHandler.Wrapper(handlers, false);
    } else {
        throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
    }
}
 
Example #6
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static PredicatedHandler handlePredicateOperatorNode(String contents, PredicateOperatorNode node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
    Predicate predicate = handlePredicateNode(contents, node.getLeft(), predicateBuilders, parser);
    HandlerWrapper ret = handlePredicatedAction(contents, node.getRight(), predicateBuilders, handlerBuilders, parser);
    HandlerWrapper elseBranch = null;
    if(node.getElseBranch() != null) {
        elseBranch = handlePredicatedAction(contents, node.getElseBranch(), predicateBuilders, handlerBuilders, parser);
    }
    return new PredicatedHandler(predicate, ret, elseBranch);
}
 
Example #7
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static List<PredicatedHandler> handleNode(String contents, Node node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser attributeParser) {
    if(node instanceof BlockNode) {
        return handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, attributeParser);
    } else if(node instanceof ExpressionNode) {
        HandlerWrapper handler =  handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, attributeParser);
        return Collections.singletonList(new PredicatedHandler(Predicates.truePredicate(), handler));
    } else if(node instanceof PredicateOperatorNode) {
        return Collections.singletonList(handlePredicateOperatorNode(contents, (PredicateOperatorNode)node, predicateBuilders, handlerBuilders, attributeParser));
    } else {
        throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
    }
}
 
Example #8
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static Predicate parsePredicate(String string, ClassLoader classLoader) {
    Deque<Token> tokens = tokenize(string);
    Node node = parse(string, tokens);
    Map<String, PredicateBuilder> predicateBuilders = loadPredicateBuilders(classLoader);
    final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
    return handlePredicateNode(string, node, predicateBuilders, attributeParser);
}
 
Example #9
Source File: PredicatedHandlersParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static List<PredicatedHandler> parse(final String contents, final ClassLoader classLoader) {
    Deque<Token> tokens = tokenize(contents);

    Node node = parse(contents, tokens);
    Map<String, PredicateBuilder> predicateBuilders = loadPredicateBuilders(classLoader);
    Map<String, HandlerBuilder> handlerBuilders = loadHandlerBuilders(classLoader);

    final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
    return handleNode(contents, node, predicateBuilders, handlerBuilders, attributeParser);
}
 
Example #10
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private static Map<String, PredicateBuilder> loadPredicateBuilders(final ClassLoader classLoader) {
    ServiceLoader<PredicateBuilder> loader = ServiceLoader.load(PredicateBuilder.class, classLoader);
    final Map<String, PredicateBuilder> ret = new HashMap<>();
    for (PredicateBuilder builder : loader) {
        if (ret.containsKey(builder.name())) {
            if (ret.get(builder.name()).getClass() != builder.getClass()) {
                throw UndertowMessages.MESSAGES.moreThanOnePredicateWithName(builder.name(), builder.getClass(), ret.get(builder.name()).getClass());
            }
        } else {
            ret.put(builder.name(), builder);
        }
    }
    return ret;
}
 
Example #11
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private static Predicate handlePredicateExpressionNode(String contents, ExpressionNode node, Map<String, PredicateBuilder> handlerBuilders, ExchangeAttributeParser parser) {
    Token token = node.getToken();
    PredicateBuilder builder = handlerBuilders.get(token.getToken());
    if (builder == null) {
        throw error(contents, token.getPosition(), "no predicate named " + token.getToken() + " known predicates are " + handlerBuilders.keySet());
    }
    Map<String, Object> parameters = new HashMap<>();

    for(Map.Entry<String, Node> val : node.getValues().entrySet()) {
        String name = val.getKey();
        if(name == null) {
            if(builder.defaultParameter() == null) {
                throw error(contents, token.getPosition(), "default parameter not supported");
            }
            name = builder.defaultParameter();
        }
        Class<?> type = builder.parameters().get(name);
        if(type == null) {
            throw error(contents, val.getValue().getToken().getPosition(), "unknown parameter " + name);
        }
        if(val.getValue() instanceof ValueNode) {
            parameters.put(name, coerceToType(contents, val.getValue().getToken(), type, parser));
        } else if(val.getValue() instanceof ArrayNode) {
            parameters.put(name, readArrayType(contents, name, (ArrayNode)val.getValue(), parser, type));
        } else {
            throw error(contents, val.getValue().getToken().getPosition(), "unexpected node " + val.getValue());
        }
    }
    return builder.build(parameters);
}
 
Example #12
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private static List<PredicatedHandler> handleBlockNode(String contents, BlockNode node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
    List<PredicatedHandler> ret = new ArrayList<>();
    for(Node line : node.getBlock()) {
        ret.addAll(handleNode(contents, line, predicateBuilders, handlerBuilders, parser));
    }
    return ret;

}
 
Example #13
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private static HandlerWrapper handlePredicatedAction(String contents, Node node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
    if(node instanceof ExpressionNode) {
        return handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, parser);
    } else if(node instanceof BlockNode) {
        List<PredicatedHandler> handlers = handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, parser);
        return  new PredicatesHandler.Wrapper(handlers, false);
    } else {
        throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
    }
}
 
Example #14
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private static PredicatedHandler handlePredicateOperatorNode(String contents, PredicateOperatorNode node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser parser) {
    Predicate predicate = handlePredicateNode(contents, node.getLeft(), predicateBuilders, parser);
    HandlerWrapper ret = handlePredicatedAction(contents, node.getRight(), predicateBuilders, handlerBuilders, parser);
    HandlerWrapper elseBranch = null;
    if(node.getElseBranch() != null) {
        elseBranch = handlePredicatedAction(contents, node.getElseBranch(), predicateBuilders, handlerBuilders, parser);
    }
    return new PredicatedHandler(predicate, ret, elseBranch);
}
 
Example #15
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
private static List<PredicatedHandler> handleNode(String contents, Node node, Map<String, PredicateBuilder> predicateBuilders, Map<String, HandlerBuilder> handlerBuilders, ExchangeAttributeParser attributeParser) {
    if(node instanceof BlockNode) {
        return handleBlockNode(contents, (BlockNode) node, predicateBuilders, handlerBuilders, attributeParser);
    } else if(node instanceof ExpressionNode) {
        HandlerWrapper handler =  handleHandlerNode(contents, (ExpressionNode) node, handlerBuilders, attributeParser);
        return Collections.singletonList(new PredicatedHandler(Predicates.truePredicate(), handler));
    } else if(node instanceof PredicateOperatorNode) {
        return Collections.singletonList(handlePredicateOperatorNode(contents, (PredicateOperatorNode)node, predicateBuilders, handlerBuilders, attributeParser));
    } else {
        throw error(contents, node.getToken().getPosition(), "unexpected token " + node.getToken());
    }
}
 
Example #16
Source File: PredicatedHandlersParser.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public static Predicate parsePredicate(String string, ClassLoader classLoader) {
    Deque<Token> tokens = tokenize(string);
    Node node = parse(string, tokens);
    Map<String, PredicateBuilder> predicateBuilders = loadPredicateBuilders(classLoader);
    final ExchangeAttributeParser attributeParser = ExchangeAttributes.parser(classLoader);
    return handlePredicateNode(string, node, predicateBuilders, attributeParser);
}
 
Example #17
Source File: HandlerListingTestCase.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Test
public void listPredicates() {
    out.println();
    out.println();
    out.println();
    out.println("predicates");
    ArrayList<PredicateBuilder> builds = new ArrayList<PredicateBuilder>();
    for (PredicateBuilder i : ServiceLoader.load(PredicateBuilder.class, getClass().getClassLoader())) {
        builds.add(i);
    }
    Collections.sort(builds, new Comparator<PredicateBuilder>() {
        @Override
        public int compare(PredicateBuilder o1, PredicateBuilder o2) {
            return o1.name().compareTo(o2.name());
        }
    });
    for (PredicateBuilder handler : builds) {
        out.print("|" + handler.name());
        out.print("\t|");

        List<String> parms = new ArrayList<>(handler.parameters().keySet());
        Collections.sort(parms);
        Iterator<String> it = parms.iterator();
        while (it.hasNext()) {
            String paramName = it.next();
            out.print(paramName + ": ");
            Class<?> obj = handler.parameters().get(paramName);
            if (obj == ExchangeAttribute.class) {
                out.print("attribute");
            } else if (obj.equals(ExchangeAttribute[].class)) {
                out.print("attribute[]");
            } else if (obj.equals(String.class)) {
                out.print("String");
            } else if (obj.equals(String[].class)) {
                out.print("String[]");
            } else if (obj.equals(Long.class)) {
                out.print("Long");
            } else if (obj.equals(Long[].class)) {
                out.print("Long[]");
            } else if (obj.equals(Boolean.class)) {
                out.print("Boolean");
            } else {
                out.print(obj);
            }
            if(handler.requiredParameters().contains(paramName)) {
                out.print(" (required)");
            }
            if (it.hasNext()) {
                out.print(", ");
            }
        }
        out.print("\t|");
        if(handler.defaultParameter() != null) {
            out.print(handler.defaultParameter());
        }
        out.print("\t|\n");
    }
}
 
Example #18
Source File: UndertowMessages.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Message(id = 44, value = "More than one predicate with name %s. Builder class %s and %s")
IllegalStateException moreThanOnePredicateWithName(String name, Class<? extends PredicateBuilder> aClass, Class<? extends PredicateBuilder> existing);
 
Example #19
Source File: UndertowMessages.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Message(id = 44, value = "More than one predicate with name %s. Builder class %s and %s")
IllegalStateException moreThanOnePredicateWithName(String name, Class<? extends PredicateBuilder> aClass, Class<? extends PredicateBuilder> existing);