From ec3c24f78bb5674f5ee947be1b2db29fcfe9cc91 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 16 Mar 2021 18:16:50 +0100 Subject: [PATCH] starting work on Feature #20979 --- CHANGELOG.md | 5 +- .../wsthreddssync/server/BeanConverter.java | 2 +- .../server/SyncronizeWithThredds.java | 5 +- .../ThreddsWorkspaceSyncServiceImpl.java | 88 +++++++++++++------ .../widgets/wsthreddssync/server/WsUtil.java | 5 +- 5 files changed, 71 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b74765..042f6bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,13 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [v1.3.0-SNAPSHOT] - 2021-03-03 +## [v1.3.0-SNAPSHOT] - 2021-03-16 [#20847] Support the roles of THREDDS Admin and THREDDS Publisher +[#20979] Changed the business logic to invoke the lib ws-thredds + + ## [v1.2.0] - 2020-07-21 [#19676] Migrated to git/jenkins diff --git a/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/BeanConverter.java b/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/BeanConverter.java index 008e8cf..5fa7a67 100644 --- a/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/BeanConverter.java +++ b/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/BeanConverter.java @@ -54,7 +54,7 @@ public class BeanConverter { ws.setSelectedScope(selectedScope); logger.debug("Resolved SCOPE: " + selectedScope + " from token"); } catch (Exception e) { - // TODO: handle exception + logger.error("I cannot resolve the scope for the token: "+t.getConfiguration().getTargetToken()); } } diff --git a/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/SyncronizeWithThredds.java b/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/SyncronizeWithThredds.java index 36bfc5f..c6bd2be 100644 --- a/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/SyncronizeWithThredds.java +++ b/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/SyncronizeWithThredds.java @@ -90,8 +90,9 @@ public class SyncronizeWithThredds { try{ if(firstSync) { - if(thConfig==null) - throw new Exception("A valid folder configuration must be provided to perforom synchronization"); + if(thConfig==null) { + throw new Exception("A valid folder configuration must be provided to perform the synchronization"); + } logger.info("First sync setting synchronized folder configuration: "+thConfig); workspaceThreddsSynchronize.setSynchronizedFolder(thConfig, folderId); diff --git a/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/ThreddsWorkspaceSyncServiceImpl.java b/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/ThreddsWorkspaceSyncServiceImpl.java index 48f1082..1fb9448 100644 --- a/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/ThreddsWorkspaceSyncServiceImpl.java +++ b/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/ThreddsWorkspaceSyncServiceImpl.java @@ -2,7 +2,6 @@ package org.gcube.portlets.widgets.wsthreddssync.server; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -100,31 +99,62 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem public ThSyncStatus doSyncFolder(final String folderId, WsThreddsSynchFolderConfiguration clientConfig) throws Exception{ logger.info("Performing doSyncFolder method on id: "+folderId +", config: "+clientConfig); + String originalScope = null; + String originalToken = null; + try { - GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest()); + + PortalContext portalContext = PortalContext.getConfiguration(); + final GCubeUser user = portalContext.getCurrentUser(this.getThreadLocalRequest()); + final String wsScope = portalContext.getCurrentScope(this.getThreadLocalRequest()); + final String wsUserToken = portalContext.getCurrentUserToken(wsScope, user.getUsername()); + //Thread Local contexts + originalScope = wsScope; + originalToken = wsUserToken; + String targetScopeUserToken = null; ThSynchFolderConfiguration config = null; if(clientConfig!=null) { - targetScopeUserToken = PortalContext.getConfiguration().getCurrentUserToken(clientConfig.getSelectedScope().getScopeName(), user.getUsername()); + targetScopeUserToken = portalContext.getCurrentUserToken(clientConfig.getSelectedScope().getScopeName(), user.getUsername()); config = BeanConverter.toThSynchFolderConfiguration(clientConfig, folderId, targetScopeUserToken); logger.debug("Creating server config "+config); } + + /* Commented because Feature #20979 logger.info("Calling doSyncFolder on folderId: "+folderId +", config: "+config); String wsScope = PortalContext.getConfiguration().getCurrentScope(this.getThreadLocalRequest()); String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername()); - - ThSyncStatus status = getSyncService().doSyncFolder(folderId, config, wsScope, wsUserToken); + */ + + logger.info("Calling doSyncFolder on folderId: "+folderId +", config: "+config); + logger.info("Changing the context to target scope: "+clientConfig.getSelectedScope().getScopeName() +", target token: "+targetScopeUserToken.substring(0,10)+"-MASKED-TOKEN"); + String targetScope = clientConfig.getSelectedScope().getScopeName(); + + ThSyncStatus status = getSyncService().doSyncFolder(folderId, config, targetScope, targetScopeUserToken); logger.debug("Returning for folderId "+folderId+" the syncStatus: "+status); return status; }catch (Exception e) { logger.error("Do sync Folder error: ",e); throw new Exception("Sorry, an error occurred during synchonization phase, try again later"); + }finally { + + String currentScope = PortalContext.getConfiguration().getCurrentScope(this.getThreadLocalRequest()); + + if(originalScope!=null && originalScope.compareTo(currentScope)!=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); + } + + } } + } - - + /** * Gets the available THREDDS catalogues for target scope. @@ -143,14 +173,16 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem String originalToken = null; List listCtlgs = null; try { - GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest()); - String wsScope = PortalContext.getConfiguration().getCurrentScope(this.getThreadLocalRequest()); - String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername()); + + PortalContext portalContext = PortalContext.getConfiguration(); + final GCubeUser user = portalContext.getCurrentUser(this.getThreadLocalRequest()); + final String wsScope = portalContext.getCurrentScope(this.getThreadLocalRequest()); + final String wsUserToken = portalContext.getCurrentUserToken(wsScope, user.getUsername()); //Thread Local contexts originalScope = wsScope; originalToken = wsUserToken; - String targetScopeUserToken = PortalContext.getConfiguration().getCurrentUserToken(targetFullScope, user.getUsername()); + String targetScopeUserToken = portalContext.getCurrentUserToken(targetFullScope, user.getUsername()); listCtlgs = getSyncService().getAvailableCataloguesByToken(targetFullScope, wsUserToken, targetScopeUserToken); logger.debug("Retuning "+listCtlgs.size()+" Catalogues for scope: "+targetFullScope); @@ -193,10 +225,10 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem //DEV MODE if (!isWithinPortal()){ - mapScopesRoles = new HashMap(); - mapScopesRoles.put("/gcube/devNext/NextNext", GatewayRolesThredds.DATA_MANAGER); +// mapScopesRoles = new HashMap(); + //mapScopesRoles.put("/gcube/devsec/devVRE", GatewayRolesThredds.DATA_EDITOR); // mapScopesRoles.put( "/gcube", GatewayRolesThredds.THREDDS_PUBLISHER); -// mapScopesRoles.put( "/gcube/devNext/NextNext", GatewayRolesThredds.THREDDS_PUBLISHER); +// mapScopesRoles.put( "/gcube/devNext/NextNext", GatewayRolesThredds.DATA_EDITOR); return mapScopesRoles; } @@ -267,9 +299,9 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem // } if(scopeType!=null){ - GatewayRolesThredds roles = WsUtil.getThreddsRoleFor(user, gCubeGroup); + GatewayRolesThredds gatewayRole = WsUtil.getThreddsRoleFor(user, gCubeGroup); //Adding only the scope where the user has the THREDDS_ADMIN role - GcubeScope gcubeScope = checkDataManagerCapacityAndThreddsCatalogue(roles, gCubeGroup.getGroupName(), fullScope, groupManager, scopeType); + GcubeScope gcubeScope = checkDataManagerCapacityAndThreddsCatalogue(gatewayRole, gCubeGroup.getGroupName(), fullScope, groupManager, scopeType); if(gcubeScope!=null) { listOfScopes.add(gcubeScope); } @@ -386,18 +418,18 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem } - /** - * Register callback for id. - * - * @param folderId the folder id - * @throws Exception the exception - */ - public void registerCallbackForId(String folderId) throws Exception{ - GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest()); - String wsScope = PortalContext.getConfiguration().getCurrentScope(this.getThreadLocalRequest()); - String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername()); - getSyncService().registerCallbackForId(folderId, wsScope, wsUserToken); - } +// /** +// * Register callback for id. +// * +// * @param folderId the folder id +// * @throws Exception the exception +// */ +// public void registerCallbackForId(String folderId) throws Exception{ +// GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest()); +// String wsScope = PortalContext.getConfiguration().getCurrentScope(this.getThreadLocalRequest()); +// String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername()); +// getSyncService().registerCallbackForId(folderId, wsScope, wsUserToken); +// } /** diff --git a/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/WsUtil.java b/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/WsUtil.java index 64148d8..67da4de 100644 --- a/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/WsUtil.java +++ b/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/WsUtil.java @@ -133,7 +133,8 @@ public class WsUtil { for (GCubeGroup gCubeGroup : listOfGroups) { GatewayRolesThredds threddsRole = getThreddsRoleFor(user, gCubeGroup); if(threddsRole != null) { - mapRoleByGroupSingleVre.put(gCubeGroup.getGroupName(), threddsRole); + String toFullScope = groupManager.getInfrastructureScope(gCubeGroup.getGroupId()); + mapRoleByGroupSingleVre.put(toFullScope, threddsRole); } } @@ -172,7 +173,7 @@ public class WsUtil { threddsRoles.add(GatewayRolesThredds.DATA_EDITOR); } } - + logger.info("For user: "+user.getUsername()+" in the scope: "+scope.getGroupName()+" read the role/s: " + threddsRoles); GatewayRolesThredds toReturn = null;