2015-05-29 18:32:49 +02:00
|
|
|
package org.gcube.common.authorization.library;
|
|
|
|
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
2016-01-22 17:28:11 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2015-05-29 18:32:49 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
|
|
|
|
|
|
|
|
public class AuthorizedTasks {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds a {@link Callable} task to the current scope and user.
|
|
|
|
* @param task the task
|
|
|
|
* @return an equivalent {@link Callable} task bound to the current scope and user
|
|
|
|
*/
|
|
|
|
static public <V> Callable<V> bind(final Callable<V> task) {
|
|
|
|
|
|
|
|
final String callScope = ScopeProvider.instance.get();
|
|
|
|
|
2016-01-22 17:28:11 +01:00
|
|
|
final ClientInfo userCall = AuthorizationProvider.instance.get();
|
|
|
|
|
|
|
|
final String token = SecurityTokenProvider.instance.get();
|
2015-05-29 18:32:49 +02:00
|
|
|
|
|
|
|
return new Callable<V>() {
|
|
|
|
@Override
|
|
|
|
public V call() throws Exception {
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(callScope);
|
|
|
|
AuthorizationProvider.instance.set(userCall);
|
2016-01-22 17:28:11 +01:00
|
|
|
SecurityTokenProvider.instance.set(token);
|
2015-05-29 18:32:49 +02:00
|
|
|
try {
|
|
|
|
return task.call();
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
ScopeProvider.instance.reset();
|
|
|
|
AuthorizationProvider.instance.reset();
|
2016-01-22 17:28:11 +01:00
|
|
|
SecurityTokenProvider.instance.reset();
|
2015-05-29 18:32:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds a {@link Runnable} task to the current scope and user.
|
|
|
|
* @param task the task
|
|
|
|
* @return an equivalent {@link Runnable} task bound to the current scope and user
|
|
|
|
*/
|
|
|
|
static public <V> Runnable bind(final Runnable task) {
|
|
|
|
|
|
|
|
final String callScope = ScopeProvider.instance.get();
|
|
|
|
|
2016-01-22 17:28:11 +01:00
|
|
|
final ClientInfo userCall = AuthorizationProvider.instance.get();
|
|
|
|
|
|
|
|
final String token = SecurityTokenProvider.instance.get();
|
2015-05-29 18:32:49 +02:00
|
|
|
|
|
|
|
return new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ScopeProvider.instance.set(callScope);
|
|
|
|
AuthorizationProvider.instance.set(userCall);
|
2016-01-22 17:28:11 +01:00
|
|
|
SecurityTokenProvider.instance.set(token);
|
2015-05-29 18:32:49 +02:00
|
|
|
try {
|
|
|
|
task.run();
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
ScopeProvider.instance.reset();
|
|
|
|
AuthorizationProvider.instance.reset();
|
2016-01-22 17:28:11 +01:00
|
|
|
SecurityTokenProvider.instance.reset();
|
2015-05-29 18:32:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|