in progess

This commit is contained in:
Francesco Mangiacrapa 2021-05-10 18:08:39 +02:00
parent 5d027814f0
commit f2dc0cce19
4 changed files with 196 additions and 177 deletions

View File

@ -1,8 +1,5 @@
package org.gcube.portlets.widgets.wsthreddssync.server; package org.gcube.portlets.widgets.wsthreddssync.server;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.portal.wssynclibrary.shared.thredds.Sync_Status; import org.gcube.portal.wssynclibrary.shared.thredds.Sync_Status;
import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor; import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor;
import org.gcube.portal.wssynclibrary.shared.thredds.ThSynchFolderConfiguration; import org.gcube.portal.wssynclibrary.shared.thredds.ThSynchFolderConfiguration;

View File

@ -21,6 +21,7 @@ import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor;
import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncStatus; import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncStatus;
import org.gcube.portal.wssynclibrary.shared.thredds.ThSynchFolderConfiguration; import org.gcube.portal.wssynclibrary.shared.thredds.ThSynchFolderConfiguration;
import org.gcube.portal.wssynclibrary.thredds.WorkspaceThreddsSynchronize; import org.gcube.portal.wssynclibrary.thredds.WorkspaceThreddsSynchronize;
import org.gcube.usecases.ws.thredds.faults.WorkspaceNotSynchedException;
import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -44,55 +45,54 @@ public class SyncronizeWithThredds {
/** The Constant sdf. */ /** The Constant sdf. */
//private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); //private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
/** /**
* Instantiates a new publish on thredds. * Instantiates a new publish on thredds.
* *
* @param wsScopeUserToken the ws scope user token * @param wsScopeUserToken the ws scope user token
* @param username the username * @param username the username
* @param httpSession the http session * @param httpSession the http session
*/ */
public SyncronizeWithThredds() { public SyncronizeWithThredds() {
this.workspaceThreddsSynchronize = WorkspaceThreddsSynchronize.getInstance(); this.workspaceThreddsSynchronize = WorkspaceThreddsSynchronize.getInstance();
} }
/** /**
* Sets the context parameters. * Sets the context parameters.
* *
* @param scope the scope * @param scope the scope
* @param userToken the user token * @param userToken the user token
*/ */
private void setContextParameters(String scope, String userToken) { private void setContextParameters(String scope, String userToken) {
logger.debug("Setting context parameters with scope: "+scope +", user token: "+userToken.substring(0,10)+"-MASKED-TOKEN"); logger.debug("Setting context parameters with scope: " + scope + ", user token: " + userToken.substring(0, 10)
+ "-MASKED-TOKEN");
ScopeProvider.instance.set(scope); ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(userToken); SecurityTokenProvider.instance.set(userToken);
} }
/** /**
* Change context for UMA. * Change context for UMA.
* *
* @param httpRequest the http request * @param httpRequest the http request
* @param user the user * @param user the user
* @param targetScope the target scope * @param targetScope the target scope
* @return the string representing the previous UMA Token read from {@link UmaJWTProvider#get()} * @return the string representing the previous UMA Token read from
* {@link UmaJWTProvider#get()}
*/ */
private String changeContextForUMA(HttpServletRequest httpRequest, GCubeUser user, String targetScope) { private String changeContextForUMA(HttpServletRequest httpRequest, GCubeUser user, String targetScope) {
logger.debug("Setting UMA context with target scope: "+targetScope +", user: "+user.getUsername()); logger.debug("Setting UMA context with target scope: " + targetScope + ", user: " + user.getUsername());
String previousUMAToken = null; String previousUMAToken = null;
try { try {
previousUMAToken=UmaJWTProvider.instance.get(); previousUMAToken = UmaJWTProvider.instance.get();
JWTToken umaToken = OIDCUmaUtil.getUMAToken(httpRequest, user.getUsername(), targetScope); JWTToken umaToken = OIDCUmaUtil.getUMAToken(httpRequest, user.getUsername(), targetScope);
setContextForUMA(umaToken.getRaw()); setContextForUMA(umaToken.getRaw());
}catch (Exception e) { } catch (Exception e) {
logger.warn("Error on set context for UMA: ",e); logger.warn("Error on set context for UMA: ", e);
if(previousUMAToken!=null) { if (previousUMAToken != null) {
logger.info("Setting previous UMA Token: "+previousUMAToken.substring(0,10)+"-MASKED-TOKEN"); logger.info("Setting previous UMA Token: " + previousUMAToken.substring(0, 10) + "-MASKED-TOKEN");
setContextForUMA(previousUMAToken); setContextForUMA(previousUMAToken);
} }
} }
return previousUMAToken; return previousUMAToken;
} }
@ -104,18 +104,103 @@ public class SyncronizeWithThredds {
*/ */
private void setContextForUMA(String umaToken) { private void setContextForUMA(String umaToken) {
logger.info("called set context for UMA"); logger.info("called set context for UMA");
if(umaToken==null || umaToken.isEmpty()) { if (umaToken == null || umaToken.isEmpty()) {
logger.info("Uma Token is null or empty, skipping operation"); logger.info("Uma Token is null or empty, skipping operation");
return; return;
} }
logger.info("Going to set UMA Token: "+umaToken.substring(0,10)+"-MASKED-TOKEN"); logger.info("Going to set UMA Token: " + umaToken.substring(0, 10) + "-MASKED-TOKEN");
UmaJWTProvider.instance.set(umaToken); UmaJWTProvider.instance.set(umaToken);
logger.debug("UmaJWTProvider instance set performed to : "+umaToken.substring(0,10)+"-MASKED-TOKEN"); logger.debug("UmaJWTProvider instance set performed to : " + umaToken.substring(0, 10) + "-MASKED-TOKEN");
} }
/**
* Checks if is item synched.
*
* @param folderId the folder id
* @return the th sync folder descriptor
* @throws ItemNotSynched the item not synched
* @throws Exception the exception
*/
public ThSyncFolderDescriptor isItemSynched(String folderId) throws ItemNotSynched, Exception {
ThSyncFolderDescriptor config = null;
try {
config = workspaceThreddsSynchronize.getConfiguration(folderId);
} catch (WorkspaceNotSynchedException e) {
logger.debug("WorkspaceNotSynchedException catched, the item id: " + folderId + " is not synched");
throw new ItemNotSynched("the item id: " + folderId + " is not synched");
} catch (Exception e) {
logger.debug("Error on getting configuration for the item id: " + folderId + ", returning null (means not synched)");
return null;
}
return config;
}
/**
* Gets the available catalogues.
*
* @param httpRequest the http request
* @param user the user
* @param targetScope the target scope
* @return the available catalogues
* @throws Exception the exception
*/
public List<ThCatalogueBean> getAvailableCatalogues(HttpServletRequest httpRequest, GCubeUser user,
String targetScope) throws Exception {
String originalScope = null;
String originalToken = null;
String previousUmaToken = null;
List<ThCatalogueBean> listCatalogues = null;
try {
// context switch for Uma token
previousUmaToken = changeContextForUMA(httpRequest, user, targetScope);
// context switch for gcube-token and scope
PortalContext pConfig = PortalContext.getConfiguration();
String wsScope = pConfig.getCurrentScope(httpRequest);
String wsUserToken = pConfig.getCurrentUserToken(wsScope, user.getUsername());
// Thread Local contexts
originalScope = wsScope;
originalToken = wsUserToken;
// getting token into target scope
String targetScopeUserToken = PortalContext.getConfiguration().getCurrentUserToken(targetScope,
user.getUsername());
setContextParameters(targetScope, targetScopeUserToken);
// calling the engine
listCatalogues = workspaceThreddsSynchronize.getAvailableCatalogues();
} catch (Exception e) {
logger.error("Error on getting available Catalogues in the scope: " + targetScope, e);
} finally {
// resetting UMA token in the WS scope
setContextForUMA(previousUmaToken);
if (originalScope != 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 listCatalogues;
}
//NOT UPDATED
/** /**
@ -167,52 +252,33 @@ public class SyncronizeWithThredds {
/** // /**
* Gets the synched status from item property. // * Gets the synched status from item property.
* // *
* @param folderId the folder id // * @param folderId the folder id
* @param scope the scope // * @param scope the scope
* @param username the username // * @param username the username
* @return the synched status from item property // * @return the synched status from item property
* @throws Exception the exception // * @throws Exception the exception
*/ // */
public Sync_Status getSynchedStatusFromItemProperty(String folderId, String scope, String username) throws Exception{ // public Sync_Status getSynchedStatusFromItemProperty(String folderId, String scope, String username) throws Exception{
//
try { // try {
try { // try {
ScopeProvider.instance.set(scope); // ScopeProvider.instance.set(scope);
return workspaceThreddsSynchronize.getSynchedStatusFromItemProperty(folderId, username); // return workspaceThreddsSynchronize.getSynchedStatusFromItemProperty(folderId, username);
}catch (ItemNotSynched e) { // }catch (ItemNotSynched e) {
logger.info("The folder id: "+folderId +" is not synched returning null as "+Sync_Status.class.getSimpleName()); // logger.info("The folder id: "+folderId +" is not synched returning null as "+Sync_Status.class.getSimpleName());
return null; // return null;
} // }
//
}catch (Exception e) { // }catch (Exception e) {
logger.error("Error on getSynchedStatusFromItemProperty for id: "+folderId, e); // logger.error("Error on getSynchedStatusFromItemProperty for id: "+folderId, e);
throw new Exception("Sorry, an error occurred during read sync status from HL properties, try again later"); // throw new Exception("Sorry, an error occurred during read sync status from HL properties, try again later");
} // }
} // }
/**
* Checks if is item synched.
*
* @param folderId the folder id
* @param scope the scope
* @param username the username
* @return true, if is item synched
* @throws ItemNotSynched the item not synched
* @throws Exception the exception
*/
public boolean isItemSynched(String folderId, String scope, String username) throws ItemNotSynched, Exception{
Sync_Status value = getSynchedStatusFromItemProperty(folderId, scope, username);
if(value!=null)
return true;
return false;
}
/** /**
@ -281,57 +347,4 @@ public class SyncronizeWithThredds {
} }
/**
* Gets the available catalogues.
*
* @param httpRequest the http request
* @param user the user
* @param targetScope the target scope
* @return the available catalogues
* @throws Exception the exception
*/
public List<ThCatalogueBean> getAvailableCatalogues(HttpServletRequest httpRequest, GCubeUser user,
String targetScope) throws Exception {
String originalScope = null;
String originalToken = null;
String previousUmaToken = null;
List<ThCatalogueBean> listCatalogues = null;
try {
// context switch for Uma token
previousUmaToken = changeContextForUMA(httpRequest, user, targetScope);
// context switch for gcube-token and scope
PortalContext pConfig = PortalContext.getConfiguration();
String wsScope = pConfig.getCurrentScope(httpRequest);
String wsUserToken = pConfig.getCurrentUserToken(wsScope, user.getUsername());
// Thread Local contexts
originalScope = wsScope;
originalToken = wsUserToken;
// getting token into target scope
String targetScopeUserToken = PortalContext.getConfiguration().getCurrentUserToken(targetScope,
user.getUsername());
setContextParameters(targetScope, targetScopeUserToken);
// calling the engine
listCatalogues = workspaceThreddsSynchronize.getAvailableCatalogues();
} catch (Exception e) {
logger.error("Error on getting available Catalogues in the scope: " + targetScope, e);
} finally {
// resetting UMA token in the WS scope
setContextForUMA(previousUmaToken);
if (originalScope != 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 listCatalogues;
}
} }

View File

@ -7,9 +7,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.portal.PortalContext; import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portal.wssynclibrary.shared.ItemNotSynched; import org.gcube.portal.wssynclibrary.shared.ItemNotSynched;
import org.gcube.portal.wssynclibrary.shared.WorkspaceFolderLocked; import org.gcube.portal.wssynclibrary.shared.WorkspaceFolderLocked;
import org.gcube.portal.wssynclibrary.shared.thredds.Sync_Status; import org.gcube.portal.wssynclibrary.shared.thredds.Sync_Status;
@ -83,8 +81,7 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
} }
} }
//MOVED
/** /**
* Gets the available THREDDS catalogues for target scope. * Gets the available THREDDS catalogues for target scope.
* *
@ -92,6 +89,7 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
* @return the available catalogues for scope * @return the available catalogues for scope
* @throws Exception the exception * @throws Exception the exception
*/ */
//UPDATED
@Override @Override
public List<ThCatalogueBean> getAvailableCataloguesForScope(String targetFullScope) throws Exception { public List<ThCatalogueBean> getAvailableCataloguesForScope(String targetFullScope) throws Exception {
@ -100,6 +98,7 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
List<ThCatalogueBean> listCtlgs = null; List<ThCatalogueBean> listCtlgs = null;
try { try {
GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest()); GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest());
listCtlgs = getSyncService().getAvailableCatalogues(this.getThreadLocalRequest(), user, targetFullScope); listCtlgs = getSyncService().getAvailableCatalogues(this.getThreadLocalRequest(), user, targetFullScope);
@ -116,6 +115,59 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
return listCtlgs; return listCtlgs;
} }
/**
* Checks if is item synched.
*
* @param folderId the folder id
* @return the ws thredds synch folder descriptor
* @throws WorkspaceFolderLocked the workspace folder locked
* @throws Exception the exception
*/
/* (non-Javadoc)
* @see org.gcube.portlets.widgets.wsthreddssync.client.rpc.ThreddsWorkspaceSyncService#isItemSynched(java.lang.String)
*/
//UPDATED
@Override
public WsThreddsSynchFolderDescriptor isItemSynched(String folderId) throws WorkspaceFolderLocked, Exception{
logger.debug("Performing isItemSynched for foldeId: "+folderId);
try {
ThSyncFolderDescriptor theConfig = getSyncService().isItemSynched(folderId);
if(theConfig!=null) {
logger.info("Folder id: "+folderId+" is synched");
//String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername());
//ThSyncFolderDescriptor serverFolderSync = getSyncService().checkItemSynched(folderId,wsScope,wsUserToken);
//the status is not set here (into isItemSynched implementation).
Sync_Status theStatus = null;
WsThreddsSynchFolderDescriptor toWsThreddFolder = BeanConverter.toWsThreddsFolderConfig(theConfig, theStatus);
logger.debug("IsItemSynched for id: "+folderId +" returning: "+toWsThreddFolder);
return toWsThreddFolder;
}
logger.info("Folder id: "+folderId+" is not synched, returning null descriptor");
return null;
} catch (ItemNotSynched e) {
logger.info("The folderId: "+folderId +" is not synched, returning null FolderDescriptor");
return null;
} catch (WorkspaceFolderLocked e) {
logger.warn(e.getMessage() +", sending exception to client...");
throw new WorkspaceFolderLocked(e.getFolderId(), e.getMessage());
}catch (Exception e) {
logger.info("Error on isItemSynched for folderId: "+folderId, e);
throw new Exception(e);
}
}
@ -317,55 +369,6 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
return null; return null;
} }
/**
* Checks if is item synched.
*
* @param folderId the folder id
* @return the ws thredds synch folder descriptor
* @throws WorkspaceFolderLocked the workspace folder locked
* @throws Exception the exception
*/
/* (non-Javadoc)
* @see org.gcube.portlets.widgets.wsthreddssync.client.rpc.ThreddsWorkspaceSyncService#isItemSynched(java.lang.String)
*/
@Override
public WsThreddsSynchFolderDescriptor isItemSynched(String folderId) throws WorkspaceFolderLocked, Exception{
logger.debug("Performing isItemSynched for foldeId: "+folderId);
try {
GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest());
String wsScope = PortalContext.getConfiguration().getCurrentScope(this.getThreadLocalRequest());
Sync_Status theStatus = getSyncService().getSynchedStatusFromItemProperty(folderId, wsScope, user.getUsername());
if(theStatus!=null) {
logger.info("Folder id: "+folderId+" is synched");
String wsUserToken = PortalContext.getConfiguration().getCurrentUserToken(wsScope, user.getUsername());
ThSyncFolderDescriptor serverFolderSync = getSyncService().checkItemSynched(folderId,wsScope,wsUserToken);
WsThreddsSynchFolderDescriptor toWsThreddFolder = BeanConverter.toWsThreddsFolderConfig(serverFolderSync, theStatus);
logger.debug("IsItemSynched for id: "+folderId +" returning: "+toWsThreddFolder);
return toWsThreddFolder;
}
logger.info("Folder id: "+folderId+" is not synched, returning null descriptor");
return null;
} catch (ItemNotSynched e) {
logger.info("The folderId: "+folderId +" is not synched, returning null FolderDescriptor");
return null;
} catch (WorkspaceFolderLocked e) {
logger.warn(e.getMessage() +", sending exception to client...");
throw new WorkspaceFolderLocked(e.getFolderId(), e.getMessage());
}catch (Exception e) {
logger.info("Error on isItemSynched for folderId: "+folderId, e);
throw new Exception(e);
}
}
/** /**
* Register callback for id. * Register callback for id.

View File

@ -10,8 +10,9 @@ import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor;
/** /**
* The Class WsThreddsSynchFolderDescriptor. * The Class WsThreddsSynchFolderDescriptor.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
* Feb 20, 2018 *
* May 10, 2021
*/ */
public class WsThreddsSynchFolderDescriptor implements Serializable { public class WsThreddsSynchFolderDescriptor implements Serializable {
@ -109,6 +110,11 @@ public class WsThreddsSynchFolderDescriptor implements Serializable {
this.serverFolderDescriptor = serverFolderDescriptor; this.serverFolderDescriptor = serverFolderDescriptor;
} }
/**
* To string.
*
* @return the string
*/
/* (non-Javadoc) /* (non-Javadoc)
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */