forked from restlet/restlet-framework-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
608 lines (557 loc) · 17.5 KB
/
Server.java
File metadata and controls
608 lines (557 loc) · 17.5 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/**
* Copyright 2005-2013 Restlet S.A.S.
*
* The contents of this file are subject to the terms of one of the following
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
* not use this file except in compliance with one of these Licenses.
*
* You can obtain a copy of the Apache 2.0 license at
* http://www.opensource.org/licenses/apache-2.0
*
* You can obtain a copy of the LGPL 3.0 license at
* http://www.opensource.org/licenses/lgpl-3.0
*
* You can obtain a copy of the LGPL 2.1 license at
* http://www.opensource.org/licenses/lgpl-2.1
*
* You can obtain a copy of the CDDL 1.0 license at
* http://www.opensource.org/licenses/cddl1
*
* You can obtain a copy of the EPL 1.0 license at
* http://www.opensource.org/licenses/eclipse-1.0
*
* See the Licenses for the specific language governing permissions and
* limitations under the Licenses.
*
* Alternatively, you can obtain a royalty free commercial license with less
* limitations, transferable or non-transferable, directly at
* http://www.restlet.com/products/restlet-framework
*
* Restlet is a registered trademark of Restlet S.A.S.
*/
package org.restlet;
import java.util.Arrays;
import java.util.List;
import org.restlet.data.Protocol;
import org.restlet.engine.Engine;
import org.restlet.engine.RestletHelper;
import org.restlet.resource.ServerResource;
/**
* Connector acting as a generic server. It internally uses one of the available
* connector helpers registered with the Restlet engine.<br>
* <br>
* Concurrency note: instances of this class or its subclasses can be invoked by
* several threads at the same time and therefore must be thread-safe. You
* should be especially careful when storing state in member variables.<br>
* <br>
* For advanced cases, it is possible to obtained the wrapped
* {@link RestletHelper} instance that is used by this client to handle the
* calls via the "org.restlet.engine.helper" attribute stored in the
* {@link Context} object.
*
* @author Jerome Louvel
*/
public class Server extends Connector {
/** The listening address if specified. */
private volatile String address;
/** The helper provided by the implementation. */
private final RestletHelper<Server> helper;
/** The next Restlet. */
private volatile Restlet next;
/** The listening port if specified. */
private volatile int port;
/**
* Constructor.
*
* @param context
* The context.
* @param protocols
* The connector protocols.
* @param port
* The listening port.
* @param next
* The next Restlet.
*/
public Server(Context context, List<Protocol> protocols, int port,
Restlet next) {
this(context, protocols, null, port, next);
}
/**
* Constructor.
*
* @param context
* The context.
* @param protocols
* The connector protocols.
* @param address
* The optional listening IP address (useful if multiple IP
* addresses available). You can also use a domain name as an
* alias for the IP address to listen to.
* @param port
* The listening port.
* @param next
* The next Restlet.
*/
public Server(Context context, List<Protocol> protocols, String address,
int port, Restlet next) {
this(context, protocols, address, port, next, null);
}
/**
* Constructor.
*
* @param context
* The context.
* @param protocols
* The connector protocols.
* @param address
* The optional listening IP address (useful if multiple IP
* addresses available). You can also use a domain name as an
* alias for the IP address to listen to.
* @param port
* The listening port.
* @param next
* The next Restlet.
* @param helperClass
* Optional helper class name.
*/
public Server(Context context, List<Protocol> protocols, String address,
int port, Restlet next, String helperClass) {
super(context, protocols);
this.address = address;
this.port = port;
this.next = next;
if (Engine.getInstance() != null) {
this.helper = Engine.getInstance().createHelper(this, helperClass);
} else {
this.helper = null;
}
if (context != null && this.helper != null) {
context.getAttributes().put("org.restlet.engine.helper",
this.helper);
}
}
/**
* Constructor. Note that it uses the protocol's default port.
*
* @param context
* The parent context.
* @param protocol
* The connector protocol.
*/
public Server(Context context, Protocol protocol) {
this(context, protocol, (protocol == null) ? -1 : protocol
.getDefaultPort());
}
/**
* Constructor.
*
* @param context
* The context.
* @param protocol
* The connector protocol.
* @param nextClass
* The next server resource.
*/
public Server(Context context, Protocol protocol,
Class<? extends ServerResource> nextClass) {
this(context, protocol);
setNext(createFinder(nextClass));
}
/**
* Constructor.
*
* @param context
* The parent context.
* @param protocol
* The connector protocol.
* @param port
* The listening port.
*/
public Server(Context context, Protocol protocol, int port) {
this(context, protocol, port, (Restlet) null);
}
/**
* Constructor.
*
* @param context
* The context.
* @param protocol
* The connector protocol.
* @param port
* The listening port.
* @param nextClass
* The next server resource.
*/
public Server(Context context, Protocol protocol, int port,
Class<? extends ServerResource> nextClass) {
this(context, protocol, port);
setNext(createFinder(nextClass));
}
/**
* Constructor.
*
* @param context
* The context.
* @param protocol
* The connector protocol.
* @param port
* The listening port.
* @param next
* The next Restlet.
*/
public Server(Context context, Protocol protocol, int port, Restlet next) {
this(context, protocol, null, port, next);
}
/**
* Constructor using the protocol's default port.
*
* @param context
* The context.
* @param protocol
* The connector protocol.
* @param next
* The next Restlet.
*/
public Server(Context context, Protocol protocol, Restlet next) {
this(context, protocol, null, (protocol == null) ? -1 : protocol
.getDefaultPort(), next);
}
/**
* Constructor.
*
* @param context
* The context.
* @param protocol
* The connector protocol.
* @param address
* The optional listening IP address (useful if multiple IP
* addresses available). You can also use a domain name as an
* alias for the IP address to listen to.
* @param port
* The listening port.
* @param next
* The next Restlet.
*/
public Server(Context context, Protocol protocol, String address, int port,
Restlet next) {
this(context, (protocol == null) ? null : Arrays.asList(protocol),
address, port, next);
}
/**
* Constructor.
*
* @param protocols
* The connector protocols.
* @param port
* The listening port.
* @param next
* The next Restlet.
*/
public Server(List<Protocol> protocols, int port, Restlet next) {
this((Context) null, protocols, port, next);
}
/**
* Constructor.
*
* @param protocols
* The connector protocols.
* @param address
* The optional listening IP address (useful if multiple IP
* addresses available). You can also use a domain name as an
* alias for the IP address to listen to.
* @param port
* The listening port.
* @param next
* The next Restlet.
*/
public Server(List<Protocol> protocols, String address, int port,
Restlet next) {
this((Context) null, protocols, address, port, next);
}
/**
* Constructor.
*
* @param protocol
* The connector protocol.
*/
public Server(Protocol protocol) {
this((Context) null, protocol, (Restlet) null);
}
/**
* Constructor using the protocol's default port.
*
* @param protocol
* The connector protocol.
* @param nextClass
* The next server resource.
*/
public Server(Protocol protocol, Class<? extends ServerResource> nextClass) {
this((Context) null, protocol);
setNext(createFinder(nextClass));
}
/**
* Constructor.
*
* @param protocol
* The connector protocol.
* @param port
* The listening port.
*/
public Server(Protocol protocol, int port) {
this((Context) null, protocol, port, (Restlet) null);
}
/**
* Constructor.
*
* @param protocol
* The connector protocol.
* @param port
* The listening port.
* @param nextClass
* The next server resource.
*/
public Server(Protocol protocol, int port,
Class<? extends ServerResource> nextClass) {
this(protocol, port);
setNext(createFinder(nextClass));
}
/**
* Constructor.
*
* @param protocol
* The connector protocol.
* @param port
* The listening port.
* @param next
* The next Restlet.
*/
public Server(Protocol protocol, int port, Restlet next) {
this((Context) null, protocol, port, next);
}
/**
* Constructor using the protocol's default port.
*
* @param protocol
* The connector protocol.
* @param next
* The next Restlet.
*/
public Server(Protocol protocol, Restlet next) {
this((Context) null, protocol, next);
}
/**
* Constructor using the protocol's default port.
*
* @param protocol
* The connector protocol.
* @param address
* The listening IP address (useful if multiple IP addresses
* available). You can also use a domain name as an alias for the
* IP address to listen to.
*/
public Server(Protocol protocol, String address) {
this((Context) null, protocol, address, protocol.getDefaultPort(), null);
}
/**
* Constructor using the protocol's default port.
*
* @param protocol
* The connector protocol.
* @param address
* The listening IP address (useful if multiple IP addresses
* available). You can also use a domain name as an alias for the
* IP address to listen to.
* @param nextClass
* The next server resource.
*/
public Server(Protocol protocol, String address,
Class<? extends ServerResource> nextClass) {
this(protocol, address);
setNext(createFinder(nextClass));
}
/**
* Constructor.
*
* @param protocol
* The connector protocol.
* @param address
* The optional listening IP address (useful if multiple IP
* addresses available). You can also use a domain name as an
* alias for the IP address to listen to.
* @param port
* The listening port.
*/
public Server(Protocol protocol, String address, int port) {
this((Context) null, protocol, address, port, null);
}
/**
* Constructor.
*
* @param protocol
* The connector protocol.
* @param address
* The optional listening IP address (useful if multiple IP
* addresses available). You can also use a domain name as an
* alias for the IP address to listen to.
* @param port
* The listening port.
* @param next
* The next Restlet.
*/
public Server(Protocol protocol, String address, int port, Restlet next) {
this((Context) null, protocol, address, port, next);
}
/**
* Constructor using the protocol's default port.
*
* @param protocol
* The connector protocol.
* @param address
* The listening IP address (useful if multiple IP addresses
* available). You can also use a domain name as an alias for the
* IP address to listen to.
* @param next
* The next Restlet.
*/
public Server(Protocol protocol, String address, Restlet next) {
this((Context) null, protocol, address, protocol.getDefaultPort(), next);
}
/**
* Returns the actual server port after it has started. If an ephemeral port
* is used it will be returned, otherwise the fixed port will be provided.
*
* @return The actual server port.
*/
public int getActualPort() {
return (getPort() == 0) ? getEphemeralPort() : getPort();
}
/**
* Returns the optional listening IP address (local host used if null).
*
* @return The optional listening IP address (local host used if null).
*/
public String getAddress() {
return this.address;
}
/**
* Returns the actual ephemeral port used when the listening port is set to
* '0'. The default value is '-1' if no ephemeral port is known. See
* InetSocketAddress#InetSocketAddress(int) and ServerSocket#getLocalPort()
* methods for details.
*
* @return The actual ephemeral port used.
*/
public int getEphemeralPort() {
return (Integer) getHelper().getAttributes().get("ephemeralPort");
}
/**
* Returns the internal server.
*
* @return The internal server.
*/
private RestletHelper<Server> getHelper() {
return this.helper;
}
/**
* Returns the next Restlet.
*
* @return The next Restlet.
*/
public Restlet getNext() {
return this.next;
}
/**
* Returns the listening port if specified.
*
* @return The listening port if specified.
*/
public int getPort() {
return this.port;
}
@Override
public void handle(Request request, Response response) {
super.handle(request, response);
if (getNext() != null) {
getNext().handle(request, response);
}
}
/**
* Indicates if a next Restlet is set.
*
* @return True if a next Restlet is set.
*/
public boolean hasNext() {
return this.next != null;
}
/**
* Indicates the underlying connector helper is available.
*
* @return True if the underlying connector helper is available.
*/
@Override
public boolean isAvailable() {
return getHelper() != null;
}
/**
* Sets the optional listening IP address (local host used if null).
*
* @param address
* The optional listening IP address (local host used if null).
*/
public void setAddress(String address) {
this.address = address;
}
/**
* Sets the next Restlet as a Finder for a given resource class. When the
* call is delegated to the Finder instance, a new instance of the resource
* class will be created and will actually handle the request.
*
* @param nextClass
* The next resource class to attach.
*/
public void setNext(Class<? extends ServerResource> nextClass) {
setNext(createFinder(nextClass));
}
/**
* Sets the next Restlet.
*
* @param next
* The next Restlet.
*/
public void setNext(Restlet next) {
this.next = next;
}
/**
* Sets the listening port if specified. Note that '0' means that the system
* will pick up an ephemeral port at the binding time. This ephemeral can be
* retrieved once the server is started using the
* {@link #getEphemeralPort()} method.
*
* @param port
* The listening port if specified.
*/
protected void setPort(int port) {
this.port = port;
}
@Override
public synchronized void start() throws Exception {
if (isStopped()) {
if (getHelper() != null) {
getHelper().start();
}
// Must be invoked as a last step
super.start();
}
}
@Override
public synchronized void stop() throws Exception {
if (isStarted()) {
// Must be invoked as a first step
super.stop();
if (getHelper() != null) {
getHelper().stop();
}
}
}
}