1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.ws.server;
17
18 import org.openehealth.ipf.commons.ihe.core.ClientAuthType;
19
20 import javax.servlet.Servlet;
21 import java.io.IOException;
22 import java.net.ServerSocket;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Random;
26
27 import static java.util.Objects.requireNonNull;
28
29
30
31
32
33
34
35
36 public abstract class ServletServer {
37 private Servlet servlet;
38 private String servletName;
39 private int port;
40 private String contextPath;
41 private String servletPath;
42 private String contextResource;
43 private boolean secure;
44 private String keystoreFile;
45 private String keystorePass;
46 private String truststoreFile;
47 private String truststorePass;
48 private Map<String, String> initParameters = new HashMap<>();
49 private ClientAuthType clientAuthType;
50
51
52
53
54
55
56 public abstract void start();
57
58
59
60
61
62
63 public abstract void stop();
64
65 public Map<String, String> getInitParameters() {
66 return initParameters;
67 }
68
69
70
71
72
73 public void setServletPath(String servletPath) {
74 requireNonNull(servletPath, "servletPath cannot be null");
75 this.servletPath = servletPath;
76 }
77
78
79
80
81 public String getServletPath() {
82 return servletPath;
83 }
84
85
86
87
88
89 public void setContextPath(String contextPath) {
90 requireNonNull(contextPath, "contextPath cannot be null");
91 this.contextPath = contextPath;
92 }
93
94
95
96
97 public String getContextPath() {
98 return contextPath;
99 }
100
101
102
103
104
105 public void setPort(int port) {
106 this.port = port;
107 }
108
109
110
111
112 public int getPort() {
113 return port;
114 }
115
116
117
118
119
120
121 public void setServlet(Servlet servlet) {
122 requireNonNull(servlet, "servlet cannot be null");
123 this.servlet = servlet;
124 }
125
126
127
128
129 public Servlet getServlet() {
130 return servlet;
131 }
132
133 public String getServletName() {
134 return servletName;
135 }
136
137 public void setServletName(String servletName) {
138 this.servletName = servletName;
139 }
140
141
142
143
144 public boolean isSecure() {
145 return secure;
146 }
147
148
149
150
151
152 public void setSecure(boolean secure) {
153 this.secure = secure;
154 }
155
156
157
158
159 public String getKeystoreFile() {
160 return keystoreFile;
161 }
162
163
164
165
166
167 public void setKeystoreFile(String keystoreFile) {
168 this.keystoreFile = keystoreFile;
169 }
170
171
172
173
174 public String getKeystorePass() {
175 return keystorePass;
176 }
177
178
179
180
181
182 public void setKeystorePass(String keystorePass) {
183 this.keystorePass = keystorePass;
184 }
185
186
187
188
189 public String getTruststoreFile() {
190 return truststoreFile;
191 }
192
193
194
195
196
197 public void setTruststoreFile(String truststoreFile) {
198 this.truststoreFile = truststoreFile;
199 }
200
201
202
203
204 public String getTruststorePass() {
205 return truststorePass;
206 }
207
208
209
210
211
212 public void setTruststorePass(String truststorePass) {
213 this.truststorePass = truststorePass;
214 }
215
216
217
218
219 public String getContextResource() {
220 return contextResource;
221 }
222
223
224
225
226
227 public void setContextResource(String contextResource) {
228 this.contextResource = contextResource;
229 }
230
231
232
233
234 public ClientAuthType getClientAuthType() {
235 return clientAuthType;
236 }
237
238
239
240
241
242 public void setClientAuthType(ClientAuthType clientAuthType) {
243 this.clientAuthType = clientAuthType;
244 }
245
246
247
248
249 public static int getFreePort() {
250 int port = 8000;
251 boolean portFree = false;
252 while (!portFree) {
253 port = 8000 + new Random().nextInt(2000);
254 portFree = isPortFree(port);
255 }
256 return port;
257 }
258
259 private static boolean isPortFree(int port) {
260 ServerSocket socket = null;
261 try {
262 socket = new ServerSocket(port);
263 return true;
264 }
265 catch (IOException e) {
266 return false;
267 }
268 finally {
269 if (socket != null) {
270 try {
271 socket.close();
272 } catch (IOException e) {
273 return false;
274 }
275 }
276 }
277 }
278 }