Skip to content

Commit 4a5268e

Browse files
authored
Merge pull request #2252 from ControlSystemStudio/cf_unauthenticated_client
Add a separate client for doing search without http auth header
2 parents c3692db + 1d5013d commit 4a5268e

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

app/channel/channelfinder/src/main/java/org/phoebus/channelfinder/ChannelFinderClientImpl.java

+31-23
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,17 @@
6565
*
6666
*/
6767
public class ChannelFinderClientImpl implements ChannelFinderClient {
68-
private final WebResource service;
68+
private final WebResource cfAuthenticatedResource;
69+
private final WebResource cfResource;
70+
6971
private final ExecutorService executor;
7072

7173
private static final String resourceChannels = "resources/channels";
7274
private static final String resourceProperties = "resources/properties";
7375
private static final String resourceTags = "resources/tags";
7476

77+
78+
private static CFProperties properties = new CFProperties();
7579
private static final Logger log = Logger.getLogger(ChannelFinderClient.class.getName());
7680
/**
7781
* A Builder class to help create the client to the Channelfinder Service
@@ -99,11 +103,10 @@ public static class CFCBuilder {
99103

100104
private ExecutorService executor = Executors.newSingleThreadExecutor();
101105

102-
private CFProperties properties = new CFProperties();
103106

104107
private CFCBuilder()
105108
{
106-
this.uri = URI.create(this.properties.getPreferenceValue("serviceURL"));
109+
this.uri = URI.create(properties.getPreferenceValue("serviceURL"));
107110
this.protocol = this.uri.getScheme();
108111
}
109112

@@ -261,19 +264,24 @@ public boolean verify(String hostname, SSLSession session) {
261264
properties.getPreferenceValue("username"),
262265
properties.getPreferenceValue("password"));
263266
}
267+
264268
return new ChannelFinderClientImpl(this.uri, this.clientConfig, this.httpBasicAuthFilter, this.executor);
265269
}
266270
}
267271

268272
ChannelFinderClientImpl(URI uri, ClientConfig config, HTTPBasicAuthFilter httpBasicAuthFilter,
269273
ExecutorService executor) {
270274
Client client = Client.create(config);
275+
client.setFollowRedirects(true);
276+
cfResource = client.resource(uri.toString());
277+
cfAuthenticatedResource = client.resource(uri.toString());
271278
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())));
273284
}
274-
// client.addFilter(new RawLoggingFilter(Logger.getLogger(RawLoggingFilter.class.getName())));
275-
client.setFollowRedirects(true);
276-
service = client.resource(uri.toString());
277285
this.executor = executor;
278286
}
279287

@@ -292,7 +300,7 @@ public Collection<String> call() throws Exception {
292300
List<XmlProperty> xmlproperties = new ArrayList<XmlProperty>();
293301
try {
294302
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),
296304
new TypeReference<List<XmlProperty>>() {
297305
});
298306
} catch (JsonParseException e) {
@@ -320,7 +328,7 @@ public List<Property> call() throws Exception {
320328
List<XmlProperty> xmlproperties = new ArrayList<>();
321329
try {
322330
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),
324332
new TypeReference<List<XmlProperty>>() {
325333
});
326334
} catch (Exception e) {
@@ -347,7 +355,7 @@ public Collection<String> call() {
347355
List<XmlTag> xmltags = new ArrayList<XmlTag>();
348356
try {
349357
xmltags = mapper.readValue(
350-
service.path(resourceTags)
358+
cfResource.path(resourceTags)
351359
.accept(MediaType.APPLICATION_JSON)
352360
.get(String.class), new TypeReference<List<XmlTag>>() { });
353361
} catch ( JsonParseException | JsonMappingException e) {
@@ -414,7 +422,7 @@ public Channel call() throws UniformInterfaceException
414422
{
415423
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
416424
try {
417-
return new Channel(mapper.readValue(service.path(resourceChannels).path(channelName)
425+
return new Channel(mapper.readValue(cfResource.path(resourceChannels).path(channelName)
418426
.get(ClientResponse.class).getEntityInputStream(), XmlChannel.class));
419427
} catch (JsonParseException | JsonMappingException e) {
420428
log.log(Level.WARNING, "Failed to process the list of channels", e);
@@ -451,7 +459,7 @@ public SetChannel(XmlChannel xmlChannel) {
451459
public void run() {
452460
ObjectMapper mapper = new ObjectMapper();
453461
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)
455463
.put(mapper.writeValueAsString(this.pxmlChannel));
456464
} catch (JsonProcessingException e) {
457465
log.log(Level.WARNING, "Failed to process the list of channel ", e);
@@ -486,7 +494,7 @@ public void run() {
486494
mapper.writeValue(out, this.pxmlchannels);
487495
final byte[] data = ((ByteArrayOutputStream) out).toByteArray();
488496
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);
490498
} catch (JsonParseException | JsonMappingException e) {
491499
log.log(Level.WARNING, "Failed to process the list of channels ", e);
492500
} catch ( IOException e) {
@@ -574,7 +582,7 @@ public SetTag(XmlTag xmlTag) {
574582
public void run() {
575583
ObjectMapper mapper = new ObjectMapper();
576584
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)
578586
.accept(MediaType.APPLICATION_JSON).put(mapper.writeValueAsString(this.pxmlTag));
579587
} catch (JsonProcessingException e) {
580588
log.log(Level.WARNING, "Failed to process the list of tags ", e);
@@ -671,7 +679,7 @@ private class SetProperty implements Runnable {
671679
@Override
672680
public void run() {
673681
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)
675683
.accept(MediaType.APPLICATION_JSON).put(mapper.writeValueAsString(this.pxmlProperty));
676684
} catch (JsonProcessingException e) {
677685
log.log(Level.WARNING, "Failed to process the list of properties ", e);
@@ -701,7 +709,7 @@ private class UpdateChannel implements Runnable {
701709
@Override
702710
public void run() {
703711
try {
704-
service.path(resourceChannels)
712+
cfAuthenticatedResource.path(resourceChannels)
705713
.path(this.channel.getName())
706714
.type(MediaType.APPLICATION_JSON)
707715
.post(mapper.writeValueAsString(this.channel));
@@ -770,7 +778,7 @@ private class UpdateTag implements Runnable {
770778
@Override
771779
public void run() {
772780
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)
774782
.post(mapper.writeValueAsString(this.pxmlTag));
775783
} catch (UniformInterfaceException e) {
776784
throw new ChannelFinderException(e);
@@ -814,7 +822,7 @@ private class UpdateChannelProperty implements Runnable {
814822
@Override
815823
public void run() {
816824
try {
817-
service.path(resourceProperties)
825+
cfAuthenticatedResource.path(resourceProperties)
818826
.path(this.pxmlProperty.getName())
819827
.type(MediaType.APPLICATION_JSON)
820828
.put(mapper.writeValueAsString(this.pxmlProperty));
@@ -885,7 +893,7 @@ private class UpdateProperty implements Runnable {
885893
@Override
886894
public void run() {
887895
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)
889897
.accept(MediaType.APPLICATION_JSON).post(mapper.writeValueAsString(this.pxmlProperty));
890898
} catch (UniformInterfaceException e) {
891899
throw new ChannelFinderException(e);
@@ -1035,7 +1043,7 @@ public Collection<Channel> call() throws Exception {
10351043
List<XmlChannel> xmlchannels = new ArrayList<XmlChannel>();
10361044
long start = System.currentTimeMillis();
10371045
try {
1038-
xmlchannels = mapper.readValue(service.path(resourceChannels).queryParams(this.map)
1046+
xmlchannels = mapper.readValue(cfResource.path(resourceChannels).queryParams(this.map)
10391047
.accept(MediaType.APPLICATION_JSON).get(String.class), new TypeReference<List<XmlChannel>>() {
10401048
});
10411049
} catch (Exception e) {
@@ -1142,7 +1150,7 @@ private class DeleteElement implements Runnable
11421150

11431151
@Override
11441152
public void run() {
1145-
service.path(elementType).path(elementName).delete();
1153+
cfAuthenticatedResource.path(elementType).path(elementName).delete();
11461154
}
11471155

11481156
}
@@ -1237,7 +1245,7 @@ private class DeleteElementfromChannel implements Runnable
12371245
@Override
12381246
public void run()
12391247
{
1240-
service.path(this.elementType).path(this.elementName).path(this.channelName)
1248+
cfAuthenticatedResource.path(this.elementType).path(this.elementName).path(this.channelName)
12411249
.accept(MediaType.APPLICATION_JSON).delete();
12421250
}
12431251

@@ -1300,7 +1308,7 @@ public Collection<Channel> getAllChannels()
13001308
List<XmlChannel> xmlchannels = new ArrayList<XmlChannel>();
13011309
try {
13021310
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),
13041312
new TypeReference<List<XmlChannel>>() {
13051313
});
13061314
} catch (JsonParseException | JsonMappingException e) {

app/channel/channelfinder/src/main/resources/channelfinder_preferences.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
serviceURL=http://localhost:8080/ChannelFinder
66
username=admin
7-
password=adminPass
7+
password=adminPass
8+
9+
rawFiltering=false

0 commit comments

Comments
 (0)