adding context switch to getConfiguration when needed

This commit is contained in:
Francesco Mangiacrapa 2021-05-18 12:55:44 +02:00
parent 5cc52d9e3c
commit 54600fe288
2 changed files with 56 additions and 26 deletions

View File

@ -28,11 +28,12 @@ import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* The Class SyncronizeWithThredds.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Feb 7, 2018
* updated May, 2021
*/
public class SyncronizeWithThredds {
@ -40,7 +41,7 @@ public class SyncronizeWithThredds {
private Logger logger = LoggerFactory.getLogger(SyncronizeWithThredds.class);
/** The workspace thredds synchronize. */
private WorkspaceThreddsSynchronize workspaceThreddsSynchronize;
private WorkspaceThreddsSynchronize workspaceThreddsSynchronizeLib;
/** The Constant sdf. */
// private static final SimpleDateFormat sdf = new
@ -54,7 +55,7 @@ public class SyncronizeWithThredds {
* @param httpSession the http session
*/
public SyncronizeWithThredds() {
this.workspaceThreddsSynchronize = WorkspaceThreddsSynchronize.getInstance();
this.workspaceThreddsSynchronizeLib = WorkspaceThreddsSynchronize.getInstance();
}
/**
@ -133,7 +134,7 @@ public class SyncronizeWithThredds {
ContainerType itemType) throws ItemNotSynched, Exception {
setContextParameters(scope, userToken);
return workspaceThreddsSynchronize.isItemSynched(folderId, itemProperties, itemType);
return workspaceThreddsSynchronizeLib.isItemSynched(folderId, itemProperties, itemType);
}
/**
@ -142,25 +143,44 @@ public class SyncronizeWithThredds {
* @param folderId the folder id
* @param loadStatus the load status. If true it loads the status by calling the
* {@link SyncEngine#check(String, boolean)} but it is time
* consuming. Otherwise it callas the
* consuming. Otherwise it calls the
* {@link SyncEngine#getConfig(String)} without sync status
* @param scope the scope
* @param userToken the user token
* @param httpRequest the http request
* @param user the user
* @return the configuration
* @throws ItemNotSynched the item not synched
* @throws Exception the exception
*/
public ThSyncFolderDescriptor getConfiguration(String folderId, boolean loadStatus, String scope, String userToken)
public ThSyncFolderDescriptor getConfiguration(String folderId, boolean loadStatus, HttpServletRequest httpRequest, GCubeUser user)
throws ItemNotSynched, Exception {
ThSyncFolderDescriptor config = null;
String wsScope = PortalContext.getConfiguration().getCurrentScope(httpRequest);
String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername());
String originalScope = wsScope;
String originalToken = wsUserToken;
String previousUmaToken = null;
String targetScope = null;
try {
setContextParameters(scope, userToken);
setContextParameters(wsScope, wsUserToken);
if (loadStatus) {
config = workspaceThreddsSynchronize.checkItemSynched(folderId);
config = workspaceThreddsSynchronizeLib.getConfiguration(folderId);
// context switch for UMA token
targetScope = config.getConfiguration().getTargetContext();
previousUmaToken = changeContextForUMA(httpRequest, user, targetScope);
// context switch for gcube-token and scope
// getting token into target scope
String targetScopeUserToken = PortalContext.getConfiguration().getCurrentUserToken(targetScope,
user.getUsername());
setContextParameters(targetScope, targetScopeUserToken);
config = workspaceThreddsSynchronizeLib.checkItemSynched(folderId);
} else {
config = workspaceThreddsSynchronize.getConfiguration(folderId);
config = workspaceThreddsSynchronizeLib.getConfiguration(folderId);
}
} catch (WorkspaceNotSynchedException e) {
@ -171,6 +191,22 @@ public class SyncronizeWithThredds {
} catch (Exception e) {
logger.error("Error on reading the configuration for id: " + folderId, e);
throw e;
} finally {
if(previousUmaToken!=null) {
// resetting UMA token in the WS scope
setContextForUMA(previousUmaToken);
}
if (originalScope != null && targetScope!=null && originalScope.compareTo(targetScope) != 0) {
logger.info("Resetting the scope: " + originalScope + " which was original WS context");
ScopeProvider.instance.set(originalScope);
if (originalToken != null) {
logger.info("Resetting the user token: " + originalToken.substring(0, 10)
+ "-MASKED-TOKEN which was original WS context");
SecurityTokenProvider.instance.set(originalToken);
}
}
}
return config;
@ -208,7 +244,7 @@ public class SyncronizeWithThredds {
setContextParameters(targetScope, targetScopeUserToken);
// calling the engine
listCatalogues = workspaceThreddsSynchronize.getAvailableCatalogues();
listCatalogues = workspaceThreddsSynchronizeLib.getAvailableCatalogues();
} catch (Exception e) {
logger.error("Error on getting available Catalogues in the scope: " + targetScope, e);
@ -276,7 +312,7 @@ public class SyncronizeWithThredds {
user.getUsername());
setContextParameters(targetScope, targetScopeUserToken);
ThSyncFolderDescriptor folder = workspaceThreddsSynchronize.checkItemSynched(folderId);
ThSyncFolderDescriptor folder = workspaceThreddsSynchronizeLib.checkItemSynched(folderId);
} catch (ItemNotSynched e) {
firstSync = true;
@ -289,11 +325,11 @@ public class SyncronizeWithThredds {
if (firstSync) {
logger.info("First sync setting the synchronized folder configuration: " + thConfig);
workspaceThreddsSynchronize.setSynchronizedFolder(thConfig, folderId);
workspaceThreddsSynchronizeLib.setSynchronizedFolder(thConfig, folderId);
}
logger.info("Calling do sync on folder id: " + folderId);
return workspaceThreddsSynchronize.doSync(folderId);
return workspaceThreddsSynchronizeLib.doSync(folderId);
} catch (Exception e) {
logger.error("Error on do sync: ", e);
@ -362,7 +398,7 @@ public class SyncronizeWithThredds {
user.getUsername());
setContextParameters(targetScope, targetScopeUserToken);
return workspaceThreddsSynchronize.doUnSync(folderId, deleteRemoteContent);
return workspaceThreddsSynchronizeLib.doUnSync(folderId, deleteRemoteContent);
} catch (ItemNotSynched e) {
throw new Exception("The item with id: " + folderId + " is not synched");
@ -401,7 +437,7 @@ public class SyncronizeWithThredds {
throws ItemNotSynched, Exception {
setContextParameters(scope, userToken);
return workspaceThreddsSynchronize.monitorSyncStatus(itemId);
return workspaceThreddsSynchronizeLib.monitorSyncStatus(itemId);
}
/**
@ -414,7 +450,7 @@ public class SyncronizeWithThredds {
*/
protected void registerCallbackForId(String folderId, String scope, String userToken) throws Exception {
setContextParameters(scope, userToken);
workspaceThreddsSynchronize.registerCallbackForId(folderId);
workspaceThreddsSynchronizeLib.registerCallbackForId(folderId);
}
}

View File

@ -130,11 +130,7 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
try {
GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest());
String wsScope = PortalContext.getConfiguration().getCurrentScope(this.getThreadLocalRequest());
String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername());
ThSyncFolderDescriptor theConfig = getSyncService().getConfiguration(folderId, loadStatus, wsScope,
wsUserToken);
ThSyncFolderDescriptor theConfig = getSyncService().getConfiguration(folderId, loadStatus, this.getThreadLocalRequest(), user);
if (theConfig != null) {
logger.info("Folder id: " + folderId + " is synched");
@ -208,9 +204,7 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
try {
GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest());
String wsScope = PortalContext.getConfiguration().getCurrentScope(this.getThreadLocalRequest());
String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername());
ThSyncFolderDescriptor foldeDesr = getSyncService().getConfiguration(folderId, false, wsScope, wsUserToken);
ThSyncFolderDescriptor foldeDesr = getSyncService().getConfiguration(folderId, false, this.getThreadLocalRequest(), user);
return getSyncService().doUnSync(folderId, true, foldeDesr.getConfiguration(), this.getThreadLocalRequest(),
user);
} catch (Exception e) {