65
65
*
66
66
*/
67
67
public class ChannelFinderClientImpl implements ChannelFinderClient {
68
- private final WebResource service ;
68
+ private final WebResource cfAuthenticatedResource ;
69
+ private final WebResource cfResource ;
70
+
69
71
private final ExecutorService executor ;
70
72
71
73
private static final String resourceChannels = "resources/channels" ;
72
74
private static final String resourceProperties = "resources/properties" ;
73
75
private static final String resourceTags = "resources/tags" ;
74
76
77
+
78
+ private static CFProperties properties = new CFProperties ();
75
79
private static final Logger log = Logger .getLogger (ChannelFinderClient .class .getName ());
76
80
/**
77
81
* A Builder class to help create the client to the Channelfinder Service
@@ -99,11 +103,10 @@ public static class CFCBuilder {
99
103
100
104
private ExecutorService executor = Executors .newSingleThreadExecutor ();
101
105
102
- private CFProperties properties = new CFProperties ();
103
106
104
107
private CFCBuilder ()
105
108
{
106
- this .uri = URI .create (this . properties .getPreferenceValue ("serviceURL" ));
109
+ this .uri = URI .create (properties .getPreferenceValue ("serviceURL" ));
107
110
this .protocol = this .uri .getScheme ();
108
111
}
109
112
@@ -261,19 +264,24 @@ public boolean verify(String hostname, SSLSession session) {
261
264
properties .getPreferenceValue ("username" ),
262
265
properties .getPreferenceValue ("password" ));
263
266
}
267
+
264
268
return new ChannelFinderClientImpl (this .uri , this .clientConfig , this .httpBasicAuthFilter , this .executor );
265
269
}
266
270
}
267
271
268
272
ChannelFinderClientImpl (URI uri , ClientConfig config , HTTPBasicAuthFilter httpBasicAuthFilter ,
269
273
ExecutorService executor ) {
270
274
Client client = Client .create (config );
275
+ client .setFollowRedirects (true );
276
+ cfResource = client .resource (uri .toString ());
277
+ cfAuthenticatedResource = client .resource (uri .toString ());
271
278
if (httpBasicAuthFilter != null ) {
272
- client .addFilter (httpBasicAuthFilter );
279
+ cfAuthenticatedResource .addFilter (httpBasicAuthFilter );
280
+ }
281
+ // TODO add a preference to add logging
282
+ if (Boolean .parseBoolean (properties .getPreferenceValue ("rawFiltering" ))) {
283
+ client .addFilter (new RawLoggingFilter (Logger .getLogger (RawLoggingFilter .class .getName ())));
273
284
}
274
- // client.addFilter(new RawLoggingFilter(Logger.getLogger(RawLoggingFilter.class.getName())));
275
- client .setFollowRedirects (true );
276
- service = client .resource (uri .toString ());
277
285
this .executor = executor ;
278
286
}
279
287
@@ -292,7 +300,7 @@ public Collection<String> call() throws Exception {
292
300
List <XmlProperty > xmlproperties = new ArrayList <XmlProperty >();
293
301
try {
294
302
xmlproperties = mapper .readValue (
295
- service .path (resourceProperties ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
303
+ cfResource .path (resourceProperties ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
296
304
new TypeReference <List <XmlProperty >>() {
297
305
});
298
306
} catch (JsonParseException e ) {
@@ -320,7 +328,7 @@ public List<Property> call() throws Exception {
320
328
List <XmlProperty > xmlproperties = new ArrayList <>();
321
329
try {
322
330
xmlproperties = mapper .readValue (
323
- service .path (resourceProperties ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
331
+ cfResource .path (resourceProperties ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
324
332
new TypeReference <List <XmlProperty >>() {
325
333
});
326
334
} catch (Exception e ) {
@@ -347,7 +355,7 @@ public Collection<String> call() {
347
355
List <XmlTag > xmltags = new ArrayList <XmlTag >();
348
356
try {
349
357
xmltags = mapper .readValue (
350
- service .path (resourceTags )
358
+ cfResource .path (resourceTags )
351
359
.accept (MediaType .APPLICATION_JSON )
352
360
.get (String .class ), new TypeReference <List <XmlTag >>() { });
353
361
} catch ( JsonParseException | JsonMappingException e ) {
@@ -414,7 +422,7 @@ public Channel call() throws UniformInterfaceException
414
422
{
415
423
mapper .configure (DeserializationFeature .ACCEPT_SINGLE_VALUE_AS_ARRAY , true );
416
424
try {
417
- return new Channel (mapper .readValue (service .path (resourceChannels ).path (channelName )
425
+ return new Channel (mapper .readValue (cfResource .path (resourceChannels ).path (channelName )
418
426
.get (ClientResponse .class ).getEntityInputStream (), XmlChannel .class ));
419
427
} catch (JsonParseException | JsonMappingException e ) {
420
428
log .log (Level .WARNING , "Failed to process the list of channels" , e );
@@ -451,7 +459,7 @@ public SetChannel(XmlChannel xmlChannel) {
451
459
public void run () {
452
460
ObjectMapper mapper = new ObjectMapper ();
453
461
try {
454
- service .path (resourceChannels ).path (this .pxmlChannel .getName ()).type (MediaType .APPLICATION_JSON )
462
+ cfAuthenticatedResource .path (resourceChannels ).path (this .pxmlChannel .getName ()).type (MediaType .APPLICATION_JSON )
455
463
.put (mapper .writeValueAsString (this .pxmlChannel ));
456
464
} catch (JsonProcessingException e ) {
457
465
log .log (Level .WARNING , "Failed to process the list of channel " , e );
@@ -486,7 +494,7 @@ public void run() {
486
494
mapper .writeValue (out , this .pxmlchannels );
487
495
final byte [] data = ((ByteArrayOutputStream ) out ).toByteArray ();
488
496
String test = new String (data );
489
- service .path (resourceChannels ).type (MediaType .APPLICATION_JSON ).put (test );
497
+ cfAuthenticatedResource .path (resourceChannels ).type (MediaType .APPLICATION_JSON ).put (test );
490
498
} catch (JsonParseException | JsonMappingException e ) {
491
499
log .log (Level .WARNING , "Failed to process the list of channels " , e );
492
500
} catch ( IOException e ) {
@@ -574,7 +582,7 @@ public SetTag(XmlTag xmlTag) {
574
582
public void run () {
575
583
ObjectMapper mapper = new ObjectMapper ();
576
584
try {
577
- service .path (resourceTags ).path (this .pxmlTag .getName ()).type (MediaType .APPLICATION_JSON )
585
+ cfAuthenticatedResource .path (resourceTags ).path (this .pxmlTag .getName ()).type (MediaType .APPLICATION_JSON )
578
586
.accept (MediaType .APPLICATION_JSON ).put (mapper .writeValueAsString (this .pxmlTag ));
579
587
} catch (JsonProcessingException e ) {
580
588
log .log (Level .WARNING , "Failed to process the list of tags " , e );
@@ -671,7 +679,7 @@ private class SetProperty implements Runnable {
671
679
@ Override
672
680
public void run () {
673
681
try {
674
- service .path (resourceProperties ).path (this .pxmlProperty .getName ()).type (MediaType .APPLICATION_JSON )
682
+ cfAuthenticatedResource .path (resourceProperties ).path (this .pxmlProperty .getName ()).type (MediaType .APPLICATION_JSON )
675
683
.accept (MediaType .APPLICATION_JSON ).put (mapper .writeValueAsString (this .pxmlProperty ));
676
684
} catch (JsonProcessingException e ) {
677
685
log .log (Level .WARNING , "Failed to process the list of properties " , e );
@@ -701,7 +709,7 @@ private class UpdateChannel implements Runnable {
701
709
@ Override
702
710
public void run () {
703
711
try {
704
- service .path (resourceChannels )
712
+ cfAuthenticatedResource .path (resourceChannels )
705
713
.path (this .channel .getName ())
706
714
.type (MediaType .APPLICATION_JSON )
707
715
.post (mapper .writeValueAsString (this .channel ));
@@ -770,7 +778,7 @@ private class UpdateTag implements Runnable {
770
778
@ Override
771
779
public void run () {
772
780
try {
773
- service .path (resourceTags ).path (this .pxmlTag .getName ()).type (MediaType .APPLICATION_JSON )
781
+ cfAuthenticatedResource .path (resourceTags ).path (this .pxmlTag .getName ()).type (MediaType .APPLICATION_JSON )
774
782
.post (mapper .writeValueAsString (this .pxmlTag ));
775
783
} catch (UniformInterfaceException e ) {
776
784
throw new ChannelFinderException (e );
@@ -814,7 +822,7 @@ private class UpdateChannelProperty implements Runnable {
814
822
@ Override
815
823
public void run () {
816
824
try {
817
- service .path (resourceProperties )
825
+ cfAuthenticatedResource .path (resourceProperties )
818
826
.path (this .pxmlProperty .getName ())
819
827
.type (MediaType .APPLICATION_JSON )
820
828
.put (mapper .writeValueAsString (this .pxmlProperty ));
@@ -885,7 +893,7 @@ private class UpdateProperty implements Runnable {
885
893
@ Override
886
894
public void run () {
887
895
try {
888
- service .path (resourceProperties ).path (this .pxmlProperty .getName ()).type (MediaType .APPLICATION_JSON )
896
+ cfAuthenticatedResource .path (resourceProperties ).path (this .pxmlProperty .getName ()).type (MediaType .APPLICATION_JSON )
889
897
.accept (MediaType .APPLICATION_JSON ).post (mapper .writeValueAsString (this .pxmlProperty ));
890
898
} catch (UniformInterfaceException e ) {
891
899
throw new ChannelFinderException (e );
@@ -1035,7 +1043,7 @@ public Collection<Channel> call() throws Exception {
1035
1043
List <XmlChannel > xmlchannels = new ArrayList <XmlChannel >();
1036
1044
long start = System .currentTimeMillis ();
1037
1045
try {
1038
- xmlchannels = mapper .readValue (service .path (resourceChannels ).queryParams (this .map )
1046
+ xmlchannels = mapper .readValue (cfResource .path (resourceChannels ).queryParams (this .map )
1039
1047
.accept (MediaType .APPLICATION_JSON ).get (String .class ), new TypeReference <List <XmlChannel >>() {
1040
1048
});
1041
1049
} catch (Exception e ) {
@@ -1142,7 +1150,7 @@ private class DeleteElement implements Runnable
1142
1150
1143
1151
@ Override
1144
1152
public void run () {
1145
- service .path (elementType ).path (elementName ).delete ();
1153
+ cfAuthenticatedResource .path (elementType ).path (elementName ).delete ();
1146
1154
}
1147
1155
1148
1156
}
@@ -1237,7 +1245,7 @@ private class DeleteElementfromChannel implements Runnable
1237
1245
@ Override
1238
1246
public void run ()
1239
1247
{
1240
- service .path (this .elementType ).path (this .elementName ).path (this .channelName )
1248
+ cfAuthenticatedResource .path (this .elementType ).path (this .elementName ).path (this .channelName )
1241
1249
.accept (MediaType .APPLICATION_JSON ).delete ();
1242
1250
}
1243
1251
@@ -1300,7 +1308,7 @@ public Collection<Channel> getAllChannels()
1300
1308
List <XmlChannel > xmlchannels = new ArrayList <XmlChannel >();
1301
1309
try {
1302
1310
xmlchannels = mapper .readValue (
1303
- service .path (resourceChannels ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
1311
+ cfAuthenticatedResource .path (resourceChannels ).accept (MediaType .APPLICATION_JSON ).get (String .class ),
1304
1312
new TypeReference <List <XmlChannel >>() {
1305
1313
});
1306
1314
} catch (JsonParseException | JsonMappingException e ) {
0 commit comments