Skip to content

Commit

Permalink
Olog ES client preferences to use @preference
Browse files Browse the repository at this point in the history
  • Loading branch information
georgweiss committed Dec 13, 2024
1 parent f549079 commit 4064f8f
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 130 deletions.
2 changes: 0 additions & 2 deletions app/logbook/olog/client-es/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<artifactId>app-logbook-olog-client-es</artifactId>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
Expand All @@ -21,7 +20,6 @@
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart -->
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpHeaders;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -89,13 +88,9 @@ public static class OlogClientBuilder {
private final String protocol;
private String username = null;
private String password = null;
private String connectTimeoutAsString = null;
private Boolean permissiveHostnameVerifier;

private final OlogProperties properties = new OlogProperties();

private OlogClientBuilder() {
this.ologURI = URI.create(this.properties.getPreferenceValue("olog_url"));
this.ologURI = URI.create(Preferences.olog_url);
this.protocol = this.ologURI.getScheme();
}

Expand Down Expand Up @@ -157,45 +152,29 @@ public OlogClient create() {
this.clientConfig = new DefaultClientConfig();
}
}
if(this.username == null || this.password == null){
if (this.username == null || this.password == null) {
ScopedAuthenticationToken scopedAuthenticationToken = getCredentialsFromSecureStore();
if(scopedAuthenticationToken != null){
if (scopedAuthenticationToken != null) {
this.username = scopedAuthenticationToken.getUsername();
this.password = scopedAuthenticationToken.getPassword();
} else {
this.username = Preferences.username != null ? Preferences.username : this.username;
this.password = Preferences.password != null ? Preferences.password : this.password;
}
else{
this.username = ifNullReturnPreferenceValue(this.username, "username");
this.password = ifNullReturnPreferenceValue(this.password, "password");
}
}
this.connectTimeoutAsString = ifNullReturnPreferenceValue(this.connectTimeoutAsString, "connectTimeout");
int connectTimeout = 0;
try {
connectTimeout = Integer.parseInt(connectTimeoutAsString);
} catch (NumberFormatException e) {
Logger.getLogger(OlogClientBuilder.class.getPackageName())
.warning("connectTimeout preference not set or invalid, using 0 (=infinite)");
}

int connectTimeout = Preferences.connectTimeout;
this.clientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, connectTimeout);

this.permissiveHostnameVerifier = Boolean.parseBoolean(this.properties.getPreferenceValue("permissive_hostname_verifier"));
if (this.permissiveHostnameVerifier) {
if (Preferences.permissive_hostname_verifier) {
HostnameVerifier allHostsValid = (hostname, session) -> true;
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

return new OlogClient(this.ologURI, this.clientConfig, this.withHTTPAuthentication, this.username, this.password);
}

private String ifNullReturnPreferenceValue(String value, String key) {
if (value == null) {
return this.properties.getPreferenceValue(key);
} else {
return value;
}
}

private ScopedAuthenticationToken getCredentialsFromSecureStore(){
private ScopedAuthenticationToken getCredentialsFromSecureStore() {
try {
SecureStore secureStore = new SecureStore();
return secureStore.getScopedAuthenticationToken(AuthenticationScope.LOGBOOK);
Expand Down Expand Up @@ -405,24 +384,9 @@ public List<LogEntry> listLogs() {
return new ArrayList<>();
}

/**
* List of level values as defined in the properties file.
*/
private List<String> levels;

/**
* Service URL as configured by properties.
*/
private String serviceUrl;

@Override
public Collection<String> listLevels() {
if (levels == null) {
OlogProperties ologProperties = new OlogProperties();
String[] levelList = ologProperties.getPreferenceValue("levels").split(",");
levels = Arrays.asList(levelList);
}
return levels;
return Arrays.stream(Preferences.levels).toList();
}

@Override
Expand Down Expand Up @@ -466,11 +430,7 @@ public Collection<Tag> listTags() {

@Override
public String getServiceUrl() {
if (serviceUrl == null) {
OlogProperties ologProperties = new OlogProperties();
serviceUrl = ologProperties.getPreferenceValue("olog_url");
}
return serviceUrl;
return Preferences.olog_url;
}

@Override
Expand Down Expand Up @@ -549,7 +509,7 @@ public String serviceInfo() {
}

@Override
public SearchResult getArchivedEntries(long id){
public SearchResult getArchivedEntries(long id) {
try {
final OlogSearchResult ologSearchResult = OlogObjectMappers.logEntryDeserializer.readValue(
service.path("logs/archived/" + id)
Expand All @@ -566,7 +526,7 @@ public SearchResult getArchivedEntries(long id){
}

@Override
public Collection<LogTemplate> getTemplates(){
public Collection<LogTemplate> getTemplates() {
try {
return OlogObjectMappers.logEntryDeserializer.readValue(
service.path("templates").accept(MediaType.APPLICATION_JSON).get(String.class),
Expand All @@ -579,7 +539,7 @@ public Collection<LogTemplate> getTemplates(){
}

@Override
public LogTemplate saveTemplate(LogTemplate template) throws LogbookException{
public LogTemplate saveTemplate(LogTemplate template) throws LogbookException {
ClientResponse clientResponse = service.path("templates").accept(MediaType.APPLICATION_JSON_TYPE)
.header("Content-Type", MediaType.APPLICATION_JSON_TYPE)
.put(ClientResponse.class, template);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2024 European Spallation Source ERIC.
*/

package org.phoebus.olog.es.api;

import org.phoebus.framework.preferences.AnnotatedPreferences;
import org.phoebus.framework.preferences.Preference;

public class Preferences {

@Preference
public static String olog_url;

@Preference
public static int connectTimeout;

@Preference
public static boolean permissive_hostname_verifier;

@Preference
public static String username;

@Preference
public static String password;

@Preference
public static boolean debug;

@Preference public static String[] levels;

static
{
AnnotatedPreferences.initialize(Preferences.class, "/olog_es_preferences.properties");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
Expand All @@ -39,6 +38,7 @@
import org.phoebus.logbook.LogClient;
import org.phoebus.logbook.Logbook;
import org.phoebus.logbook.Tag;
import org.phoebus.olog.es.api.Preferences;
import org.phoebus.ui.dialog.ListSelectionPopOver;
import org.phoebus.ui.dialog.PopOver;
import org.phoebus.ui.time.TimeRelativeIntervalPane;
Expand All @@ -51,7 +51,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static org.phoebus.logbook.olog.ui.LogbookQueryUtil.Keys;
Expand All @@ -62,8 +61,6 @@
*/
public class AdvancedSearchViewController {

static final Logger logger = Logger.getLogger(AdvancedSearchViewController.class.getName());

@FXML
Label levelLabel;

Expand Down Expand Up @@ -108,7 +105,7 @@ public class AdvancedSearchViewController {
@FXML
private TextField attachmentTypes;

private SearchParameters searchParameters;
private final SearchParameters searchParameters;

private final SimpleBooleanProperty sortAscending = new SimpleBooleanProperty(false);
private final SimpleBooleanProperty requireAttachments = new SimpleBooleanProperty(false);
Expand Down Expand Up @@ -254,7 +251,7 @@ public void initialize() {
if (tagSearchPopover.isShowing()) {
tagSearchPopover.hide();
} else {
List<String> selectedTags = Arrays.stream( searchParameters.tagsProperty().getValueSafe().split(","))
List<String> selectedTags = Arrays.stream(searchParameters.tagsProperty().getValueSafe().split(","))
.map(String::trim)
.filter(it -> !it.isEmpty())
.collect(Collectors.toList());
Expand All @@ -272,7 +269,7 @@ public void initialize() {
if (logbookSearchPopover.isShowing()) {
logbookSearchPopover.hide();
} else {
List<String> selectedLogbooks = Arrays.stream( searchParameters.logbooksProperty().getValueSafe().split(","))
List<String> selectedLogbooks = Arrays.stream(searchParameters.logbooksProperty().getValueSafe().split(","))
.map(String::trim)
.filter(it -> !it.isEmpty())
.collect(Collectors.toList());
Expand All @@ -286,7 +283,7 @@ public void initialize() {
}
});

List<String> levelList = logClient.listLevels().stream().collect(Collectors.toList());
List<String> levelList = Arrays.stream(Preferences.levels).toList();
levelSelector.getItems().add("");
levelSelector.getItems().addAll(levelList);

Expand All @@ -307,15 +304,15 @@ public AnchorPane getPane() {
/**
* Updates non-text field controls so that search parameter values are correctly rendered.
*
* @param queryString
* @param queryString Query string containing search terms and values
*/
private void updateControls(String queryString) {
Map<String, String> queryStringParameters = LogbookQueryUtil.parseHumanReadableQueryString(queryString);
queryStringParameters.entrySet().stream().forEach(entry -> {
Keys keys = Keys.findKey(entry.getKey());
if (keys != null) {
if (keys.equals(Keys.LEVEL)) {
List<String> levels = logClient.listLevels().stream().collect(Collectors.toList());
List<String> levels = Arrays.stream(Preferences.levels).toList();
if (levels.contains(entry.getValue())) {
searchParameters.levelProperty().setValue(entry.getValue());
} else {
Expand Down Expand Up @@ -371,7 +368,7 @@ protected List<String> getValidatedTagsSelection(String tags) {
return validatedLogbookNames;
}

public SimpleBooleanProperty getSortAscending(){
public SimpleBooleanProperty getSortAscending() {
return sortAscending;
}

Expand Down
Loading

0 comments on commit 4064f8f

Please sign in to comment.