This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
/
JettyServerFactory.java
152 lines (128 loc) · 5.73 KB
/
JettyServerFactory.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
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.rest.api.standalone.jetty;
import java.lang.management.ManagementFactory;
import java.util.concurrent.ArrayBlockingQueue;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class JettyServerFactory implements FactoryBean<Server> {
private static final String KEYSTORE_TYPE_PKCS12 = "pkcs12";
private static final String KEYSTORE_TYPE_JKS = "jks";
@Autowired
private JettyConfiguration jettyConfiguration;
@Override
public Server getObject() throws Exception {
// Setup ThreadPool
QueuedThreadPool threadPool = new JettyQueuedThreadPool(
jettyConfiguration.getPoolMaxThreads(),
jettyConfiguration.getPoolMinThreads(),
jettyConfiguration.getPoolIdleTimeout(),
new ArrayBlockingQueue<Runnable>(jettyConfiguration.getPoolQueueSize())
);
threadPool.setName("gravitee-listener");
Server server = new Server(threadPool);
// Extra options
server.setDumpAfterStart(false);
server.setDumpBeforeStop(false);
server.setStopAtShutdown(true);
// Setup JMX
if (jettyConfiguration.isJmxEnabled()) {
MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbContainer);
}
// HTTP Configuration
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setOutputBufferSize(32768);
httpConfig.setRequestHeaderSize(8192);
httpConfig.setResponseHeaderSize(8192);
httpConfig.setSendServerVersion(false);
httpConfig.setSendDateHeader(false);
// Setup Jetty HTTP or HTTPS Connector
if (jettyConfiguration.isSecured()) {
httpConfig.setSecureScheme("https");
httpConfig.setSecurePort(jettyConfiguration.getHttpPort());
// SSL Context Factory
SslContextFactory sslContextFactory = new SslContextFactory.Server();
if (jettyConfiguration.getKeyStorePath() != null) {
sslContextFactory.setKeyStorePath(jettyConfiguration.getKeyStorePath());
sslContextFactory.setKeyStorePassword(jettyConfiguration.getKeyStorePassword());
if (KEYSTORE_TYPE_PKCS12.equalsIgnoreCase(jettyConfiguration.getKeyStoreType())) {
sslContextFactory.setKeyStoreType(KEYSTORE_TYPE_PKCS12);
}
}
if (jettyConfiguration.getTrustStorePath() != null) {
sslContextFactory.setTrustStorePath(jettyConfiguration.getTrustStorePath());
sslContextFactory.setTrustStorePassword(jettyConfiguration.getTrustStorePassword());
if (KEYSTORE_TYPE_PKCS12.equalsIgnoreCase(jettyConfiguration.getTrustStoreType())) {
sslContextFactory.setTrustStoreType(KEYSTORE_TYPE_PKCS12);
}
}
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
httpsConfig.addCustomizer(new SecureRequestCustomizer());
ServerConnector https = new ServerConnector(
server,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(httpsConfig)
);
https.setHost(jettyConfiguration.getHttpHost());
https.setPort(jettyConfiguration.getHttpPort());
server.addConnector(https);
} else {
ServerConnector http = new ServerConnector(
server,
jettyConfiguration.getAcceptors(),
jettyConfiguration.getSelectors(),
new HttpConnectionFactory(httpConfig)
);
http.setHost(jettyConfiguration.getHttpHost());
http.setPort(jettyConfiguration.getHttpPort());
http.setIdleTimeout(jettyConfiguration.getIdleTimeout());
server.addConnector(http);
}
// Setup Jetty statistics
if (jettyConfiguration.isStatisticsEnabled()) {
StatisticsHandler stats = new StatisticsHandler();
stats.setHandler(server.getHandler());
server.setHandler(stats);
}
if (jettyConfiguration.isAccessLogEnabled()) {
CustomRequestLog requestLog = new CustomRequestLog(
new AsyncRequestLogWriter(jettyConfiguration.getAccessLogPath()),
CustomRequestLog.EXTENDED_NCSA_FORMAT
);
server.setRequestLog(requestLog);
}
return server;
}
@Override
public Class<?> getObjectType() {
return Server.class;
}
@Override
public boolean isSingleton() {
return true;
}
}