io.undertow.predicate.Predicate Java Examples

The following examples show how to use io.undertow.predicate.Predicate. 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: ProteusHandler.java    From proteus with Apache License 2.0 6 votes vote down vote up
public synchronized ProteusHandler add(HttpString method, String template, Predicate predicate, HttpHandler handler)
{
    PathTemplateMatcher<RoutingMatch> matcher = matches.get(method);

    if (matcher == null) {
        matches.put(method, matcher = new PathTemplateMatcher<>());
    }

    RoutingMatch res = matcher.get(template);

    if (res == null) {
        matcher.add(template, res = new RoutingMatch());
    }

    if (allMethodsMatcher.get(template) == null) {
        allMethodsMatcher.add(template, res);
    }

    res.predicatedHandlers.add(new HandlerHolder(predicate, handler));

    return this;
}
 
Example #2
Source File: ProxyHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Builder setIdempotentRequestPredicate(Predicate idempotentRequestPredicate) {
    if(idempotentRequestPredicate == null) {
        throw UndertowMessages.MESSAGES.argumentCannotBeNull("idempotentRequestPredicate");
    }
    this.idempotentRequestPredicate = idempotentRequestPredicate;
    return this;
}
 
Example #3
Source File: ProxyHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
static void handleFailure(HttpServerExchange exchange, ProxyClientHandler proxyClientHandler, Predicate idempotentRequestPredicate, IOException e) {
    UndertowLogger.PROXY_REQUEST_LOGGER.proxyRequestFailed(exchange.getRequestURI(), e);
    if(exchange.isResponseStarted()) {
        IoUtils.safeClose(exchange.getConnection());
    } else if(idempotentRequestPredicate.resolve(exchange) && proxyClientHandler != null) {
        proxyClientHandler.failed(exchange); //this will attempt a retry if configured to do so
    } else {
        exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
        exchange.endExchange();
    }
}
 
Example #4
Source File: ProxyHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
ProxyAction(final ProxyConnection clientConnection, final HttpServerExchange exchange, Map<HttpString, ExchangeAttribute> requestHeaders,
            boolean rewriteHostHeader, boolean reuseXForwarded, ProxyClientHandler proxyClientHandler, Predicate idempotentPredicate) {
    this.clientConnection = clientConnection;
    this.exchange = exchange;
    this.requestHeaders = requestHeaders;
    this.rewriteHostHeader = rewriteHostHeader;
    this.reuseXForwarded = reuseXForwarded;
    this.proxyClientHandler = proxyClientHandler;
    this.idempotentPredicate = idempotentPredicate;
}
 
Example #5
Source File: AccessLogHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public AccessLogHandler(final HttpHandler next, final AccessLogReceiver accessLogReceiver, String formatString, final ExchangeAttribute attribute, Predicate predicate) {
    this.next = next;
    this.accessLogReceiver = accessLogReceiver;
    this.predicate = predicate;
    this.formatString = handleCommonNames(formatString);
    this.tokens = attribute;
}
 
Example #6
Source File: AccessLogHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public AccessLogHandler(final HttpHandler next, final AccessLogReceiver accessLogReceiver, final String formatString, ClassLoader classLoader, Predicate predicate) {
    this.next = next;
    this.accessLogReceiver = accessLogReceiver;
    this.predicate = predicate;
    this.formatString = handleCommonNames(formatString);
    this.tokens = ExchangeAttributes.parser(classLoader, new SubstituteEmptyWrapper("-")).parse(this.formatString);
}
 
Example #7
Source File: RoutingHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public synchronized RoutingHandler add(HttpString method, String template, Predicate predicate, HttpHandler handler) {
    PathTemplateMatcher<RoutingMatch> matcher = matches.get(method);
    if (matcher == null) {
        matches.put(method, matcher = new PathTemplateMatcher<>());
    }
    RoutingMatch res = matcher.get(template);
    if (res == null) {
        matcher.add(template, res = new RoutingMatch());
    }
    if (allMethodsMatcher.get(template) == null) {
        allMethodsMatcher.add(template, res);
    }
    res.predicatedHandlers.add(new HandlerHolder(predicate, handler));
    return this;
}
 
Example #8
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 #9
Source File: AccessLogHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public AccessLogHandler(final HttpHandler next, final AccessLogReceiver accessLogReceiver, String formatString, final ExchangeAttribute attribute, Predicate predicate) {
    this.next = next;
    this.accessLogReceiver = accessLogReceiver;
    this.predicate = predicate;
    this.formatString = handleCommonNames(formatString);
    this.tokens = attribute;
}
 
Example #10
Source File: AccessLogHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public AccessLogHandler(final HttpHandler next, final AccessLogReceiver accessLogReceiver, final String formatString, ClassLoader classLoader, Predicate predicate) {
    this.next = next;
    this.accessLogReceiver = accessLogReceiver;
    this.predicate = predicate;
    this.formatString = handleCommonNames(formatString);
    this.tokens = ExchangeAttributes.parser(classLoader, new SubstituteEmptyWrapper("-")).parse(this.formatString);
}
 
Example #11
Source File: PredicateContextAttribute.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    Map<String, Object> context = exchange.getAttachment(Predicate.PREDICATE_CONTEXT);
    if (context != null) {
        context.put(name, newValue);
    }
}
 
Example #12
Source File: PredicateContextAttribute.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public String readAttribute(final HttpServerExchange exchange) {
    Map<String, Object> context = exchange.getAttachment(Predicate.PREDICATE_CONTEXT);
    if (context != null) {
        Object object = context.get(name);
        return object == null ? null : object.toString();
    }
    return null;
}
 
