package org.gcube.smartgears.handlers; import static org.gcube.smartgears.utils.Utils.notEmpty; import static org.gcube.smartgears.utils.Utils.rethrowUnchecked; import java.util.ArrayList; import java.util.Collection; import java.util.Set; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.resources.gcore.Resource; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.publisher.ScopedPublisher; import org.gcube.smartgears.provider.ProviderFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class AbstractProfilePublisher

implements ProfilePublisher { private static final Logger log = LoggerFactory.getLogger(AbstractProfilePublisher.class); //the underlying IS publisher private final ScopedPublisher publisher; //private AuthorizationProvider authProvider ; public AbstractProfilePublisher() { this.publisher=ProviderFactory.provider().publisher(); //this.authProvider = ProviderFactory.provider().authorizationProvider(); } protected abstract P getProfile(); protected abstract boolean isRoot(); protected abstract void sharePublished(P profile); protected abstract Set getAllowedContexts(); /** * Removes the application from one or more scopes. * @param scopes the scopes */ public void removeFrom(Collection contexts) { P profile = getProfile(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); log.debug("using context {}",contextCL.getClass().getSimpleName()); String previousToken = SecurityTokenProvider.instance.get(); String previousScope = ScopeProvider.instance.get(); try{//This classloader set is needed for the jaxb context if (previousToken!=null) SecurityTokenProvider.instance.reset(); if (isRoot()) Thread.currentThread().setContextClassLoader(AbstractProfilePublisher.class.getClassLoader()); profile = publisher.remove(profile, new ArrayList(contexts)); } catch (Exception e) { rethrowUnchecked(e); } finally{ SecurityTokenProvider.instance.set(previousToken); ScopeProvider.instance.set(previousScope); if (isRoot()) Thread.currentThread().setContextClassLoader(contextCL); } log.debug("after remove application profile contains scopes {}",profile.scopes().asCollection()); sharePublished(profile); } public void addToAll(){ this.addTo(getAllowedContexts()); } /** * Adds for the first time the current resource profile of the application in one or more scopes. * @param contexts the contexts */ public void addTo(Collection contexts) { notEmpty("contexts",contexts); P profile = getProfile(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); log.debug("using context {}",contextCL.getClass().getSimpleName()); String previousToken = SecurityTokenProvider.instance.get(); String previousScope = ScopeProvider.instance.get(); try{//This classloader set is needed for the jaxb context if (previousToken!=null) SecurityTokenProvider.instance.reset();; if (isRoot()) Thread.currentThread().setContextClassLoader(AbstractProfilePublisher.class.getClassLoader()); ScopeProvider.instance.set(contexts.stream().findFirst().get()); profile = publisher.create(profile, new ArrayList(contexts)); } catch (Exception e) { rethrowUnchecked(e); } finally{ SecurityTokenProvider.instance.set(previousToken); ScopeProvider.instance.set(previousScope); if (isRoot()) Thread.currentThread().setContextClassLoader(contextCL); } sharePublished(profile); log.debug("shared profile with scopes {}", profile.scopes().asCollection()); } public void update() { P profile = getProfile(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); log.debug("using context {}",contextCL.getClass().getSimpleName()); String previousToken = SecurityTokenProvider.instance.get(); String previousScope = ScopeProvider.instance.get(); try{//This classloader set is needed for the jaxb context if (previousToken!=null) SecurityTokenProvider.instance.reset(); if (isRoot()) Thread.currentThread().setContextClassLoader(AbstractProfilePublisher.class.getClassLoader()); profile = publisher.update(profile); } catch (Exception e) { rethrowUnchecked(e); } finally{ SecurityTokenProvider.instance.set(previousToken); ScopeProvider.instance.set(previousScope); if (isRoot()) Thread.currentThread().setContextClassLoader(contextCL); } sharePublished(profile); } }