Skip to content

Commit

Permalink
Remember enable unauthenticated user in core + gui
Browse files Browse the repository at this point in the history
  • Loading branch information
Rylern committed Feb 21, 2024
1 parent 7f1d688 commit 231ec4d
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 184 deletions.
4 changes: 4 additions & 0 deletions src/main/java/qupath/ext/omero/core/WebClients.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public static CompletableFuture<WebClient> createClient(String url, WebClient.Au
return WebClient.create(serverURI.get(), authentication, args).thenApply(client -> {
if (client.getStatus().equals(WebClient.Status.SUCCESS)) {
ClientsPreferencesManager.addURI(client.getApisHandler().getWebServerURI());
ClientsPreferencesManager.setEnableUnauthenticated(client.getApisHandler().getWebServerURI(), switch (authentication) {
case ENFORCE -> false;
case TRY_TO_SKIP, SKIP -> true;
});
updateClients(client, Operation.ADD);
}
clientsBeingCreated.remove(serverURI.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.ext.omero.core.ClientsPreferencesManager;
import qupath.lib.images.servers.ImageServer;
import qupath.lib.images.servers.ImageServerBuilder;
import qupath.ext.omero.core.WebClient;
Expand Down Expand Up @@ -84,7 +85,12 @@ public boolean matchClassName(String... classNames) {
private static Optional<WebClient> getClientAndCheckURIReachable(URI uri, String... args) {
try {
if (RequestSender.isLinkReachableWithGet(uri).get()) {
WebClient client = WebClients.createClientSync(uri.toString(), WebClient.Authentication.TRY_TO_SKIP, args);
WebClient client = WebClients.createClientSync(
uri.toString(),
ClientsPreferencesManager.getEnableUnauthenticated(uri).orElse(true) ? WebClient.Authentication.TRY_TO_SKIP : WebClient.Authentication.ENFORCE,
args
);

if (client.getStatus().equals(WebClient.Status.SUCCESS)) {
return Optional.of(client);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import qupath.ext.omero.gui.UiUtilities;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
* Window that provides an input allowing the user to write a server URL.
Expand All @@ -27,7 +29,16 @@ public class NewServerForm extends VBox {
public NewServerForm() throws IOException {
UiUtilities.loadFXML(this, NewServerForm.class.getResource("new_server_form.fxml"));

url.setText(ClientsPreferencesManager.getLastServerURI());
String lastURI = ClientsPreferencesManager.getLastServerURI();
url.setText(lastURI);

boolean skipAuthentication = true;
try {
skipAuthentication = ClientsPreferencesManager.getEnableUnauthenticated(new URI(lastURI)).orElse(true);
} catch (URISyntaxException ignored) {
// Nothing happens if the last URI is not valid
}
this.skipAuthentication.setSelected(skipAuthentication);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import javafx.collections.ListChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.ext.omero.core.ClientsPreferencesManager;
import qupath.ext.omero.core.WebClient;
import qupath.ext.omero.core.WebClients;
import qupath.ext.omero.gui.UiUtilities;
import qupath.ext.omero.gui.browser.serverbrowser.newconnectionoptions.NewConnectionOptions;
import qupath.fx.dialogs.Dialogs;

import java.io.IOException;
Expand Down Expand Up @@ -67,53 +67,39 @@ public void run() {
logger.error("Error while creating the browser", e);
}
} else {
NewConnectionOptions newConnectionOptions = null;
try {
newConnectionOptions = new NewConnectionOptions();
} catch (IOException e) {
logger.error("Error when creating the new connection options form", e);
}

boolean dialogConfirmed = newConnectionOptions != null && Dialogs.showConfirmDialog(
resources.getString("Browser.NewConnectionOptions.title"),
newConnectionOptions
);

if (dialogConfirmed) {
WebClients.createClient(
uri.toString(),
newConnectionOptions.canSkipAuthentication() ? WebClient.Authentication.TRY_TO_SKIP : WebClient.Authentication.ENFORCE
).thenAccept(client -> Platform.runLater(() -> {
if (client.getStatus().equals(WebClient.Status.SUCCESS)) {
try {
browser = new Browser(client);
this.client = client;
} catch (IOException e) {
logger.error("Error while creating the browser", e);
}
} else if (client.getStatus().equals(WebClient.Status.FAILED)) {
Optional<WebClient.FailReason> failReason = client.getFailReason();
String message = null;
WebClients.createClient(
uri.toString(),
ClientsPreferencesManager.getEnableUnauthenticated(uri).orElse(true) ? WebClient.Authentication.TRY_TO_SKIP : WebClient.Authentication.ENFORCE
).thenAccept(client -> Platform.runLater(() -> {
if (client.getStatus().equals(WebClient.Status.SUCCESS)) {
try {
browser = new Browser(client);
this.client = client;
} catch (IOException e) {
logger.error("Error while creating the browser", e);
}
} else if (client.getStatus().equals(WebClient.Status.FAILED)) {
Optional<WebClient.FailReason> failReason = client.getFailReason();
String message = null;

if (failReason.isPresent()) {
if (failReason.get().equals(WebClient.FailReason.INVALID_URI_FORMAT)) {
message = MessageFormat.format(resources.getString("Browser.BrowserCommand.invalidURI"), uri.toString());
} else if (failReason.get().equals(WebClient.FailReason.ALREADY_CREATING)) {
message = MessageFormat.format(resources.getString("Browser.BrowserCommand.alreadyCreating"), uri.toString());
}
} else {
message = MessageFormat.format(resources.getString("Browser.BrowserCommand.connectionFailed"), uri.toString());
if (failReason.isPresent()) {
if (failReason.get().equals(WebClient.FailReason.INVALID_URI_FORMAT)) {
message = MessageFormat.format(resources.getString("Browser.BrowserCommand.invalidURI"), uri.toString());
} else if (failReason.get().equals(WebClient.FailReason.ALREADY_CREATING)) {
message = MessageFormat.format(resources.getString("Browser.BrowserCommand.alreadyCreating"), uri.toString());
}
} else {
message = MessageFormat.format(resources.getString("Browser.BrowserCommand.connectionFailed"), uri.toString());
}

if (message != null) {
Dialogs.showErrorMessage(
resources.getString("Browser.BrowserCommand.webServer"),
message
);
}
if (message != null) {
Dialogs.showErrorMessage(
resources.getString("Browser.BrowserCommand.webServer"),
message
);
}
}));
}
}
}));
}
} else {
UiUtilities.showWindow(browser);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
Expand Down Expand Up @@ -63,6 +64,8 @@ public class Connection extends VBox {
@FXML
private Button remove;
@FXML
private CheckBox skipAuthentication;
@FXML
private TitledPane imagesPane;
@FXML
private VBox imagesContainer;
Expand Down Expand Up @@ -115,33 +118,19 @@ private void onBrowseClicked(ActionEvent ignoredEvent) {

@FXML
private void onConnectClicked(ActionEvent ignoredEvent) {
NewConnectionOptions newConnectionOptions = null;
try {
newConnectionOptions = new NewConnectionOptions();
} catch (IOException e) {
logger.error("Error when creating the new connection options form", e);
}

boolean dialogConfirmed = newConnectionOptions != null && Dialogs.showConfirmDialog(
resources.getString("ConnectionsManager.NewConnectionOptions.title"),
newConnectionOptions
);

if (dialogConfirmed) {
WebClients.createClient(
serverURI.toString(),
newConnectionOptions.canSkipAuthentication() ? WebClient.Authentication.TRY_TO_SKIP : WebClient.Authentication.ENFORCE
).thenAccept(client -> Platform.runLater(() -> {
if (client.getStatus().equals(WebClient.Status.SUCCESS)) {
Dialogs.showInfoNotification(
resources.getString("ConnectionsManager.Connection.webServer"),
MessageFormat.format(resources.getString("ConnectionsManager.Connection.connectedTo"), serverURI.toString())
);
} else if (client.getStatus().equals(WebClient.Status.FAILED)) {
showConnectionError(serverURI.toString(), client.getFailReason().orElse(null));
}
}));
}
WebClients.createClient(
serverURI.toString(),
skipAuthentication.isSelected() ? WebClient.Authentication.TRY_TO_SKIP : WebClient.Authentication.ENFORCE
).thenAccept(client -> Platform.runLater(() -> {
if (client.getStatus().equals(WebClient.Status.SUCCESS)) {
Dialogs.showInfoNotification(
resources.getString("ConnectionsManager.Connection.webServer"),
MessageFormat.format(resources.getString("ConnectionsManager.Connection.connectedTo"), serverURI.toString())
);
} else if (client.getStatus().equals(WebClient.Status.FAILED)) {
showConnectionError(serverURI.toString(), client.getFailReason().orElse(null));
}
}));
}

@FXML
Expand Down Expand Up @@ -234,6 +223,11 @@ private void onRemoveClicked(ActionEvent ignoredEvent) {
}
}

@FXML
private void onSkipAuthenticationClicked(ActionEvent ignoredEvent) {
ClientsPreferencesManager.setEnableUnauthenticated(serverURI, skipAuthentication.isSelected());
}

private void initUI() throws IOException {
UiUtilities.loadFXML(this, Connection.class.getResource("connection.fxml"));

Expand Down Expand Up @@ -262,6 +256,8 @@ private void initUI() throws IOException {
imagesContainer.getChildren().add(new Image(client, uri));
}
}

skipAuthentication.setSelected(ClientsPreferencesManager.getEnableUnauthenticated(serverURI).orElse(true));
}

private void setUpListeners() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Tooltip text="%Browser.NewServer.enterOmeroUrl" />
</tooltip>
</TextField>
<CheckBox fx:id="skipAuthentication" mnemonicParsing="false" selected="true" text="%Browser.BrowseMenu.enablePublic">
<CheckBox fx:id="skipAuthentication" mnemonicParsing="false" text="%Browser.BrowseMenu.enablePublic">
<tooltip>
<Tooltip text="%Browser.BrowseMenu.skipAuthentication" />
</tooltip>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.Tooltip?>
Expand Down Expand Up @@ -45,6 +46,7 @@
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</HBox>
<CheckBox fx:id="skipAuthentication" mnemonicParsing="false" onAction="#onSkipAuthenticationClicked" text="%ConnectionsManager.Connection.skipAuthentication" />
<TitledPane fx:id="imagesPane" expanded="false" text="%ConnectionsManager.Connection.noImage">
<content>
<VBox fx:id="imagesContainer" spacing="5.0" />
Expand Down

This file was deleted.

7 changes: 1 addition & 6 deletions src/main/resources/qupath/ext/omero/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ Browser.BrowserCommand.alreadyCreating = A connection to {0} is already being es
Browser.BrowserCommand.connectionFailed = Connection to {0} failed.\n\nCheck the following:\n- Valid credentials.\n- Access permission.\n- Correct URL.
Browser.BrowserCommand.webServer = Web server
Browser.NewConnectionOptions.title = New connection
Browser.NewConnectionOptions.skipAuthentication = Enable logging in as a public user
Browser.ServerBrowser.omeroWebServer = OMERO web server
Browser.ServerBrowser.server = Server:
Browser.ServerBrowser.username = Username:
Expand Down Expand Up @@ -313,14 +310,12 @@ ConnectionsManager.Connection.connectDescription = Attempt to connect to this se
ConnectionsManager.Connection.disconnect = Disconnect
ConnectionsManager.Connection.disconnectDescription = Disconnect from this server
ConnectionsManager.Connection.removeDescription = Close the connection to this server and remove it from this list
ConnectionsManager.Connection.skipAuthentication = Enable logging in as a public user
ConnectionsManager.Connection.noImage = 0 image
ConnectionsManager.Connection.loginSuccessful = Login successful: {0} ("{1}")
ConnectionsManager.Connection.logoutSuccessful = Log out successful
ConnectionsManager.Connection.logoutSuccessfulButNoUnauthenticated = Log out successful, but creating an unauthenticated connection to this server failed
ConnectionsManager.NewConnectionOptions.title = New connection
ConnectionsManager.NewConnectionOptions.skipAuthentication = Enable logging in as a public user
ConnectionsManager.ConnectionManager.noClients = No clients
ConnectionsManager.ConnectionManager.webClients = Web clients
Expand Down
Loading

0 comments on commit 231ec4d

Please sign in to comment.