package org.gcube.portlets.user.wswidget; import static org.gcube.common.authorization.client.Constants.authorizationService; import java.util.ArrayList; import java.util.List; import javax.portlet.RenderRequest; import javax.portlet.ResourceRequest; import javax.servlet.http.HttpServletRequest; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.UserInfo; import org.gcube.common.portal.PortalContext; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portlets.user.wswidget.shared.ImageType; import org.gcube.portlets.user.wswidget.shared.ItemType; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; import org.gcube.vomanagement.usermanagement.model.GCubeUser; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.util.PortalUtil; public class Utils { private static Log _log = LogFactoryUtil.getLog(Utils.class); private final static String DEFAULT_ROLE = "OrganizationMember"; public static final String WORKSPACE_MY_SPECIAL_FOLDERS_PATH = "/Workspace/MySpecialFolders"; public static final String SPECIAL_FOLDERS_NAME = "MySpecialFolders"; public static final String VRE_FOLDERS_LABEL = "My VRE Folders"; public static final String HOME_LABEL = "Home"; public static final ItemType[] FOLDERS = new ItemType[] {ItemType.PRIVATE_FOLDER, ItemType.SHARED_FOLDER, ItemType.VRE_FOLDER}; /** * Checks if is folder. * * @param type * the type * @return true, if is folder */ public static boolean isFolder(ItemType type) { for (ItemType folder : FOLDERS) if (type == folder) return true; return false; } public static String getIconImage(String type) { switch (type) { case "DOC": return "description"; case "XLS": return "description"; case "PPT": return "description"; case "PDF": return "picture_as_pdf"; case "IMAGE": return "panorama"; case "MOVIE": return "movie_creation"; case "HTML": return "web"; case "RAR": return "archive"; case "ZIP": return "archive"; case "TXT": return "description"; default: return "insert_drive_file"; } } public static String getIconColor(String type) { switch (type) { case "DOC": return "#0277bd"; // light-blue darken-3 case "XLS": return "#4caf50"; //green case "PPT": return "#fb8c00"; //orange case "PDF": return "#f44336"; //red case "IMAGE": return "#d81b60"; //light blue case "MOVIE": return "#90caf9"; case "HTML": return "#0277bd"; case "RAR": return "#ffc107"; case "ZIP": return "#ffc107";//amber case "TXT": return "#CCC"; // gray default: return "#CCC"; // gray } } public static GCubeUser getCurrentUser(RenderRequest request) { long userId; try { userId = PortalUtil.getUser(request).getUserId(); return getCurrentUser(userId); } catch (Exception e) { e.printStackTrace(); } return null; } public static GCubeUser getCurrentUser(HttpServletRequest request) { long userId; try { userId = PortalUtil.getUser(request).getUserId(); return getCurrentUser(userId); } catch (Exception e) { e.printStackTrace(); } return null; } public static GCubeUser getCurrentUser(ResourceRequest request) { long userId; try { userId = PortalUtil.getUser(request).getUserId(); return getCurrentUser(userId); } catch (Exception e) { e.printStackTrace(); } return null; } public static GCubeUser getCurrentUser(long userId) { try { return new LiferayUserManager().getUserById(userId); } catch (Exception e) { e.printStackTrace(); } return null; } public static String getCurrentContext(ResourceRequest request) { long groupId = -1; try { groupId = PortalUtil.getScopeGroupId(request); return getCurrentContext(groupId); } catch (Exception e) { e.printStackTrace(); } return null; } public static String getCurrentContext(RenderRequest request) { long groupId = -1; try { groupId = PortalUtil.getScopeGroupId(request); return getCurrentContext(groupId); } catch (Exception e) { e.printStackTrace(); } return null; } public static String getCurrentContext(HttpServletRequest request) { long groupId = -1; try { groupId = PortalUtil.getScopeGroupId(request); return getCurrentContext(groupId); } catch (Exception e) { e.printStackTrace(); } return null; } public static String getCurrentContext(long groupId) { try { PortalContext pContext = PortalContext.getConfiguration(); return pContext.getCurrentScope(""+groupId); } catch (Exception e) { e.printStackTrace(); } return null; } /** *

* Returns the gCube authorisation token for the given user *

* @param scope infrastrucure context (scope) * @param username the GCubeUser username @see {@link GCubeUser} * @return the Token for the user in the context, or null if a token for this user could not be found */ public static String getCurrentUserToken(String scope, String username) { String userToken = null; try { ScopeProvider.instance.set(scope); userToken = authorizationService().resolveTokenByUserAndContext(username, scope); SecurityTokenProvider.instance.set(userToken); } catch (ObjectNotFound ex) { userToken = generateAuthorizationToken(username, scope); SecurityTokenProvider.instance.set(userToken); _log.debug("generateAuthorizationToken OK for " + username + " in scope " + scope); } catch (Exception e) { _log.error("Error while trying to generate token for user " + username + "in scope " + scope); e.printStackTrace(); return null; } return userToken; } /** * * @param username * @param scope * @throws Exception */ private static String generateAuthorizationToken(String username, String scope) { List userRoles = new ArrayList<>(); userRoles.add(DEFAULT_ROLE); String token; try { token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope); } catch (Exception e) { e.printStackTrace(); return null; } return token; } public static ImageType getFileIconImageType(String extension) { if (extension == null || extension.compareTo("") == 0) return ImageType.NONE; extension = extension.toLowerCase(); switch (extension) { case "doc": case "docx": return ImageType.DOC; case "rtf": case "txt": return ImageType.TXT; case "xls": case "xlsx": return ImageType.XLS; case "ppt": case "pptx": return ImageType.PPT; case "pdf": return ImageType.PDF; case "jpg": case "jpeg": case "gif": case "bmp": case "png": case "tif": case "tiff": return ImageType.IMAGE; case "avi": case "mp4": case "mpeg": return ImageType.MOVIE; case "html": case "htm": case "jsp": return ImageType.HTML; case "rar": return ImageType.RAR; case "zip": case "tar": case "tar.gz": case ".cpgz": case ".gz": return ImageType.ZIP; default: return ImageType.NONE; } } }