001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.web.server;
018
019import java.io.File;
020import java.io.IOException;
021import java.net.InetAddress;
022import java.util.Arrays;
023import java.util.LinkedHashSet;
024import java.util.Set;
025
026import org.springframework.util.Assert;
027
028/**
029 * Abstract base class for {@link ConfigurableWebServerFactory} implementations.
030 *
031 * @author Phillip Webb
032 * @author Dave Syer
033 * @author Andy Wilkinson
034 * @author Stephane Nicoll
035 * @author Ivan Sopov
036 * @author EddĂș MelĂ©ndez
037 * @author Brian Clozel
038 * @since 2.0.0
039 */
040public abstract class AbstractConfigurableWebServerFactory
041                implements ConfigurableWebServerFactory {
042
043        private int port = 8080;
044
045        private InetAddress address;
046
047        private Set<ErrorPage> errorPages = new LinkedHashSet<>();
048
049        private Ssl ssl;
050
051        private SslStoreProvider sslStoreProvider;
052
053        private Http2 http2;
054
055        private Compression compression;
056
057        private String serverHeader;
058
059        /**
060         * Create a new {@link AbstractConfigurableWebServerFactory} instance.
061         */
062        public AbstractConfigurableWebServerFactory() {
063        }
064
065        /**
066         * Create a new {@link AbstractConfigurableWebServerFactory} instance with the
067         * specified port.
068         * @param port the port number for the web server
069         */
070        public AbstractConfigurableWebServerFactory(int port) {
071                this.port = port;
072        }
073
074        /**
075         * The port that the web server listens on.
076         * @return the port
077         */
078        public int getPort() {
079                return this.port;
080        }
081
082        @Override
083        public void setPort(int port) {
084                this.port = port;
085        }
086
087        /**
088         * Return the address that the web server binds to.
089         * @return the address
090         */
091        public InetAddress getAddress() {
092                return this.address;
093        }
094
095        @Override
096        public void setAddress(InetAddress address) {
097                this.address = address;
098        }
099
100        /**
101         * Returns a mutable set of {@link ErrorPage ErrorPages} that will be used when
102         * handling exceptions.
103         * @return the error pages
104         */
105        public Set<ErrorPage> getErrorPages() {
106                return this.errorPages;
107        }
108
109        @Override
110        public void setErrorPages(Set<? extends ErrorPage> errorPages) {
111                Assert.notNull(errorPages, "ErrorPages must not be null");
112                this.errorPages = new LinkedHashSet<>(errorPages);
113        }
114
115        @Override
116        public void addErrorPages(ErrorPage... errorPages) {
117                Assert.notNull(errorPages, "ErrorPages must not be null");
118                this.errorPages.addAll(Arrays.asList(errorPages));
119        }
120
121        public Ssl getSsl() {
122                return this.ssl;
123        }
124
125        @Override
126        public void setSsl(Ssl ssl) {
127                this.ssl = ssl;
128        }
129
130        public SslStoreProvider getSslStoreProvider() {
131                return this.sslStoreProvider;
132        }
133
134        @Override
135        public void setSslStoreProvider(SslStoreProvider sslStoreProvider) {
136                this.sslStoreProvider = sslStoreProvider;
137        }
138
139        public Http2 getHttp2() {
140                return this.http2;
141        }
142
143        @Override
144        public void setHttp2(Http2 http2) {
145                this.http2 = http2;
146        }
147
148        public Compression getCompression() {
149                return this.compression;
150        }
151
152        @Override
153        public void setCompression(Compression compression) {
154                this.compression = compression;
155        }
156
157        public String getServerHeader() {
158                return this.serverHeader;
159        }
160
161        @Override
162        public void setServerHeader(String serverHeader) {
163                this.serverHeader = serverHeader;
164        }
165
166        /**
167         * Return the absolute temp dir for given web server.
168         * @param prefix server name
169         * @return the temp dir for given server.
170         */
171        protected final File createTempDir(String prefix) {
172                try {
173                        File tempDir = File.createTempFile(prefix + ".", "." + getPort());
174                        tempDir.delete();
175                        tempDir.mkdir();
176                        tempDir.deleteOnExit();
177                        return tempDir;
178                }
179                catch (IOException ex) {
180                        throw new WebServerException(
181                                        "Unable to create tempDir. java.io.tmpdir is set to "
182                                                        + System.getProperty("java.io.tmpdir"),
183                                        ex);
184                }
185        }
186
187}