diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextCustomizer.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextCustomizer.java index 54b51ca2fbc..9ad2a3a3085 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextCustomizer.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextCustomizer.java @@ -26,7 +26,7 @@ import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption; import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.boot.web.servlet.server.AbstractConfigurableServletWebServerFactory; +import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; @@ -106,8 +106,8 @@ class SpringBootTestContextCustomizer implements ContextCustomizer { private boolean isSslEnabled(ApplicationContext context) { try { - AbstractConfigurableServletWebServerFactory webServerFactory = context - .getBean(AbstractConfigurableServletWebServerFactory.class); + AbstractServletWebServerFactory webServerFactory = context + .getBean(AbstractServletWebServerFactory.class); return webServerFactory.getSsl() != null && webServerFactory.getSsl().isEnabled(); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/WebTestClientContextCustomizer.java b/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/WebTestClientContextCustomizer.java index 24b49467d1f..e0a6b71108a 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/WebTestClientContextCustomizer.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/WebTestClientContextCustomizer.java @@ -23,7 +23,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.reactive.server.AbstractConfigurableReactiveWebServerFactory; +import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; @@ -120,9 +120,10 @@ class WebTestClientContextCustomizer implements ContextCustomizer { private boolean isSslEnabled(ApplicationContext context) { try { - AbstractConfigurableReactiveWebServerFactory webServerFactory = context - .getBean(AbstractConfigurableReactiveWebServerFactory.class); - return webServerFactory.getSsl() != null && webServerFactory.getSsl().isEnabled(); + AbstractReactiveWebServerFactory webServerFactory = context + .getBean(AbstractReactiveWebServerFactory.class); + return webServerFactory.getSsl() != null + && webServerFactory.getSsl().isEnabled(); } catch (NoSuchBeanDefinitionException ex) { return false; diff --git a/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyReactiveWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyReactiveWebServerFactory.java index 7fd68b86f05..d909cf56ff5 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyReactiveWebServerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyReactiveWebServerFactory.java @@ -18,6 +18,8 @@ package org.springframework.boot.web.embedded.jetty; import java.net.InetSocketAddress; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.eclipse.jetty.server.AbstractConnector; import org.eclipse.jetty.server.ConnectionFactory; import org.eclipse.jetty.server.HttpConfiguration; @@ -41,6 +43,9 @@ import org.springframework.http.server.reactive.JettyHttpHandlerAdapter; */ public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFactory { + private static final Log logger = LogFactory + .getLog(JettyReactiveWebServerFactory.class); + /** * The number of acceptor threads to use. */ @@ -85,7 +90,8 @@ public class JettyReactiveWebServerFactory extends AbstractReactiveWebServerFact ServletContextHandler contextHandler = new ServletContextHandler(server, "", false, false); contextHandler.addServlet(servletHolder, "/"); - this.logger.info("Server initialized with port: " + port); + JettyReactiveWebServerFactory.logger + .info("Server initialized with port: " + port); return server; } diff --git a/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactory.java index f2eab5ab07f..d346f1073e0 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactory.java @@ -16,13 +16,7 @@ package org.springframework.boot.web.reactive.server; -import java.io.File; -import java.io.IOException; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import org.springframework.boot.web.server.WebServerException; +import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory; /** * Abstract base class for {@link ReactiveWebServerFactory} implementations. @@ -30,10 +24,9 @@ import org.springframework.boot.web.server.WebServerException; * @author Brian Clozel * @since 2.0.0 */ -public abstract class AbstractReactiveWebServerFactory extends - AbstractConfigurableReactiveWebServerFactory implements ReactiveWebServerFactory { - - protected final Log logger = LogFactory.getLog(getClass()); +public abstract class AbstractReactiveWebServerFactory + extends AbstractConfigurableWebServerFactory + implements ConfigurableReactiveWebServerFactory { public AbstractReactiveWebServerFactory() { } @@ -42,25 +35,4 @@ public abstract class AbstractReactiveWebServerFactory extends super(port); } - /** - * Return the absolute temp dir for given web server. - * @param prefix server name - * @return The temp dir for given server. - */ - protected File createTempDir(String prefix) { - try { - File tempDir = File.createTempFile(prefix + ".", "." + getPort()); - tempDir.delete(); - tempDir.mkdir(); - tempDir.deleteOnExit(); - return tempDir; - } - catch (IOException ex) { - throw new WebServerException( - "Unable to create tempDir. java.io.tmpdir is set to " - + System.getProperty("java.io.tmpdir"), - ex); - } - } - } diff --git a/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/ConfigurableReactiveWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/ConfigurableReactiveWebServerFactory.java index 7a9cfce5393..364a22d5188 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/ConfigurableReactiveWebServerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/ConfigurableReactiveWebServerFactory.java @@ -19,11 +19,12 @@ package org.springframework.boot.web.reactive.server; import org.springframework.boot.web.server.ConfigurableWebServerFactory; /** - * Interface that represents customizations to a {@link ReactiveWebServerFactory}. + * Configurable {@link ReactiveWebServerFactory}. * * @author Brian Clozel * @since 2.0.0 */ -public interface ConfigurableReactiveWebServerFactory extends ConfigurableWebServerFactory { +public interface ConfigurableReactiveWebServerFactory + extends ConfigurableWebServerFactory, ReactiveWebServerFactory { } diff --git a/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/AbstractConfigurableReactiveWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java similarity index 64% rename from spring-boot/src/main/java/org/springframework/boot/web/reactive/server/AbstractConfigurableReactiveWebServerFactory.java rename to spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java index 8c43a39fe97..85ff5e5576a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/reactive/server/AbstractConfigurableReactiveWebServerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java @@ -14,34 +14,38 @@ * limitations under the License. */ -package org.springframework.boot.web.reactive.server; +package org.springframework.boot.web.server; +import java.io.File; +import java.io.IOException; import java.net.InetAddress; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; -import org.springframework.boot.web.server.Compression; -import org.springframework.boot.web.server.ErrorPage; -import org.springframework.boot.web.server.Ssl; -import org.springframework.boot.web.server.SslStoreProvider; import org.springframework.util.Assert; /** - * Abstract base class for {@link ConfigurableReactiveWebServerFactory} implementations. + * Abstract base class for {@link ConfigurableWebServerFactory} implementations. * + * @author Phillip Webb + * @author Dave Syer + * @author Andy Wilkinson + * @author Stephane Nicoll + * @author Ivan Sopov + * @author Eddú Meléndez * @author Brian Clozel * @since 2.0.0 */ -public class AbstractConfigurableReactiveWebServerFactory - implements ConfigurableReactiveWebServerFactory { +public class AbstractConfigurableWebServerFactory + implements ConfigurableWebServerFactory { private int port = 8080; - private Set errorPages = new LinkedHashSet<>(); - private InetAddress address; + private Set errorPages = new LinkedHashSet<>(); + private Ssl ssl; private SslStoreProvider sslStoreProvider; @@ -51,31 +55,26 @@ public class AbstractConfigurableReactiveWebServerFactory private String serverHeader; /** - * Create a new {@link AbstractConfigurableReactiveWebServerFactory} instance. + * Create a new {@link AbstractConfigurableWebServerFactory} instance. */ - public AbstractConfigurableReactiveWebServerFactory() { + public AbstractConfigurableWebServerFactory() { } /** - * Create a new {@link AbstractConfigurableReactiveWebServerFactory} instance with the + * Create a new {@link AbstractConfigurableWebServerFactory} instance with the * specified port. - * @param port the port number for the reactive web server + * @param port the port number for the web server */ - public AbstractConfigurableReactiveWebServerFactory(int port) { + public AbstractConfigurableWebServerFactory(int port) { this.port = port; } - @Override - public void setAddress(InetAddress address) { - this.address = address; - } - /** - * Return the address that the reactive web server binds to. - * @return the address + * The port that the web server server listens on. + * @return the port */ - public InetAddress getAddress() { - return this.address; + public int getPort() { + return this.port; } @Override @@ -84,11 +83,25 @@ public class AbstractConfigurableReactiveWebServerFactory } /** - * The port that the reactive web server listens on. - * @return the port + * Return the address that the web server binds to. + * @return the address */ - public int getPort() { - return this.port; + public InetAddress getAddress() { + return this.address; + } + + @Override + public void setAddress(InetAddress address) { + this.address = address; + } + + /** + * Returns a mutable set of {@link ErrorPage ErrorPages} that will be used when + * handling exceptions. + * @return the error pages + */ + public Set getErrorPages() { + return this.errorPages; } @Override @@ -103,13 +116,8 @@ public class AbstractConfigurableReactiveWebServerFactory this.errorPages.addAll(Arrays.asList(errorPages)); } - /** - * Return a mutable set of {@link ErrorPage ErrorPages} that will be used when - * handling exceptions. - * @return the error pages - */ - public Set getErrorPages() { - return this.errorPages; + public Ssl getSsl() { + return this.ssl; } @Override @@ -117,8 +125,8 @@ public class AbstractConfigurableReactiveWebServerFactory this.ssl = ssl; } - public Ssl getSsl() { - return this.ssl; + public SslStoreProvider getSslStoreProvider() { + return this.sslStoreProvider; } @Override @@ -126,10 +134,6 @@ public class AbstractConfigurableReactiveWebServerFactory this.sslStoreProvider = sslStoreProvider; } - public SslStoreProvider getSslStoreProvider() { - return this.sslStoreProvider; - } - public Compression getCompression() { return this.compression; } @@ -148,4 +152,25 @@ public class AbstractConfigurableReactiveWebServerFactory this.serverHeader = serverHeader; } + /** + * Return the absolute temp dir for given web server. + * @param prefix server name + * @return The temp dir for given server. + */ + protected final File createTempDir(String prefix) { + try { + File tempDir = File.createTempFile(prefix + ".", "." + getPort()); + tempDir.delete(); + tempDir.mkdir(); + tempDir.deleteOnExit(); + return tempDir; + } + catch (IOException ex) { + throw new WebServerException( + "Unable to create tempDir. java.io.tmpdir is set to " + + System.getProperty("java.io.tmpdir"), + ex); + } + } + } diff --git a/spring-boot/src/main/java/org/springframework/boot/web/server/ConfigurableWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/server/ConfigurableWebServerFactory.java index 6cbcbedd8b5..345441d0e83 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/server/ConfigurableWebServerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/server/ConfigurableWebServerFactory.java @@ -20,11 +20,12 @@ import java.net.InetAddress; import java.util.Set; /** - * Simple interface that represents customizations to a {@link WebServerFactory}. + * A configurable {@link WebServerFactory}. * * @author Phillip Webb * @author Brian Clozel * @since 2.0.0 + * @see ErrorPageRegistry */ public interface ConfigurableWebServerFactory extends WebServerFactory, ErrorPageRegistry { diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractConfigurableServletWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractConfigurableServletWebServerFactory.java deleted file mode 100644 index 07b9b5be3be..00000000000 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractConfigurableServletWebServerFactory.java +++ /dev/null @@ -1,381 +0,0 @@ -/* - * Copyright 2012-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.web.servlet.server; - -import java.io.File; -import java.net.InetAddress; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import org.springframework.boot.web.server.Compression; -import org.springframework.boot.web.server.ErrorPage; -import org.springframework.boot.web.server.MimeMappings; -import org.springframework.boot.web.server.Ssl; -import org.springframework.boot.web.server.SslStoreProvider; -import org.springframework.boot.web.servlet.ServletContextInitializer; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; - -/** - * Abstract base class for {@link ConfigurableServletWebServerFactory} implementations. - * - * @author Phillip Webb - * @author Dave Syer - * @author Andy Wilkinson - * @author Stephane Nicoll - * @author Ivan Sopov - * @author Eddú Meléndez - * @author Brian Clozel - * @since 2.0.0 - * @see AbstractServletWebServerFactory - */ -public abstract class AbstractConfigurableServletWebServerFactory - implements ConfigurableServletWebServerFactory { - - private static final int DEFAULT_SESSION_TIMEOUT = (int) TimeUnit.MINUTES - .toSeconds(30); - - private String contextPath = ""; - - private String displayName; - - private boolean registerDefaultServlet = true; - - private int port = 8080; - - private List initializers = new ArrayList<>(); - - private File documentRoot; - - private Set errorPages = new LinkedHashSet<>(); - - private MimeMappings mimeMappings = new MimeMappings(MimeMappings.DEFAULT); - - private InetAddress address; - - private int sessionTimeout = DEFAULT_SESSION_TIMEOUT; - - private boolean persistSession; - - private File sessionStoreDir; - - private Ssl ssl; - - private SslStoreProvider sslStoreProvider; - - private Jsp jsp = new Jsp(); - - private Compression compression; - - private String serverHeader; - - private Map localeCharsetMappings = new HashMap<>(); - - /** - * Create a new {@link AbstractConfigurableServletWebServerFactory} instance. - */ - public AbstractConfigurableServletWebServerFactory() { - } - - /** - * Create a new {@link AbstractConfigurableServletWebServerFactory} instance with the - * specified port. - * @param port the port number for the web server - */ - public AbstractConfigurableServletWebServerFactory(int port) { - this.port = port; - } - - /** - * Create a new {@link AbstractConfigurableServletWebServerFactory} instance with the - * specified context path and port. - * @param contextPath the context path for the web server - * @param port the port number for the web server - */ - public AbstractConfigurableServletWebServerFactory(String contextPath, int port) { - checkContextPath(contextPath); - this.contextPath = contextPath; - this.port = port; - } - - @Override - public void setContextPath(String contextPath) { - checkContextPath(contextPath); - this.contextPath = contextPath; - } - - private void checkContextPath(String contextPath) { - Assert.notNull(contextPath, "ContextPath must not be null"); - if (contextPath.length() > 0) { - if ("/".equals(contextPath)) { - throw new IllegalArgumentException( - "Root ContextPath must be specified using an empty string"); - } - if (!contextPath.startsWith("/") || contextPath.endsWith("/")) { - throw new IllegalArgumentException( - "ContextPath must start with '/' and not end with '/'"); - } - } - } - - /** - * Returns the context path for the web server. The path will start with "/" and not - * end with "/". The root context is represented by an empty string. - * @return the context path - */ - public String getContextPath() { - return this.contextPath; - } - - @Override - public void setDisplayName(String displayName) { - this.displayName = displayName; - } - - public String getDisplayName() { - return this.displayName; - } - - @Override - public void setPort(int port) { - this.port = port; - } - - /** - * The port that the web server server listens on. - * @return the port - */ - public int getPort() { - return this.port; - } - - @Override - public void setAddress(InetAddress address) { - this.address = address; - } - - /** - * Return the address that the web server binds to. - * @return the address - */ - public InetAddress getAddress() { - return this.address; - } - - @Override - public void setSessionTimeout(int sessionTimeout) { - this.sessionTimeout = sessionTimeout; - } - - @Override - public void setSessionTimeout(int sessionTimeout, TimeUnit timeUnit) { - Assert.notNull(timeUnit, "TimeUnit must not be null"); - this.sessionTimeout = (int) timeUnit.toSeconds(sessionTimeout); - } - - /** - * Return the session timeout in seconds. - * @return the timeout in seconds - */ - public int getSessionTimeout() { - return this.sessionTimeout; - } - - @Override - public void setPersistSession(boolean persistSession) { - this.persistSession = persistSession; - } - - public boolean isPersistSession() { - return this.persistSession; - } - - @Override - public void setSessionStoreDir(File sessionStoreDir) { - this.sessionStoreDir = sessionStoreDir; - } - - public File getSessionStoreDir() { - return this.sessionStoreDir; - } - - @Override - public void setInitializers(List initializers) { - Assert.notNull(initializers, "Initializers must not be null"); - this.initializers = new ArrayList<>(initializers); - } - - @Override - public void addInitializers(ServletContextInitializer... initializers) { - Assert.notNull(initializers, "Initializers must not be null"); - this.initializers.addAll(Arrays.asList(initializers)); - } - - @Override - public void setDocumentRoot(File documentRoot) { - this.documentRoot = documentRoot; - } - - /** - * Returns the document root which will be used by the web context to serve static - * files. - * @return the document root - */ - public File getDocumentRoot() { - return this.documentRoot; - } - - @Override - public void setErrorPages(Set errorPages) { - Assert.notNull(errorPages, "ErrorPages must not be null"); - this.errorPages = new LinkedHashSet<>(errorPages); - } - - @Override - public void addErrorPages(ErrorPage... errorPages) { - Assert.notNull(errorPages, "ErrorPages must not be null"); - this.errorPages.addAll(Arrays.asList(errorPages)); - } - - /** - * Returns a mutable set of {@link ErrorPage ErrorPages} that will be used when - * handling exceptions. - * @return the error pages - */ - public Set getErrorPages() { - return this.errorPages; - } - - @Override - public void setMimeMappings(MimeMappings mimeMappings) { - this.mimeMappings = new MimeMappings(mimeMappings); - } - - /** - * Returns the mime-type mappings. - * @return the mimeMappings the mime-type mappings. - */ - public MimeMappings getMimeMappings() { - return this.mimeMappings; - } - - @Override - public void setRegisterDefaultServlet(boolean registerDefaultServlet) { - this.registerDefaultServlet = registerDefaultServlet; - } - - /** - * Flag to indicate that the default servlet should be registered. - * @return true if the default servlet is to be registered - */ - public boolean isRegisterDefaultServlet() { - return this.registerDefaultServlet; - } - - @Override - public void setSsl(Ssl ssl) { - this.ssl = ssl; - } - - public Ssl getSsl() { - return this.ssl; - } - - @Override - public void setSslStoreProvider(SslStoreProvider sslStoreProvider) { - this.sslStoreProvider = sslStoreProvider; - } - - public SslStoreProvider getSslStoreProvider() { - return this.sslStoreProvider; - } - - @Override - public void setJsp(Jsp jsp) { - this.jsp = jsp; - } - - public Jsp getJsp() { - return this.jsp; - } - - public Compression getCompression() { - return this.compression; - } - - @Override - public void setCompression(Compression compression) { - this.compression = compression; - } - - public String getServerHeader() { - return this.serverHeader; - } - - @Override - public void setServerHeader(String serverHeader) { - this.serverHeader = serverHeader; - } - - /** - * Return the Locale to Charset mappings. - * @return the charset mappings - */ - public Map getLocaleCharsetMappings() { - return this.localeCharsetMappings; - } - - @Override - public void setLocaleCharsetMappings(Map localeCharsetMappings) { - Assert.notNull(localeCharsetMappings, "localeCharsetMappings must not be null"); - this.localeCharsetMappings = localeCharsetMappings; - } - - /** - * Utility method that can be used by subclasses wishing to combine the specified - * {@link ServletContextInitializer} parameters with those defined in this instance. - * @param initializers the initializers to merge - * @return a complete set of merged initializers (with the specified parameters - * appearing first) - */ - protected final ServletContextInitializer[] mergeInitializers( - ServletContextInitializer... initializers) { - List mergedInitializers = new ArrayList<>(); - mergedInitializers.addAll(Arrays.asList(initializers)); - mergedInitializers.addAll(this.initializers); - return mergedInitializers - .toArray(new ServletContextInitializer[mergedInitializers.size()]); - } - - /** - * Returns whether or not the JSP servlet should be registered with the web server. - * @return {@code true} if the servlet should be registered, otherwise {@code false} - */ - protected boolean shouldRegisterJspServlet() { - return this.jsp != null && this.jsp.getRegistered() && ClassUtils - .isPresent(this.jsp.getClassName(), getClass().getClassLoader()); - } - -} diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java index a506b0de95c..d9f83c54a40 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java @@ -22,10 +22,15 @@ import java.net.JarURLConnection; import java.net.URL; import java.net.URLClassLoader; import java.net.URLConnection; +import java.nio.charset.Charset; import java.security.CodeSource; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.jar.JarFile; import org.apache.commons.logging.Log; @@ -33,34 +38,258 @@ import org.apache.commons.logging.LogFactory; import org.springframework.boot.ApplicationHome; import org.springframework.boot.ApplicationTemp; -import org.springframework.boot.web.server.WebServerException; +import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory; +import org.springframework.boot.web.server.MimeMappings; +import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; /** - * Abstract base class for {@link ServletWebServerFactory} implementations. + * Abstract base class for {@link ConfigurableServletWebServerFactory} implementations. * * @author Phillip Webb * @author Dave Syer + * @author Andy Wilkinson + * @author Stephane Nicoll + * @author Ivan Sopov + * @author Eddú Meléndez + * @author Brian Clozel * @since 2.0.0 */ -public abstract class AbstractServletWebServerFactory extends - AbstractConfigurableServletWebServerFactory implements ServletWebServerFactory { +public abstract class AbstractServletWebServerFactory + extends AbstractConfigurableWebServerFactory + implements ConfigurableServletWebServerFactory { - protected final Log logger = LogFactory.getLog(getClass()); + private static final int DEFAULT_SESSION_TIMEOUT = (int) TimeUnit.MINUTES + .toSeconds(30); private static final String[] COMMON_DOC_ROOTS = { "src/main/webapp", "public", "static" }; + protected final Log logger = LogFactory.getLog(getClass()); + + private String contextPath = ""; + + private String displayName; + + private int sessionTimeout = DEFAULT_SESSION_TIMEOUT; + + private boolean persistSession; + + private File sessionStoreDir; + + private boolean registerDefaultServlet = true; + + private MimeMappings mimeMappings = new MimeMappings(MimeMappings.DEFAULT); + + private File documentRoot; + + private List initializers = new ArrayList<>(); + + private Jsp jsp = new Jsp(); + + private Map localeCharsetMappings = new HashMap<>(); + + /** + * Create a new {@link AbstractServletWebServerFactory} instance. + */ public AbstractServletWebServerFactory() { - super(); } + /** + * Create a new {@link AbstractServletWebServerFactory} instance with the specified + * port. + * @param port the port number for the web server + */ public AbstractServletWebServerFactory(int port) { super(port); } + /** + * Create a new {@link AbstractServletWebServerFactory} instance with the specified + * context path and port. + * @param contextPath the context path for the web server + * @param port the port number for the web server + */ public AbstractServletWebServerFactory(String contextPath, int port) { - super(contextPath, port); + super(port); + checkContextPath(contextPath); + this.contextPath = contextPath; + } + + /** + * Returns the context path for the web server. The path will start with "/" and not + * end with "/". The root context is represented by an empty string. + * @return the context path + */ + public String getContextPath() { + return this.contextPath; + } + + @Override + public void setContextPath(String contextPath) { + checkContextPath(contextPath); + this.contextPath = contextPath; + } + + private void checkContextPath(String contextPath) { + Assert.notNull(contextPath, "ContextPath must not be null"); + if (contextPath.length() > 0) { + if ("/".equals(contextPath)) { + throw new IllegalArgumentException( + "Root ContextPath must be specified using an empty string"); + } + if (!contextPath.startsWith("/") || contextPath.endsWith("/")) { + throw new IllegalArgumentException( + "ContextPath must start with '/' and not end with '/'"); + } + } + } + + public String getDisplayName() { + return this.displayName; + } + + @Override + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + /** + * Return the session timeout in seconds. + * @return the timeout in seconds + */ + public int getSessionTimeout() { + return this.sessionTimeout; + } + + @Override + public void setSessionTimeout(int sessionTimeout) { + this.sessionTimeout = sessionTimeout; + } + + @Override + public void setSessionTimeout(int sessionTimeout, TimeUnit timeUnit) { + Assert.notNull(timeUnit, "TimeUnit must not be null"); + this.sessionTimeout = (int) timeUnit.toSeconds(sessionTimeout); + } + + public boolean isPersistSession() { + return this.persistSession; + } + + @Override + public void setPersistSession(boolean persistSession) { + this.persistSession = persistSession; + } + + public File getSessionStoreDir() { + return this.sessionStoreDir; + } + + @Override + public void setSessionStoreDir(File sessionStoreDir) { + this.sessionStoreDir = sessionStoreDir; + } + + /** + * Flag to indicate that the default servlet should be registered. + * @return true if the default servlet is to be registered + */ + public boolean isRegisterDefaultServlet() { + return this.registerDefaultServlet; + } + + @Override + public void setRegisterDefaultServlet(boolean registerDefaultServlet) { + this.registerDefaultServlet = registerDefaultServlet; + } + + /** + * Returns the mime-type mappings. + * @return the mimeMappings the mime-type mappings. + */ + public MimeMappings getMimeMappings() { + return this.mimeMappings; + } + + @Override + public void setMimeMappings(MimeMappings mimeMappings) { + this.mimeMappings = new MimeMappings(mimeMappings); + } + + /** + * Returns the document root which will be used by the web context to serve static + * files. + * @return the document root + */ + public File getDocumentRoot() { + return this.documentRoot; + } + + @Override + public void setDocumentRoot(File documentRoot) { + this.documentRoot = documentRoot; + } + + @Override + public void setInitializers(List initializers) { + Assert.notNull(initializers, "Initializers must not be null"); + this.initializers = new ArrayList<>(initializers); + } + + @Override + public void addInitializers(ServletContextInitializer... initializers) { + Assert.notNull(initializers, "Initializers must not be null"); + this.initializers.addAll(Arrays.asList(initializers)); + } + + public Jsp getJsp() { + return this.jsp; + } + + @Override + public void setJsp(Jsp jsp) { + this.jsp = jsp; + } + + /** + * Return the Locale to Charset mappings. + * @return the charset mappings + */ + public Map getLocaleCharsetMappings() { + return this.localeCharsetMappings; + } + + @Override + public void setLocaleCharsetMappings(Map localeCharsetMappings) { + Assert.notNull(localeCharsetMappings, "localeCharsetMappings must not be null"); + this.localeCharsetMappings = localeCharsetMappings; + } + + /** + * Utility method that can be used by subclasses wishing to combine the specified + * {@link ServletContextInitializer} parameters with those defined in this instance. + * @param initializers the initializers to merge + * @return a complete set of merged initializers (with the specified parameters + * appearing first) + */ + protected final ServletContextInitializer[] mergeInitializers( + ServletContextInitializer... initializers) { + List mergedInitializers = new ArrayList<>(); + mergedInitializers.addAll(Arrays.asList(initializers)); + mergedInitializers.addAll(this.initializers); + return mergedInitializers + .toArray(new ServletContextInitializer[mergedInitializers.size()]); + } + + /** + * Returns whether or not the JSP servlet should be registered with the web server. + * @return {@code true} if the servlet should be registered, otherwise {@code false} + */ + protected boolean shouldRegisterJspServlet() { + return this.jsp != null && this.jsp.getRegistered() && ClassUtils + .isPresent(this.jsp.getClassName(), getClass().getClassLoader()); } /** @@ -152,7 +381,7 @@ public abstract class AbstractServletWebServerFactory extends } } - File getExplodedWarFileDocumentRoot(File codeSourceFile) { + protected final File getExplodedWarFileDocumentRoot(File codeSourceFile) { if (this.logger.isDebugEnabled()) { this.logger.debug("Code archive: " + codeSourceFile); } @@ -236,25 +465,4 @@ public abstract class AbstractServletWebServerFactory extends return dir; } - /** - * Returns the absolute temp dir for given server. - * @param prefix server name - * @return The temp dir for given server. - */ - protected File createTempDir(String prefix) { - try { - File tempDir = File.createTempFile(prefix + ".", "." + getPort()); - tempDir.delete(); - tempDir.mkdir(); - tempDir.deleteOnExit(); - return tempDir; - } - catch (IOException ex) { - throw new WebServerException( - "Unable to create tempDir. java.io.tmpdir is set to " - + System.getProperty("java.io.tmpdir"), - ex); - } - } - } diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java index 089700bfbca..a86adbfe464 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java @@ -29,7 +29,7 @@ import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.ServletContextInitializer; /** - * Simple interface that represents customizations to an {@link ServletWebServerFactory}. + * A configurable {@link ServletWebServerFactory}. * * @author Dave Syer * @author Andy Wilkinson @@ -41,7 +41,7 @@ import org.springframework.boot.web.servlet.ServletContextInitializer; * @see WebServerFactoryCustomizer */ public interface ConfigurableServletWebServerFactory - extends ConfigurableWebServerFactory { + extends ConfigurableWebServerFactory, ServletWebServerFactory { /** * Sets the context path for the web server. The context should start with a "/"