Search in sources :

Example 11 with HandlerWrapper

use of io.undertow.server.HandlerWrapper in project undertow by undertow-io.

the class BypassServletTestCase method setup.

@BeforeClass
public static void setup() throws ServletException {
    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServlet(new ServletInfo("servlet", MessageServlet.class).addMapping("/").addInitParam(MessageServlet.MESSAGE, "This is a servlet")).addListener(new ListenerInfo(TestListener.class)).addInitialHandlerChainWrapper(new HandlerWrapper() {

        @Override
        public HttpHandler wrap(final HttpHandler handler) {
            return new HttpHandler() {

                @Override
                public void handleRequest(final HttpServerExchange exchange) throws Exception {
                    if (exchange.getRelativePath().equals("/async")) {
                        exchange.getResponseSender().send("This is not a servlet", IoCallback.END_EXCHANGE);
                    } else {
                        handler.handleRequest(exchange);
                    }
                }
            };
        }
    });
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) DeploymentManager(io.undertow.servlet.api.DeploymentManager) PathHandler(io.undertow.server.handlers.PathHandler) HandlerWrapper(io.undertow.server.HandlerWrapper) SimpleServletTestCase(io.undertow.servlet.test.SimpleServletTestCase) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServletInfo(io.undertow.servlet.api.ServletInfo) HttpServerExchange(io.undertow.server.HttpServerExchange) ListenerInfo(io.undertow.servlet.api.ListenerInfo) ServletContainer(io.undertow.servlet.api.ServletContainer) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) MessageServlet(io.undertow.servlet.test.util.MessageServlet) BeforeClass(org.junit.BeforeClass)

Example 12 with HandlerWrapper

use of io.undertow.server.HandlerWrapper in project wildfly by wildfly.

the class ListenerAdd method performRuntime.

@Override
protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model) throws OperationFailedException {
    final PathAddress address = context.getCurrentAddress();
    final PathAddress parent = address.getParent();
    final String name = context.getCurrentAddressValue();
    final String bindingRef = ListenerResourceDefinition.SOCKET_BINDING.resolveModelAttribute(context, model).asString();
    final String workerName = ListenerResourceDefinition.WORKER.resolveModelAttribute(context, model).asString();
    final String bufferPoolName = ListenerResourceDefinition.BUFFER_POOL.resolveModelAttribute(context, model).asString();
    final boolean enabled = ListenerResourceDefinition.ENABLED.resolveModelAttribute(context, model).asBoolean();
    final boolean peerHostLookup = ListenerResourceDefinition.RESOLVE_PEER_ADDRESS.resolveModelAttribute(context, model).asBoolean();
    final boolean secure = ListenerResourceDefinition.SECURE.resolveModelAttribute(context, model).asBoolean();
    OptionMap listenerOptions = OptionList.resolveOptions(context, model, ListenerResourceDefinition.LISTENER_OPTIONS);
    OptionMap socketOptions = OptionList.resolveOptions(context, model, ListenerResourceDefinition.SOCKET_OPTIONS);
    String serverName = parent.getLastElement().getValue();
    final ServiceName listenerServiceName = UndertowService.listenerName(name);
    final ListenerService service = createService(name, serverName, context, model, listenerOptions, socketOptions);
    if (peerHostLookup) {
        service.addWrapperHandler(new HandlerWrapper() {

            @Override
            public HttpHandler wrap(HttpHandler handler) {
                return new PeerNameResolvingHandler(handler);
            }
        });
    }
    service.setEnabled(enabled);
    if (secure) {
        service.addWrapperHandler(MarkSecureHandler.WRAPPER);
    }
    List<String> disallowedMethods = ListenerResourceDefinition.DISALLOWED_METHODS.unwrap(context, model);
    if (!disallowedMethods.isEmpty()) {
        final Set<HttpString> methodSet = new HashSet<>();
        for (String i : disallowedMethods) {
            methodSet.add(new HttpString(i.trim()));
        }
        service.addWrapperHandler(new HandlerWrapper() {

            @Override
            public HttpHandler wrap(HttpHandler handler) {
                return new DisallowedMethodsHandler(handler, methodSet);
            }
        });
    }
    final ServiceName socketBindingServiceName = context.getCapabilityServiceName(ListenerResourceDefinition.SOCKET_CAPABILITY, bindingRef, SocketBinding.class);
    final ServiceName workerServiceName = context.getCapabilityServiceName(ListenerResourceDefinition.IO_WORKER_CAPABILITY, workerName, XnioWorker.class);
    final ServiceName bufferPoolServiceName = context.getCapabilityServiceName(ListenerResourceDefinition.IO_BUFFER_POOL_CAPABILITY, bufferPoolName, Pool.class);
    final ServiceBuilder<? extends UndertowListener> serviceBuilder = context.getServiceTarget().addService(listenerServiceName, service);
    serviceBuilder.addDependency(workerServiceName, XnioWorker.class, service.getWorker()).addDependency(socketBindingServiceName, SocketBinding.class, service.getBinding()).addDependency(bufferPoolServiceName, (Injector) service.getBufferPool()).addDependency(UndertowService.SERVER.append(serverName), Server.class, service.getServerService()).addAliases(ListenerResourceDefinition.LISTENER_CAPABILITY.getCapabilityServiceName(name));
    configureAdditionalDependencies(context, serviceBuilder, model, service);
    serviceBuilder.install();
}
Also used : SocketBinding(org.jboss.as.network.SocketBinding) HttpHandler(io.undertow.server.HttpHandler) DisallowedMethodsHandler(io.undertow.server.handlers.DisallowedMethodsHandler) PeerNameResolvingHandler(io.undertow.server.handlers.PeerNameResolvingHandler) HttpString(io.undertow.util.HttpString) HandlerWrapper(io.undertow.server.HandlerWrapper) ServiceName(org.jboss.msc.service.ServiceName) PathAddress(org.jboss.as.controller.PathAddress) OptionMap(org.xnio.OptionMap) HashSet(java.util.HashSet) HttpString(io.undertow.util.HttpString)

Aggregations

HandlerWrapper (io.undertow.server.HandlerWrapper)12 HttpHandler (io.undertow.server.HttpHandler)9 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)5 ServletInfo (io.undertow.servlet.api.ServletInfo)4 ServletExtension (io.undertow.servlet.ServletExtension)3 ListenerInfo (io.undertow.servlet.api.ListenerInfo)3 LoginConfig (io.undertow.servlet.api.LoginConfig)3 AuthenticationMechanismFactory (io.undertow.security.api.AuthenticationMechanismFactory)2 NotificationReceiver (io.undertow.security.api.NotificationReceiver)2 AuthMethodConfig (io.undertow.servlet.api.AuthMethodConfig)2 ErrorPage (io.undertow.servlet.api.ErrorPage)2 FilterInfo (io.undertow.servlet.api.FilterInfo)2 MimeMapping (io.undertow.servlet.api.MimeMapping)2 SecurityConstraint (io.undertow.servlet.api.SecurityConstraint)2 ServletContainerInitializerInfo (io.undertow.servlet.api.ServletContainerInitializerInfo)2 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2