Search in sources :

Example 46 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class AsyncContextImpl method dispatch.

@Override
public void dispatch() {
    if (dispatched) {
        throw UndertowServletMessages.MESSAGES.asyncRequestAlreadyDispatched();
    }
    final HttpServletRequestImpl requestImpl = this.servletRequestContext.getOriginalRequest();
    Deployment deployment = requestImpl.getServletContext().getDeployment();
    if (requestSupplied && servletRequest instanceof HttpServletRequest) {
        ServletContainer container = deployment.getServletContainer();
        final String requestURI = ((HttpServletRequest) servletRequest).getRequestURI();
        DeploymentManager context = container.getDeploymentByPath(requestURI);
        if (context == null) {
            throw UndertowServletMessages.MESSAGES.couldNotFindContextToDispatchTo(requestImpl.getOriginalContextPath());
        }
        String toDispatch = requestURI.substring(context.getDeployment().getServletContext().getContextPath().length());
        String qs = ((HttpServletRequest) servletRequest).getQueryString();
        if (qs != null && !qs.isEmpty()) {
            toDispatch = toDispatch + "?" + qs;
        }
        dispatch(context.getDeployment().getServletContext(), toDispatch);
    } else {
        // original request
        ServletContainer container = deployment.getServletContainer();
        DeploymentManager context = container.getDeploymentByPath(requestImpl.getOriginalContextPath());
        if (context == null) {
            // this should never happen
            throw UndertowServletMessages.MESSAGES.couldNotFindContextToDispatchTo(requestImpl.getOriginalContextPath());
        }
        // UNDERTOW-1591 use original, decoded value to dispatch, this should match: ServletInitialHandler.handleRequest
        String toDispatch = requestImpl.getExchange().getRelativePath();
        String qs = requestImpl.getOriginalQueryString();
        if (qs != null && !qs.isEmpty()) {
            toDispatch = toDispatch + "?" + qs;
        }
        dispatch(context.getDeployment().getServletContext(), toDispatch);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) Deployment(io.undertow.servlet.api.Deployment)

Example 47 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class ProgramaticAutobahnServer method run.

public void run() {
    Xnio xnio = Xnio.getInstance();
    try {
        XnioWorker worker = xnio.createWorker(OptionMap.builder().set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 10).set(Options.WORKER_TASK_MAX_THREADS, 12).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
        OptionMap serverOptions = OptionMap.builder().set(Options.TCP_NODELAY, true).set(Options.REUSE_ADDRESSES, true).getMap();
        DefaultByteBufferPool pool = new DefaultByteBufferPool(true, 8192);
        HttpOpenListener openListener = new HttpOpenListener(pool);
        ChannelListener acceptListener = ChannelListeners.openListenerAdapter(openListener);
        AcceptingChannel<StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(port), acceptListener, serverOptions);
        server.resumeAccepts();
        final ServletContainer container = ServletContainer.Factory.newInstance();
        DeploymentInfo builder = new DeploymentInfo().setClassLoader(ProgramaticAutobahnServer.class.getClassLoader()).setContextPath("/").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addFilter(new FilterInfo("filter", JsrWebSocketFilter.class)).addFilterUrlMapping("filter", "/*", DispatcherType.REQUEST).addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(pool).setWorker(worker).setDispatchToWorkerThread(true).addEndpoint(new ServerEndpointConfigImpl(ProgramaticAutobahnEndpoint.class, "/")).addExtension(new PerMessageDeflateHandshake()));
        deploymentManager = container.addDeployment(builder);
        deploymentManager.deploy();
        openListener.setRootHandler(deploymentManager.start());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) XnioWorker(org.xnio.XnioWorker) InetSocketAddress(java.net.InetSocketAddress) ServerEndpointConfigImpl(io.undertow.websockets.jsr.ServerEndpointConfigImpl) StreamConnection(org.xnio.StreamConnection) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) ServletException(javax.servlet.ServletException) Xnio(org.xnio.Xnio) JsrWebSocketFilter(io.undertow.websockets.jsr.JsrWebSocketFilter) OptionMap(org.xnio.OptionMap) ServletContainer(io.undertow.servlet.api.ServletContainer) PerMessageDeflateHandshake(io.undertow.websockets.extensions.PerMessageDeflateHandshake) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo)

