Example usage for org.springframework.boot.web.servlet.server AbstractServletWebServerFactory addInitializers

List of usage examples for org.springframework.boot.web.servlet.server AbstractServletWebServerFactory addInitializers

Introduction

In this page you can find the example usage for org.springframework.boot.web.servlet.server AbstractServletWebServerFactory addInitializers.

Prototype

@Override
    public void addInitializers(ServletContextInitializer... initializers) 

Source Link

Usage

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void faultyFilterCausesStartFailure() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.addInitializers((servletContext) -> servletContext.addFilter("faulty", new Filter() {

        @Override/*  ww  w. j a  v a2 s  .com*/
        public void init(FilterConfig filterConfig) throws ServletException {
            throw new ServletException("Faulty filter");
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
        }

    }));
    this.thrown.expect(WebServerException.class);
    factory.getWebServer().start();
}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

private String setUpFactoryForCompression(int contentSize, String[] mimeTypes, String[] excludedUserAgents)
        throws Exception {
    char[] chars = new char[contentSize];
    Arrays.fill(chars, 'F');
    String testContent = new String(chars);
    AbstractServletWebServerFactory factory = getFactory();
    Compression compression = new Compression();
    compression.setEnabled(true);/*from   www  .j ava  2  s  . c o m*/
    if (mimeTypes != null) {
        compression.setMimeTypes(mimeTypes);
    }
    if (excludedUserAgents != null) {
        compression.setExcludedUserAgents(excludedUserAgents);
    }
    factory.setCompression(compression);
    factory.addInitializers(new ServletRegistrationBean<HttpServlet>(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setContentType("text/plain");
            resp.setContentLength(testContent.length());
            resp.getWriter().write(testContent);
            resp.getWriter().flush();
        }

    }, "/test.txt"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    return testContent;
}