task_21379
Francesco Mangiacrapa 3 years ago
parent 41159eb8a6
commit 1ffc7616c9

@ -42,7 +42,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>maven-portal-bom</artifactId>
<version>3.6.0</version>
<version>3.6.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@ -88,6 +88,13 @@
<version>[1.0.0, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>oidc-library-portal</artifactId>
<version>[1.0.0, 2.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.gcube.spatial-data</groupId> -->
<!-- <artifactId>ws-thredds</artifactId> -->

@ -14,6 +14,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* The Class BeanConverter.
*
@ -36,7 +37,7 @@ public class BeanConverter {
public static WsThreddsSynchFolderDescriptor toWsThreddsFolderConfig(ThSyncFolderDescriptor t,
Sync_Status theStatus) {
if (t == null)
if (t == null || t.getConfiguration()==null)
return null;
WsThreddsSynchFolderDescriptor ws = new WsThreddsSynchFolderDescriptor();
@ -45,16 +46,16 @@ public class BeanConverter {
// FROM TARGET TOKEN TO SCOPE
// t.getConfiguration().getTargetToken()
String targetContext = t.getConfiguration().getTargetContext();
if (t.getConfiguration().getTargetToken() != null) {
if (targetContext != null) {
try {
AuthorizationEntry entry = authorizationService().get(t.getConfiguration().getTargetToken());
String scope = entry.getContext();
GcubeScope selectedScope = new GcubeScope(toScopeTitle(scope), scope, toGcubeScope(scope));
GcubeScope selectedScope = new GcubeScope(toScopeTitle(targetContext), targetContext, toGcubeScope(targetContext));
ws.setSelectedScope(selectedScope);
logger.debug("Resolved SCOPE: " + selectedScope + " from token");
logger.debug("Got target SCOPE: " + selectedScope + " from configuration");
} catch (Exception e) {
logger.error("I cannot resolve the scope for the token: "+t.getConfiguration().getTargetToken());
logger.error("I cannot read the scope for: "+targetContext);
}
}
@ -104,16 +105,17 @@ public class BeanConverter {
}
}
/**
* To th synch folder configuration.
*
* @param t the t
* @param rootFolderId the root folder id
* @param targetScopeUserToken the target scope user token
* @param t the t
* @param rootFolderId the root folder id
* @param targetContext the target context
* @return the th synch folder configuration
*/
public static ThSynchFolderConfiguration toThSynchFolderConfiguration(WsThreddsSynchFolderConfiguration t,
String rootFolderId, String targetScopeUserToken) {
String rootFolderId, String targetContext) {
if (t == null)
return null;
@ -121,7 +123,7 @@ public class BeanConverter {
ThSynchFolderConfiguration ts = new ThSynchFolderConfiguration();
ts.setFilter(t.getFilter());
ts.setRemotePath(t.getRemotePath());
ts.setTargetToken(targetScopeUserToken);
ts.setTargetContext(targetContext);
ts.setToCreateCatalogName(t.getCatalogName());
ts.setRootFolderId(rootFolderId);
return ts;

@ -5,8 +5,14 @@ package org.gcube.portlets.widgets.wsthreddssync.server;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.provider.UmaJWTProvider;
import org.gcube.common.portal.PortalContext;
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;
@ -15,9 +21,11 @@ import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncFolderDescriptor;
import org.gcube.portal.wssynclibrary.shared.thredds.ThSyncStatus;
import org.gcube.portal.wssynclibrary.shared.thredds.ThSynchFolderConfiguration;
import org.gcube.portal.wssynclibrary.thredds.WorkspaceThreddsSynchronize;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* The Class SyncronizeWithThredds.
*
@ -61,6 +69,54 @@ public class SyncronizeWithThredds {
SecurityTokenProvider.instance.set(userToken);
}
/**
* Change context for UMA.
*
* @param httpRequest the http request
* @param user the user
* @param targetScope the target scope
* @return the string representing the previous UMA Token read from {@link UmaJWTProvider#get()}
*/
private String changeContextForUMA(HttpServletRequest httpRequest, GCubeUser user, String targetScope) {
logger.debug("Setting UMA context with target scope: "+targetScope +", user: "+user.getUsername());
String previousUMAToken = null;
try {
previousUMAToken=UmaJWTProvider.instance.get();
JWTToken umaToken = OIDCUmaUtil.getUMAToken(httpRequest, user.getUsername(), targetScope);
setContextForUMA(umaToken.getRaw());
}catch (Exception e) {
logger.warn("Error on set context for UMA: ",e);
if(previousUMAToken!=null) {
logger.info("Setting previous UMA Token: "+previousUMAToken.substring(0,10)+"-MASKED-TOKEN");
setContextForUMA(previousUMAToken);
}
}
return previousUMAToken;
}
/**
* Reset context for UMA.
*
* @param umaToken the uma token
* @return the string
*/
private void setContextForUMA(String umaToken) {
logger.info("called set context for UMA");
if(umaToken==null || umaToken.isEmpty()) {
logger.info("Uma Token is null or empty, skipping operation");
return;
}
logger.info("Going to set UMA Token: "+umaToken.substring(0,10)+"-MASKED-TOKEN");
UmaJWTProvider.instance.set(umaToken);
logger.debug("UmaJWTProvider instance set performed to : "+umaToken.substring(0,10)+"-MASKED-TOKEN");
}
/**
* Do sync folder.
@ -225,19 +281,57 @@ public class SyncronizeWithThredds {
}
/**
* Gets the available catalogues by token.
* Gets the available catalogues.
*
* @param scope the scope
* @param userToken the user token
* @param targetToken the target token
* @return the available catalogues by token
* @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> getAvailableCataloguesByToken(String scope, String userToken, String targetToken) throws Exception {
setContextParameters(scope, userToken);
return workspaceThreddsSynchronize.getAvailableCataloguesByToken(targetToken);
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;
}
}

@ -82,6 +82,40 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
return false;
}
}
//MOVED
/**
* Gets the available THREDDS catalogues for target scope.
*
* @param targetFullScope the target scope
* @return the available catalogues for scope
* @throws Exception the exception
*/
@Override
public List<ThCatalogueBean> getAvailableCataloguesForScope(String targetFullScope) throws Exception {
if(targetFullScope==null)
throw new Exception("Invalid scope null");
List<ThCatalogueBean> listCtlgs = null;
try {
GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest());
listCtlgs = getSyncService().getAvailableCatalogues(this.getThreadLocalRequest(), user, targetFullScope);
logger.debug("Retuning "+listCtlgs.size()+" Catalogues for scope: "+targetFullScope);
if(logger.isDebugEnabled()){
for (ThCatalogueBean thCatalogueBean : listCtlgs) {
logger.debug(thCatalogueBean.toString());
}
}
}catch (Exception e) {
logger.error("Error on checking available Catalogue in the scope: "+targetFullScope, e);
}
return listCtlgs;
}
@ -102,12 +136,11 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
try {
GCubeUser user = PortalContext.getConfiguration().getCurrentUser(this.getThreadLocalRequest());
String targetScopeUserToken = null;
ThSynchFolderConfiguration config = null;
if(clientConfig!=null) {
targetScopeUserToken = PortalContext.getConfiguration().getCurrentUserToken(clientConfig.getSelectedScope().getScopeName(), user.getUsername());
config = BeanConverter.toThSynchFolderConfiguration(clientConfig, folderId, targetScopeUserToken);
logger.debug("Creating server config "+config);
if (clientConfig != null && clientConfig.getSelectedScope() != null) {
config = BeanConverter.toThSynchFolderConfiguration(clientConfig, folderId,
clientConfig.getSelectedScope().getScopeName());
logger.debug("Creating server config " + config);
}
logger.info("Calling doSyncFolder on folderId: "+folderId +", config: "+config);
@ -125,58 +158,6 @@ public class ThreddsWorkspaceSyncServiceImpl extends RemoteServiceServlet implem
}
/**
* Gets the available THREDDS catalogues for target scope.
*
* @param targetFullScope the target scope
* @return the available catalogues for scope
* @throws Exception the exception
*/
@Override
public List<ThCatalogueBean> getAvailableCataloguesForScope(String targetFullScope) throws Exception {
if(targetFullScope==null)
throw new Exception("Invalid scope null");
String originalScope = null;
String originalToken = null;
List<ThCatalogueBean> 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());
//Thread Local contexts
originalScope = wsScope;
originalToken = wsUserToken;
String targetScopeUserToken = PortalContext.getConfiguration().getCurrentUserToken(targetFullScope, user.getUsername());
listCtlgs = getSyncService().getAvailableCataloguesByToken(targetFullScope, wsUserToken, targetScopeUserToken);
logger.debug("Retuning "+listCtlgs.size()+" Catalogues for scope: "+targetFullScope);
if(logger.isDebugEnabled()){
for (ThCatalogueBean thCatalogueBean : listCtlgs) {
logger.debug(thCatalogueBean.toString());
}
}
}catch (Exception e) {
logger.error("Error on checking available Catalogue in the scope: "+targetFullScope, e);
}finally {
if(originalScope!=null && originalScope.compareTo(targetFullScope)!=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 listCtlgs;
}
/**
* Gets the list of Scopes (Root-VO, VOs and VREs) for user and the Thredds roles that user has in them.

Loading…
Cancel
Save