Example 48 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class ClientEndpointReconnectTestCase method setup.

@BeforeClass
public static void setup() throws Exception {
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(ClientEndpointReconnectTestCase.class.getClassLoader()).setContextPath("/ws").setResourceManager(new TestResourceLoader(ClientEndpointReconnectTestCase.class)).setClassIntrospecter(TestClassIntrospector.INSTANCE).addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(new DefaultByteBufferPool(true, 8192)).setWorker(DefaultServer.getWorkerSupplier()).addEndpoint(DisconnectServerEndpoint.class).addEndpoint(AnnotatedClientReconnectEndpoint.class).addListener(containerReady -> deployment = containerReady).setReconnectHandler(new WebSocketReconnectHandler() {

        @Override
        public long disconnected(CloseReason closeReason, URI connectionUri, Session session, int disconnectCount) {
            if (disconnectCount < 3) {
                return 1;
            } else {
                return -1;
            }
        }

        @Override
        public long reconnectFailed(IOException exception, URI connectionUri, Session session, int failedCount) {
            failed = true;
            return -1;
        }
    })).setDeploymentName("servletContext.war");
    deploymentManager = container.addDeployment(builder);
    deploymentManager.deploy();
    DefaultServer.setRootHandler(Handlers.path().addPrefixPath("/ws", deploymentManager.start()));
}
Also used : AfterClass(org.junit.AfterClass) Handlers(io.undertow.Handlers) Session(javax.websocket.Session) BeforeClass(org.junit.BeforeClass) ServletException(javax.servlet.ServletException) TestClassIntrospector(io.undertow.servlet.test.util.TestClassIntrospector) CloseReason(javax.websocket.CloseReason) HttpOneOnly(io.undertow.testutils.HttpOneOnly) DefaultServer(io.undertow.testutils.DefaultServer) RunWith(org.junit.runner.RunWith) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) Test(org.junit.Test) IOException(java.io.IOException) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) ServletContainer(io.undertow.servlet.api.ServletContainer) DeploymentManager(io.undertow.servlet.api.DeploymentManager) WebSocketReconnectHandler(io.undertow.websockets.jsr.WebSocketReconnectHandler) TestResourceLoader(io.undertow.servlet.test.util.TestResourceLoader) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) URI(java.net.URI) Assert(org.junit.Assert) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) WebSocketReconnectHandler(io.undertow.websockets.jsr.WebSocketReconnectHandler) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) TestResourceLoader(io.undertow.servlet.test.util.TestResourceLoader) IOException(java.io.IOException) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) URI(java.net.URI) CloseReason(javax.websocket.CloseReason) ServletContainer(io.undertow.servlet.api.ServletContainer) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Session(javax.websocket.Session) BeforeClass(org.junit.BeforeClass)

Example 49 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class WebsocketBasicAuthTestCase method setup.

