Search in sources :

Example 61 with Servlet

use of javax.servlet.Servlet in project undertow by undertow-io.

the class WebSocketServletTest method testText.

@Test
public void testText() throws Exception {
    final AtomicBoolean connected = new AtomicBoolean(false);
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentUtils.setupServlet(new ServletInfo("websocket", WebSocketServlet.class, new ImmediateInstanceFactory<Servlet>(new WebSocketServlet(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
            connected.set(true);
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
                    final String string = message.getData();
                    if (string.equals("hello")) {
                        WebSockets.sendText("world", channel, null);
                    } else {
                        WebSockets.sendText(string, channel, null);
                    }
                }
            });
            channel.resumeReceives();
        }
    }))).addMapping("/*"));
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(io.netty.handler.codec.http.websocketx.WebSocketVersion.V13, new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/servletContext/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(US_ASCII), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) WebSocketServlet(io.undertow.servlet.websockets.WebSocketServlet) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) ServletInfo(io.undertow.servlet.api.ServletInfo) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) ServletContainer(io.undertow.servlet.api.ServletContainer) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) WebSocketServlet(io.undertow.servlet.websockets.WebSocketServlet) Servlet(javax.servlet.Servlet) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Test(org.junit.Test)

Example 62 with Servlet

use of javax.servlet.Servlet in project wildfly by wildfly.

the class WebHostService method addWebDeployment.

@Override
public WebDeploymentController addWebDeployment(final WebDeploymentBuilder webDeploymentBuilder) throws Exception {
    DeploymentInfo d = new DeploymentInfo();
    d.setDeploymentName(webDeploymentBuilder.getContextRoot());
    d.setContextPath(webDeploymentBuilder.getContextRoot());
    d.setClassLoader(webDeploymentBuilder.getClassLoader());
    d.setResourceManager(new FileResourceManager(webDeploymentBuilder.getDocumentRoot().getCanonicalFile(), 1024 * 1024));
    d.setIgnoreFlush(false);
    for (ServletBuilder servlet : webDeploymentBuilder.getServlets()) {
        ServletInfo s;
        if (servlet.getServlet() == null) {
            s = new ServletInfo(servlet.getServletName(), (Class<? extends Servlet>) servlet.getServletClass());
        } else {
            s = new ServletInfo(servlet.getServletName(), (Class<? extends Servlet>) servlet.getServletClass(), new ImmediateInstanceFactory<>(servlet.getServlet()));
        }
        if (servlet.isForceInit()) {
            s.setLoadOnStartup(1);
        }
        s.addMappings(servlet.getUrlMappings());
        for (Map.Entry<String, String> param : servlet.getInitParams().entrySet()) {
            s.addInitParam(param.getKey(), param.getValue());
        }
        d.addServlet(s);
    }
    if (controlPoint != null) {
        d.addOuterHandlerChainWrapper(GlobalRequestControllerHandler.wrapper(controlPoint, webDeploymentBuilder.getAllowRequestPredicates()));
    }
    return new WebDeploymentControllerImpl(d);
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) FileResourceManager(io.undertow.server.handlers.resource.FileResourceManager) ServletBuilder(org.jboss.as.web.host.ServletBuilder) Servlet(javax.servlet.Servlet) ImmediateInstanceFactory(io.undertow.servlet.util.ImmediateInstanceFactory) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Map(java.util.Map)

Example 63 with Servlet

use of javax.servlet.Servlet in project atmosphere by Atmosphere.

the class AtmosphereResourceTest method testCompletionAwareForSyncButStartAsync.

@Test
public void testCompletionAwareForSyncButStartAsync() throws IOException, ServletException {
    Servlet s = mock(Servlet.class);
    framework.addInitParameter(ApplicationConfig.RESPONSE_COMPLETION_AWARE, "true");
    ReflectorServletProcessor handler = new ReflectorServletProcessor(s);
    handler.init(framework.getAtmosphereConfig());
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/a").build();
    AtmosphereResponseImpl response = mock(AtmosphereResponseImpl.class);
    AtmosphereResourceImpl res = new AtmosphereResourceImpl();
    res.initialize(framework.getAtmosphereConfig(), framework.getBroadcasterFactory().get(), request, response, null, null);
    res.transport(AtmosphereResource.TRANSPORT.WEBSOCKET);
    request.setAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE, res);
    request.setAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE, res);
    AsyncContext ac = request.startAsync();
    handler.onRequest(res);
    verify(response, times(0)).onComplete();
    ac.complete();
    verify(response, times(1)).onComplete();
}
Also used : Servlet(javax.servlet.Servlet) AsyncContext(javax.servlet.AsyncContext) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) Test(org.testng.annotations.Test)

