View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Simple abstraction of an embedded servlet server (e.g. Jetty or Tomcat).
32   * <p>
33   * Note: any exceptions thrown are treated as assertion failures.
34   * @author Jens Riemschneider
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       * Starts the server.
53       * <p>
54       * Note: any exceptions thrown are treated as assertion failures.
55       */
56      public abstract void start();
57      
58      /**
59       * Stops the server if it was running.
60       * <p>
61       * Note: any exceptions thrown are treated as assertion failures.
62       */
63      public abstract void stop();
64  
65      public Map<String, String> getInitParameters() {
66          return initParameters;
67      }
68  
69      /**
70       * @param servletPath
71       *          the path of the servlet itself (including patterns if needed).
72       */
73      public void setServletPath(String servletPath) {
74          requireNonNull(servletPath, "servletPath cannot be null");
75          this.servletPath = servletPath;
76      }
77  
78      /**
79       * @return the path of the servlet itself (including patterns if needed).
80       */
81      public String getServletPath() {
82          return servletPath;
83      }
84  
85      /**
86       * @param contextPath
87       *          the path of the context in which the servlet is running.
88       */
89      public void setContextPath(String contextPath) {
90          requireNonNull(contextPath, "contextPath cannot be null");
91          this.contextPath = contextPath;
92      }
93  
94      /**
95       * @return the path of the context in which the servlet is running.
96       */
97      public String getContextPath() {
98          return contextPath;
99      }
100 
101     /**
102      * @param port
103      *          the port that the server is started on.
104      */
105     public void setPort(int port) {
106         this.port = port;
107     }
108 
109     /**
110      * @return the port that the server is started on.
111      */
112     public int getPort() {
113         return port;
114     }
115     
116     /**
117      * The servlet to use within the servlet.
118      * @param servlet
119      *          the servlet.      
120      */
121     public void setServlet(Servlet servlet) {
122         requireNonNull(servlet, "servlet cannot be null");
123         this.servlet = servlet;
124     }
125 
126     /**
127      * @return the servlet to use within the servlet.
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      * @return <code>true</code> to enable SSL.
143      */
144     public boolean isSecure() {
145         return secure;
146     }
147 
148     /**
149      * @param secure
150      *          <code>true</code> to enable SSL.
151      */
152     public void setSecure(boolean secure) {
153         this.secure = secure;
154     }
155 
156     /**
157      * @return key store location.
158      */
159     public String getKeystoreFile() {
160         return keystoreFile;
161     }
162 
163     /**
164      * @param keystoreFile
165      *          key store location.
166      */
167     public void setKeystoreFile(String keystoreFile) {
168         this.keystoreFile = keystoreFile;
169     }
170 
171     /**
172      * @return key store password.
173      */
174     public String getKeystorePass() {
175         return keystorePass;
176     }
177 
178     /**
179      * @param keystorePass
180      *          key store password.
181      */
182     public void setKeystorePass(String keystorePass) {
183         this.keystorePass = keystorePass;
184     }
185 
186     /**
187      * @return trust store location.
188      */
189     public String getTruststoreFile() {
190         return truststoreFile;
191     }
192 
193     /**
194      * @param truststoreFile
195      *          trust store location.
196      */
197     public void setTruststoreFile(String truststoreFile) {
198         this.truststoreFile = truststoreFile;
199     }
200 
201     /**
202      * @return trust store password.
203      */
204     public String getTruststorePass() {
205         return truststorePass;
206     }
207 
208     /**
209      * @param truststorePass
210      *          trust store password.
211      */
212     public void setTruststorePass(String truststorePass) {
213         this.truststorePass = truststorePass;
214     }
215 
216     /**
217      * @return location of a spring application context to be started with the web-app.
218      */
219     public String getContextResource() {
220         return contextResource;
221     }
222 
223     /**
224      * @param contextResource
225      *          location of a spring application context to be started with the web-app.
226      */
227     public void setContextResource(String contextResource) {
228         this.contextResource = contextResource;
229     }
230 
231     /**
232      * @return type of client authentication.
233      */
234     public ClientAuthType getClientAuthType() {
235         return clientAuthType;
236     }
237 
238     /**
239      * @param clientAuthType
240      *      type of client authentication.
241      */
242     public void setClientAuthType(ClientAuthType clientAuthType) {
243         this.clientAuthType = clientAuthType;
244     }
245 
246     /**
247      * @return a free port for testing between 8000-9999.
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 }