org.springframework.boot.web.server.WebServer Java Examples

The following examples show how to use org.springframework.boot.web.server.WebServer. 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: ExploreSpring5URLPatternUsingRouterFunctions.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    servletWrapper.setAsyncSupported(true);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example #2
Source File: FunctionalWebApplication.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example #3
Source File: FunctionalWebApplication.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    servletWrapper.setAsyncSupported(true);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example #4
Source File: NettyTcpServerFactory.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
/**
 * Reactive container (temporarily replaced by servlets)
 * @param httpHandler httpHandler
 * @return NettyTcpServer
 */
@Override
public WebServer getWebServer(HttpHandler httpHandler) {
    try {
        ServletContext servletContext = getServletContext();
        if(servletContext != null) {
            ServletRegistration.Dynamic servletRegistration = servletContext.addServlet("default", new ServletHttpHandlerAdapter(httpHandler));
            servletRegistration.setAsyncSupported(true);
            servletRegistration.addMapping("/");
        }

        //Server port
        InetSocketAddress serverAddress = getServerSocketAddress(getAddress(),getPort());
        return new NettyTcpServer(serverAddress, properties, protocolHandlers,serverListeners,channelHandlerSupplier);
    }catch (Exception e){
        throw new IllegalStateException(e.getMessage(),e);
    }
}
 
Example #5
Source File: VertxReactiveWebServerFactory.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public WebServer getWebServer(HttpHandler httpHandler) {
    HttpServerOptions httpServerOptions = customizeHttpServerOptions(properties.getHttpServerOptions());
    VertxHttpHandlerAdapter handler = new VertxHttpHandlerAdapter(httpHandler);

    return new VertxWebServer(vertx, httpServerOptions, handler);
}
 
Example #6
Source File: SpringTomcatApplicationItTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void verifySingleConnector() {
    // Relevant to Tomcat 9.0
    assertThat(applicationContext).isInstanceOf(WebServerApplicationContext.class);
    final WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer();
    assertThat(webServer).isInstanceOf(TomcatWebServer.class);
    assertThat(((TomcatWebServer) webServer).getTomcat()
                                            .getEngine()
                                            .getService()
                                            .findConnectors()).hasSize(1);
}
 
Example #7
Source File: SpringTomcatApplication.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Bean to configure Armeria Tomcat service.
 * @return configuration bean.
 */
@Bean
public ArmeriaServerConfigurator armeriaTomcat() {
    final WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer();
    if (webServer instanceof TomcatWebServer) {
        final Tomcat tomcat = ((TomcatWebServer) webServer).getTomcat();

        return serverBuilder -> serverBuilder.service("prefix:/tomcat/api/rest/v1",
                                                      TomcatService.of(tomcat));
    }
    return serverBuilder -> { };
}
 
Example #8
Source File: ArmeriaReactiveWebServerFactoryTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void runServer(ReactiveWebServerFactory factory,
                              HttpHandler httpHandler,
                              Consumer<WebServer> validator) {
    final WebServer server = factory.getWebServer(httpHandler);
    server.start();
    try {
        validator.accept(server);
    } finally {
        server.stop();
    }
}
 
Example #9
Source File: ServiceDiscoveryConfiguration.java    From Kafdrop with Apache License 2.0 5 votes vote down vote up
private Integer getServicePort()
{
   return Optional.ofNullable(webContext.getWebServer())
      .map(WebServer::getPort)
      .filter(i -> i != -1)
      .orElse(null);
}
 
Example #10
Source File: ServerlessServletEmbeddedServerFactory.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
    this.initializers = initializers;
    for (ServletContextInitializer i : initializers) {
        try {
            if (handler.getServletContext() == null) {
                throw new WebServerException("Attempting to initialize ServletEmbeddedWebServer without ServletContext in Handler", null);
            }
            i.onStartup(handler.getServletContext());
        } catch (ServletException e) {
            throw new WebServerException("Could not initialize Servlets", e);
        }
    }
    return this;
}
 
Example #11
Source File: DiscoveryClientRegistrationInvoker.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Override
public void customize(ConfigurableApplicationContext context) {
    if(context instanceof ServletWebServerApplicationContext
            && !AdminEndpointApplicationRunListener.isEmbeddedServletServer(context.getEnvironment())) {
        MetaDataProvider metaDataProvider = context.getBean(MetaDataProvider.class);
        WebServer webServer = new WebServer() {
            @Override
            public void start() throws WebServerException {

            }

            @Override
            public void stop() throws WebServerException {

            }

            @Override
            public int getPort() {
                return metaDataProvider.getServerPort();
            }
        };
        context.publishEvent(
                new ServletWebServerInitializedEvent(
                        webServer,
                        new ServletWebServerApplicationContext())
        );
    }
}
 
Example #12
Source File: NettyTcpServerFactory.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
/**
 * Get servlet container
 * @param initializers Initialize the
 * @return NettyTcpServer
 */
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
    ServletContext servletContext = Objects.requireNonNull(getServletContext());
    try {
        //The default servlet
        if (super.isRegisterDefaultServlet()) {
            servletContext.addServlet("default",new ServletDefaultHttpServlet())
                    .addMapping("/");
        }

        //JSP is not supported
        if(super.shouldRegisterJspServlet()){
            Jsp jsp = getJsp();
        }

        //Initialize the
        for (ServletContextInitializer initializer : super.mergeInitializers(initializers)) {
            initializer.onStartup(servletContext);
        }

        //Server port
        InetSocketAddress serverAddress = getServerSocketAddress(getAddress(),getPort());
        return new NettyTcpServer(serverAddress, properties, protocolHandlers,serverListeners,channelHandlerSupplier);
    }catch (Exception e){
        throw new IllegalStateException(e.getMessage(),e);
    }
}
 
