|
|
|
@ -14,8 +14,6 @@ import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
|
import org.gcube.oidc.rest.JWTToken;
|
|
|
|
|
import org.gcube.portal.oidc.lr62.OIDCUmaUtil;
|
|
|
|
|
import org.gcube.portal.wssynclibrary.shared.ItemNotSynched;
|
|
|
|
|
import org.gcube.portal.wssynclibrary.shared.WorkspaceFolderLocked;
|
|
|
|
|
import org.gcube.portal.wssynclibrary.shared.thredds.Sync_Status;
|
|
|
|
|
import org.gcube.portal.wssynclibrary.shared.thredds.ThCatalogueBean;
|
|
|
|
|
import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor;
|
|
|
|
|
import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncStatus;
|
|
|
|
@ -30,12 +28,10 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
/**
|
|
|
|
|
* The Class SyncronizeWithThredds.
|
|
|
|
|
*
|
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
|
* Feb 7, 2018
|
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Feb 7, 2018
|
|
|
|
|
*/
|
|
|
|
|
public class SyncronizeWithThredds {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** The logger. */
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(SyncronizeWithThredds.class);
|
|
|
|
|
|
|
|
|
@ -43,7 +39,8 @@ public class SyncronizeWithThredds {
|
|
|
|
|
private WorkspaceThreddsSynchronize workspaceThreddsSynchronize;
|
|
|
|
|
|
|
|
|
|
/** 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.
|
|
|
|
@ -116,7 +113,6 @@ public class SyncronizeWithThredds {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if is item synched.
|
|
|
|
|
*
|
|
|
|
@ -133,7 +129,8 @@ public class SyncronizeWithThredds {
|
|
|
|
|
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)");
|
|
|
|
|
logger.debug("Error on getting configuration for the item id: " + folderId
|
|
|
|
|
+ ", returning null (means not synched)");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -175,6 +172,7 @@ public class SyncronizeWithThredds {
|
|
|
|
|
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
|
|
|
|
@ -193,109 +191,164 @@ public class SyncronizeWithThredds {
|
|
|
|
|
return listCatalogues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//NOT UPDATED
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do sync folder.
|
|
|
|
|
*
|
|
|
|
|
* @param folderId the folder id
|
|
|
|
|
* @param thConfig the th config
|
|
|
|
|
* @param scope the scope
|
|
|
|
|
* @param userToken the user token
|
|
|
|
|
* @param httpRequest the http request
|
|
|
|
|
* @param user the user
|
|
|
|
|
* @return the th sync status
|
|
|
|
|
* @throws Exception the exception
|
|
|
|
|
*/
|
|
|
|
|
public synchronized ThSyncStatus doSyncFolder(final String folderId, ThSynchFolderConfiguration thConfig, String scope, String userToken) throws Exception{
|
|
|
|
|
logger.debug("Perfoming doSynFolder on folderId: "+folderId);
|
|
|
|
|
public synchronized ThSyncStatus doSyncFolder(final String folderId, ThSynchFolderConfiguration thConfig,
|
|
|
|
|
HttpServletRequest httpRequest, GCubeUser user) throws Exception {
|
|
|
|
|
logger.debug("called doSynFolder for folderId: " + folderId);
|
|
|
|
|
|
|
|
|
|
boolean firstSync = false;
|
|
|
|
|
String originalScope = null;
|
|
|
|
|
String originalToken = null;
|
|
|
|
|
String previousUmaToken = null;
|
|
|
|
|
|
|
|
|
|
if (thConfig == null) {
|
|
|
|
|
throw new Exception("A valid folder configuration must be provided to perform the synchronization");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String targetScope = thConfig.getTargetContext();
|
|
|
|
|
if (targetScope == null || targetScope.isEmpty()) {
|
|
|
|
|
throw new Exception("Error, the target scope is not valid!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("going to doSynFolder for folderId: " + folderId + ", target scope is: " + targetScope);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
setContextParameters(scope, userToken);
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
ThSyncFolderDescriptor folder = workspaceThreddsSynchronize.checkItemSynched(folderId);
|
|
|
|
|
}catch (ItemNotSynched e) {
|
|
|
|
|
|
|
|
|
|
} catch (ItemNotSynched e) {
|
|
|
|
|
firstSync = true;
|
|
|
|
|
// TODO: handle exception
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
logger.error("Error on check item sync: ",e);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Error on check item sync: ", e);
|
|
|
|
|
throw new Exception("Sorry an error occurred during folder publishing, refresh and try again");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
if(firstSync) {
|
|
|
|
|
if(thConfig==null) {
|
|
|
|
|
throw new Exception("A valid folder configuration must be provided to perform the synchronization");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
logger.info("First sync setting synchronized folder configuration: "+thConfig);
|
|
|
|
|
if (firstSync) {
|
|
|
|
|
logger.info("First sync setting the synchronized folder configuration: " + thConfig);
|
|
|
|
|
workspaceThreddsSynchronize.setSynchronizedFolder(thConfig, folderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("Calling do sync on folder id: "+folderId);
|
|
|
|
|
logger.info("Calling do sync on folder id: " + folderId);
|
|
|
|
|
return workspaceThreddsSynchronize.doSync(folderId);
|
|
|
|
|
//SessionUtil.setTransferPublishingOnThredds(httpSession, status);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
logger.error("Error on do sync: ",e);
|
|
|
|
|
throw new Exception(e.getMessage() +", refresh and try again");
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Error on do sync: ", e);
|
|
|
|
|
throw new Exception(e.getMessage() + ", refresh and try again");
|
|
|
|
|
|
|
|
|
|
} 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do un sync.
|
|
|
|
|
*
|
|
|
|
|
* @param folderId the folder id
|
|
|
|
|
* @param deleteRemoteContent the delete remote content
|
|
|
|
|
* @param thConfig the th config
|
|
|
|
|
* @param httpRequest the http request
|
|
|
|
|
* @param user the user
|
|
|
|
|
* @return the boolean
|
|
|
|
|
* @throws Exception the exception
|
|
|
|
|
*/
|
|
|
|
|
public Boolean doUnSync(String folderId, boolean deleteRemoteContent, ThSynchFolderConfiguration thConfig,
|
|
|
|
|
HttpServletRequest httpRequest, GCubeUser user) throws Exception {
|
|
|
|
|
logger.debug("called doUnSync for folderId: " + folderId);
|
|
|
|
|
|
|
|
|
|
String originalScope = null;
|
|
|
|
|
String originalToken = null;
|
|
|
|
|
String previousUmaToken = null;
|
|
|
|
|
|
|
|
|
|
if (thConfig == null) {
|
|
|
|
|
throw new Exception("A valid folder configuration must be provided to perform the synchronization");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String targetScope = thConfig.getTargetContext();
|
|
|
|
|
if (targetScope == null || targetScope.isEmpty()) {
|
|
|
|
|
throw new Exception("Error, the target scope is not valid!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("going to doSynFolder for folderId: " + folderId + ", target scope is: " + targetScope);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * Gets the synched status from item property.
|
|
|
|
|
// *
|
|
|
|
|
// * @param folderId the folder id
|
|
|
|
|
// * @param scope the scope
|
|
|
|
|
// * @param username the username
|
|
|
|
|
// * @return the synched status from item property
|
|
|
|
|
// * @throws Exception the exception
|
|
|
|
|
// */
|
|
|
|
|
// public Sync_Status getSynchedStatusFromItemProperty(String folderId, String scope, String username) throws Exception{
|
|
|
|
|
//
|
|
|
|
|
// try {
|
|
|
|
|
// try {
|
|
|
|
|
// ScopeProvider.instance.set(scope);
|
|
|
|
|
// return workspaceThreddsSynchronize.getSynchedStatusFromItemProperty(folderId, username);
|
|
|
|
|
// }catch (ItemNotSynched e) {
|
|
|
|
|
// logger.info("The folder id: "+folderId +" is not synched returning null as "+Sync_Status.class.getSimpleName());
|
|
|
|
|
// return null;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// }catch (Exception 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");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
return workspaceThreddsSynchronize.doUnSync(folderId, deleteRemoteContent);
|
|
|
|
|
|
|
|
|
|
} catch (ItemNotSynched e) {
|
|
|
|
|
throw new Exception("The item with id: " + folderId + " is not synched");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Error on check item sync: ", e);
|
|
|
|
|
throw new Exception("Sorry an error occurred during folder publishing, refresh and try again");
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
|
|
// resetting UMA token in the WS scope
|
|
|
|
|
setContextForUMA(previousUmaToken);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check item synched.
|
|
|
|
|
*
|
|
|
|
|
* @param folderId the folder id
|
|
|
|
|
* @param scope the scope
|
|
|
|
|
* @param userToken the user token
|
|
|
|
|
* @return the th sync folder descriptor
|
|
|
|
|
* @throws ItemNotSynched the item not synched
|
|
|
|
|
* @throws WorkspaceFolderLocked the workspace folder locked
|
|
|
|
|
* @throws Exception the exception
|
|
|
|
|
*/
|
|
|
|
|
public ThSyncFolderDescriptor checkItemSynched(String folderId, String scope, String userToken) throws ItemNotSynched, WorkspaceFolderLocked, Exception{
|
|
|
|
|
setContextParameters(scope, userToken);
|
|
|
|
|
return workspaceThreddsSynchronize.checkItemSynched(folderId);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOT UPDATED
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the sync status.
|
|
|
|
@ -307,32 +360,13 @@ public class SyncronizeWithThredds {
|
|
|
|
|
* @throws ItemNotSynched the item not synched
|
|
|
|
|
* @throws Exception the exception
|
|
|
|
|
*/
|
|
|
|
|
public ThSyncStatus monitorSyncStatus(String itemId, String scope, String userToken) throws ItemNotSynched, Exception{
|
|
|
|
|
public ThSyncStatus monitorSyncStatus(String itemId, String scope, String userToken)
|
|
|
|
|
throws ItemNotSynched, Exception {
|
|
|
|
|
|
|
|
|
|
setContextParameters(scope, userToken);
|
|
|
|
|
return workspaceThreddsSynchronize.monitorSyncStatus(itemId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do un sync.
|
|
|
|
|
*
|
|
|
|
|
* @param folderId the folder id
|
|
|
|
|
* @param deleteRemoteContent the delete remote content
|
|
|
|
|
* @param scope the scope
|
|
|
|
|
* @param userToken the user token
|
|
|
|
|
* @return the boolean
|
|
|
|
|
* @throws Exception the exception
|
|
|
|
|
*/
|
|
|
|
|
public Boolean doUnSync(String folderId, boolean deleteRemoteContent, String scope, String userToken) throws Exception {
|
|
|
|
|
|
|
|
|
|
setContextParameters(scope, userToken);
|
|
|
|
|
return workspaceThreddsSynchronize.doUnSync(folderId, deleteRemoteContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register callback for id.
|
|
|
|
|
*
|
|
|
|
@ -346,5 +380,4 @@ public class SyncronizeWithThredds {
|
|
|
|
|
workspaceThreddsSynchronize.registerCallbackForId(folderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|