/
SymmetricWebServer.java
377 lines (302 loc) · 13.3 KB
/
SymmetricWebServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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.jumpmind.symmetric;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import javax.management.Attribute;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.servlet.ServletContext;
import mx4j.tools.adaptor.http.HttpAdaptor;
import mx4j.tools.adaptor.http.XSLTProcessor;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.http.security.Constraint;
import org.eclipse.jetty.http.security.Password;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.server.ssl.SslSocketConnector;
import org.eclipse.jetty.webapp.WebAppContext;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.SecurityConstants;
import org.jumpmind.symmetric.common.SystemConstants;
import org.jumpmind.symmetric.common.logging.ILog;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.util.AppUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* Start up SymmetricDS through an embedded Jetty instance.
*
* @see SymmetricLauncher#main(String[])
*/
public class SymmetricWebServer {
protected static final ILog log = LogFactory.getLog(SymmetricWebServer.class);
protected static final String DEFAULT_WEBAPP_DIR = "../web";
/**
* The type of HTTP connection to create for this SymmetricDS web server
*/
public enum Mode {
HTTP, HTTPS, MIXED;
}
private Server server;
private WebAppContext webapp;
protected boolean join = true;
protected boolean createJmxServer = true;
protected String webHome = "/sync";
protected int maxIdleTime = 7200000;
protected int httpPort = -1;
protected int httpsPort = -1;
protected String basicAuthUsername = null;
protected String basicAuthPassword = null;
protected String propertiesFile = null;
protected String host = null;
protected boolean noNio = false;
protected boolean noDirectBuffer = false;
protected String webAppDir = DEFAULT_WEBAPP_DIR;
public SymmetricWebServer() {
}
public SymmetricWebServer(String propertiesUrl) {
this(propertiesUrl, DEFAULT_WEBAPP_DIR);
}
public SymmetricWebServer(String propertiesUrl, String webappDir) {
this.propertiesFile = propertiesUrl ;
this.webAppDir = webappDir;
}
public SymmetricWebServer(int maxIdleTime, String propertiesUrl, boolean join, boolean noNio, boolean noDirectBuffer) {
this(propertiesUrl, DEFAULT_WEBAPP_DIR);
this.maxIdleTime = maxIdleTime;
this.join = join;
this.noDirectBuffer = noDirectBuffer;
this.noNio = noNio;
}
public SymmetricWebServer(int maxIdleTime, String propertiesUrl) {
this(propertiesUrl, DEFAULT_WEBAPP_DIR);
this.maxIdleTime = maxIdleTime;
}
public SymmetricWebServer start(int port, String propertiesUrl) throws Exception {
this.propertiesFile = propertiesUrl ;
return start(port);
}
public SymmetricWebServer start() throws Exception {
if (httpPort > 0 && httpsPort > 0) {
return startMixed(httpPort, httpsPort);
} else if (httpPort > 0) {
return start(httpPort);
} else if (httpsPort > 0) {
return startSecure(httpsPort);
} else {
throw new IllegalStateException("Either an http or https port needs to be set before starting the server.");
}
}
public SymmetricWebServer start(int port) throws Exception {
return start(port, 0, Mode.HTTP);
}
public SymmetricWebServer startSecure(int port) throws Exception {
return start(0, port, Mode.HTTPS);
}
public SymmetricWebServer startMixed(int port, int securePort) throws Exception {
return start(port, securePort, Mode.MIXED);
}
public SymmetricWebServer start(int port, int securePort, Mode mode) throws Exception {
// indicate to the app that we are in standalone mode
System.setProperty(Constants.PROP_STANDALONE_WEB, "true");
server = new Server();
server.setConnectors(getConnectors(port, securePort, mode));
setupBasicAuthIfNeeded(server);
webapp = new WebAppContext();
webapp.setContextPath(webHome);
webapp.setWar(webAppDir);
webapp.getSessionHandler().getSessionManager().setMaxInactiveInterval(maxIdleTime/1000);
server.setHandler(webapp);
if (!StringUtils.isBlank(propertiesFile)) {
System.setProperty(Constants.OVERRIDE_PROPERTIES_FILE_1, propertiesFile);
}
server.start();
if (createJmxServer) {
int httpJmxPort = port != 0 ? port + 1 : securePort + 1;
registerHttpJmxAdaptor(httpJmxPort);
}
if (join) {
log.info("WebServerAboutToJoin");
server.join();
}
return this;
}
protected ServletContext getServletContext() {
return webapp != null ? webapp.getServletContext() : null;
}
public ISymmetricEngine getEngine() {
ISymmetricEngine engine = null;
ServletContext servletContext = getServletContext();
if (servletContext != null) {
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
engine = appContext.getBean(ISymmetricEngine.class);
}
return engine;
}
protected void setupBasicAuthIfNeeded(Server server) {
if (StringUtils.isNotBlank(basicAuthUsername) ) {
ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);;
constraint.setRoles(new String[]{SecurityConstants.EMBEDDED_WEBSERVER_DEFAULT_ROLE});
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
//sh.setConstraintMappings(new ConstraintMapping[] {cm});
sh.addConstraintMapping(cm);
sh.setAuthenticator(new BasicAuthenticator());
HashLoginService loginService = new HashLoginService();
loginService.putUser(basicAuthUsername, new Password(basicAuthPassword), null);
sh.setLoginService(loginService);
server.setHandler(sh);
}
}
protected Connector[] getConnectors(int port, int securePort, Mode mode) {
ArrayList<Connector> connectors = new ArrayList<Connector>();
String keyStoreFile = System.getProperty(SecurityConstants.SYSPROP_KEYSTORE);
if (mode.equals(Mode.HTTP) || mode.equals(Mode.MIXED)) {
Connector connector = null;
if (noNio) {
connector = new SocketConnector();
} else {
SelectChannelConnector nioConnector = new SelectChannelConnector();
nioConnector.setUseDirectBuffers(!noDirectBuffer);
connector = nioConnector;
}
connector.setPort(port);
connector.setHost(host);
connector.setMaxIdleTime(maxIdleTime);
connectors.add(connector);
log.info("WebServerStarting", port);
}
if (mode.equals(Mode.HTTPS) || mode.equals(Mode.MIXED)) {
Connector connector = new SslSocketConnector();
String keyStorePassword = System.getProperty(SecurityConstants.SYSPROP_KEYSTORE_PASSWORD);
keyStorePassword = (keyStorePassword != null) ? keyStorePassword : SecurityConstants.KEYSTORE_PASSWORD;
((SslSocketConnector) connector).setKeystore(keyStoreFile);
((SslSocketConnector) connector).setKeyPassword(keyStorePassword);
((SslSocketConnector) connector).setMaxIdleTime(maxIdleTime);
connector.setPort(securePort);
connector.setHost(host);
connectors.add(connector);
log.info("WebServerSecureStarting", securePort);
}
return connectors.toArray(new Connector[connectors.size()]);
}
protected void registerHttpJmxAdaptor(int jmxPort) throws Exception {
if (AppUtils.isSystemPropertySet(SystemConstants.JMX_HTTP_CONSOLE_ENABLED, true)) {
log.info("JMXConsoleStarting", jmxPort);
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = getHttpJmxAdaptorName();
mbeanServer.createMBean(HttpAdaptor.class.getName(), name);
if (!AppUtils.isSystemPropertySet(SystemConstants.JMX_HTTP_CONSOLE_LOCALHOST_ENABLED, true)) {
mbeanServer.setAttribute(name, new Attribute("Host", "0.0.0.0"));
}
mbeanServer.setAttribute(name, new Attribute("Port", new Integer(jmxPort)));
ObjectName processorName = getXslJmxAdaptorName();
mbeanServer.createMBean(XSLTProcessor.class.getName(), processorName);
mbeanServer.setAttribute(name, new Attribute("ProcessorName", processorName));
mbeanServer.invoke(name, "start", null, null);
}
}
protected ObjectName getHttpJmxAdaptorName() throws MalformedObjectNameException {
return new ObjectName("Server:name=HttpAdaptor");
}
protected ObjectName getXslJmxAdaptorName() throws MalformedObjectNameException {
return new ObjectName("Server:name=XSLTProcessor");
}
protected void removeHttpJmxAdaptor() {
if (AppUtils.isSystemPropertySet(SystemConstants.JMX_HTTP_CONSOLE_ENABLED, true)) {
try {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
mbeanServer.unregisterMBean(getHttpJmxAdaptorName());
mbeanServer.unregisterMBean(getXslJmxAdaptorName());
} catch (Exception e) {
log.warn("JMXAdaptorUnregisterFailed");
}
}
}
public void stop() throws Exception {
if (server != null) {
if (createJmxServer) {
removeHttpJmxAdaptor();
}
server.stop();
}
}
public static void main(String[] args) throws Exception {
new SymmetricWebServer().start(8080);
}
public boolean isJoin() {
return join;
}
public void setJoin(boolean join) {
this.join = join;
}
public void setWebHome(String webHome) {
this.webHome = webHome;
}
public int getMaxIdleTime() {
return maxIdleTime;
}
public void setMaxIdleTime(int maxIdleTime) {
this.maxIdleTime = maxIdleTime;
}
public void setHttpPort(int httpPort) {
this.httpPort = httpPort;
}
public void setHttpsPort(int httpsPort) {
this.httpsPort = httpsPort;
}
public void setPropertiesFile(String propertiesFile) {
this.propertiesFile = propertiesFile;
}
public void setCreateJmxServer(boolean createJmxServer) {
this.createJmxServer = createJmxServer;
}
public void setHost(String host) {
this.host = host;
}
public void setBasicAuthPassword(String basicAuthPassword) {
this.basicAuthPassword = basicAuthPassword;
}
public void setBasicAuthUsername(String basicAuthUsername) {
this.basicAuthUsername = basicAuthUsername;
}
public void setWebAppDir(String webAppDir) {
this.webAppDir = webAppDir;
}
public void setNoNio(boolean noNio) {
this.noNio = noNio;
}
public void setNoDirectBuffer(boolean noDirectBuffer) {
this.noDirectBuffer = noDirectBuffer;
}
}