diff --git a/src/main/java/fr/igred/omero/RepositoryObjectWrapper.java b/src/main/java/fr/igred/omero/RepositoryObjectWrapper.java index f8cc8bfb..f2f48e3c 100644 --- a/src/main/java/fr/igred/omero/RepositoryObjectWrapper.java +++ b/src/main/java/fr/igred/omero/RepositoryObjectWrapper.java @@ -20,6 +20,7 @@ import fr.igred.omero.client.Browser; import fr.igred.omero.client.Client; +import fr.igred.omero.client.ConnectionHandler; import fr.igred.omero.exception.AccessException; import fr.igred.omero.exception.ExceptionHandler; import fr.igred.omero.exception.ServiceException; @@ -122,7 +123,7 @@ private static List importCandidates(DataObject target, ImportLibrary li * @throws AccessException Cannot access data. * @throws IOException Cannot read file. */ - protected static boolean importImages(Client client, DataObject target, int threads, String... paths) + protected static boolean importImages(ConnectionHandler client, DataObject target, int threads, String... paths) throws ServiceException, AccessException, IOException { boolean success; @@ -169,7 +170,7 @@ protected static boolean importImages(Client client, DataObject target, int thre * @throws AccessException Cannot access data. * @throws IOException Cannot read file. */ - protected static List importImage(Client client, DataObject target, String path) + protected static List importImage(ConnectionHandler client, DataObject target, String path) throws ServiceException, AccessException, IOException { ImportConfig config = new ImportConfig(); String type = PojoMapper.getGraphType(target.getClass()); diff --git a/src/main/java/fr/igred/omero/client/Client.java b/src/main/java/fr/igred/omero/client/Client.java index d53e475f..fcda6638 100644 --- a/src/main/java/fr/igred/omero/client/Client.java +++ b/src/main/java/fr/igred/omero/client/Client.java @@ -19,14 +19,9 @@ import fr.igred.omero.exception.AccessException; -import fr.igred.omero.exception.ExceptionHandler; import fr.igred.omero.exception.ServiceException; -import ome.formats.OMEROMetadataStoreClient; import omero.api.IAdminPrx; import omero.api.IQueryPrx; -import omero.gateway.Gateway; -import omero.gateway.LoginCredentials; -import omero.gateway.exception.DSOutOfServiceException; import omero.gateway.facility.AdminFacility; import omero.gateway.facility.BrowseFacility; import omero.gateway.facility.DataManagerFacility; @@ -43,147 +38,7 @@ /** * Client interface to connect to OMERO, browse through all the data accessible to the user and modify it. */ -public interface Client extends AdminManager, Browser, DataManager { - - - /** - * Returns the Gateway. - * - * @return The Gateway. - */ - Gateway getGateway(); - - - /** - * Gets the user id. - * - * @return The user ID. - */ - default long getId() { - return getUser().getId(); - } - - - /** - * Gets the current group ID. - * - * @return The group ID. - */ - default long getCurrentGroupId() { - return getCtx().getGroupID(); - } - - - /** - * Get the ID of the current session - * - * @return See above - * - * @throws ServiceException If the connection is broken, or not logged in - */ - default String getSessionId() throws ServiceException { - return ExceptionHandler.of(getGateway(), - g -> g.getSessionId(getUser().asDataObject())) - .rethrow(DSOutOfServiceException.class, - ServiceException::new, - "Could not retrieve session ID") - .get(); - } - - - /** - * Check if the client is still connected to the server - * - * @return See above. - */ - default boolean isConnected() { - return getGateway().isConnected(); - } - - - /** - * Connects to OMERO using a session ID. - * - * @param hostname Name of the host. - * @param port Port used by OMERO. - * @param sessionId The session ID. - * - * @throws ServiceException Cannot connect to OMERO. - */ - default void connect(String hostname, int port, String sessionId) - throws ServiceException { - connect(new LoginCredentials(sessionId, sessionId, hostname, port)); - } - - - /** - * Connects the user to OMERO. - *

Uses the argument to connect to the gateway. - *

Connects to the group specified in the argument. - * - * @param hostname Name of the host. - * @param port Port used by OMERO. - * @param username Username of the user. - * @param password Password of the user. - * @param groupID ID of the group to connect. - * - * @throws ServiceException Cannot connect to OMERO. - */ - default void connect(String hostname, int port, String username, char[] password, Long groupID) - throws ServiceException { - LoginCredentials cred = new LoginCredentials(username, - String.valueOf(password), - hostname, - port); - cred.setGroupID(groupID); - connect(cred); - } - - - /** - * Connects the user to OMERO. - *

Uses the argument to connect to the gateway. - *

Connects to the default group of the user. - * - * @param hostname Name of the host. - * @param port Port used by OMERO. - * @param username Username of the user. - * @param password Password of the user. - * - * @throws ServiceException Cannot connect to OMERO. - */ - default void connect(String hostname, int port, String username, char[] password) - throws ServiceException { - connect(new LoginCredentials(username, - String.valueOf(password), - hostname, - port)); - } - - - /** - * Connects the user to OMERO. Gets the SecurityContext and the BrowseFacility. - * - * @param credentials User credentials. - * - * @throws ServiceException Cannot connect to OMERO. - */ - void connect(LoginCredentials credentials) throws ServiceException; - - - /** - * Disconnects the user - */ - void disconnect(); - - - /** - * Change the current group used by the current user; - * - * @param groupId The group ID. - */ - void switchGroup(long groupId); - +public interface Client extends AdminManager, Browser, ConnectionHandler, DataManager { /** * Gets the {@link BrowseFacility} used to access the data from OMERO. @@ -293,25 +148,6 @@ default AdminFacility getAdminFacility() throws ExecutionException { } - /** - * Creates or recycles the import store. - * - * @return See above. - * - * @throws ServiceException Cannot connect to OMERO. - */ - OMEROMetadataStoreClient getImportStore() - throws ServiceException; - - - /** - * Closes the import store. - */ - default void closeImport() { - getGateway().closeImport(getCtx(), null); - } - - /** * Returns a Client associated with the provided username. *

The user calling this function needs to have administrator rights. diff --git a/src/main/java/fr/igred/omero/client/ConnectionHandler.java b/src/main/java/fr/igred/omero/client/ConnectionHandler.java new file mode 100644 index 00000000..9421d1d8 --- /dev/null +++ b/src/main/java/fr/igred/omero/client/ConnectionHandler.java @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2020-2024 GReD + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 Franklin + * Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package fr.igred.omero.client; + + +import fr.igred.omero.exception.AccessException; +import fr.igred.omero.exception.ExceptionHandler; +import fr.igred.omero.exception.ServiceException; +import fr.igred.omero.meta.ExperimenterWrapper; +import ome.formats.OMEROMetadataStoreClient; +import omero.gateway.Gateway; +import omero.gateway.LoginCredentials; +import omero.gateway.SecurityContext; +import omero.gateway.exception.DSOutOfServiceException; + +import java.util.NoSuchElementException; +import java.util.concurrent.ExecutionException; + + +/** + * Interface to handle the connection to an OMERO server through a {@link Gateway} for a given user and in a specific + * {@link SecurityContext}. + */ +public interface ConnectionHandler { + + /** + * Returns the {@link Gateway} used for the connection. + * + * @return See above. + */ + Gateway getGateway(); + + + /** + * Returns the current user. + * + * @return See above. + */ + ExperimenterWrapper getUser(); + + + /** + * Returns the current {@link SecurityContext}. + * + * @return See above + */ + SecurityContext getCtx(); + + + /** + * Gets the user id. + * + * @return The user ID. + */ + default long getId() { + return getUser().getId(); + } + + + /** + * Gets the current group ID. + * + * @return The group ID. + */ + default long getCurrentGroupId() { + return getCtx().getGroupID(); + } + + + /** + * Get the ID of the current session + * + * @return See above + * + * @throws ServiceException If the connection is broken, or not logged in + */ + default String getSessionId() throws ServiceException { + return ExceptionHandler.of(getGateway(), + g -> g.getSessionId(getUser().asDataObject())) + .rethrow(DSOutOfServiceException.class, + ServiceException::new, + "Could not retrieve session ID") + .get(); + } + + + /** + * Check if the client is still connected to the server + * + * @return See above. + */ + default boolean isConnected() { + return getGateway().isConnected(); + } + + + /** + * Connects to OMERO using a session ID. + * + * @param hostname Name of the host. + * @param port Port used by OMERO. + * @param sessionId The session ID. + * + * @throws ServiceException Cannot connect to OMERO. + */ + default void connect(String hostname, int port, String sessionId) + throws ServiceException { + connect(new LoginCredentials(sessionId, sessionId, hostname, port)); + } + + + /** + * Connects the user to OMERO. + *

Uses the argument to connect to the gateway. + *

Connects to the group specified in the argument. + * + * @param hostname Name of the host. + * @param port Port used by OMERO. + * @param username Username of the user. + * @param password Password of the user. + * @param groupID ID of the group to connect. + * + * @throws ServiceException Cannot connect to OMERO. + */ + default void connect(String hostname, int port, String username, char[] password, Long groupID) + throws ServiceException { + LoginCredentials cred = new LoginCredentials(username, + String.valueOf(password), + hostname, + port); + cred.setGroupID(groupID); + connect(cred); + } + + + /** + * Connects the user to OMERO. + *

Uses the argument to connect to the gateway. + *

Connects to the default group of the user. + * + * @param hostname Name of the host. + * @param port Port used by OMERO. + * @param username Username of the user. + * @param password Password of the user. + * + * @throws ServiceException Cannot connect to OMERO. + */ + default void connect(String hostname, int port, String username, char[] password) + throws ServiceException { + connect(new LoginCredentials(username, + String.valueOf(password), + hostname, + port)); + } + + + /** + * Connects the user to OMERO. Gets the SecurityContext and the BrowseFacility. + * + * @param credentials User credentials. + * + * @throws ServiceException Cannot connect to OMERO. + */ + void connect(LoginCredentials credentials) throws ServiceException; + + + /** + * Disconnects the user + */ + void disconnect(); + + + /** + * Change the current group used by the current user; + * + * @param groupId The group ID. + */ + void switchGroup(long groupId); + + + /** + * Creates or recycles the import store. + * + * @return See above. + * + * @throws ServiceException Cannot connect to OMERO. + */ + OMEROMetadataStoreClient getImportStore() + throws ServiceException; + + + /** + * Closes the import store. + */ + default void closeImport() { + getGateway().closeImport(getCtx(), null); + } + + + /** + * Returns a ConnectionHandler associated with the provided username. + *

The user calling this function needs to have administrator rights. + *

All actions realized with the returned ConnectionHandler will be considered as his. + * + * @param username The username. + * + * @return The client corresponding to the new user. + * + * @throws ServiceException Cannot connect to OMERO. + * @throws AccessException Cannot access data. + * @throws ExecutionException A Facility can't be retrieved or instantiated. + * @throws NoSuchElementException The requested user does not exist. + */ + ConnectionHandler sudo(String username) + throws ServiceException, AccessException, ExecutionException; + +} diff --git a/src/main/java/fr/igred/omero/containers/DatasetWrapper.java b/src/main/java/fr/igred/omero/containers/DatasetWrapper.java index 6626a928..c45a0406 100644 --- a/src/main/java/fr/igred/omero/containers/DatasetWrapper.java +++ b/src/main/java/fr/igred/omero/containers/DatasetWrapper.java @@ -23,6 +23,7 @@ import fr.igred.omero.annotations.TagAnnotationWrapper; import fr.igred.omero.client.Browser; import fr.igred.omero.client.Client; +import fr.igred.omero.client.ConnectionHandler; import fr.igred.omero.client.DataManager; import fr.igred.omero.core.ImageWrapper; import fr.igred.omero.exception.AccessException; @@ -430,7 +431,7 @@ public void removeImage(Client client, ImageWrapper image) * @throws AccessException Cannot access data. * @throws IOException Cannot read file. */ - public boolean importImages(Client client, String... paths) + public boolean importImages(ConnectionHandler client, String... paths) throws ServiceException, AccessException, IOException { return importImages(client, 1, paths); } @@ -449,7 +450,7 @@ public boolean importImages(Client client, String... paths) * @throws AccessException Cannot access data. * @throws IOException Cannot read file. */ - public boolean importImages(Client client, int threads, String... paths) + public boolean importImages(ConnectionHandler client, int threads, String... paths) throws ServiceException, AccessException, IOException { return importImages(client, data, threads, paths); } @@ -467,7 +468,7 @@ public boolean importImages(Client client, int threads, String... paths) * @throws AccessException Cannot access data. * @throws IOException Cannot read file. */ - public List importImage(Client client, String path) + public List importImage(ConnectionHandler client, String path) throws ServiceException, AccessException, IOException { return importImage(client, data, path); } diff --git a/src/main/java/fr/igred/omero/containers/FolderWrapper.java b/src/main/java/fr/igred/omero/containers/FolderWrapper.java index 0b399f40..85c0b102 100644 --- a/src/main/java/fr/igred/omero/containers/FolderWrapper.java +++ b/src/main/java/fr/igred/omero/containers/FolderWrapper.java @@ -21,7 +21,7 @@ import fr.igred.omero.ObjectWrapper; import fr.igred.omero.RepositoryObjectWrapper; import fr.igred.omero.client.Browser; -import fr.igred.omero.client.Client; +import fr.igred.omero.client.ConnectionHandler; import fr.igred.omero.client.DataManager; import fr.igred.omero.core.ImageWrapper; import fr.igred.omero.exception.AccessException; @@ -95,7 +95,7 @@ public FolderWrapper(Folder folder) { * @throws ServiceException Cannot connect to OMERO. * @throws AccessException Cannot access data. */ - public FolderWrapper(Client client, String name) + public FolderWrapper(ConnectionHandler client, String name) throws ServiceException, AccessException { super(new FolderData()); data.setName(name); diff --git a/src/main/java/fr/igred/omero/core/PixelsWrapper.java b/src/main/java/fr/igred/omero/core/PixelsWrapper.java index 1a4fe548..a1aefaa3 100644 --- a/src/main/java/fr/igred/omero/core/PixelsWrapper.java +++ b/src/main/java/fr/igred/omero/core/PixelsWrapper.java @@ -20,7 +20,7 @@ import fr.igred.omero.ObjectWrapper; import fr.igred.omero.client.Browser; -import fr.igred.omero.client.Client; +import fr.igred.omero.client.ConnectionHandler; import fr.igred.omero.exception.AccessException; import fr.igred.omero.exception.ExceptionHandler; import fr.igred.omero.exception.ServiceException; @@ -370,7 +370,7 @@ public int getSizeT() { * * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - boolean createRawDataFacility(Client client) throws ExecutionException { + boolean createRawDataFacility(ConnectionHandler client) throws ExecutionException { boolean created = false; if (rawDataFacility == null) { rawDataFacility = client.getGateway() @@ -400,7 +400,7 @@ void destroyRawDataFacility() { * @throws AccessException If an error occurs while retrieving the plane data from the pixels source. * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - public double[][][][][] getAllPixels(Client client) + public double[][][][][] getAllPixels(ConnectionHandler client) throws AccessException, ExecutionException { return getAllPixels(client, null, null, null, null, null); } @@ -421,7 +421,7 @@ public double[][][][][] getAllPixels(Client client) * @throws AccessException If an error occurs while retrieving the plane data from the pixels source. * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - public double[][][][][] getAllPixels(Client client, + public double[][][][][] getAllPixels(ConnectionHandler client, int[] xBounds, int[] yBounds, int[] cBounds, @@ -477,7 +477,7 @@ public double[][][][][] getAllPixels(Client client, * @throws AccessException If an error occurs while retrieving the plane data from the pixels source. * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - double[][] getTile(Client client, Coordinates start, int width, int height) + double[][] getTile(ConnectionHandler client, Coordinates start, int width, int height) throws AccessException, ExecutionException { boolean rdf = createRawDataFacility(client); double[][] tile = ExceptionHandler.of(this, @@ -538,7 +538,7 @@ private double[][] getTileUnchecked(SecurityContext ctx, Coordinates start, int * @throws AccessException If an error occurs while retrieving the plane data from the pixels source. * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - public byte[][][][] getRawPixels(Client client, int bpp) + public byte[][][][] getRawPixels(ConnectionHandler client, int bpp) throws AccessException, ExecutionException { return getRawPixels(client, null, null, null, null, null, bpp); } @@ -560,7 +560,7 @@ public byte[][][][] getRawPixels(Client client, int bpp) * @throws AccessException If an error occurs while retrieving the plane data from the pixels source. * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - public byte[][][][] getRawPixels(Client client, + public byte[][][][] getRawPixels(ConnectionHandler client, int[] xBounds, int[] yBounds, int[] cBounds, @@ -617,7 +617,7 @@ public byte[][][][] getRawPixels(Client client, * @throws AccessException If an error occurs while retrieving the plane data from the pixels source. * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - byte[] getRawTile(Client client, Coordinates start, int width, int height, int bpp) + byte[] getRawTile(ConnectionHandler client, Coordinates start, int width, int height, int bpp) throws AccessException, ExecutionException { boolean rdf = createRawDataFacility(client); byte[] tile = ExceptionHandler.of(this, diff --git a/src/main/java/fr/igred/omero/screen/ScreenWrapper.java b/src/main/java/fr/igred/omero/screen/ScreenWrapper.java index df009ba6..fdac53f1 100644 --- a/src/main/java/fr/igred/omero/screen/ScreenWrapper.java +++ b/src/main/java/fr/igred/omero/screen/ScreenWrapper.java @@ -20,7 +20,7 @@ import fr.igred.omero.RepositoryObjectWrapper; import fr.igred.omero.client.Browser; -import fr.igred.omero.client.Client; +import fr.igred.omero.client.ConnectionHandler; import fr.igred.omero.client.DataManager; import fr.igred.omero.core.ImageWrapper; import fr.igred.omero.exception.AccessException; @@ -342,7 +342,7 @@ public void reload(Browser browser) * @throws AccessException Cannot access data. * @throws IOException Cannot read file. */ - public boolean importImages(Client client, String... paths) + public boolean importImages(ConnectionHandler client, String... paths) throws ServiceException, AccessException, IOException { return importImages(client, 1, paths); } @@ -361,7 +361,7 @@ public boolean importImages(Client client, String... paths) * @throws AccessException Cannot access data. * @throws IOException Cannot read file. */ - public boolean importImages(Client client, int threads, String... paths) + public boolean importImages(ConnectionHandler client, int threads, String... paths) throws ServiceException, AccessException, IOException { return importImages(client, data, threads, paths); } @@ -379,7 +379,7 @@ public boolean importImages(Client client, int threads, String... paths) * @throws AccessException Cannot access data. * @throws IOException Cannot read file. */ - public List importImage(Client client, String path) + public List importImage(ConnectionHandler client, String path) throws ServiceException, AccessException, IOException { return importImage(client, data, path); }