-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServlet.java
More file actions
308 lines (257 loc) · 11.9 KB
/
HttpServlet.java
File metadata and controls
308 lines (257 loc) · 11.9 KB
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
package javaxt.http.servlet;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;
import org.eclipse.jetty.server.handler.AbstractHandler;
//******************************************************************************
//** HttpServlet Class
//******************************************************************************
/**
* The HttpServer requires an implementation of an HttpServlet in order to
* process HTTP requests.
*
******************************************************************************/
public abstract class HttpServlet {
private Authenticator authenticator;
private javax.net.ssl.KeyManager[] kms;
private javax.net.ssl.TrustManager[] tms;
private String sslProvider;
private ServletContext servletContext;
private String servletPath = "/";
//**************************************************************************
//** init
//**************************************************************************
/** Called by the servlet container to indicate to a servlet that it is
* being placed into service.
*/
public void init(Object servletConfig) throws ServletException {}
//**************************************************************************
//** destroy
//**************************************************************************
/** Called by the servlet container to indicate to a servlet that it is
* being taken out of service or that the server is shutting down.
*/
public void destroy(){};
//**************************************************************************
//** processRequest
//**************************************************************************
/** This method is called each time the server receives an http request (GET,
* POST, HEAD, etc.). Use this method to formulate a response to the client.
*/
public abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException;
//**************************************************************************
//** getServletContext
//**************************************************************************
/** Returns the ServletContext.
*/
public ServletContext getServletContext(){
return servletContext;
}
//**************************************************************************
//** setServletContext
//**************************************************************************
public void setServletContext(ServletContext servletContext){
this.servletContext = servletContext;
//Instantiate the WebSocketServer
if (servletContext.getAttribute("org.eclipse.jetty.server.Handler") instanceof AbstractHandler){
if (servletContext.getAttribute("javaxt.http.websocket.WebSocketServer")==null){
servletContext.setAttribute("javaxt.http.websocket.WebSocketServer",
new javaxt.http.websocket.WebSocketServer(this)
);
}
}
}
//**************************************************************************
//** getServletPath
//**************************************************************************
/** Returns the path to the servlet. This path starts with a "/" character
* and includes either the servlet name or a path to the servlet, but does
* not include any extra path information or a query string.
*/
public String getServletPath(){
return servletPath;
}
//**************************************************************************
//** setServletPath
//**************************************************************************
/** Used to set the path to the servlet.
*/
public void setServletPath(String servletPath){
if (servletPath==null){
servletPath = "/";
}
else{
if (servletPath.length()>1 && servletPath.startsWith("/")) servletPath = servletPath.substring(1);
if (servletPath.endsWith("/")) servletPath = servletPath.substring(0, servletPath.length()-1);
servletPath = "/" + servletPath;
}
this.servletPath = servletPath;
}
//**************************************************************************
//** log
//**************************************************************************
/** Writes the specified message to a servlet log. This method has not
* been implemented.
*/
public void log(String str){
//TODO: Implement logger
}
// //**************************************************************************
// //** setPaths
// //**************************************************************************
// /** Used to set the context and servlet paths used in the
// * HttpServletRequest.getContextPath() and the
// * HttpServletRequest.getServletPath() methods.
// */
// public void setPaths(String contextPath, String servletPath){
// //TODO: Update logic used to assign context path
// //this.getServletContext().setContextPath(contextPath);
// //this.servletPath = servletPath;
// }
// //**************************************************************************
// //** setSessionStore
// //**************************************************************************
// public void setSessionStore(java.io.File file){
// FileSessionDataStore sessionStore = new FileSessionDataStore();
// sessionStore.setStoreDir(file);
// this.sessionStore = sessionStore;
// }
//
//**************************************************************************
//** setAuthenticator
//**************************************************************************
/** Used to define an Authenticator used to authenticate requests.
*/
public void setAuthenticator(Authenticator authenticator){
this.authenticator = authenticator;
}
//**************************************************************************
//** getAuthenticator
//**************************************************************************
/** Returns a new instance of an Authenticator used to authenticate users.
*/
protected Authenticator getAuthenticator(HttpServletRequest request){
if (authenticator!=null) return authenticator.newInstance(request);
else return null;
}
//**************************************************************************
//** setKeyStore
//**************************************************************************
/** Used to specify a KeyStore. The KeyStore is used to store keys and
* certificates for SSL.
*/
public void setKeyStore(KeyStore keystore, String passphrase) throws Exception {
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keystore, passphrase.toCharArray());
kms = kmf.getKeyManagers();
}
//**************************************************************************
//** setKeyStore
//**************************************************************************
/** Used to specify a KeyStore. The KeyStore is used to store keys and
* certificates for SSL.
*/
public void setKeyStore(java.io.File keyStoreFile, String passphrase) throws Exception {
char[] pw = passphrase.toCharArray();
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(new java.io.FileInputStream(keyStoreFile), pw);
setKeyStore(keystore, passphrase);
}
//**************************************************************************
//** setKeyManager
//**************************************************************************
/** Used to specify a KeyManager. The KeyManager is responsible for managing
* keys and certificates found in a KeyStore and is used to initialize the
* SSLContext. Typically, users are not required to specify a KeyManager.
* Instead, a KeyManager is selected for you whenever the setKeyStore()
* method is called. However, in some cases, the default KeyManager is not
* adequate (e.g. managing KeyStores with multiple SSL certificates) and
* users need to specify a different KeyManager.
*/
public void setKeyManager(javax.net.ssl.KeyManager keyManager) throws Exception {
kms = new javax.net.ssl.KeyManager[]{keyManager};
}
//**************************************************************************
//** setTrustStore
//**************************************************************************
/** Used to set the TrustStore and initialize the TrustManagerFactory. The
* TrustStore is used to store public keys and certificates for SSL.
*/
public void setTrustStore(KeyStore truststore) throws Exception {
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(truststore);
tms = tmf.getTrustManagers();
}
//**************************************************************************
//** setTrustStore
//**************************************************************************
/** Used to set the TrustStore and initialize the TrustManagerFactory. The
* TrustStore is used to store public keys and certificates for SSL.
*/
public void setTrustStore(java.io.File trustStoreFile, String passphrase) throws Exception {
char[] pw = passphrase.toCharArray();
KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(new java.io.FileInputStream(trustStoreFile), pw);
setTrustStore(truststore);
}
//**************************************************************************
//** setSSLProvider
//**************************************************************************
/** Used to specify an Security Provider used to decrypt SSL/TLS messages.
*/
public void setSSLProvider(java.security.Provider provider){
if (provider!=null){
sslProvider = provider.getName();
//java.security.Security.addProvider(provider);
}
else sslProvider = null;
}
//**************************************************************************
//** setSSLProvider
//**************************************************************************
/** Used to specify an Security Provider used to decrypt SSL/TLS messages.
*/
public void setSSLProvider(String provider){
setSSLProvider(java.security.Security.getProvider(provider));
}
//**************************************************************************
//** getSSLContext
//**************************************************************************
/** Used to initialize an SSLContext which, in turn is used by an SSLEngine
* decrypt SSL/TLS messages.
*/
public SSLContext getSSLContext() throws ServletException {
/*//Debug use only!
java.security.Provider provider = new SSLProvider();
java.security.Security.addProvider(provider);
setSSLProvider(provider);
*/
SSLContext sslContext = null;
try{
if (sslProvider==null) sslContext = SSLContext.getInstance("TLS");
else sslContext = SSLContext.getInstance("TLS", sslProvider);
sslContext.init(kms, tms, null);
}
catch(Exception e){
ServletException se = new ServletException("Failed to initialize SSLContext.");
se.initCause(e);
throw se;
}
return sslContext;
}
//**************************************************************************
//** supportsHttps
//**************************************************************************
/** Returns true if the servlet has been configured to support HTTP/SSL.
* This is determined by checking if a KeyStore or a KeyManager has been
* assigned.
*/
public boolean supportsHttps(){
if (kms!=null && kms.length>0){
if (kms[0]!=null) return true;
}
return false;
}
}