Lucio Lelii 2016-09-22 08:44:29 +00:00
parent e97e7585c3
commit eb941576f5
7 changed files with 62 additions and 12 deletions

View File

@ -16,6 +16,9 @@ public final class AuthorizationEndpoint implements Comparable<AuthorizationEndp
@XmlAttribute @XmlAttribute
private String infrastructure; private String infrastructure;
@XmlAttribute(name="cacheValidityInMillis")
long clientCacheValidity = 10*60*1000; //10 minutes
@XmlElement @XmlElement
private String host; private String host;
@ -55,6 +58,10 @@ public final class AuthorizationEndpoint implements Comparable<AuthorizationEndp
public int getPriority() { public int getPriority() {
return priority; return priority;
} }
public long getClientCacheValidity() {
return clientCacheValidity;
}
@Override @Override
public int compareTo(AuthorizationEndpoint o) { public int compareTo(AuthorizationEndpoint o) {

View File

@ -17,38 +17,47 @@ public class AuthorizationEndpointScanner {
private static Logger log = LoggerFactory.getLogger(AuthorizationEndpointScanner.class); private static Logger log = LoggerFactory.getLogger(AuthorizationEndpointScanner.class);
private static Map<Integer, AuthorizationEndpoint> endpoints; private static EndpointsContainer endpoints;
/** /**
* The path used to find service map configuration files. * The path used to find service map configuration files.
*/ */
static final String configurationPattern = "authorization-endpoint.xml"; static final String configurationPattern = ".*\\.authorization";
/** /**
* Scans the classpath for {@link ServiceMap}s. * Scans the classpath for {@link ServiceMap}s.
*/ */
public static synchronized Map<Integer, AuthorizationEndpoint> endpoints() { public static synchronized EndpointsContainer endpoints() {
if (endpoints==null || endpoints.size()==0){ if (endpoints==null || endpoints.getEndpoints().size()==0){
endpoints = new HashMap<Integer, AuthorizationEndpoint>(); Map<Integer, AuthorizationEndpoint> endpointsMap = new HashMap<Integer, AuthorizationEndpoint>();
try { try {
JAXBContext context = JAXBContext.newInstance(AuthorizationEndpoint.class); JAXBContext context = JAXBContext.newInstance(AuthorizationEndpoint.class);
Unmarshaller um = context.createUnmarshaller(); Unmarshaller um = context.createUnmarshaller();
String defaultInfrastructure = null;
ClasspathScanner scanner = ClasspathScannerFactory.scanner(); ClasspathScanner scanner = ClasspathScannerFactory.scanner();
for (ClasspathResource r : scanner.scan(new NameMatcher(configurationPattern))){ for (ClasspathResource r : scanner.scan(new NameMatcher(configurationPattern))){
AuthorizationEndpoint endpoint = (AuthorizationEndpoint)um.unmarshal(r.stream()); AuthorizationEndpoint endpoint = (AuthorizationEndpoint)um.unmarshal(r.stream());
if (!endpoints.containsKey(endpoint.getInfrastructure()) if (defaultInfrastructure==null)
|| endpoints.get(endpoint.getInfrastructure()).getPriority()> endpoint.getPriority()) defaultInfrastructure = endpoint.getInfrastructure();
endpoints.put(endpoint.getInfrastructure().hashCode(), endpoint);
if (!endpointsMap.containsKey(endpoint.getInfrastructure())
|| endpointsMap.get(endpoint.getInfrastructure()).getPriority()> endpoint.getPriority()){
if ((r.name()).startsWith("default"))
defaultInfrastructure = endpoint.getInfrastructure();
endpointsMap.put(endpoint.getInfrastructure().hashCode(), endpoint);
}
log.info("loaded endpoint {} ",endpoint.toString()); log.info("loaded endpoint {} ",endpoint.toString());
} }
if (endpoints.size()==0)
if (endpointsMap.size()==0)
throw new Exception("no endpoints retreived"); throw new Exception("no endpoints retreived");
endpoints = new EndpointsContainer(endpointsMap, defaultInfrastructure);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("could not load authorization endpoints", e); throw new RuntimeException("could not load authorization endpoints", e);
} }

View File

@ -0,0 +1,22 @@
package org.gcube.common.authorization.library.enpoints;
import java.util.Map;
public class EndpointsContainer {
private Map<Integer, AuthorizationEndpoint> endpoints;
private String defaultInfrastructure;
protected EndpointsContainer(Map<Integer, AuthorizationEndpoint> endpoints,
String defaultInfrastructure) {
super();
this.endpoints = endpoints;
this.defaultInfrastructure = defaultInfrastructure;
}
public Map<Integer, AuthorizationEndpoint> getEndpoints() {
return endpoints;
}
public String getDefaultInfrastructure() {
return defaultInfrastructure;
}
}

View File

@ -1,5 +1,7 @@
package org.gcube.common.authorization.library.policies; package org.gcube.common.authorization.library.policies;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
@ -16,6 +18,10 @@ public class User extends UserEntity {
super(identifier); super(identifier);
} }
protected User(List<String> excludes) {
super(excludes);
}
@Override @Override
public UserEntityType getType() { public UserEntityType getType() {
return UserEntityType.USER; return UserEntityType.USER;

View File

@ -1,5 +1,7 @@
package org.gcube.common.authorization.library.policies; package org.gcube.common.authorization.library.policies;
import java.util.Arrays;
public class Users { public class Users {
@ -10,5 +12,9 @@ public class Users {
public static User all(){ public static User all(){
return new User(); return new User();
} }
public static User allExcept(String ... identifiers){
return new User(Arrays.asList(identifiers));
}
} }

View File

@ -58,7 +58,7 @@ public class EndpointBinder {
@Test @Test
public void scan(){ public void scan(){
Map<Integer, AuthorizationEndpoint> endpoints = AuthorizationEndpointScanner.endpoints(); Map<Integer, AuthorizationEndpoint> endpoints = AuthorizationEndpointScanner.endpoints().getEndpoints();
System.out.println(endpoints); System.out.println(endpoints);
} }