ref 6279:Statistical Algorithms Importer - getUserId doesn't work when session expires and Reload is performed

https://support.d4science.org/issues/6279

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/widgets/github-connector@142313 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2017-02-07 10:51:14 +00:00
parent 1552367f65
commit 3f1d57de66
7 changed files with 227 additions and 43 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" output="target/github-connector-1.2.0-SNAPSHOT/WEB-INF/classes" path="src/main/java"> <classpathentry kind="src" output="target/github-connector-1.3.0-SNAPSHOT/WEB-INF/classes" path="src/main/java">
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
@ -22,8 +22,9 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="target/github-connector-1.2.0-SNAPSHOT/WEB-INF/classes"/> <classpathentry kind="output" path="target/github-connector-1.3.0-SNAPSHOT/WEB-INF/classes"/>
</classpath> </classpath>

View File

@ -1,4 +1,12 @@
<ReleaseNotes> <ReleaseNotes>
<Changeset component="org.gcube.portlets.widgets.githubconnector.1-3-0"
date="2017-02-15">
<Change>Updated PortalContext support[ticket #6279]</Change>
</Changeset>
<Changeset component="org.gcube.portlets.widgets.githubconnector.1-2-0"
date="2016-12-15">
<Change>Added Portal Context</Change>
</Changeset>
<Changeset component="org.gcube.portlets.widgets.githubconnector.1-1-0" <Changeset component="org.gcube.portlets.widgets.githubconnector.1-1-0"
date="2016-10-01"> date="2016-10-01">
<Change>Removed accesslogger dependency</Change> <Change>Removed accesslogger dependency</Change>

47
pom.xml
View File

@ -12,7 +12,7 @@
<groupId>org.gcube.portlets.widgets</groupId> <groupId>org.gcube.portlets.widgets</groupId>
<artifactId>github-connector</artifactId> <artifactId>github-connector</artifactId>
<version>1.2.0-SNAPSHOT</version> <version>1.3.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>github-connector</description> <description>github-connector</description>
@ -163,6 +163,12 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>client-context-library</artifactId>
<version>[1.0.0-SNAPSHOT,)</version>
<scope>compile</scope>
</dependency>
<!-- Authorization --> <!-- Authorization -->
<dependency> <dependency>
@ -177,12 +183,12 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- GCube Widgets --> <!-- GCube Widgets
<dependency> <dependency>
<groupId>org.gcube.portlets.user</groupId> <groupId>org.gcube.portlets.user</groupId>
<artifactId>gcube-widgets</artifactId> <artifactId>gcube-widgets</artifactId>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency> -->
<!-- Storage --> <!-- Storage -->
@ -373,6 +379,41 @@
</plugin> </plugin>
</plugins> </plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
gwt-maven-plugin
</artifactId>
<versionRange>
[2.7.0,)
</versionRange>
<goals>
<goal>resources</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build> </build>
<repositories> <repositories>

View File

@ -11,6 +11,7 @@ import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest; import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse; import javax.portlet.RenderResponse;
import org.gcube.common.portal.PortalContext;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -44,9 +45,9 @@ public class GitHubConnectorPortlet extends GenericPortlet {
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
logger.trace("Loading from JSP: "+VIEW_JSP); logger.trace("Loading from JSP: "+VIEW_JSP);
logger.trace("Setting user in session using PortalContext");
PortalContext.setUserInSession(request);
//logger.trace("setting context using ScopeHelper");
//ScopeHelper.setContext(request);
logger.trace("passing to the render"); logger.trace("passing to the render");
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(VIEW_JSP); PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(VIEW_JSP);

View File

@ -19,14 +19,14 @@ import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/** /**
* *
* @author Giancarlo Panichi * @author Giancarlo Panichi email: <a
* email: <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a> * href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
* *
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class GitHubConnectorServiceImpl extends RemoteServiceServlet implements public class GitHubConnectorServiceImpl extends RemoteServiceServlet implements
GitHubConnectorService { GitHubConnectorService {
private static Logger logger = LoggerFactory private static Logger logger = LoggerFactory
.getLogger(GitHubConnectorServiceImpl.class); .getLogger(GitHubConnectorServiceImpl.class);
@ -45,11 +45,13 @@ public class GitHubConnectorServiceImpl extends RemoteServiceServlet implements
@Override @Override
public UserInfo hello() throws ServiceException { public UserInfo hello() throws ServiceException {
try { try {
ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(this.getThreadLocalRequest()); ServiceCredentials serviceCredentials = SessionUtil
.getServiceCredentials(this.getThreadLocalRequest());
logger.debug("hello()"); logger.debug("hello()");
UserInfo userInfo = new UserInfo(serviceCredentials.getUserName(), UserInfo userInfo = new UserInfo(serviceCredentials.getUserName(),
serviceCredentials.getGroupId(), serviceCredentials.getGroupName(), serviceCredentials.getGroupId(),
serviceCredentials.getGroupName(),
serviceCredentials.getScope(), serviceCredentials.getScope(),
serviceCredentials.getEmail(), serviceCredentials.getEmail(),
serviceCredentials.getFullName()); serviceCredentials.getFullName());
@ -70,7 +72,8 @@ public class GitHubConnectorServiceImpl extends RemoteServiceServlet implements
public ArrayList<GitHubRepository> getRepositories(String repositoryOwner, public ArrayList<GitHubRepository> getRepositories(String repositoryOwner,
GitHubCredential gitHubCredential) throws ServiceException { GitHubCredential gitHubCredential) throws ServiceException {
try { try {
ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(this.getThreadLocalRequest()); ServiceCredentials serviceCredentials = SessionUtil
.getServiceCredentials(this.getThreadLocalRequest());
logger.debug("getRepository(): " + gitHubCredential); logger.debug("getRepository(): " + gitHubCredential);
GitConnectorService gitConnectorService = new GitConnectorService( GitConnectorService gitConnectorService = new GitConnectorService(
serviceCredentials.getUserName(), gitHubCredential); serviceCredentials.getUserName(), gitHubCredential);
@ -89,13 +92,16 @@ public class GitHubConnectorServiceImpl extends RemoteServiceServlet implements
public void cloneRepository(GitHubCloneSession gitHubCloneSession) public void cloneRepository(GitHubCloneSession gitHubCloneSession)
throws ServiceException { throws ServiceException {
try { try {
ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(this.getThreadLocalRequest()); ServiceCredentials serviceCredentials = SessionUtil
.getServiceCredentials(this.getThreadLocalRequest());
logger.debug("cloneRepository(): " + gitHubCloneSession); logger.debug("cloneRepository(): " + gitHubCloneSession);
GitConnectorService gitConnectorService = new GitConnectorService( GitConnectorService gitConnectorService = new GitConnectorService(
serviceCredentials.getUserName(), serviceCredentials.getUserName(),
gitHubCloneSession.getGitHubCredential()); gitHubCloneSession.getGitHubCredential());
gitConnectorService.cloneRepository(gitHubCloneSession.getDestinationFolderId(), gitConnectorService.cloneRepository(
gitHubCloneSession.getRepositoryOwner(), gitHubCloneSession.getRepositoryName()); gitHubCloneSession.getDestinationFolderId(),
gitHubCloneSession.getRepositoryOwner(),
gitHubCloneSession.getRepositoryName());
return; return;
} catch (ServiceException e) { } catch (ServiceException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -6,10 +6,15 @@ package org.gcube.portlets.widgets.githubconnector.server;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
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.portlets.widgets.githubconnector.server.util.ServiceCredentials; import org.gcube.portlets.widgets.githubconnector.server.util.ServiceCredentials;
import org.gcube.portlets.widgets.githubconnector.shared.Constants; import org.gcube.portlets.widgets.githubconnector.shared.Constants;
import org.gcube.portlets.widgets.githubconnector.shared.exception.ServiceException; import org.gcube.portlets.widgets.githubconnector.shared.exception.ServiceException;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
/** /**
* *
@ -22,46 +27,160 @@ public class SessionUtil {
private static final Logger logger = Logger.getLogger(SessionUtil.class); private static final Logger logger = Logger.getLogger(SessionUtil.class);
/**
*
* @param httpServletRequest
* @return
* @throws TDGWTServiceException
*/
public static ServiceCredentials getServiceCredentials( public static ServiceCredentials getServiceCredentials(
HttpServletRequest httpServletRequest) throws ServiceException { HttpServletRequest httpServletRequest)
throws ServiceException {
return getServiceCredentials(httpServletRequest, null);
}
/**
*
* @param httpServletRequest
* @param scopeGroupId
* @return
* @throws TDGWTServiceException
*/
public static ServiceCredentials getServiceCredentials(
HttpServletRequest httpServletRequest, String scopeGroupId)
throws ServiceException {
ServiceCredentials sCredentials = null; ServiceCredentials sCredentials = null;
String userName = null; String userName = null;
String scope = null; String scope = null;
String token = null; String token = null;
String groupId = null;
String groupName = null;
if (Constants.DEBUG_MODE) { if (Constants.DEBUG_MODE) {
logger.info("No credential found in session, use test user!"); logger.info("No credential found in session, use test user!");
/*
* InfoLocale infoLocale = getInfoLocale(httpServletRequest, null);
* Locale locale = new Locale(infoLocale.getLanguage());
*
* ResourceBundle messages = ResourceBundle.getBundle(
* StatAlgoImporterServiceMessagesConstants.TDGWTServiceMessages,
* locale);
*/
userName = Constants.DEFAULT_USER; userName = Constants.DEFAULT_USER;
scope = Constants.DEFAULT_SCOPE; scope = Constants.DEFAULT_SCOPE;
token = Constants.DEFAULT_TOKEN; token = Constants.DEFAULT_TOKEN;
logger.info("Set SecurityToken: " + token);
SecurityTokenProvider.instance.set(token);
logger.info("Set ScopeProvider: " + scope);
ScopeProvider.instance.set(scope);
sCredentials = new ServiceCredentials(userName, scope, token); sCredentials = new ServiceCredentials(userName, scope, token);
} else { } else {
logger.info("Retrieving credential in session!"); logger.info("Retrieving credential in session!");
PortalContext pContext = PortalContext.getConfiguration(); PortalContext pContext = PortalContext.getConfiguration();
userName = pContext.getCurrentUser(httpServletRequest) boolean hasScopeGroupId = false;
.getUsername();
scope = pContext.getCurrentScope(httpServletRequest);
token = pContext.getCurrentUserToken(httpServletRequest);
String name = pContext.getCurrentUser(httpServletRequest)
.getFirstName();
String lastName = pContext.getCurrentUser(httpServletRequest)
.getLastName();
String fullName = pContext.getCurrentUser(httpServletRequest).getFullname();
String email = pContext.getCurrentUser(httpServletRequest)
.getEmail();
String groupId = String.valueOf(pContext
.getCurrentGroupId(httpServletRequest));
String groupName = pContext.getCurrentGroupName(httpServletRequest);
sCredentials = new ServiceCredentials(userName, fullName, name, lastName,
email, scope, groupId, groupName, token);
if (scopeGroupId != null && !scopeGroupId.isEmpty()) {
hasScopeGroupId = true;
} else {
hasScopeGroupId = false;
}
if (hasScopeGroupId) {
scope = pContext.getCurrentScope(scopeGroupId);
} else {
scope = pContext.getCurrentScope(httpServletRequest);
}
if (scope == null || scope.isEmpty()) {
String error = "Error retrieving scope: " + scope;
logger.error(error);
throw new ServiceException(error);
}
GCubeUser gCubeUser = pContext.getCurrentUser(httpServletRequest);
if (gCubeUser == null) {
String error = "Error retrieving gCubeUser in scope " + scope
+ ": " + gCubeUser;
logger.error(error);
throw new ServiceException(error);
}
userName = gCubeUser.getUsername();
if (userName == null || userName.isEmpty()) {
String error = "Error retrieving username in scope " + scope
+ ": " + userName;
logger.error(error);
throw new ServiceException(error);
}
token = pContext.getCurrentUserToken(scope, userName);
if (token == null || token.isEmpty()) {
String error = "Error retrieving token for " + userName
+ " in " + scope + ": " + token;
logger.error(error);
throw new ServiceException(error);
}
String name = gCubeUser.getFirstName();
String lastName = gCubeUser.getLastName();
String fullName = gCubeUser.getFullname();
String userAvatarURL = gCubeUser.getUserAvatarURL();
String email = gCubeUser.getEmail();
if (hasScopeGroupId) {
logger.info("Set SecurityToken: " + token);
SecurityTokenProvider.instance.set(token);
logger.info("Set ScopeProvider: " + scope);
ScopeProvider.instance.set(scope);
groupId = scopeGroupId;
long gId;
try {
gId = Long.parseLong(scopeGroupId);
} catch (Throwable e) {
String error = "Error retrieving groupId: " + scopeGroupId;
logger.error(error, e);
throw new ServiceException(error);
}
GCubeGroup group;
try {
group = new LiferayGroupManager().getGroup(gId);
} catch (Throwable e) {
String error = "Error retrieving group: " + groupName;
logger.error(error);
throw new ServiceException(error);
}
groupName = group.getGroupName();
} else {
groupId = String.valueOf(pContext
.getCurrentGroupId(httpServletRequest));
groupName = pContext.getCurrentGroupName(httpServletRequest);
}
sCredentials = new ServiceCredentials(userName, fullName, name,
lastName, email, scope, groupId, groupName, userAvatarURL,
token);
} }
logger.info("ServiceCredential: " + sCredentials); logger.info("ServiceCredentials: " + sCredentials);
return sCredentials; return sCredentials;
} }

View File

@ -19,6 +19,7 @@ public class ServiceCredentials implements Serializable {
private String scope; private String scope;
private String groupId; private String groupId;
private String groupName; private String groupName;
private String userAvatarURL;
private String token; private String token;
public ServiceCredentials() { public ServiceCredentials() {
@ -34,7 +35,7 @@ public class ServiceCredentials implements Serializable {
public ServiceCredentials(String userName, String fullName, String name, public ServiceCredentials(String userName, String fullName, String name,
String lastName, String email, String scope, String groupId, String lastName, String email, String scope, String groupId,
String groupName, String token) { String groupName, String userAvatarURL, String token) {
super(); super();
this.userName = userName; this.userName = userName;
this.fullName = fullName; this.fullName = fullName;
@ -44,6 +45,7 @@ public class ServiceCredentials implements Serializable {
this.scope = scope; this.scope = scope;
this.groupId = groupId; this.groupId = groupId;
this.groupName = groupName; this.groupName = groupName;
this.userAvatarURL = userAvatarURL;
this.token = token; this.token = token;
} }
@ -111,6 +113,14 @@ public class ServiceCredentials implements Serializable {
this.groupName = groupName; this.groupName = groupName;
} }
public String getUserAvatarURL() {
return userAvatarURL;
}
public void setUserAvatarURL(String userAvatarURL) {
this.userAvatarURL = userAvatarURL;
}
public String getToken() { public String getToken() {
return token; return token;
} }
@ -124,10 +134,8 @@ public class ServiceCredentials implements Serializable {
return "ServiceCredentials [userName=" + userName + ", fullName=" return "ServiceCredentials [userName=" + userName + ", fullName="
+ fullName + ", name=" + name + ", lastName=" + lastName + fullName + ", name=" + name + ", lastName=" + lastName
+ ", email=" + email + ", scope=" + scope + ", groupId=" + ", email=" + email + ", scope=" + scope + ", groupId="
+ groupId + ", groupName=" + groupName + ", token=" + token + groupId + ", groupName=" + groupName + ", userAvatarURL="
+ "]"; + userAvatarURL + ", token=" + token + "]";
} }
}
}