001/*
002 * Copyright 2002-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 *      https://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.remoting.support;
018
019import java.io.IOException;
020import java.net.InetSocketAddress;
021import java.util.List;
022import java.util.Map;
023import java.util.concurrent.Executor;
024
025import com.sun.net.httpserver.Authenticator;
026import com.sun.net.httpserver.Filter;
027import com.sun.net.httpserver.HttpContext;
028import com.sun.net.httpserver.HttpHandler;
029import com.sun.net.httpserver.HttpServer;
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032
033import org.springframework.beans.factory.DisposableBean;
034import org.springframework.beans.factory.FactoryBean;
035import org.springframework.beans.factory.InitializingBean;
036
037/**
038 * {@link org.springframework.beans.factory.FactoryBean} that creates a simple
039 * HTTP server, based on the HTTP server that is included in Sun's JRE 1.6.
040 * Starts the HTTP server on initialization and stops it on destruction.
041 * Exposes the resulting {@link com.sun.net.httpserver.HttpServer} object.
042 *
043 * <p>Allows for registering {@link com.sun.net.httpserver.HttpHandler HttpHandlers}
044 * for specific {@link #setContexts context paths}. Alternatively,
045 * register such context-specific handlers programmatically on the
046 * {@link com.sun.net.httpserver.HttpServer} itself.
047 *
048 * @author Juergen Hoeller
049 * @author Arjen Poutsma
050 * @since 2.5.1
051 * @see #setPort
052 * @see #setContexts
053 * @deprecated as of Spring Framework 5.1, in favor of embedded Tomcat/Jetty/Undertow
054 */
055@Deprecated
056@org.springframework.lang.UsesSunHttpServer
057public class SimpleHttpServerFactoryBean implements FactoryBean<HttpServer>, InitializingBean, DisposableBean {
058
059        protected final Log logger = LogFactory.getLog(getClass());
060
061        private int port = 8080;
062
063        private String hostname;
064
065        private int backlog = -1;
066
067        private int shutdownDelay = 0;
068
069        private Executor executor;
070
071        private Map<String, HttpHandler> contexts;
072
073        private List<Filter> filters;
074
075        private Authenticator authenticator;
076
077        private HttpServer server;
078
079
080        /**
081         * Specify the HTTP server's port. Default is 8080.
082         */
083        public void setPort(int port) {
084                this.port = port;
085        }
086
087        /**
088         * Specify the HTTP server's hostname to bind to. Default is localhost;
089         * can be overridden with a specific network address to bind to.
090         */
091        public void setHostname(String hostname) {
092                this.hostname = hostname;
093        }
094
095        /**
096         * Specify the HTTP server's TCP backlog. Default is -1,
097         * indicating the system's default value.
098         */
099        public void setBacklog(int backlog) {
100                this.backlog = backlog;
101        }
102
103        /**
104         * Specify the number of seconds to wait until HTTP exchanges have
105         * completed when shutting down the HTTP server. Default is 0.
106         */
107        public void setShutdownDelay(int shutdownDelay) {
108                this.shutdownDelay = shutdownDelay;
109        }
110
111        /**
112         * Set the JDK concurrent executor to use for dispatching incoming requests.
113         * @see com.sun.net.httpserver.HttpServer#setExecutor
114         */
115        public void setExecutor(Executor executor) {
116                this.executor = executor;
117        }
118
119        /**
120         * Register {@link com.sun.net.httpserver.HttpHandler HttpHandlers}
121         * for specific context paths.
122         * @param contexts a Map with context paths as keys and HttpHandler
123         * objects as values
124         * @see org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter
125         * @see org.springframework.remoting.caucho.SimpleHessianServiceExporter
126         */
127        public void setContexts(Map<String, HttpHandler> contexts) {
128                this.contexts = contexts;
129        }
130
131        /**
132         * Register common {@link com.sun.net.httpserver.Filter Filters} to be
133         * applied to all locally registered {@link #setContexts contexts}.
134         */
135        public void setFilters(List<Filter> filters) {
136                this.filters = filters;
137        }
138
139        /**
140         * Register a common {@link com.sun.net.httpserver.Authenticator} to be
141         * applied to all locally registered {@link #setContexts contexts}.
142         */
143        public void setAuthenticator(Authenticator authenticator) {
144                this.authenticator = authenticator;
145        }
146
147
148        @Override
149        public void afterPropertiesSet() throws IOException {
150                InetSocketAddress address = (this.hostname != null ?
151                                new InetSocketAddress(this.hostname, this.port) : new InetSocketAddress(this.port));
152                this.server = HttpServer.create(address, this.backlog);
153                if (this.executor != null) {
154                        this.server.setExecutor(this.executor);
155                }
156                if (this.contexts != null) {
157                        this.contexts.forEach((key, context) -> {
158                                HttpContext httpContext = this.server.createContext(key, context);
159                                if (this.filters != null) {
160                                        httpContext.getFilters().addAll(this.filters);
161                                }
162                                if (this.authenticator != null) {
163                                        httpContext.setAuthenticator(this.authenticator);
164                                }
165                        });
166                }
167                if (logger.isInfoEnabled()) {
168                        logger.info("Starting HttpServer at address " + address);
169                }
170                this.server.start();
171        }
172
173        @Override
174        public HttpServer getObject() {
175                return this.server;
176        }
177
178        @Override
179        public Class<? extends HttpServer> getObjectType() {
180                return (this.server != null ? this.server.getClass() : HttpServer.class);
181        }
182
183        @Override
184        public boolean isSingleton() {
185                return true;
186        }
187
188        @Override
189        public void destroy() {
190                logger.info("Stopping HttpServer");
191                this.server.stop(this.shutdownDelay);
192        }
193
194}