Fixed PortalContext for get token

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/accounting-manager@135047 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2016-11-29 15:28:57 +00:00
parent 975ee71103
commit e09671daae
2 changed files with 71 additions and 16 deletions

View File

@ -20,6 +20,7 @@ import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceExcept
import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup; import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -34,8 +35,7 @@ public class SessionUtil {
private static Logger logger = LoggerFactory.getLogger(SessionUtil.class); private static Logger logger = LoggerFactory.getLogger(SessionUtil.class);
public static ServiceCredentials getServiceCredentials( public static ServiceCredentials getServiceCredentials(
HttpServletRequest httpServletRequest) HttpServletRequest httpServletRequest) throws ServiceException {
throws ServiceException {
return getServiceCredentials(httpServletRequest, null); return getServiceCredentials(httpServletRequest, null);
} }
@ -47,6 +47,8 @@ public class SessionUtil {
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!");
@ -59,10 +61,20 @@ public class SessionUtil {
} else { } else {
logger.info("Retrieving credential in session!"); logger.info("Retrieving credential in session!");
PortalContext pContext = PortalContext.getConfiguration(); PortalContext pContext = PortalContext.getConfiguration();
boolean hasScopeGroupId = false;
if (scopeGroupId != null && !scopeGroupId.isEmpty()) { if (scopeGroupId != null && !scopeGroupId.isEmpty()) {
hasScopeGroupId = true;
} else {
hasScopeGroupId = false;
}
if (hasScopeGroupId) {
scope = pContext.getCurrentScope(scopeGroupId); scope = pContext.getCurrentScope(scopeGroupId);
} else { } else {
scope = pContext.getCurrentScope(httpServletRequest); scope = pContext.getCurrentScope(httpServletRequest);
} }
if (scope == null || scope.isEmpty()) { if (scope == null || scope.isEmpty()) {
@ -71,8 +83,16 @@ public class SessionUtil {
throw new ServiceException(error); throw new ServiceException(error);
} }
userName = pContext.getCurrentUser(httpServletRequest) GCubeUser gCubeUser = pContext.getCurrentUser(httpServletRequest);
.getUsername();
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()) { if (userName == null || userName.isEmpty()) {
String error = "Error retrieving username in scope " + scope String error = "Error retrieving username in scope " + scope
@ -81,7 +101,7 @@ public class SessionUtil {
throw new ServiceException(error); throw new ServiceException(error);
} }
token = pContext.getCurrentUserToken(httpServletRequest); token = pContext.getCurrentUserToken(scope, httpServletRequest);
if (token == null || token.isEmpty()) { if (token == null || token.isEmpty()) {
String error = "Error retrieving token for " + userName String error = "Error retrieving token for " + userName
@ -90,22 +110,55 @@ public class SessionUtil {
throw new ServiceException(error); throw new ServiceException(error);
} }
String name = pContext.getCurrentUser(httpServletRequest) String name = gCubeUser
.getFirstName(); .getFirstName();
String lastName = pContext.getCurrentUser(httpServletRequest) String lastName = gCubeUser
.getLastName(); .getLastName();
String fullName = pContext.getCurrentUser(httpServletRequest) String fullName = gCubeUser
.getFullname(); .getFullname();
String userAvatarURL = pContext.getCurrentUser(httpServletRequest) String userAvatarURL = gCubeUser
.getUserAvatarURL(); .getUserAvatarURL();
String email = pContext.getCurrentUser(httpServletRequest) String email = gCubeUser
.getEmail(); .getEmail();
String groupId = String.valueOf(pContext
.getCurrentGroupId(httpServletRequest));
String groupName = pContext.getCurrentGroupName(httpServletRequest); if (hasScopeGroupId) {
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, sCredentials = new ServiceCredentials(userName, fullName, name,
lastName, email, scope, groupId, groupName, userAvatarURL, lastName, email, scope, groupId, groupName, userAvatarURL,
token); token);
@ -154,8 +207,9 @@ public class SessionUtil {
if (Constants.DEBUG_MODE) { if (Constants.DEBUG_MODE) {
contexts.add(serviceCredentials.getScope()); contexts.add(serviceCredentials.getScope());
for(int i=0; i<50 ; i++){ for (int i = 0; i < 50; i++) {
contexts.add("/d4science.research-infrastructures.eu/gCubeApps/PerformanceEvaluationInAquaculture"+i); contexts.add("/d4science.research-infrastructures.eu/gCubeApps/PerformanceEvaluationInAquaculture"
+ i);
} }
} else { } else {
contexts.add(serviceCredentials.getScope()); contexts.add(serviceCredentials.getScope());

View File

@ -186,11 +186,12 @@ public class CSVManager {
logger.debug("Temp File: " + tempFile.toString()); logger.debug("Temp File: " + tempFile.toString());
// Create the CSVFormat object // Create the CSVFormat object
CSVFormat format = CSVFormat.RFC4180.withHeader() CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader()
.withDelimiter(','); .withDelimiter(',');
PrintStream printStream = new PrintStream(tempFile.toFile()); PrintStream printStream = new PrintStream(tempFile.toFile());
// CSV Write Example using CSVPrinter // CSV Write Example using CSVPrinter
CSVPrinter printer = new CSVPrinter(printStream, format); CSVPrinter printer = new CSVPrinter(printStream, format);
printer.printRecord(accountingDataModel.getHeader()); printer.printRecord(accountingDataModel.getHeader());
for (AccountingDataRow row : accountingDataModel.getRows()) { for (AccountingDataRow row : accountingDataModel.getRows()) {