Example #13
Source File: VertxReactiveWebServerFactoryTest.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCustomizeHttpServerOptions() {
    webServerFactory.registerHttpServerOptionsCustomizer(mockCustomizer);
    WebServer webServer = webServerFactory.getWebServer(null);
    assertThat(webServer).isNotNull();
    verify(mockCustomizer).apply(mockHttpServerOptions);
}
 
Example #14
Source File: ArkTomcatServletWebServerFactory.java    From sofa-ark with Apache License 2.0 5 votes vote down vote up
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
    if (embeddedServerService == null) {
        return super.getWebServer(initializers);
    } else if (embeddedServerService.getEmbedServer() == null) {
        embeddedServerService.setEmbedServer(initEmbedTomcat());
    }
    Tomcat embedTomcat = embeddedServerService.getEmbedServer();
    prepareContext(embedTomcat.getHost(), initializers);
    return getWebServer(embedTomcat);
}
 
Example #15
Source File: NacosDiscoveryAutoDeregister.java    From nacos-spring-boot-project with Apache License 2.0 4 votes vote down vote up
public NacosDiscoveryAutoDeregister(NacosDiscoveryProperties discoveryProperties,
		WebServer webServer) {
	this.discoveryProperties = discoveryProperties;
	this.webServer = webServer;
}
 
Example #16
Source File: VertxReactiveWebServerFactoryTest.java    From vertx-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldCreateWebServer() {
    WebServer webServer = webServerFactory.getWebServer(null);
    assertThat(webServer).isNotNull();
    assertThat(webServer).isInstanceOf(VertxWebServer.class);
}
 
Example #17
Source File: ServletApplicationFactoryTest.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
@Override
public WebServer getWebServer() {
	return this.server;
}
 
Example #18
Source File: ServletApplicationFactoryTest.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
private TestWebServerInitializedEvent(String name, int port) {
	super(mock(WebServer.class));
	when(server.getPort()).thenReturn(port);
	when(context.getServerNamespace()).thenReturn(name);
}
 
Example #19
Source File: DefaultApplicationFactoryTest.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
@Override
public WebServer getWebServer() {
	return this.server;
}
 
Example #20
Source File: DefaultApplicationFactoryTest.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
private TestWebServerInitializedEvent(String name, int port) {
	super(mock(WebServer.class));
	when(server.getPort()).thenReturn(port);
	when(context.getServerNamespace()).thenReturn(name);
}
 
Example #21
Source File: RSocketNettyReactiveWebServerFactory.java    From spring-boot-rsocket with Apache License 2.0 4 votes vote down vote up
@Override
public WebServer getWebServer(HttpHandler httpHandler) {
	return new RSocketWebServer(createRSocketStarter(httpHandler));
}
 
Example #22
Source File: ArmeriaReactiveWebServerFactoryTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static void runEchoServer(ReactiveWebServerFactory factory,
                                  Consumer<WebServer> validator) {
    runServer(factory, EchoHandler.INSTANCE, validator);
}
 
Example #23
Source File: ArmeriaReactiveWebServerFactoryTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private WebClient httpClient(WebServer server) {
    return WebClient.builder("http://example.com:" + server.getPort())
                    .factory(clientFactory)
                    .build();
}
 
Example #24
Source File: ArmeriaReactiveWebServerFactoryTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private WebClient httpsClient(WebServer server) {
    return WebClient.builder("https://example.com:" + server.getPort())
                    .factory(clientFactory)
                    .build();
}
 
Example #25
Source File: LealoneTomcatWebServer.java    From Lealone-Plugins with Apache License 2.0 4 votes vote down vote up
public LealoneTomcatWebServer(WebServer server) {
    this.server = server;
}
 
Example #26
Source File: LealoneTomcatServletWebServerFactory.java    From Lealone-Plugins with Apache License 2.0 4 votes vote down vote up
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
    return new LealoneTomcatWebServer(super.getWebServer(initializers));
}
 
Example #27
Source File: ServerlessReactiveServletEmbeddedServerFactory.java    From aws-serverless-java-container with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressFBWarnings("MTIA_SUSPECT_SERVLET_INSTANCE_FIELD")
public WebServer getWebServer(HttpHandler httpHandler) {
    handler = new ServletHttpHandlerAdapter(httpHandler);
    return this;
}
 
Example #28
Source File: MockServletWebServerFactory.java    From spring-security-oauth2-boot with Apache License 2.0 4 votes vote down vote up
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
	this.webServer = spy(new MockServletWebServer(mergeInitializers(initializers), getPort()));
	return this.webServer;
}
 
Example #29
Source File: ArkTomcatServletWebServerFactory.java    From sofa-ark with Apache License 2.0 2 votes vote down vote up
/**
 * Factory method called to create the {@link TomcatWebServer}. Subclasses can
 * override this method to return a different {@link TomcatWebServer} or apply
 * additional processing to the Tomcat server.
 * @param tomcat the Tomcat server.
 * @return a new {@link TomcatWebServer} instance
 */
protected WebServer getWebServer(Tomcat tomcat) {
    return new ArkTomcatWebServer(tomcat, getPort() >= 0, tomcat);
}