Example #13
Source File: FilePredicate.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public Predicate build(final Map<String, Object> config) {
    ExchangeAttribute value = (ExchangeAttribute) config.get("value");
    Boolean requireContent = (Boolean)config.get("require-content");
    if(value == null) {
        value = ExchangeAttributes.relativePath();
    }
    return new FilePredicate(value, requireContent == null ? false : requireContent);
}
 
Example #14
Source File: DirectoryPredicate.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public Predicate build(final Map<String, Object> config) {
    ExchangeAttribute value = (ExchangeAttribute) config.get("value");
    if(value == null) {
        value = ExchangeAttributes.relativePath();
    }
    return new DirectoryPredicate(value);
}
 
Example #15
Source File: ProxyHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
ProxyClientHandler(HttpServerExchange exchange, ProxyClient.ProxyTarget target, long timeout, int maxRetryAttempts, Predicate idempotentPredicate) {
    this.exchange = exchange;
    this.timeout = timeout;
    this.maxRetryAttempts = maxRetryAttempts;
    this.target = target;
    this.idempotentPredicate = idempotentPredicate;
}
 
Example #16
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 #17
Source File: ProxyHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private HTTPTrailerChannelListener(final Attachable source, final Attachable target, HttpServerExchange exchange, ProxyClientHandler proxyClientHandler, Predicate idempotentPredicate) {
    this.source = source;
    this.target = target;
    this.exchange = exchange;
    this.proxyClientHandler = proxyClientHandler;
    this.idempotentPredicate = idempotentPredicate;
}
 
Example #18
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 #19
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 #20
Source File: RoutingHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public synchronized RoutingHandler add(String method, String template, Predicate predicate, HttpHandler handler) {
    PathTemplateMatcher<RoutingMatch> matcher = matches.get(method);
    if (matcher == null) {
        matches.put(method, matcher = new PathTemplateMatcher<>());
    }
    RoutingMatch res = matcher.get(template);
    if (res == null) {
        matcher.add(template, res = new RoutingMatch());
    }
    if (allMethodsMatcher.get(template) == null) {
        allMethodsMatcher.add(template, res);
    }
    res.predicatedHandlers.add(new HandlerHolder(predicate, handler));
    return this;
}
 
Example #21
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 #22
Source File: DirectoryPredicate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Predicate build(final Map<String, Object> config) {
    ExchangeAttribute value = (ExchangeAttribute) config.get("value");
    if(value == null) {
        value = ExchangeAttributes.relativePath();
    }
    return new DirectoryPredicate(value);
}
 
Example #23
Source File: FilePredicate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Predicate build(final Map<String, Object> config) {
    ExchangeAttribute value = (ExchangeAttribute) config.get("value");
    Boolean requireContent = (Boolean)config.get("require-content");
    if(value == null) {
        value = ExchangeAttributes.relativePath();
    }
    return new FilePredicate(value, requireContent == null ? false : requireContent);
}
 
Example #24
Source File: PredicateContextAttribute.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    Map<String, Object> context = exchange.getAttachment(Predicate.PREDICATE_CONTEXT);
    if (context != null) {
        context.put(name, newValue);
    }
}
 
Example #25
Source File: HttpContinueAcceptingHandlerTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    final HttpContinueAcceptingHandler handler = new HttpContinueAcceptingHandler(blockingHandler, new Predicate() {
        @Override
        public boolean resolve(HttpServerExchange value) {
            return accept;
        }
    });
    DefaultServer.setRootHandler(handler);
    blockingHandler.setRootHandler(new HttpHandler() {
        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                byte[] buffer = new byte[1024];
                final ByteArrayOutputStream b = new ByteArrayOutputStream();
                int r = 0;
                final OutputStream outputStream = exchange.getOutputStream();
                final InputStream inputStream =  exchange.getInputStream();
                while ((r = inputStream.read(buffer)) > 0) {
                    b.write(buffer, 0, r);
                }
                outputStream.write(b.toByteArray());
                outputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
 
Example #26
Source File: MaxRequestContentLengthPredicate.java    From proteus with Apache License 2.0 5 votes vote down vote up
@Override
public Predicate build(final Map<String, Object> config)
{
    Long max = (Long) config.get("value");

    return new MaxRequestContentLengthPredicate(max);
}
 
Example #27
Source File: PredicateContextAttribute.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String readAttribute(final HttpServerExchange exchange) {
    Map<String, Object> context = exchange.getAttachment(Predicate.PREDICATE_CONTEXT);
    if (context != null) {
        Object object = context.get(name);
        return object == null ? null : object.toString();
    }
    return null;
}
 
Example #28
Source File: ContentEncodedResourceManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ContentEncodedResourceManager(Path encodedResourcesRoot, CachingResourceManager encodedResourceManager, ContentEncodingRepository contentEncodingRepository, int minResourceSize, int maxResourceSize, Predicate encodingAllowed) {
    this.encodedResourcesRoot = encodedResourcesRoot;
    this.encoded = encodedResourceManager;
    this.contentEncodingRepository = contentEncodingRepository;
    this.minResourceSize = minResourceSize;
    this.maxResourceSize = maxResourceSize;
    this.encodingAllowed = encodingAllowed;
}
 
Example #29
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 #30
Source File: ConnectHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public ConnectHandler(HttpHandler next, Predicate allowed) {
    this.next = next;
    this.allowed = allowed;
}