Page 1 of 2

CommandLineImporter configuration for group

PostPosted: Fri Apr 05, 2019 10:43 am
by derekeh
Hi,
From my java app I am using the ome.formats.importer.cli.CommandLineImporter to upload images.
I've included my code below.
The user belongs to 2 groups. Private and Public. The default is the private one.
If the user uploads to the private group, do not need to set the groupId and everthing works ok.

If, however, I try to upload to the public group by adding the public groupId to the configuration I get a "Failed to load target" ERROR.
How do I configure the cli to be able to upload to the public group?

Thanks in advance
Derek

Code: Select all
public String upload_via_omero_cli(String[] paths, long datasetID, boolean isPublic) throws Exception {
            String RET="OK";
            ImportConfig config = new ImportConfig();
            long public_group_id=53;
            config.email.set("");
            config.sendFiles.set(true);
            config.sendReport.set(false);
            config.contOnError.set(false);
            config.debug.set(false);
            config.hostname.set(props.getProperty("server"));
            config.port.set(Integer.parseInt(props.getProperty("port")));
            config.username.set(props.getProperty("db_user"));
            config.password.set(props.getProperty("db_passwd"));
            config.target.set("Dataset:"+datasetID);
       if(isPublic)
         config.group.set(public_group_id);
           
            boolean setting=false;
            CommandLineImporter cli=null;
            try {
                cli = new CommandLineImporter(config,paths,setting);
                cli.start();
            }
            catch(Exception e){RET="ERROR: "+e.getMessage();}
            finally {
                if(cli!=null)
                    cli.cleanup();
            }
            return RET;
    }

Re: CommandLineImporter configuration for group

PostPosted: Fri Apr 05, 2019 10:44 am
by derekeh
Using Omero 5.4.0

Re: CommandLineImporter configuration for group

PostPosted: Fri Apr 05, 2019 10:49 am
by jmoore
Hi Derek,

the exception is a known issue that will be fixed in the upcoming 5.5.0. See:


In your case, which group is the Dataset in?
~Josh

Re: CommandLineImporter configuration for group

PostPosted: Fri Apr 05, 2019 11:08 am
by derekeh
Hi Josh,
Thanks for replying.
If my dataset is in the public group and I configure with the public_group_id I get the error.
If I move the dataset to the private group and do not configure the public_group_id it works.

Derek

Re: CommandLineImporter configuration for group

PostPosted: Mon Apr 08, 2019 9:52 am
by derekeh
Any suggestions as to how I might fix this?
Is there a hook to attach a new securityContext(groupId) to the CLI?

Anything?
Derek

Re: CommandLineImporter configuration for group

PostPosted: Mon Apr 08, 2019 10:59 am
by jmoore
Hi Derek,

how are you creating your session before running the code? If I were going to try this myself, I'd likely start by creating the session from the command-line, and then handing it off to the Java code. In that case, I could choose which group I wanted to use:

Code: Select all
$ bin/omero login USER@localhost
Created session for USER@localhost. Idle timeout: 10 min. Current group: DEFAULTGROUP
$ bin/omero sessions group 805
Using session for USER@localhost. Idle timeout: 10 min. Current group: DEFAULTGROUP
Group 'DEFAULTGROUP' (id=804) switched to 'OTHERGROUP' (id=805)


Is changing the group at the session level an option for you? Or are you re-using the session between multiple activities?

~J.

Re: CommandLineImporter configuration for group

PostPosted: Mon Apr 08, 2019 12:20 pm
by derekeh
Well that has opened up a whole lot of questions....

Before I go into that can we clarify that in the java code, using config.group.set(group_id) :
1. Is meant to configure the command Line Importer to use the [group_id]
2. It doesn't work

Thanks
Derek

Re: CommandLineImporter configuration for group

PostPosted: Mon Apr 08, 2019 1:38 pm
by jmoore
derekeh wrote:1. Is meant to configure the command Line Importer to use the [group_id]
2. It doesn't work


In general, yes. You're not misusing the API. Unfortunately, it only specifies the client's active group, where if I follow what's going on, the server-side session hasn't been updated and therefore the Dataset isn't being found. I'd imagine it's a fixable bug, but not without a new release, so it would seem you'll need a workaround.

~Josh.

Re: CommandLineImporter configuration for group

PostPosted: Tue Apr 09, 2019 7:27 am
by derekeh
OK
how are you creating your session before running the code?

I wasn't creating a session specifically. I presumed that configuring and starting the java CLI did that under the hood.
Well, it must because it works with the default group.

I'd likely start by creating the session from the command-line,

The end user will not have access to /bin/omero. The code is deployed as a web app.
I'd prefer to complete the whole task via java rather than invoke external scripts to run /bin/omero - spirit of encapsulation and all that.....

In all other transactions in my code, eg creating projects, datasets etc I use the Gateway and can change the user's group with:
Code: Select all
ExperimenterData user = gateway.connect(loginCredentials);
new SecurityContext(user.getGroupId())


I can get the sessionID from
Code: Select all
gateway.getSession(user)


I read somewhere (escapes me right now) that I can reuse the connection by supplying the sessionID in place of the username and null for the password.

Can I use this information somehow to invoke the CLI (or alternatively via importLibrary.importCandidates)?
Or any other way to import the images to a non default group?

Lots of questions I know but I'm running out of possible scenarios and of course the ability to switch groups (private/public) to upload images to is rather crucial.

Thanks again for your help.
Derek

Re: CommandLineImporter configuration for group

PostPosted: Tue Apr 09, 2019 8:12 am
by jmoore
derekeh wrote:I wasn't creating a session specifically. I presumed that configuring and starting the java CLI did that under the hood. Well, it must because it works with the default group.


Indeed.

The end user will not have access to /bin/omero. The code is deployed as a web app.
I'd prefer to complete the whole task via java rather than invoke external scripts to run /bin/omero - spirit of encapsulation and all that.....


Of course. But this gives you an easy way to test if you'd like. We'll be happy to provide the same in Java.

In all other transactions in my code, eg creating projects, datasets etc I use the Gateway and can change the user's group with:
Code: Select all
ExperimenterData user = gateway.connect(loginCredentials);
new SecurityContext(user.getGroupId())



This does roughly the same as the config.group.set call which is to set the client-side group. My hunch is that you will need to create your session with the right server-side group to workaround your current issue.


I read somewhere (escapes me right now) that I can reuse the connection by supplying the sessionID in place of the username and null for the password.


Correct, or a potential alternative would be to set the group on the server-session which is safe as long as the session is not being re-used by multiple clients, which is why I was asking where your session was coming from.

For example, you might try this:

Code: Select all
import ome.formats.importer.*;

public class login {
  public final static String HOST = "YOURHOST";
  public final static String PORT = "4064";
  public static void main(String[] args) throws Exception {
    omero.client c = new omero.client(new String[]{"--omero.host="+HOST, "--omero.port="+PORT,"--omero.group=OTHERGROUP"});
    try {
      c.createSession("USER", "PASSWORD");
      System.out.println("Session:" + c.getSessionId());
      System.out.println("Group:" + c.getSession().getAdminService().getEventContext().groupId);
      run(c.getSessionId());
    } finally {
      c.__del__();
    }
  }

  public static void run(String session) throws Exception {
    ImportConfig config = new ImportConfig();
    config.sessionKey.set(session);
    config.hostname.set(HOST);
    config.port.set(Integer.parseInt(PORT));
    ome.formats.OMEROMetadataStoreClient store = config.createStore();
    try {
      System.out.println(store);
    } finally {
      store.logout();
    }
  }

}


where in the run method you'd make use of the sessionKey in your current code.
~J