Example 64 with Servlet

use of javax.servlet.Servlet in project gitiles by GerritCodeReview.

the class DevServer method createRootedDocServlet.

private Servlet createRootedDocServlet(DebugRenderer renderer, String docRoot) {
    File docRepo = new File(docRoot);
    FileKey repoKey = FileKey.exact(docRepo, FS.DETECTED);
    RepositoryResolver<HttpServletRequest> resolver = (req, name) -> {
        try {
            return RepositoryCache.open(repoKey, true);
        } catch (IOException e) {
            throw new RepositoryNotFoundException(repoKey.getFile(), e);
        }
    };
    return new RootedDocServlet(resolver, new RootedDocAccess(docRepo), renderer);
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) PathResource(org.eclipse.jetty.util.resource.PathResource) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) URISyntaxException(java.net.URISyntaxException) GitilesAccess(com.google.gitiles.GitilesAccess) LoggerFactory(org.slf4j.LoggerFactory) InetAddress(java.net.InetAddress) Strings(com.google.common.base.Strings) Config(org.eclipse.jgit.lib.Config) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) RepositoryDescription(com.google.gitiles.RepositoryDescription) Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) GitilesServlet(com.google.gitiles.GitilesServlet) RepositoryCache(org.eclipse.jgit.lib.RepositoryCache) Logger(org.slf4j.Logger) Servlet(javax.servlet.Servlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) PathServlet(com.google.gitiles.PathServlet) Set(java.util.Set) IOException(java.io.IOException) Constants(org.eclipse.jgit.lib.Constants) RepositoryResolver(org.eclipse.jgit.transport.resolver.RepositoryResolver) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) STATIC_PREFIX(com.google.gitiles.GitilesServlet.STATIC_PREFIX) UnknownHostException(java.net.UnknownHostException) FileKey(org.eclipse.jgit.lib.RepositoryCache.FileKey) File(java.io.File) RootedDocServlet(com.google.gitiles.RootedDocServlet) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Paths(java.nio.file.Paths) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) FS(org.eclipse.jgit.util.FS) DebugRenderer(com.google.gitiles.DebugRenderer) Collections(java.util.Collections) HttpServletRequest(javax.servlet.http.HttpServletRequest) FileKey(org.eclipse.jgit.lib.RepositoryCache.FileKey) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) File(java.io.File) RootedDocServlet(com.google.gitiles.RootedDocServlet)

Example 65 with Servlet

use of javax.servlet.Servlet in project gitiles by GerritCodeReview.

the class DevServer method appHandler.

private Handler appHandler() {
    DebugRenderer renderer = new DebugRenderer(STATIC_PREFIX, Arrays.asList(cfg.getStringList("gitiles", null, "customTemplates")), sourceRoot.resolve("gitiles-servlet/src/main/resources/com/google/gitiles/templates").toString(), firstNonNull(cfg.getString("gitiles", null, "siteTitle"), "Gitiles"));
    String docRoot = cfg.getString("gitiles", null, "docroot");
    Servlet servlet;
    if (!Strings.isNullOrEmpty(docRoot)) {
        servlet = createRootedDocServlet(renderer, docRoot);
    } else {
        servlet = new GitilesServlet(cfg, renderer, null, null, null, null, null, null, null);
    }
    ServletContextHandler handler = new ServletContextHandler();
    handler.setContextPath("");
    handler.addServlet(new ServletHolder(servlet), "/*");
    return handler;
}
Also used : DebugRenderer(com.google.gitiles.DebugRenderer) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) GitilesServlet(com.google.gitiles.GitilesServlet) Servlet(javax.servlet.Servlet) PathServlet(com.google.gitiles.PathServlet) RootedDocServlet(com.google.gitiles.RootedDocServlet) GitilesServlet(com.google.gitiles.GitilesServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

Servlet (javax.servlet.Servlet)86 Test (org.junit.Test)18 HttpServlet (javax.servlet.http.HttpServlet)16 ServletException (javax.servlet.ServletException)15 IOException (java.io.IOException)11 OptingServlet (org.apache.sling.api.servlets.OptingServlet)11 GenericServlet (javax.servlet.GenericServlet)10 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)9 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)9 ServletContext (javax.servlet.ServletContext)8 UnavailableException (javax.servlet.UnavailableException)8 Resource (org.apache.sling.api.resource.Resource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)7 MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)7 ServletInfo (io.undertow.servlet.api.ServletInfo)6 ArrayList (java.util.ArrayList)5 Filter (javax.servlet.Filter)5 Context (org.apache.catalina.Context)5 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)5