Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Damian authored and Adrian Damian committed Jul 18, 2024
1 parent 1201b5b commit 92c4625
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cadc-registry/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sourceCompatibility = 1.8

group = 'org.opencadc'

version = '1.7.6'
version = '1.7.7'

description = 'OpenCADC Registry client library'
def git_url = 'https://github.com/opencadc/reg'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2023. (c) 2023.
* (c) 2024. (c) 2024.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
Expand Down Expand Up @@ -72,9 +72,11 @@
import ca.nrc.cadc.util.MultiValuedProperties;
import ca.nrc.cadc.util.PropertiesReader;
import java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeMap;
import org.apache.log4j.Logger;

Expand All @@ -90,7 +92,7 @@ public class LocalAuthority {

private static final String LOCAL_AUTH_PROP_FILE = LocalAuthority.class.getSimpleName() + ".properties";

private Map<String, String> authorityMap = new TreeMap<String, String>();
private Map<URI, Set<URI>> authorityMap = new TreeMap<>();

public LocalAuthority() {
PropertiesReader propReader = new PropertiesReader(RegistryClient.CONFIG_FILE);
Expand All @@ -104,24 +106,46 @@ public LocalAuthority() {

for (String std : mvp.keySet()) {
List<String> values = mvp.getProperty(std);
if (values.size() > 1) {
throw new IllegalStateException("found " + values.size() + " values for " + std);
}
if (values.isEmpty()) {
log.debug(std + " has no value, skipping");
} else {
String val = values.get(0);
log.debug("authorityMap: " + std + " -> " + val);
authorityMap.put(std, val);
Set<URI> vals = new HashSet<>();
URI stdURI = URI.create(std);
authorityMap.put(stdURI, vals);
for (String val : values) {
URI valURI = URI.create(val);
log.debug("authorityMap: " + stdURI + " -> " + valURI);
vals.add(valURI);
}
}
}

/**
* Returns the service URI associated with the baseStandardID. This method fails if the local authority is
* configured with more than one service URI corresponding to the baseStandard. Use `getServiceURIs` method for
* the more generic case
* @param baseStandardID base standard ID
* @return corresponding service URI (http or ivo)
* @deprecated deprecated in favour of getServiceURIs method
*/
public URI getServiceURI(String baseStandardID) {
String resourceIdentifier = authorityMap.get(baseStandardID);
if (resourceIdentifier == null) {
Set<URI> resourceIdentifiers = authorityMap.get(URI.create(baseStandardID));
if ((resourceIdentifiers == null) || (resourceIdentifiers.isEmpty())) {
throw new NoSuchElementException("not found: " + baseStandardID);
}
if (resourceIdentifiers.size() > 1) {
throw new NoSuchElementException("Multiple service URIs found for " + baseStandardID);
}
return resourceIdentifiers.iterator().next();
}

/**
* Returns the URIs of services associated with the baseStandardID.
* @param baseStandardID base standard ID URI
* @return set of service URIs
*/
public Set<URI> getServiceURIs(URI baseStandardID) {
Set<URI> resourceIdentifiers = authorityMap.get(baseStandardID);
if ((resourceIdentifiers == null)) {
throw new NoSuchElementException("not found: " + baseStandardID);
}
return URI.create(resourceIdentifier);
return resourceIdentifiers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@
package ca.nrc.cadc.reg.client;

import java.net.URI;
import java.util.Arrays;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Set;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
Expand All @@ -94,7 +97,10 @@ public class LocalAuthorityTest {
private String SERVICE_URI = "ivo://cadc.nrc.ca/cred";

private String OPENID = Standards.SECURITY_METHOD_OPENID.toASCIIString();
private String OPENID_URL = "https://oidc.example.net/";
private final URI[] OPENID_VALUES= new URI[] {
URI.create("https://oidc.example1.net/"),
URI.create("https://oidc.example2.net/")};
private Set<URI> OPENID_URLS = new HashSet<>(Arrays.asList(OPENID_VALUES));

// values from backwards compat LocalAuthority.properties
private String SERVICE_URI_COMPAT = "ivo://cadc.nrc.ca/cred/compat";
Expand Down Expand Up @@ -150,14 +156,19 @@ public void testFoundIVO() {
}

@Test
public void testFoundURL() {
public void testFoundMultipleURLs() {
try {
System.setProperty(TEST_CONFIG_DIR, "src/test/resources");

LocalAuthority loc = new LocalAuthority();
URI uri = loc.getServiceURI(OPENID);
Assert.assertNotNull(uri);
Assert.assertEquals(OPENID_URL, uri.toASCIIString());
try {
URI uri = loc.getServiceURI(OPENID);
Assert.fail("This interface cannot be used to access multiple service URIs");
} catch (NoSuchElementException expected) {
}
Set<URI> uris = loc.getServiceURIs(URI.create(OPENID));
Assert.assertEquals(2, uris.size());
Assert.assertTrue(uris.equals(OPENID_URLS));
} catch (Exception unexpected) {
log.error("unexpected exception", unexpected);
Assert.fail("unexpected exception: " + unexpected);
Expand Down
3 changes: 2 additions & 1 deletion cadc-registry/src/test/resources/cadc-registry.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ ivo://ivoa.net/std/GMS#search-1.0 = ivo://cadc.nrc.ca/gms
ivo://ivoa.net/std/CDP#delegate-1.0 = ivo://cadc.nrc.ca/cred
ivo://ivoa.net/std/CDP#proxy-1.0 = ivo://cadc.nrc.ca/cred

ivo://ivoa.net/sso#OpenID = https://oidc.example.net/
ivo://ivoa.net/sso#OpenID = https://oidc.example1.net/
ivo://ivoa.net/sso#OpenID = https://oidc.example2.net/

0 comments on commit 92c4625

Please sign in to comment.