Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support websocket URLs #21

Merged
merged 15 commits into from
Dec 17, 2019
Merged
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
}

dependencies {
api("org.openmicroscopy:omero-blitz:5.5.3")
api("org.openmicroscopy:omero-blitz:5.5.4")

implementation("commons-beanutils:commons-beanutils:1.9.3")
implementation("com.zeroc:icegrid:3.6.4")
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/omero/gateway/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -966,7 +965,7 @@ public Connector call() throws Exception {
session = prx.createSessionWithTimeouts(p, 0, timeout);
// Create the userSession
omero.client client = new omero.client(context
.getServerInformation().getHostname(), context
.getServerInformation().getHost(), context
.getServerInformation().getPort());
ServiceFactoryPrx userSession = client.createSession(session
.getUuid().getValue(), session.getUuid().getValue());
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/omero/gateway/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public ExperimenterData connect(LoginCredentials c)
throw new DSOutOfServiceException(e.getMessage(), e);
} catch (DNSException e) {
throw new DSOutOfServiceException("Can't resolve hostname "
+ c.getServer().getHostname(), e);
+ c.getServer().getHost(), e);
}
}

Expand Down Expand Up @@ -1063,10 +1063,10 @@ private SessionWrapper createSession(LoginCredentials c)
} else {
username = c.getUser().getUsername();
if (c.getServer().getPort() > 0)
secureClient = new client(c.getServer().getHostname(), c
secureClient = new client(c.getServer().getHost(), c
.getServer().getPort());
else
secureClient = new client(c.getServer().getHostname());
secureClient = new client(c.getServer().getHost());
}
secureClient.setAgent(c.getApplicationName());
ServiceFactoryPrx entryEncrypted = null;
Expand Down Expand Up @@ -1702,7 +1702,7 @@ private Connector createConnector(SecurityContext ctx, boolean permitNull)
args.toArray(new String[args.size()]));
prx = client.createSession();
} else {
client = new client(login.getServer().getHostname(),
client = new client(login.getServer().getHost(),
login.getServer().getPort());
prx = client.createSession(login.getUser().getUsername(), login
.getUser().getPassword());
Expand Down
37 changes: 32 additions & 5 deletions src/main/java/omero/gateway/LoginCredentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
*/
package omero.gateway;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import omero.IllegalArgumentException;

Expand Down Expand Up @@ -63,6 +65,16 @@ public class LoginCredentials {
/** Whether to check the client-server versions */
private boolean checkVersion = true;

/** Default websocket ports (this might be moved into omero.constants
* in future) **/
private enum DefaultPort {
WS(80), WSS(443);
private final int port;
DefaultPort(int port) {
this.port = port;
}
}

/**
* Creates a new instance
*/
Expand Down Expand Up @@ -96,10 +108,10 @@ public LoginCredentials(String[] args) {
* @param password
* The password
* @param host
* The server hostname
* The server hostname or websocket URL
*/
public LoginCredentials(String username, String password, String host) {
this(username, password, host, omero.constants.GLACIER2PORT.value);
this(username, password, host, -1);
}

/**
Expand All @@ -111,7 +123,7 @@ public LoginCredentials(String username, String password, String host) {
* @param password
* The password
* @param host
* The server hostname
* The server hostname or websocket URL
* @param port
* The server port
*/
Expand All @@ -120,8 +132,23 @@ public LoginCredentials(String username, String password, String host,
this();
user.setUsername(username);
user.setPassword(password);
server.setHostname(host);
server.setPort(port);
server.setHost(host);
if (port >= 0) {
server.setPort(port);
}
else if (server.getPort() < 0) {
// set default ports
if (!server.isURL()) {
server.setPort(omero.constants.GLACIER2PORT.value);
}
else if (server.getPort() < 0) {
try {
server.setPort(DefaultPort.valueOf(server.getProtocol().toUpperCase()).port);
} catch (IllegalArgumentException e) {
// neither ws nor wss
}
}
}
}

/**
Expand Down
167 changes: 122 additions & 45 deletions src/main/java/omero/gateway/ServerInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
*/
package omero.gateway;

import java.net.URI;
import java.net.URISyntaxException;

/**
* Holds hostname and port of an OMERO server
* Holds the network connection information of an OMERO server
*
* @author Dominik Lindner &nbsp;&nbsp;&nbsp;&nbsp; <a
* href="mailto:[email protected]">[email protected]</a>
Expand All @@ -30,57 +33,139 @@

public class ServerInformation {

/** The hostname */
private String hostname;

/** The port */
private int port;
/** The URI */
private URI uri;

/**
* Creates an empty instance
*/
public ServerInformation() {
try {
this.uri = new URI(null, null, null,
-1, null, null, null);
} catch (URISyntaxException e) {
}
}

/**
* Creates a new instance
*
* @param hostname
* The hostname or websocket URL
*/
public ServerInformation(String hostname) {
this(hostname, -1);
}

/**
* Creates a new instance
*
* @param hostname
* The hostname
* The hostname or websocket URL
* @param port
* The port
*/
public ServerInformation(String hostname, int port) {
super();
this.hostname = hostname;
this.port = port;
try {
if (hostname.contains(":/")) {
// this is already a URI like wss://example.org
this.uri = new URI(hostname);
if (port >= 0 && this.uri.getPort() < 0) {
this.uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(),
port, uri.getPath(), uri.getQuery(), uri.getFragment());
}
}
else {
if (port < 0 && hostname.contains(":")) {
try {
String[] parts = hostname.split(":");
port = Integer.parseInt(parts[parts.length-1]);
hostname = parts[parts.length-2];
} catch (Exception e) {}
}
this.uri = new URI(null,null, hostname,
port, null, null, null);
}
} catch (URISyntaxException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to log the error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about it. But at that point I don't have any reference to the Gateway, which has the Logger.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok understood

}
}

/**
* Get the host information as required by the omero.client.
* In case a websocket URL was specified the full
* URL will be returned. If only a host name was
* specified only the host name will be returned.
* @return See above.
*/
public String getHost() {
if (this.uri.isAbsolute())
return this.uri.toString();
return this.uri.getHost();
}

/**
* Return the hostname.
* Set the hostname or websocket URL
*
* @param host
* See above
*/
public void setHost(String host) {
try {
if (host.contains(":/")) {
// this is already a URI like wss://example.org
int port = this.uri.getPort();
this.uri = new URI(host);
if (port >= 0 && this.uri.getPort() < 0) {
this.uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(),
port, uri.getPath(), uri.getQuery(), uri.getFragment());
}
}
else {
int port = uri.getPort();
if (port < 0 && host.contains(":")) {
try {
String[] parts = host.split(":");
port = Integer.parseInt(parts[parts.length-1]);
host = parts[parts.length-2];
} catch (Exception e) {}
}
this.uri = new URI(uri.getScheme(), uri.getUserInfo(), host,
port, uri.getPath(), uri.getQuery(), uri.getFragment());
}
} catch (URISyntaxException e) {
}
}

/**
* Return the hostname. Even if a websocket URL
* was specified only the hostname part will
* be returned by this method. Use {@link #getHost()}
* to get the full websocket URL.
*
* @return The hostname
*/
public String getHostname() {
return hostname;
return uri.getHost();
}

/**
* Set the hostname
* @deprecated Renamed to {@link #setHost(String)}
*
* Set the hostname or websocket URL
*
* @param hostname
* See above
*/
public void setHostname(String hostname) {
this.hostname = hostname;
setHost(hostname);
}

/**
* Return the port
* @return The port
*/
public int getPort() {
return port;
return uri.getPort();
}

/**
Expand All @@ -90,43 +175,35 @@ public int getPort() {
* See above
*/
public void setPort(int port) {
this.port = port;
try {
this.uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(),
port, uri.getPath(), uri.getQuery(), uri.getFragment());
} catch (URISyntaxException e) {
}
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((hostname == null) ? 0 : hostname.hashCode());
result = prime * result + port;
return result;
/**
* Returns <code>true</code> if a websocket
* URL was specified.
* @return See above.
*/
public boolean isURL() {
return this.uri.isAbsolute();
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ServerInformation other = (ServerInformation) obj;
if (hostname == null) {
if (other.hostname != null)
return false;
} else if (!hostname.equals(other.hostname))
return false;
if (port != other.port)
return false;
return true;
/**
* Returns the protocol (lower case) if a websocket URL was specified
* (empty String otherwise).
* @return See above.
*/
public String getProtocol() {
if (isURL())
return this.uri.getScheme().toLowerCase();
return "";
}

@Override
public String toString() {
return "ServerInformation [hostname=" + hostname + ", port=" + port
+ "]";
return "ServerInformation [uri=" + uri.toString() + "]";
}


}