Search in sources :

Example 1 with Predicate

use of io.undertow.predicate.Predicate in project spring-boot by spring-projects.

the class UndertowServletWebServer method getCompressionPredicates.

private Predicate[] getCompressionPredicates(Compression compression) {
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(new MaxSizePredicate(compression.getMinResponseSize()));
    predicates.add(new CompressibleMimeTypePredicate(compression.getMimeTypes()));
    if (compression.getExcludedUserAgents() != null) {
        for (String agent : compression.getExcludedUserAgents()) {
            RequestHeaderAttribute agentHeader = new RequestHeaderAttribute(new HttpString(HttpHeaders.USER_AGENT));
            predicates.add(Predicates.not(Predicates.regex(agentHeader, agent)));
        }
    }
    return predicates.toArray(new Predicate[predicates.size()]);
}
Also used : ArrayList(java.util.ArrayList) HttpString(io.undertow.util.HttpString) RequestHeaderAttribute(io.undertow.attribute.RequestHeaderAttribute) Predicate(io.undertow.predicate.Predicate) HttpString(io.undertow.util.HttpString)

Example 2 with Predicate

use of io.undertow.predicate.Predicate in project wildfly by wildfly.

the class AccessLogAdd method performRuntime.

@Override
protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model) throws OperationFailedException {
    final PathAddress address = context.getCurrentAddress();
    final PathAddress hostAddress = address.getParent();
    final PathAddress serverAddress = hostAddress.getParent();
    final String worker = AccessLogDefinition.WORKER.resolveModelAttribute(context, model).asString();
    final String pattern = AccessLogDefinition.PATTERN.resolveModelAttribute(context, model).asString();
    final String directory = AccessLogDefinition.DIRECTORY.resolveModelAttribute(context, model).asString();
    final String filePrefix = AccessLogDefinition.PREFIX.resolveModelAttribute(context, model).asString();
    final String fileSuffix = AccessLogDefinition.SUFFIX.resolveModelAttribute(context, model).asString();
    final boolean useServerLog = AccessLogDefinition.USE_SERVER_LOG.resolveModelAttribute(context, model).asBoolean();
    final boolean rotate = AccessLogDefinition.ROTATE.resolveModelAttribute(context, model).asBoolean();
    final boolean extended = AccessLogDefinition.EXTENDED.resolveModelAttribute(context, model).asBoolean();
    final ModelNode relativeToNode = AccessLogDefinition.RELATIVE_TO.resolveModelAttribute(context, model);
    final String relativeTo = relativeToNode.isDefined() ? relativeToNode.asString() : null;
    Predicate predicate = null;
    ModelNode predicateNode = AccessLogDefinition.PREDICATE.resolveModelAttribute(context, model);
    if (predicateNode.isDefined()) {
        predicate = Predicates.parse(predicateNode.asString(), getClass().getClassLoader());
    }
    final AccessLogService service;
    if (useServerLog) {
        service = new AccessLogService(pattern, extended, predicate);
    } else {
        service = new AccessLogService(pattern, directory, relativeTo, filePrefix, fileSuffix, rotate, extended, predicate);
    }
    final String serverName = serverAddress.getLastElement().getValue();
    final String hostName = hostAddress.getLastElement().getValue();
    final ServiceName serviceName = UndertowService.accessLogServiceName(serverName, hostName);
    final ServiceBuilder<AccessLogService> builder = context.getServiceTarget().addService(serviceName, service).addDependency(IOServices.WORKER.append(worker), XnioWorker.class, service.getWorker()).addDependency(PathManagerService.SERVICE_NAME, PathManager.class, service.getPathManager()).addDependency(UndertowService.virtualHostName(serverName, hostName), Host.class, service.getHost());
    builder.setInitialMode(ServiceController.Mode.ACTIVE).install();
}
Also used : PathManager(org.jboss.as.controller.services.path.PathManager) ServiceName(org.jboss.msc.service.ServiceName) PathAddress(org.jboss.as.controller.PathAddress) ModelNode(org.jboss.dmr.ModelNode) Predicate(io.undertow.predicate.Predicate)

Example 3 with Predicate

use of io.undertow.predicate.Predicate in project undertow by undertow-io.

the class PredicatedHandlersParser method handlePredicateOperatorNode.

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);
}
Also used : HandlerWrapper(io.undertow.server.HandlerWrapper) Predicate(io.undertow.predicate.Predicate)

Example 4 with Predicate

use of io.undertow.predicate.Predicate in project undertow by undertow-io.

the class HttpContinueAcceptingHandlerTestCase method setup.

@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);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Predicate(io.undertow.predicate.Predicate) BeforeClass(org.junit.BeforeClass)

Example 5 with Predicate

use of io.undertow.predicate.Predicate in project wildfly by wildfly.

the class GlobalRequestControllerHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    RunResult result = entryPoint.beginRequest();
    try {
        if (result == RunResult.RUN) {
            next.handleRequest(exchange);
        } else {
            boolean allowed = false;
            for (Predicate allow : allowSuspendedRequests) {
                if (allow.resolve(exchange)) {
                    allowed = true;
                    ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
                    if (src != null) {
                        src.getServletRequest().setAttribute(ORG_WILDFLY_SUSPENDED, "true");
                    }
                    next.handleRequest(exchange);
                    break;
                }
            }
            if (!allowed) {
                exchange.setStatusCode(503);
                exchange.endExchange();
            }
        }
    } finally {
        if (result == RunResult.RUN && (exchange.isComplete() || !exchange.isDispatched())) {
            entryPoint.requestComplete();
        } else if (result == RunResult.RUN) {
            exchange.addExchangeCompleteListener(listener);
        }
    }
}
Also used : ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) RunResult(org.wildfly.extension.requestcontroller.RunResult) Predicate(io.undertow.predicate.Predicate)

Aggregations

Predicate (io.undertow.predicate.Predicate)6 ModelNode (org.jboss.dmr.ModelNode)2 RequestHeaderAttribute (io.undertow.attribute.RequestHeaderAttribute)1 HandlerWrapper (io.undertow.server.HandlerWrapper)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)1 HttpString (io.undertow.util.HttpString)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 SSLContext (javax.net.ssl.SSLContext)1 PathAddress (org.jboss.as.controller.PathAddress)1 PathManager (org.jboss.as.controller.services.path.PathManager)1 ServiceName (org.jboss.msc.service.ServiceName)1 BeforeClass (org.junit.BeforeClass)1 RunResult (org.wildfly.extension.requestcontroller.RunResult)1 OptionMap (org.xnio.OptionMap)1