This repository was archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleHttpServer.java
More file actions
545 lines (501 loc) · 19.7 KB
/
Copy pathSimpleHttpServer.java
File metadata and controls
545 lines (501 loc) · 19.7 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package com.kttdevelopment.simplehttpserver;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpContext;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.Executor;
/**
* <i>This class is a simplified implementation of {@link HttpServer}.</i><br>
* At least one {@link HttpHandler} must be created in order to process requests. When handling requests the server will use the most specific context. If no handler can be found it is rejected with a 404 response. <br>
* <b>Contexts are case-sensitive.</b>
*
* @see HttpServer
* @see HttpHandler
* @see SimpleHttpHandler
* @since 02.00.00
* @version 02.00.00
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
public abstract class SimpleHttpServer {
/**
* Create an empty {@link SimpleHttpServer}. Applications don't use this method.
*
* @see SimpleHttpServerImpl#createSimpleHttpServer(Integer, Integer)
* @since 02.00.00
* @author Ktt Development
*/
SimpleHttpServer(){ }
//
/**
* Creates a {@link SimpleHttpServer}.
*
* @return a {@link SimpleHttpServer}
* @throws IOException uncaught exception
*
* @since 02.00.00
* @author Ktt Development
*/
public static SimpleHttpServer create() throws IOException {
return SimpleHttpServerImpl.createSimpleHttpServer(null,null);
}
/**
* Creates a {@link SimpleHttpServer} bounded to a port.
*
* @param port port to bind to
* @return a {@link SimpleHttpServer}
* @throws java.net.BindException if server can not bind to port
* @throws NullPointerException if address is <code>null</code>
* @throws IllegalArgumentException if port is out of range
* @throws IOException uncaught exception
*
* @since 02.00.00
* @author Ktt Development
*/
public static SimpleHttpServer create(final int port) throws IOException {
return SimpleHttpServerImpl.createSimpleHttpServer(port,null);
}
/**
* Creates a {@link SimpleHttpServer} bounded to a port.
*
* @param port port to bind to
* @param backlog request backlog
* @return a {@link SimpleHttpServer}
* @throws java.net.BindException if server can not bind to port
* @throws NullPointerException if address is <code>null</code>
* @throws IllegalArgumentException if port is out of range
* @throws IOException uncaught exception
*
* @since 02.00.00
* @author Ktt Development
*/
public static SimpleHttpServer create(final int port, final int backlog) throws IOException {
return SimpleHttpServerImpl.createSimpleHttpServer(port,backlog);
}
//
/**
* Returns the native http server.
*
* @return http server
*
* @see HttpServer
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpServer getHttpServer();
//
/**
* Binds the server to a port.
*
* @param port port to bind the server to
* @return address the server is binded to
* @throws java.net.BindException if server could not be bound to port, or if it's already bound
* @throws IllegalArgumentException if port is out of range
* @throws NullPointerException if address is <code>null</code>
* @throws IOException uncaught exception
*
* @see #bind(int, int)
* @see #bind(InetSocketAddress)
* @see #bind(InetSocketAddress, int)
* @since 02.00.00
* @author Ktt Development
*/
public abstract InetSocketAddress bind(final int port) throws IOException;
/**
* Binds the server to a port.
*
* @param port port to bind the server to
* @param backlog request backlog
* @return address the server is binded to
* @throws java.net.BindException if server could not be bound to port, or if it's already bound
* @throws IllegalArgumentException if port is out of range
* @throws NullPointerException if address is <code>null</code>
* @throws IOException uncaught exception
*
* @see #bind(int)
* @see #bind(InetSocketAddress)
* @see #bind(InetSocketAddress, int)
* @since 02.00.00
* @author Ktt Development
*/
public abstract InetSocketAddress bind(final int port, final int backlog) throws IOException;
/**
* Binds the server to a port.
*
* @param addr address to bind the server to
* @throws java.net.BindException if server could not be bound to port, or if it's already bound
* @throws IllegalArgumentException if port is out of range
* @throws NullPointerException if address is <code>null</code>
* @throws IOException uncaught exception
*
* @see InetSocketAddress
* @see #bind(int)
* @see #bind(int, int)
* @see #bind(InetSocketAddress, int)
* @since 02.00.00
* @author Ktt Development
*/
public abstract void bind(final InetSocketAddress addr) throws IOException;
/**
* Binds the server to a port.
*
* @param addr address to bind the server to
* @param backlog request backlog
* @throws java.net.BindException if server could not be bound to port, or if it's already bound
* @throws NullPointerException if address is <code>null</code>
* @throws IOException uncaught exception
*
* @see InetSocketAddress
* @see #bind(int)
* @see #bind(int, int)
* @see #bind(InetSocketAddress)
* @since 02.00.00
* @author Ktt Development
*/
public abstract void bind(final InetSocketAddress addr, final int backlog) throws IOException;
//
/**
* Returns the address that the server is binded to.
*
* @return binded address
*
* @see InetSocketAddress
* @since 02.00.00
* @author Ktt Development
*/
public abstract InetSocketAddress getAddress();
//
/**
* Sets the server's executor. <br>
* For unlimited simultaneous threading use {@link java.util.concurrent.Executors#newCachedThreadPool()}; for limited simultaneous threading use {@link java.util.concurrent.Executors#newFixedThreadPool(int)}. <br>
*
* @param executor server executor
*
* @see #getExecutor()
* @see Executor
* @since 02.00.00
* @author Ktt Development
*/
public abstract void setExecutor(final Executor executor);
/**
* Returns the server's executor or null if none exists.
*
* @return server executor
*
* @see #setExecutor(Executor)
* @see Executor
* @since 02.00.00
* @author Ktt Development
*/
public abstract Executor getExecutor();
//
/**
* Creates an empty context.
*
* @param context the context
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see #createContext(String, HttpHandler)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createContext(final String context);
/**
* Creates a context mapped to a specified {@link HttpHandler}.
*
* @param context the context
* @param handler the handler
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see HttpHandler
* @see #createContext(String)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createContext(final String context, final HttpHandler handler);
//
/**
* Creates a temporary context at a random address that will remove itself after the first connection. This type of context is typically used for single use downloads or media file hosting. <br>
* To get the context use {@link HttpContext#getPath()}.
*
* @return the http context associated with the context
*
* @see HttpContext
* @see #createTemporaryContext(long)
* @see #createTemporaryContext(HttpHandler)
* @see #createTemporaryContext(HttpHandler, long)
* @see #createTemporaryContext(String)
* @see #createTemporaryContext(String, long)
* @see #createTemporaryContext(String, HttpHandler)
* @see #createTemporaryContext(String, HttpHandler, long)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createTemporaryContext();
/**
* Creates a temporary context at a random address that will remove itself after the first connection or after the max time is passed. This type of context is typically used for single use downloads or media file hosting. <br>
* To get the context use {@link HttpContext#getPath()}.
*
* @param maxTime the maximum time the context may exist for (in milliseconds)
* @return the http context associated with the context
*
* @see HttpContext
* @see #createTemporaryContext()
* @see #createTemporaryContext(HttpHandler)
* @see #createTemporaryContext(HttpHandler, long)
* @see #createTemporaryContext(String)
* @see #createTemporaryContext(String, long)
* @see #createTemporaryContext(String, HttpHandler)
* @see #createTemporaryContext(String, HttpHandler, long)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createTemporaryContext(final long maxTime);
/**
* Creates a temporary context at a random address that will remove itself after the first connection. This type of context is typically used for single use downloads or media file hosting. <br>
* To get the context use {@link HttpContext#getPath()}. <br>
* This method encapsulates the handler with a temporary one, so {@link HttpContext#getHandler()} will not return the one passed in the parameter, instead it will return the encapsulating handler.
*
* @param handler handler to use
* @return the http context associated with the context
*
* @see HttpContext
* @see HttpHandler
* @see #createTemporaryContext()
* @see #createTemporaryContext(long)
* @see #createTemporaryContext(HttpHandler, long)
* @see #createTemporaryContext(String)
* @see #createTemporaryContext(String, long)
* @see #createTemporaryContext(String, HttpHandler)
* @see #createTemporaryContext(String, HttpHandler, long)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createTemporaryContext(final HttpHandler handler);
/**
* Creates a temporary context at a random address that will remove itself after the first connection or after the max time is passed. This type of context is typically used for single use downloads or media file hosting. <br>
* To get the context use {@link HttpContext#getPath()}. <br>
* This method encapsulates the handler with a temporary one, so {@link HttpContext#getHandler()} will not return the one passed in the parameter, instead it will return the encapsulating handler.
*
* @param handler handler to use
* @param maxTime the maximum time the context may exist for (in milliseconds)
* @return the http context associated with the context
*
* @see HttpContext
* @see HttpHandler
* @see #createTemporaryContext()
* @see #createTemporaryContext(long)
* @see #createTemporaryContext(HttpHandler)
* @see #createTemporaryContext(String)
* @see #createTemporaryContext(String, long)
* @see #createTemporaryContext(String, HttpHandler)
* @see #createTemporaryContext(String, HttpHandler, long)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createTemporaryContext(final HttpHandler handler, final long maxTime);
/**
* Creates a temporary context at a specified address that will remove itself after the first connection. This type of context is typically used for single use downloads or media file hosting. <br>
*
* @param context the context
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see #createTemporaryContext()
* @see #createTemporaryContext(long)
* @see #createTemporaryContext(HttpHandler)
* @see #createTemporaryContext(HttpHandler, long)
* @see #createTemporaryContext(String, long)
* @see #createTemporaryContext(String, HttpHandler)
* @see #createTemporaryContext(String, HttpHandler, long)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createTemporaryContext(final String context);
/**
* Creates a temporary context at a specified address that will remove itself after the first connection or after the max time is passed. This type of context is typically used for single use downloads or media file hosting. <br>
*
* @param context the context
* @param maxTime the maximum time the context may exist for (in milliseconds)
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see HttpHandler
* @see #createTemporaryContext()
* @see #createTemporaryContext(long)
* @see #createTemporaryContext(HttpHandler)
* @see #createTemporaryContext(HttpHandler, long)
* @see #createTemporaryContext(String)
* @see #createTemporaryContext(String, HttpHandler)
* @see #createTemporaryContext(String, HttpHandler, long)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createTemporaryContext(final String context, final long maxTime);
/**
* Creates a temporary context at a specified address that will remove itself after the first connection. This type of context is typically used for single use downloads or media file hosting. <br>
* This method encapsulates the handler with a temporary one, so {@link HttpContext#getHandler()} will not return the one passed in the parameter, instead it will return the encapsulating handler.
*
* @param context the context
* @param handler handler to use
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see HttpHandler
* @see #createTemporaryContext()
* @see #createTemporaryContext(long)
* @see #createTemporaryContext(HttpHandler)
* @see #createTemporaryContext(HttpHandler, long)
* @see #createTemporaryContext(String)
* @see #createTemporaryContext(String, long)
* @see #createTemporaryContext(String, HttpHandler, long)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createTemporaryContext(final String context, final HttpHandler handler);
/**
* Creates a temporary context at a random address that will remove itself after the first connection or after the max time is passed. This type of context is typically used for single use downloads or media file hosting. <br>
* To get the context use {@link HttpContext#getPath()}. <br>
* This method encapsulates the handler with a temporary one, so {@link HttpContext#getHandler()} will not return the one passed in the parameter, instead it will return the encapsulating handler.
*
* @param context the context
* @param handler handler to use
* @param maxTime the maximum time the context may exist for (in milliseconds)
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see HttpHandler
* @see #createTemporaryContext()
* @see #createTemporaryContext(long)
* @see #createTemporaryContext(HttpHandler)
* @see #createTemporaryContext(HttpHandler, long)
* @see #createTemporaryContext(String)
* @see #createTemporaryContext(String, long)
* @see #createTemporaryContext(String, HttpHandler)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createTemporaryContext(final String context, final HttpHandler handler, final long maxTime);
//
/**
* Removes the context at the context.
*
* @param context the context to remove
* @throws IllegalArgumentException if no handler at that context exists
* @throws NullPointerException if the context is null
*
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract void removeContext(final String context);
/**
* Removes the context from the server.
* @param context the context to remove
* @throws IllegalArgumentException if no handler at that context exists
* @throws NullPointerException if the context is null
*
* @see #removeContext(String)
* @since 02.00.00
* @author Ktt Development
*/
public abstract void removeContext(final HttpContext context);
/**
* Returns the handler mapped to a context or null if none is found.
*
* @param context context to retrieve
* @return handler associated with that context
*
* @see #getContextHandler(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpHandler getContextHandler(final String context);
/**
* Returns the handler mapped to a context or null if it is not found.
*
* @param context context to retrieve
* @return handler associated with that context
*
* @see #getContextHandler(String)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpHandler getContextHandler(final HttpContext context);
/**
* Returns a copy of the server's contexts and their respective handlers.
*
* @return server's contexts
* @since 02.00.00
* @author Ktt Development
*/
public abstract Map<HttpContext,HttpHandler> getContexts();
//
/**
* Starts the server.
*
* @throws IllegalStateException if server is not bound to a valid port
*
* @see #stop()
* @see #stop(int)
* @since 2.00.00
* @author Ktt Development
*/
public abstract void start();
/**
* Stops the server and all active requests.
*
* @see #start()
* @see #stop(int)
* @since 02.00.00
* @author Ktt Development
*/
public abstract void stop();
/**
* Stops the server with a delay for remaining requests.
*
* @param delay delay until server stops all active requests
*
* @see #start()
* @see #stop(int)
* @since 02.00.00
* @author Ktt Development
*/
public abstract void stop(final int delay);
}