@BeforeClass
public static void setup() throws ServletException {
    final PathHandler path = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    ServletIdentityManager identityManager = new ServletIdentityManager();
    identityManager.addUser("user1", "password1", "role1");
    identityManager.addUser("charsetUser", "password-ΓΌ", "role1");
    LoginConfig loginConfig = new LoginConfig(REALM_NAME);
    Map<String, String> props = new HashMap<>();
    props.put("charset", "ISO_8859_1");
    props.put("user-agent-charsets", "Chrome,UTF-8,OPR,UTF-8");
    loginConfig.addFirstAuthMethod(new AuthMethodConfig("BASIC", props));
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").setIdentityManager(identityManager).setLoginConfig(loginConfig).addFilter(Servlets.filter("wrapper", WrapperFilter.class)).addFilterUrlMapping("wrapper", "/wrapper/*", DispatcherType.REQUEST).addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(DefaultServer.getBufferPool()).setWorker(DefaultServer.getWorker()).addEndpoint(SecuredEndpoint.class).addListener(containerReady -> deployment = containerReady));
    builder.addSecurityConstraint(new SecurityConstraint().addWebResourceCollection(new WebResourceCollection().addUrlPattern("/secured/*")).addRoleAllowed("role1").setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.DENY));
    deploymentManager = container.addDeployment(builder);
    deploymentManager.deploy();
    path.addPrefixPath(builder.getContextPath(), deploymentManager.start());
    DefaultServer.setRootHandler(path);
}
Also used : FilterChain(javax.servlet.FilterChain) ServletException(javax.servlet.ServletException) TestClassIntrospector(io.undertow.servlet.test.util.TestClassIntrospector) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) Endpoint(javax.websocket.Endpoint) MessageHandler(javax.websocket.MessageHandler) Map(java.util.Map) Filter(javax.servlet.Filter) URI(java.net.URI) BASIC(io.undertow.util.Headers.BASIC) AuthMethodConfig(io.undertow.servlet.api.AuthMethodConfig) AfterClass(org.junit.AfterClass) Session(javax.websocket.Session) ContainerProvider(javax.websocket.ContainerProvider) DefaultServer(io.undertow.testutils.DefaultServer) ServerEndpoint(javax.websocket.server.ServerEndpoint) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Principal(java.security.Principal) ServletResponse(javax.servlet.ServletResponse) SimpleServletTestCase(io.undertow.servlet.test.SimpleServletTestCase) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) SecurityInfo(io.undertow.servlet.api.SecurityInfo) BeforeClass(org.junit.BeforeClass) CloseReason(javax.websocket.CloseReason) HttpOneOnly(io.undertow.testutils.HttpOneOnly) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) RunWith(org.junit.runner.RunWith) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) HashMap(java.util.HashMap) Servlets(io.undertow.servlet.Servlets) OnOpen(javax.websocket.OnOpen) HttpServletRequest(javax.servlet.http.HttpServletRequest) PathHandler(io.undertow.server.handlers.PathHandler) AUTHORIZATION(io.undertow.util.Headers.AUTHORIZATION) SecurityConstraint(io.undertow.servlet.api.SecurityConstraint) ClientConfigurator(io.undertow.websockets.jsr.test.annotated.ClientConfigurator) ServletRequest(javax.servlet.ServletRequest) LoginConfig(io.undertow.servlet.api.LoginConfig) EndpointConfig(javax.websocket.EndpointConfig) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) ServletContainer(io.undertow.servlet.api.ServletContainer) DeploymentManager(io.undertow.servlet.api.DeploymentManager) TimeUnit(java.util.concurrent.TimeUnit) WebResourceCollection(io.undertow.servlet.api.WebResourceCollection) FilterConfig(javax.servlet.FilterConfig) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) DispatcherType(javax.servlet.DispatcherType) ServletIdentityManager(io.undertow.servlet.test.security.constraint.ServletIdentityManager) FlexBase64(io.undertow.util.FlexBase64) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) WebResourceCollection(io.undertow.servlet.api.WebResourceCollection) HashMap(java.util.HashMap) PathHandler(io.undertow.server.handlers.PathHandler) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) ServletIdentityManager(io.undertow.servlet.test.security.constraint.ServletIdentityManager) SimpleServletTestCase(io.undertow.servlet.test.SimpleServletTestCase) SecurityConstraint(io.undertow.servlet.api.SecurityConstraint) AuthMethodConfig(io.undertow.servlet.api.AuthMethodConfig) ServletContainer(io.undertow.servlet.api.ServletContainer) LoginConfig(io.undertow.servlet.api.LoginConfig) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) BeforeClass(org.junit.BeforeClass)

Example 50 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class AnnotatedAutobahnServer method run.

public void run() {
    Xnio xnio = Xnio.getInstance();
    DeploymentManager deploymentManager = null;
    try {
        XnioWorker worker = xnio.createWorker(OptionMap.builder().set(Options.WORKER_WRITE_THREADS, 4).set(Options.WORKER_READ_THREADS, 4).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 10).set(Options.WORKER_TASK_MAX_THREADS, 12).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
        OptionMap serverOptions = OptionMap.builder().set(Options.TCP_NODELAY, true).set(Options.REUSE_ADDRESSES, true).getMap();
        DefaultByteBufferPool pool = new DefaultByteBufferPool(true, 8024);
        HttpOpenListener openListener = new HttpOpenListener(pool);
        ChannelListener acceptListener = ChannelListeners.openListenerAdapter(openListener);
        AcceptingChannel<StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(port), acceptListener, serverOptions);
        server.resumeAccepts();
        final ServletContainer container = ServletContainer.Factory.newInstance();
        DeploymentInfo builder = new DeploymentInfo().setClassLoader(AnnotatedAutobahnServer.class.getClassLoader()).setContextPath("/").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo().setBuffers(pool).setWorker(worker).addEndpoint(AutobahnAnnotatedEndpoint.class).setDispatchToWorkerThread(true).addExtension(new PerMessageDeflateHandshake())).addFilter(new FilterInfo("filter", JsrWebSocketFilter.class)).addFilterUrlMapping("filter", "/*", DispatcherType.REQUEST);
        deploymentManager = container.addDeployment(builder);
        deploymentManager.deploy();
        openListener.setRootHandler(deploymentManager.start());
    } catch (Exception e) {
        log.error("failed to start server", e);
    } finally {
        if (deploymentManager != null) {
            deploymentManager.undeploy();
            try {
                deploymentManager.stop();
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : ChannelListener(org.xnio.ChannelListener) DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) DeploymentManager(io.undertow.servlet.api.DeploymentManager) XnioWorker(org.xnio.XnioWorker) InetSocketAddress(java.net.InetSocketAddress) StreamConnection(org.xnio.StreamConnection) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) Xnio(org.xnio.Xnio) OptionMap(org.xnio.OptionMap) ServletContainer(io.undertow.servlet.api.ServletContainer) PerMessageDeflateHandshake(io.undertow.websockets.extensions.PerMessageDeflateHandshake) HttpOpenListener(io.undertow.server.protocol.http.HttpOpenListener) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo)

Aggregations

ServletContainer (io.undertow.servlet.api.ServletContainer)92 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)88 DeploymentManager (io.undertow.servlet.api.DeploymentManager)78 PathHandler (io.undertow.server.handlers.PathHandler)76 BeforeClass (org.junit.BeforeClass)69 ServletInfo (io.undertow.servlet.api.ServletInfo)65 TestResourceLoader (io.undertow.servlet.test.util.TestResourceLoader)20 SimpleServletTestCase (io.undertow.servlet.test.SimpleServletTestCase)19 ServletException (javax.servlet.ServletException)19 FilterInfo (io.undertow.servlet.api.FilterInfo)18 LoginConfig (io.undertow.servlet.api.LoginConfig)18 ServletIdentityManager (io.undertow.servlet.test.security.constraint.ServletIdentityManager)16 Test (org.junit.Test)15 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)14 SecurityConstraint (io.undertow.servlet.api.SecurityConstraint)12 WebResourceCollection (io.undertow.servlet.api.WebResourceCollection)12 ListenerInfo (io.undertow.servlet.api.ListenerInfo)10 TestHttpClient (io.undertow.testutils.TestHttpClient)9 ServletSecurityInfo (io.undertow.servlet.api.ServletSecurityInfo)8 AuthMethodConfig (io.undertow.servlet.api.AuthMethodConfig)7