Search in sources :

Example 1 with ServletRegistrationBean

use of org.springframework.boot.web.servlet.ServletRegistrationBean in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method sslDisabled.

@Test
public void sslDisabled() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    Ssl ssl = getSsl(null, "password", "classpath:test.jks");
    ssl.setEnabled(false);
    factory.setSsl(ssl);
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    this.thrown.expect(SSLException.class);
    getResponse(getLocalUrl("https", "/hello"), requestFactory);
}
Also used : ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) Ssl(org.springframework.boot.web.server.Ssl) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) Test(org.junit.Test)

Example 2 with ServletRegistrationBean

use of org.springframework.boot.web.servlet.ServletRegistrationBean in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method serverHeaderCanBeCustomizedWhenUsingSsl.

@Test
public void serverHeaderCanBeCustomizedWhenUsingSsl() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.setServerHeader("MyServer");
    factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    ClientHttpResponse response = getClientResponse(getLocalUrl("https", "/hello"), HttpMethod.GET, new HttpComponentsClientHttpRequestFactory(httpClient));
    assertThat(response.getHeaders().get("Server")).containsExactly("MyServer");
}
Also used : ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) Test(org.junit.Test)

Example 3 with ServletRegistrationBean

use of org.springframework.boot.web.servlet.ServletRegistrationBean in project spring-boot by spring-projects.

the class ResourceHandlingApplication method resourceServletRegistration.

@Bean
public ServletRegistrationBean<?> resourceServletRegistration() {
    ServletRegistrationBean<?> registration = new ServletRegistrationBean<HttpServlet>(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            URL resource = getServletContext().getResource(req.getQueryString());
            if (resource == null) {
                resp.sendError(404);
            } else {
                resp.getWriter().println(resource);
                resp.getWriter().flush();
            }
        }
    });
    registration.addUrlMappings("/servletContext");
    return registration;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) URL(java.net.URL) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with ServletRegistrationBean

use of org.springframework.boot.web.servlet.ServletRegistrationBean in project spring-boot by spring-projects.

the class JettyServletWebServerFactoryTests method setUpFactoryForCompression.

@Override
@SuppressWarnings("serial")
protected // Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646
String setUpFactoryForCompression(final int contentSize, String[] mimeTypes, String[] excludedUserAgents) throws Exception {
    char[] chars = new char[contentSize];
    Arrays.fill(chars, 'F');
    final String testContent = new String(chars);
    AbstractServletWebServerFactory factory = getFactory();
    Compression compression = new Compression();
    compression.setEnabled(true);
    if (mimeTypes != null) {
        compression.setMimeTypes(mimeTypes);
    }
    if (excludedUserAgents != null) {
        compression.setExcludedUserAgents(excludedUserAgents);
    }
    factory.setCompression(compression);
    this.webServer = factory.getWebServer(new ServletRegistrationBean<HttpServlet>(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentLength(contentSize);
            resp.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
            resp.getWriter().print(testContent);
        }
    }, "/test.txt"));
    this.webServer.start();
    return testContent;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Compression(org.springframework.boot.web.server.Compression) AbstractServletWebServerFactory(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 5 with ServletRegistrationBean

use of org.springframework.boot.web.servlet.ServletRegistrationBean in project spring-boot by spring-projects.

the class UndertowServletWebServerFactoryTests method errorPage404.

@Test
public void errorPage404() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/hello"));
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(), "/hello"));
    this.webServer.start();
    assertThat(getResponse(getLocalUrl("/hello"))).isEqualTo("Hello World");
    assertThat(getResponse(getLocalUrl("/not-found"))).isEqualTo("Hello World");
}
Also used : ErrorPage(org.springframework.boot.web.server.ErrorPage) AbstractServletWebServerFactory(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) ExampleServlet(org.springframework.boot.web.servlet.server.ExampleServlet) Test(org.junit.Test)

Aggregations

ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)25 Bean (org.springframework.context.annotation.Bean)14 Test (org.junit.Test)8 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)7 HttpClient (org.apache.http.client.HttpClient)6 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)6 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)6 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)5 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)4 CamelHttpTransportServlet (org.apache.camel.component.servlet.CamelHttpTransportServlet)3 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)3 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 HttpServlet (javax.servlet.http.HttpServlet)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Compression (org.springframework.boot.web.server.Compression)2 AbstractServletWebServerFactory (org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory)2 ExampleServlet (org.springframework.boot.web.servlet.server.ExampleServlet)2