forked from restlet/restlet-framework-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.java
More file actions
1222 lines (1076 loc) · 40.8 KB
/
Engine.java
File metadata and controls
1222 lines (1076 loc) · 40.8 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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.engine;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.engine.io.IoUtils;
import org.restlet.engine.log.LoggerFacade;
/**
* Engine supporting the Restlet API. The engine acts as a registry of various
* {@link Helper} types: {@link org.restlet.engine.security.AuthenticatorHelper}
* , {@link ClientHelper}, {@link org.restlet.engine.converter.ConverterHelper}
* and {@link ServerHelper} classes.<br>
* <br>
* Note that by default the JULI logging mechanism is used but it is possible to
* replace it by providing an alternate {@link LoggerFacade} implementation. For
* this, just pass a system property named
* "org.restlet.engine.loggerFacadeClass" with the qualified class name as a
* value.
*
* @author Jerome Louvel
*/
public class Engine {
public static final String DESCRIPTOR = "META-INF/services";
public static final String DESCRIPTOR_AUTHENTICATOR = "org.restlet.engine.security.AuthenticatorHelper";
public static final String DESCRIPTOR_AUTHENTICATOR_PATH = DESCRIPTOR + "/"
+ DESCRIPTOR_AUTHENTICATOR;
public static final String DESCRIPTOR_CLIENT = "org.restlet.engine.ClientHelper";
public static final String DESCRIPTOR_CLIENT_PATH = DESCRIPTOR + "/"
+ DESCRIPTOR_CLIENT;
public static final String DESCRIPTOR_CONVERTER = "org.restlet.engine.converter.ConverterHelper";
public static final String DESCRIPTOR_CONVERTER_PATH = DESCRIPTOR + "/"
+ DESCRIPTOR_CONVERTER;
public static final String DESCRIPTOR_PROTOCOL = "org.restlet.engine.ProtocolHelper";
public static final String DESCRIPTOR_PROTOCOL_PATH = DESCRIPTOR + "/"
+ DESCRIPTOR_PROTOCOL;
public static final String DESCRIPTOR_SERVER = "org.restlet.engine.ServerHelper";
public static final String DESCRIPTOR_SERVER_PATH = DESCRIPTOR + "/"
+ DESCRIPTOR_SERVER;
/** The registered engine. */
private static volatile Engine instance = null;
// [ifdef jse,android,osgi] member
/** The org.restlet log level . */
private static volatile boolean logConfigured = false;
// [ifdef jse,android,osgi] member
/** The general log formatter. */
private static volatile Class<? extends Formatter> logFormatter = org.restlet.engine.log.SimplestFormatter.class;
// [ifdef jse,android,osgi] member
/** The general log level . */
private static volatile Level logLevel = Level.INFO;
/** Major version number. */
public static final String MAJOR_NUMBER = "@major-number@";
/** Minor version number. */
public static final String MINOR_NUMBER = "@minor-number@";
/** Release number. */
public static final String RELEASE_NUMBER = "@release-type@@release-number@";
// [ifdef jse,android,osgi] member
/** The org.restlet log level . */
private static volatile Level restletLogLevel;
/** Complete version. */
public static final String VERSION = MAJOR_NUMBER + '.' + MINOR_NUMBER
+ RELEASE_NUMBER;
/** Complete version header. */
public static final String VERSION_HEADER = "Restlet-Framework/" + VERSION;
/**
* Clears the current Restlet Engine altogether.
*/
public static synchronized void clear() {
instance = null;
}
// [ifndef gwt] method
/**
* Creates a new standalone thread with local Restlet thread variable
* properly set.
*
* @param runnable
* The runnable task to execute.
* @param name
* The thread name.
* @return The thread with proper variables ready to run the given runnable
* task.
*/
public static Thread createThreadWithLocalVariables(
final Runnable runnable, String name) {
// Save the thread local variables
final org.restlet.Application currentApplication = org.restlet.Application.getCurrent();
final Context currentContext = Context.getCurrent();
final Integer currentVirtualHost = org.restlet.routing.VirtualHost.getCurrent();
final Response currentResponse = Response.getCurrent();
return new Thread(new Runnable() {
@Override
public void run() {
// Copy the thread local variables
Response.setCurrent(currentResponse);
Context.setCurrent(currentContext);
org.restlet.routing.VirtualHost.setCurrent(currentVirtualHost);
org.restlet.Application.setCurrent(currentApplication);
try {
// Run the user task
runnable.run();
} finally {
Engine.clearThreadLocalVariables();
}
}
}, name);
}
// [ifndef gwt] method
/**
* Clears the thread local variables set by the Restlet API and engine.
*/
public static void clearThreadLocalVariables() {
Response.setCurrent(null);
Context.setCurrent(null);
org.restlet.routing.VirtualHost.setCurrent(null);
org.restlet.Application.setCurrent(null);
}
// [ifdef jse,android,osgi] method
/**
* Updates the global log configuration of the JVM programmatically.
*/
public static void configureLog() {
if ((System.getProperty("java.util.logging.config.file") == null)
&& (System.getProperty("java.util.logging.config.class") == null)) {
StringBuilder sb = new StringBuilder();
sb.append("handlers=");
sb.append(java.util.logging.ConsoleHandler.class.getCanonicalName())
.append('\n');
if (getLogLevel() != null) {
sb.append(".level=" + getLogLevel().getName()).append('\n');
}
if (getRestletLogLevel() != null) {
sb.append("org.restlet.level=" + getRestletLogLevel().getName())
.append('\n');
}
if (getLogFormatter() != null) {
String handler = java.util.logging.ConsoleHandler.class
.getCanonicalName();
sb.append(handler + ".formatter="
+ getLogFormatter().getCanonicalName() + "\n");
if (getLogLevel() != null) {
sb.append(handler + ".level=" + getLogLevel().getName()
+ "\n");
}
}
try {
LogManager.getLogManager().readConfiguration(
new ByteArrayInputStream(sb.toString().getBytes()));
} catch (Throwable t) {
t.printStackTrace();
}
}
logConfigured = true;
}
/**
* Returns an anonymous logger. By default it calls
* {@link #getLogger(String)} with a "" name.
*
* @return The logger.
*/
public static Logger getAnonymousLogger() {
return getInstance().getLoggerFacade().getAnonymousLogger();
}
/**
* Returns the registered Restlet engine.
*
* @return The registered Restlet engine.
*/
public static synchronized Engine getInstance() {
Engine result = instance;
if (result == null) {
result = register();
}
return result;
}
// [ifdef jse,android,osgi] method
/**
* Returns the general log formatter.
*
* @return The general log formatter.
*/
public static Class<? extends Formatter> getLogFormatter() {
return Engine.logFormatter;
}
/**
* Returns a logger based on the class name of the given object.
*
* @param clazz
* The parent class.
* @return The logger.
*/
public static Logger getLogger(Class<?> clazz) {
return getInstance().getLoggerFacade().getLogger(clazz);
}
/**
* Returns a logger based on the class name of the given object.
*
* @param clazz
* The parent class.
* @param defaultLoggerName
* The default logger name to use if no one can be inferred from
* the class.
* @return The logger.
*/
public static Logger getLogger(Class<?> clazz, String defaultLoggerName) {
return getInstance().getLoggerFacade().getLogger(clazz,
defaultLoggerName);
}
/**
* Returns a logger based on the class name of the given object.
*
* @param object
* The parent object.
* @param defaultLoggerName
* The default logger name to use if no one can be inferred from
* the object class.
* @return The logger.
*/
public static Logger getLogger(Object object, String defaultLoggerName) {
return getInstance().getLoggerFacade().getLogger(object,
defaultLoggerName);
}
/**
* Returns a logger based on the given logger name.
*
* @param loggerName
* The logger name.
* @return The logger.
*/
public static Logger getLogger(String loggerName) {
return getInstance().getLoggerFacade().getLogger(loggerName);
}
// [ifdef jse,android,osgi] method
/**
* Returns the general log level.
*
* @return The general log level.
*/
public static Level getLogLevel() {
return Engine.logLevel;
}
// [ifndef gwt] method
/**
* Returns the classloader resource for a given name/path.
*
* @param name
* The name/path to lookup.
* @return The resource URL.
*/
public static java.net.URL getResource(String name) {
return getInstance().getClassLoader().getResource(name);
}
// [ifdef jse,android,osgi] method
/**
* Returns the Restlet log level. For loggers with a name starting with
* "org.restlet".
*
* @return The Restlet log level.
*/
public static Level getRestletLogLevel() {
return Engine.restletLogLevel;
}
// [ifndef gwt] method
/**
* Returns the class object for the given name using the engine classloader.
*
* @param className
* The class name to lookup.
* @return The class object or null if the class was not found.
* @see #getClassLoader()
*/
public static Class<?> loadClass(String className)
throws ClassNotFoundException {
return getInstance().getClassLoader().loadClass(className);
}
/**
* Registers a new Restlet Engine.
*
* @return The registered engine.
*/
public static synchronized Engine register() {
return register(true);
}
/**
* Registers a new Restlet Engine.
*
* @param discoverPlugins
* True if plug-ins should be automatically discovered.
* @return The registered engine.
*/
public static synchronized Engine register(boolean discoverPlugins) {
// [ifdef jse,android,osgi]
if (!logConfigured) {
configureLog();
}
// [enddef]
Engine result = new Engine(discoverPlugins);
instance = result;
return result;
}
// [ifdef jse,android,osgi] method
/**
* Sets the general log formatter.
*
* @param logFormatter
* The general log formatter.
*/
public static void setLogFormatter(Class<? extends Formatter> logFormatter) {
Engine.logFormatter = logFormatter;
configureLog();
}
// [ifdef jse,android,osgi] method
/**
* Sets the general log level. Modifies the global JVM's {@link LogManager}.
*
* @param logLevel
* The general log level.
*/
public static void setLogLevel(Level logLevel) {
Engine.logLevel = logLevel;
configureLog();
}
// [ifdef jse,android,osgi] method
/**
* Sets the Restlet log level. For loggers with a name starting with
* "org.restlet".
*
* @param restletLogLevel
* The Restlet log level.
*/
public static void setRestletLogLevel(Level restletLogLevel) {
Engine.restletLogLevel = restletLogLevel;
configureLog();
}
// [ifndef gwt] member
/** Class loader to use for dynamic class loading. */
private volatile ClassLoader classLoader;
/** The logger facade to use. */
private LoggerFacade loggerFacade;
// [ifndef gwt] member
/** List of available authenticator helpers. */
private final List<org.restlet.engine.security.AuthenticatorHelper> registeredAuthenticators;
/** List of available client connectors. */
private final List<ConnectorHelper<Client>> registeredClients;
// [ifndef gwt] member
/** List of available converter helpers. */
private final List<org.restlet.engine.converter.ConverterHelper> registeredConverters;
/** List of available protocol helpers. */
private final List<org.restlet.engine.ProtocolHelper> registeredProtocols;
// [ifndef gwt] member
/** List of available server connectors. */
private final List<ConnectorHelper<org.restlet.Server>> registeredServers;
// [ifndef gwt] member
/** User class loader to use for dynamic class loading. */
private volatile ClassLoader userClassLoader;
/**
* Constructor that will automatically attempt to discover connectors.
*/
public Engine() {
this(true);
}
/**
* Constructor.
*
* @param discoverHelpers
* True if helpers should be automatically discovered.
*/
public Engine(boolean discoverHelpers) {
// Prevent engine initialization code from recreating other engines
instance = this;
// Instantiate the logger facade
if (Edition.CURRENT == Edition.GWT) {
this.loggerFacade = new LoggerFacade();
} else {
// [ifndef gwt]
this.classLoader = createClassLoader();
this.userClassLoader = null;
String loggerFacadeClass = System.getProperty(
"org.restlet.engine.loggerFacadeClass",
"org.restlet.engine.log.LoggerFacade");
try {
this.loggerFacade = (LoggerFacade) getClassLoader().loadClass(
loggerFacadeClass).newInstance();
} catch (Exception e) {
this.loggerFacade = new LoggerFacade();
this.loggerFacade.getLogger("org.restlet").log(Level.WARNING,
"Unable to register the logger facade", e);
}
// [enddef]
}
this.registeredClients = new CopyOnWriteArrayList<ConnectorHelper<Client>>();
this.registeredProtocols = new CopyOnWriteArrayList<ProtocolHelper>();
// [ifndef gwt]
this.registeredServers = new CopyOnWriteArrayList<ConnectorHelper<org.restlet.Server>>();
this.registeredAuthenticators = new CopyOnWriteArrayList<org.restlet.engine.security.AuthenticatorHelper>();
this.registeredConverters = new CopyOnWriteArrayList<org.restlet.engine.converter.ConverterHelper>();
// [enddef]
if (discoverHelpers) {
try {
discoverConnectors();
discoverProtocols();
// [ifndef gwt]
discoverAuthenticators();
discoverConverters();
// [enddef]
} catch (IOException e) {
Context.getCurrentLogger()
.log(Level.WARNING,
"An error occured while discovering the engine helpers.",
e);
}
}
}
// [ifndef gwt] method
/**
* Creates a new class loader. By default, it returns an instance of
* {@link org.restlet.engine.util.EngineClassLoader}.
*
* @return A new class loader.
*/
protected ClassLoader createClassLoader() {
return new org.restlet.engine.util.EngineClassLoader(this);
}
/**
* Creates a new helper for a given client connector.
*
* @param client
* The client to help.
* @param helperClass
* Optional helper class name.
* @return The new helper.
*/
@SuppressWarnings("unchecked")
public ConnectorHelper<Client> createHelper(Client client,
String helperClass) {
ConnectorHelper<Client> result = null;
if (client.getProtocols().size() > 0) {
ConnectorHelper<Client> connector = null;
for (final Iterator<ConnectorHelper<Client>> iter = getRegisteredClients()
.iterator(); (result == null) && iter.hasNext();) {
connector = iter.next();
if (connector.getProtocols().containsAll(client.getProtocols())) {
// [ifndef gwt]
if ((helperClass == null)
|| connector.getClass().getCanonicalName()
.equals(helperClass)) {
try {
result = connector.getClass()
.getConstructor(Client.class)
.newInstance(client);
} catch (Exception e) {
Context.getCurrentLogger()
.log(Level.SEVERE,
"Exception during the instantiation of the client connector.",
e);
}
}
// [enddef]
// [ifdef gwt] instruction uncomment
// result = new
// org.restlet.engine.adapter.GwtHttpClientHelper(client);
}
}
if (result == null) {
// Couldn't find a matching connector
StringBuilder sb = new StringBuilder();
sb.append("No available client connector supports the required protocols: ");
for (Protocol p : client.getProtocols()) {
sb.append("'").append(p.getName()).append("' ");
}
sb.append(". Please add the JAR of a matching connector to your classpath.");
if (Edition.CURRENT == Edition.ANDROID) {
sb.append(" Then, register this connector helper manually.");
}
Context.getCurrentLogger().log(Level.WARNING, sb.toString());
}
}
return result;
}
// [ifndef gwt] method
/**
* Creates a new helper for a given server connector.
*
* @param server
* The server to help.
* @param helperClass
* Optional helper class name.
* @return The new helper.
*/
@SuppressWarnings("unchecked")
public ConnectorHelper<org.restlet.Server> createHelper(
org.restlet.Server server, String helperClass) {
ConnectorHelper<org.restlet.Server> result = null;
if (server.getProtocols().size() > 0) {
ConnectorHelper<org.restlet.Server> connector = null;
for (final Iterator<ConnectorHelper<org.restlet.Server>> iter = getRegisteredServers()
.iterator(); (result == null) && iter.hasNext();) {
connector = iter.next();
if ((helperClass == null)
|| connector.getClass().getCanonicalName()
.equals(helperClass)) {
if (connector.getProtocols().containsAll(
server.getProtocols())) {
try {
result = connector.getClass()
.getConstructor(org.restlet.Server.class)
.newInstance(server);
} catch (Exception e) {
Context.getCurrentLogger()
.log(Level.SEVERE,
"Exception while instantiation the server connector.",
e);
}
}
}
}
if (result == null) {
// Couldn't find a matching connector
final StringBuilder sb = new StringBuilder();
sb.append("No available server connector supports the required protocols: ");
for (final Protocol p : server.getProtocols()) {
sb.append("'").append(p.getName()).append("' ");
}
sb.append(". Please add the JAR of a matching connector to your classpath.");
if (Edition.CURRENT == Edition.ANDROID) {
sb.append(" Then, register this connector helper manually.");
}
Context.getCurrentLogger().log(Level.WARNING, sb.toString());
}
}
return result;
}
// [ifndef gwt] method
/**
* Discovers the authenticator helpers and register the default helpers.
*
* @throws IOException
*/
private void discoverAuthenticators() throws IOException {
registerHelpers(DESCRIPTOR_AUTHENTICATOR_PATH,
getRegisteredAuthenticators(), null);
registerDefaultAuthentications();
}
/**
* Discovers the server and client connectors and register the default
* connectors.
*
* @throws IOException
*/
private void discoverConnectors() throws IOException {
// [ifndef gwt]
registerHelpers(DESCRIPTOR_CLIENT_PATH, getRegisteredClients(),
Client.class);
registerHelpers(DESCRIPTOR_SERVER_PATH, getRegisteredServers(),
org.restlet.Server.class);
// [enddef]
registerDefaultConnectors();
}
// [ifndef gwt] method
/**
* Discovers the converter helpers and register the default helpers.
*
* @throws IOException
*/
private void discoverConverters() throws IOException {
registerHelpers(DESCRIPTOR_CONVERTER_PATH, getRegisteredConverters(),
null);
registerDefaultConverters();
}
/**
* Discovers the protocol helpers and register the default helpers.
*
* @throws IOException
*/
private void discoverProtocols() throws IOException {
// [ifndef gwt] instruction
registerHelpers(DESCRIPTOR_PROTOCOL_PATH, getRegisteredProtocols(),
null);
registerDefaultProtocols();
}
// [ifndef gwt] method
/**
* Finds the converter helper supporting the given conversion.
*
* @return The converter helper or null.
*/
public org.restlet.engine.converter.ConverterHelper findHelper() {
return null;
}
// [ifndef gwt] method
/**
* Finds the authenticator helper supporting the given scheme.
*
* @param challengeScheme
* The challenge scheme to match.
* @param clientSide
* Indicates if client side support is required.
* @param serverSide
* Indicates if server side support is required.
* @return The authenticator helper or null.
*/
public org.restlet.engine.security.AuthenticatorHelper findHelper(
ChallengeScheme challengeScheme, boolean clientSide,
boolean serverSide) {
org.restlet.engine.security.AuthenticatorHelper result = null;
List<org.restlet.engine.security.AuthenticatorHelper> helpers = getRegisteredAuthenticators();
org.restlet.engine.security.AuthenticatorHelper current;
for (int i = 0; (result == null) && (i < helpers.size()); i++) {
current = helpers.get(i);
if (current.getChallengeScheme().equals(challengeScheme)
&& ((clientSide && current.isClientSide()) || !clientSide)
&& ((serverSide && current.isServerSide()) || !serverSide)) {
result = helpers.get(i);
}
}
return result;
}
// [ifndef gwt] method
/**
* Returns the class loader. It uses the delegation model with the Engine
* class's class loader as a parent. If this parent doesn't find a class or
* resource, it then tries the user class loader (via
* {@link #getUserClassLoader()} and finally the
* {@link Thread#getContextClassLoader()}.
*
* @return The engine class loader.
* @see org.restlet.engine.util.EngineClassLoader
*/
public ClassLoader getClassLoader() {
return classLoader;
}
/**
* Returns the logger facade to use.
*
* @return The logger facade to use.
*/
public LoggerFacade getLoggerFacade() {
return loggerFacade;
}
// [ifndef gwt] method
/**
* Parses a line to extract the provider class name.
*
* @param line
* The line to parse.
* @return The provider's class name or an empty string.
*/
private String getProviderClassName(String line) {
final int index = line.indexOf('#');
if (index != -1) {
line = line.substring(0, index);
}
return line.trim();
}
// [ifndef gwt] method
/**
* Returns the list of available authentication helpers.
*
* @return The list of available authentication helpers.
*/
public List<org.restlet.engine.security.AuthenticatorHelper> getRegisteredAuthenticators() {
return this.registeredAuthenticators;
}
/**
* Returns the list of available client connectors.
*
* @return The list of available client connectors.
*/
public List<ConnectorHelper<Client>> getRegisteredClients() {
return this.registeredClients;
}
// [ifndef gwt] method
/**
* Returns the list of available converters.
*
* @return The list of available converters.
*/
public List<org.restlet.engine.converter.ConverterHelper> getRegisteredConverters() {
return registeredConverters;
}
/**
* Returns the list of available protocol connectors.
*
* @return The list of available protocol connectors.
*/
public List<ProtocolHelper> getRegisteredProtocols() {
return this.registeredProtocols;
}
// [ifndef gwt] method
/**
* Returns the list of available server connectors.
*
* @return The list of available server connectors.
*/
public List<ConnectorHelper<org.restlet.Server>> getRegisteredServers() {
return this.registeredServers;
}
// [ifndef gwt] method
/**
* Returns the class loader specified by the user and that should be used in
* priority.
*
* @return The user class loader
*/
public ClassLoader getUserClassLoader() {
return userClassLoader;
}
// [ifndef gwt] method
/**
* Registers the default authentication helpers.
*/
public void registerDefaultAuthentications() {
getRegisteredAuthenticators().add(
new org.restlet.engine.security.HttpBasicHelper());
getRegisteredAuthenticators().add(
new org.restlet.engine.security.SmtpPlainHelper());
}
/**
* Registers the default client and server connectors.
*/
public void registerDefaultConnectors() {
// [ifndef gae, gwt]
getRegisteredClients().add(
new org.restlet.engine.connector.HttpClientHelper(null));
// [enddef]
// [ifndef gwt]
getRegisteredClients().add(
new org.restlet.engine.local.ClapClientHelper(null));
getRegisteredClients().add(
new org.restlet.engine.local.RiapClientHelper(null));
getRegisteredServers().add(
new org.restlet.engine.local.RiapServerHelper(null));
// [enddef]
// [ifndef gae, gwt]
getRegisteredServers().add(
new org.restlet.engine.connector.HttpServerHelper(null));
getRegisteredClients().add(
new org.restlet.engine.local.FileClientHelper(null));
getRegisteredClients().add(
new org.restlet.engine.local.ZipClientHelper(null));
// [enddef]
// [ifdef gwt] uncomment
// getRegisteredClients().add(
// new org.restlet.engine.adapter.GwtHttpClientHelper(null));
// [enddef]
}
// [ifndef gwt] method
/**
* Registers the default converters.
*/
public void registerDefaultConverters() {
getRegisteredConverters().add(
new org.restlet.engine.converter.DefaultConverter());
}
/**
* Registers the default protocols.
*/
public void registerDefaultProtocols() {
getRegisteredProtocols().add(new HttpProtocolHelper());
getRegisteredProtocols().add(new WebDavProtocolHelper());
}
// [ifndef gwt] method
/**
* Registers a helper.
*
* @param classLoader
* The classloader to use.
* @param provider
* Bynary name of the helper's class.
* @param helpers
* The list of helpers to update.
* @param constructorClass
* The constructor parameter class to look for.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void registerHelper(ClassLoader classLoader, String provider,
List helpers, Class constructorClass) {
if ((provider != null) && (!provider.equals(""))) {
// Instantiate the factory
try {
Class providerClass = classLoader.loadClass(provider);
if (constructorClass == null) {
helpers.add(providerClass.newInstance());
} else {
helpers.add(providerClass.getConstructor(constructorClass)
.newInstance(constructorClass.cast(null)));
}
} catch (Throwable t) {
Context.getCurrentLogger().log(Level.INFO,
"Unable to register the helper " + provider, t);
}
}
}
// [ifndef gwt] method
/**
* Registers a helper.
*
* @param classLoader
* The classloader to use.
* @param configUrl
* Configuration URL to parse
* @param helpers
* The list of helpers to update.
* @param constructorClass
* The constructor parameter class to look for.
*/
public void registerHelpers(ClassLoader classLoader,
java.net.URL configUrl, List<?> helpers, Class<?> constructorClass) {
try {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
configUrl.openStream(), "utf-8"), IoUtils.BUFFER_SIZE);
String line = reader.readLine();
while (line != null) {
registerHelper(classLoader, getProviderClassName(line),
helpers, constructorClass);
line = reader.readLine();
}
} catch (IOException e) {
Context.getCurrentLogger().log(
Level.SEVERE,
"Unable to read the provider descriptor: "
+ configUrl.toString());
} finally {
if (reader != null) {
reader.close();
}
}
} catch (IOException ioe) {
Context.getCurrentLogger().log(Level.SEVERE,
"Exception while detecting the helpers.", ioe);
}
}