From cc88c81a83567721c05496f45800019665434f5a Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Mon, 19 Sep 2016 10:19:49 +0000 Subject: [PATCH] [Feature #5052] Added code to decode the folderID by Base64 and then decrypt it by StringEncrypter git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/workspace-explorer-app@131476 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 15 ++ .../client/WorkspaceExplorerApp.java | 19 ++- .../WorkspaceExplorerAppController.java | 137 +++++++++++------- .../client/WorkspaceExplorerAppMainPanel.java | 8 +- .../rpc/WorkspaceExplorerAppService.java | 10 ++ .../rpc/WorkspaceExplorerAppServiceAsync.java | 10 ++ .../server/StringUtil.java | 33 ++++- .../WorkspaceExplorerAppServiceImpl.java | 31 +++- .../workspaceexplorerapp/server/WsUtil.java | 29 +++- src/test/java/TestFolderIDDecrypt.java | 19 +++ 10 files changed, 246 insertions(+), 65 deletions(-) create mode 100644 src/test/java/TestFolderIDDecrypt.java diff --git a/pom.xml b/pom.xml index 7315c5b..73b5861 100644 --- a/pom.xml +++ b/pom.xml @@ -163,6 +163,21 @@ + + org.gcube.core + common-encryption + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + compile + + + + commons-codec + commons-codec + compile + 1.8 + + + diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerApp.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerApp.java index 7c673d6..549f5ad 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerApp.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerApp.java @@ -15,12 +15,15 @@ import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.RootPanel; /** + * The Class WorkspaceExplorerApp. + * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Feb 19, 2016 */ public class WorkspaceExplorerApp implements EntryPoint { private WorkspaceExplorerAppController appController; + private WorkspaceExplorerAppMainPanel mainPanel; /** * This is the entry point method. @@ -43,7 +46,7 @@ public class WorkspaceExplorerApp implements EntryPoint { .inject();*/ appController = new WorkspaceExplorerAppController(); - appController.go(RootPanel.get(WorkspaceExplorerAppConstants.APPLICATION_DIV)); + appController.go(this); Window.addResizeHandler(new ResizeHandler() { @Override @@ -52,7 +55,7 @@ public class WorkspaceExplorerApp implements EntryPoint { } }); - WorkspaceExplorerAppMainPanel mainPanel = new WorkspaceExplorerAppMainPanel(appController.getMainPanel(), appController.getEventBus(), appController.getDisplayFields()); + mainPanel = new WorkspaceExplorerAppMainPanel(appController.getEventBus(), appController.getDisplayFields()); RootPanel.get(WorkspaceExplorerAppConstants.APPLICATION_DIV).add(mainPanel); // WorkspaceExplorerFoooterPanel footerPanel = new WorkspaceExplorerFoooterPanel(); @@ -71,7 +74,7 @@ public class WorkspaceExplorerApp implements EntryPoint { }-*/; /** - * Update window size + * Update window size. */ public static void updateSize() { @@ -95,4 +98,14 @@ public class WorkspaceExplorerApp implements EntryPoint { table.getStyle().setHeight(containerH-headerTableH, Unit.PX); } } + + + /** + * Update explorer panel. + * + * @param workspaceExplorerAppPanel the workspace explorer app panel + */ + public void updateExplorerPanel(WorkspaceExplorerAppPanel workspaceExplorerAppPanel){ + mainPanel.updateMainPanel(workspaceExplorerAppPanel); + } } diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerAppController.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerAppController.java index 75fa003..ff69980 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerAppController.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerAppController.java @@ -4,6 +4,8 @@ package org.gcube.portlets.user.workspaceexplorerapp.client; +import gwt.material.design.client.ui.MaterialToast; + import org.gcube.portlets.user.workspaceexplorerapp.client.WorkspaceExplorerSelectNotification.WorskpaceExplorerSelectNotificationListener; import org.gcube.portlets.user.workspaceexplorerapp.client.grid.DisplayField; import org.gcube.portlets.user.workspaceexplorerapp.shared.Item; @@ -14,6 +16,7 @@ import com.google.gwt.event.dom.client.ContextMenuEvent; import com.google.gwt.event.dom.client.ContextMenuHandler; import com.google.gwt.event.shared.HandlerManager; import com.google.gwt.user.client.Window; +import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.RootPanel; /** @@ -29,6 +32,7 @@ public class WorkspaceExplorerAppController { private WorkspaceExplorerAppPanel mainPanel; private WorkspaceResourcesExplorerPanel wsResourcesExplorerPanel; private final HandlerManager eventBus = new HandlerManager(null); + private WorkspaceExplorerApp app; /** * Instantiates a new workspace explorer app controller. @@ -36,12 +40,14 @@ public class WorkspaceExplorerAppController { public WorkspaceExplorerAppController() { } + /** * Go. * - * @param rootPanel the root panel + * @param app the app */ - public void go(final RootPanel rootPanel) { + public void go(WorkspaceExplorerApp app) { + this.app = app; RootPanel.getBodyElement().getStyle().setPadding(0, Unit.PX); // mainPanel.setWidth("100%"); @@ -49,8 +55,10 @@ public class WorkspaceExplorerAppController { String folderId = Window.Location.getParameter("folderId"); //TODO CHECK FOLDER ID - if(folderId==null) - Window.alert("Folder Id not found, it is not possible to retrieve a folder without a valid id"); + if(folderId==null){ + Window.alert("Folder Id not found, It is not possible to retrieve a folder without a valid id"); + //return; + } RootPanel.get().addDomHandler(new ContextMenuHandler() { @@ -61,67 +69,98 @@ public class WorkspaceExplorerAppController { } }, ContextMenuEvent.getType()); - wsResourcesExplorerPanel = new WorkspaceResourcesExplorerPanel(eventBus, folderId, false); - wsResourcesExplorerPanel.loadParentBreadcrumbByItemId(folderId, true); - WorskpaceExplorerSelectNotificationListener listener = new WorskpaceExplorerSelectNotificationListener() { - - @Override - public void onSelectedItem(Item item) { - - GWT.log("Listener Selected Item " + item); - } - - @Override - public void onFailed(Throwable throwable) { - - GWT.log("There are networks problem, please check your connection."); - } - - @Override - public void onAborted() { - - } - - @Override - public void onNotValidSelection() { - - } - }; - wsResourcesExplorerPanel.addWorkspaceExplorerSelectNotificationListener(listener); - - /*new com.google.gwt.user.client.Timer() { + MaterialToast.fireToast("Checking permissions..."); + WorkspaceExplorerAppConstants.workspaceNavigatorService.getFolderIdFromEncrypted(folderId, new AsyncCallback() { @Override - public void run() { + public void onSuccess(String folderId) { - //TODO //IS A TEST REMOVE - ArrayList tests = new ArrayList(); - for (int i = 0; i < 50; i++) { - tests.add(new Item(i+""+Random.nextInt(), "name"+i, false)); - } - wsResourcesExplorerPanel.getWsExplorer().updateExplorer(tests); + if(folderId!=null && !folderId.isEmpty()) + try { + initWorkspaceExplorer(folderId); + } + catch (Exception e) { + Window.alert("Folder Id not valid. An occurred when converting folder id"); + } } - }.schedule(1000);*/ - - mainPanel = new WorkspaceExplorerAppPanel(wsResourcesExplorerPanel); + @Override + public void onFailure(Throwable caught) { + Window.alert(caught.getMessage()); + } + }); } catch (Exception e) { e.printStackTrace(); } - } + } /** - * Gets the main panel. + * Inits the workspace explorer. * - * @return the mainPanel + * @param folderId the folder id + * @throws Exception the exception */ - public WorkspaceExplorerAppPanel getMainPanel() { - return mainPanel; + private void initWorkspaceExplorer(String folderId) throws Exception{ + + wsResourcesExplorerPanel = new WorkspaceResourcesExplorerPanel(eventBus, folderId, false); + wsResourcesExplorerPanel.loadParentBreadcrumbByItemId(folderId, true); + WorskpaceExplorerSelectNotificationListener listener = new WorskpaceExplorerSelectNotificationListener() { + + @Override + public void onSelectedItem(Item item) { + + GWT.log("Listener Selected Item " + item); + } + + @Override + public void onFailed(Throwable throwable) { + + GWT.log("There are networks problem, please check your connection."); + } + + @Override + public void onAborted() { + + } + + @Override + public void onNotValidSelection() { + + } + }; + wsResourcesExplorerPanel.addWorkspaceExplorerSelectNotificationListener(listener); + + /*new com.google.gwt.user.client.Timer() { + + @Override + public void run() { + + //TODO //IS A TEST REMOVE + ArrayList tests = new ArrayList(); + for (int i = 0; i < 50; i++) { + tests.add(new Item(i+""+Random.nextInt(), "name"+i, false)); + } + wsResourcesExplorerPanel.getWsExplorer().updateExplorer(tests); + } + }.schedule(1000);*/ + + mainPanel = new WorkspaceExplorerAppPanel(wsResourcesExplorerPanel); + app.updateExplorerPanel(mainPanel); } +// /** +// * Gets the main panel. +// * +// * @return the mainPanel +// */ +// public WorkspaceExplorerAppPanel getMainPanel() { +// return mainPanel; +// } + + /** * Gets the event bus. * @@ -137,6 +176,6 @@ public class WorkspaceExplorerAppController { * @return the display fields */ public DisplayField[] getDisplayFields(){ - return wsResourcesExplorerPanel.getDisplayFields(); + return new DisplayField[]{DisplayField.ICON, DisplayField.NAME, DisplayField.OWNER, DisplayField.CREATION_DATE}; } } diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerAppMainPanel.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerAppMainPanel.java index bfd02d7..de656cb 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerAppMainPanel.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/WorkspaceExplorerAppMainPanel.java @@ -87,10 +87,9 @@ public class WorkspaceExplorerAppMainPanel extends Composite { * @param workspaceExplorerAppPanel the workspace explorer app panel * @param handlerManager the handler manager */ - public WorkspaceExplorerAppMainPanel(WorkspaceExplorerAppPanel workspaceExplorerAppPanel, HandlerManager handlerManager, DisplayField[] displayFields) { + public WorkspaceExplorerAppMainPanel(HandlerManager handlerManager, DisplayField[] displayFields) { initWidget(uiBinder.createAndBindUi(this)); this.handlerManager = handlerManager; - explorer_main_container.add(workspaceExplorerAppPanel); we_nav_bar.getElement().setId("we_nav_bar"); we_nav_bar.getElement().setAttribute("id", "we_nav_bar"); @@ -154,4 +153,9 @@ public class WorkspaceExplorerAppMainPanel extends Composite { }); } + public void updateMainPanel(WorkspaceExplorerAppPanel workspaceExplorerAppPanel){ + explorer_main_container.clear(); + explorer_main_container.add(workspaceExplorerAppPanel); + } + } diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/rpc/WorkspaceExplorerAppService.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/rpc/WorkspaceExplorerAppService.java index 69c5eb4..d57e6ed 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/rpc/WorkspaceExplorerAppService.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/rpc/WorkspaceExplorerAppService.java @@ -147,4 +147,14 @@ public interface WorkspaceExplorerAppService extends RemoteService { */ String getPublicLinkForItemId(String itemId) throws Exception; + + /** + * Gets the folder id from encrypted. + * + * @param encryptedFolderId the encrypted folder id + * @return the folder id from encrypted + * @throws Exception the exception + */ + String getFolderIdFromEncrypted(String encryptedFolderId) + throws Exception; } diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/rpc/WorkspaceExplorerAppServiceAsync.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/rpc/WorkspaceExplorerAppServiceAsync.java index a9a10de..bd3008f 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/rpc/WorkspaceExplorerAppServiceAsync.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/client/rpc/WorkspaceExplorerAppServiceAsync.java @@ -166,4 +166,14 @@ public interface WorkspaceExplorerAppServiceAsync { */ void getPublicLinkForItemId(String itemId, AsyncCallback callback); + /** + * Gets the folder id from encrypted. + * + * @param encodedFolderID the encoded folder id + * @param callback the callback + * @return the folder id from encrypted + */ + void getFolderIdFromEncrypted( + String encodedFolderID, AsyncCallback callback); + } diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/StringUtil.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/StringUtil.java index a2fd1cb..c928148 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/StringUtil.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/StringUtil.java @@ -3,17 +3,29 @@ */ package org.gcube.portlets.user.workspaceexplorerapp.server; +import java.io.UnsupportedEncodingException; import java.text.DecimalFormat; +import org.apache.commons.codec.binary.Base64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + /** * The Class StringUtil. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Feb 22, 2016 + * Sep 16, 2016 */ public class StringUtil { + /** + * + */ + public static final String UTF_8 = "UTF-8"; + public static final Logger logger = LoggerFactory.getLogger(StringUtil.class); + /** * Readable file size. * @@ -28,4 +40,23 @@ public class StringUtil { return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups))+units[digitGroups]; } + + /** + * Base64 decode string. + * + * @param s the s + * @return the string + */ + public static String base64DecodeString(String s) { + + try { + return new String(Base64.decodeBase64(s.getBytes(UTF_8))); + } + catch (UnsupportedEncodingException e) { + logger.error("Failed to decode the String", e); + logger.error("Returning input string: " + s); + return s; + } + } + } diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WorkspaceExplorerAppServiceImpl.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WorkspaceExplorerAppServiceImpl.java index 00a1807..e28f934 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WorkspaceExplorerAppServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WorkspaceExplorerAppServiceImpl.java @@ -6,6 +6,7 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; +import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.homelibary.model.items.type.WorkspaceItemType; import org.gcube.common.homelibrary.home.workspace.Workspace; import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder; @@ -14,6 +15,7 @@ import org.gcube.common.homelibrary.home.workspace.WorkspaceSharedFolder; import org.gcube.common.homelibrary.home.workspace.exceptions.InsufficientPrivilegesException; import org.gcube.common.homelibrary.home.workspace.exceptions.ItemAlreadyExistException; import org.gcube.common.homelibrary.home.workspace.folder.FolderItem; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.portlets.user.workspaceexplorerapp.client.WorkspaceExplorerAppConstants; import org.gcube.portlets.user.workspaceexplorerapp.client.rpc.WorkspaceExplorerAppService; import org.gcube.portlets.user.workspaceexplorerapp.shared.FilterCriteria; @@ -39,7 +41,7 @@ public class WorkspaceExplorerAppServiceImpl extends RemoteServiceServlet implem * */ public static final Logger logger = LoggerFactory.getLogger(WorkspaceExplorerAppServiceImpl.class); - + public static final String UTF_8 = "UTF-8"; /** @@ -218,7 +220,6 @@ public class WorkspaceExplorerAppServiceImpl extends RemoteServiceServlet implem } } - /** * {@inheritDoc} */ @@ -719,4 +720,30 @@ public class WorkspaceExplorerAppServiceImpl extends RemoteServiceServlet implem } } + + /** + * Gets the valid id from encrypted. + * + * @param encodedFolderId the encrypted folder id + * @return the valid id from encrypted + * @throws Exception + */ + @Override + public String getFolderIdFromEncrypted(String encodedFolderId) throws Exception{ + try{ + String scope = WsUtil.getScope(this.getThreadLocalRequest().getSession()); + ScopeProvider.instance.set(scope); + logger.info("Trying to decode encoded folder Id: "+encodedFolderId); + String base64DecodedId = StringUtil.base64DecodeString(encodedFolderId); +// String useThis = "P+IpJ6F6cTaGENfKMQWmStGUE79gbri5bVGRnzOvb8YUNIsJqFrdhceBrF+/u00j"; + logger.info("Base 64 decoded folder Id: "+base64DecodedId +", now decrypting..."); + String decryptedFId = StringEncrypter.getEncrypter().decrypt(base64DecodedId); + logger.info("Decrypted folder Id: "+decryptedFId, " returning"); + return decryptedFId; + }catch(Exception e){ + logger.error("Error during decrypting folder Id: "+encodedFolderId,e); + throw new Exception("Sorry, an error occurred when decrypting the folder id. Try again or contact the support"); + } + } + } diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WsUtil.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WsUtil.java index 796b56c..4aa7f58 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WsUtil.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WsUtil.java @@ -25,7 +25,7 @@ import org.slf4j.LoggerFactory; public class WsUtil { public static final Logger logger = LoggerFactory.getLogger(WsUtil.class); - private static final String SCOPE = "scope"; + public static final String SCOPE = "scope"; /** * Gets the workspace. @@ -35,21 +35,34 @@ public class WsUtil { * @throws InternalErrorException the internal error exception * @throws HomeNotFoundException the home not found exception * @throws WorkspaceFolderNotFoundException the workspace folder not found exception - * @throws UserNotFoundException + * @throws UserNotFoundException the user not found exception */ public static Workspace getWorkspace(HttpSession httpSession) throws InternalErrorException, HomeNotFoundException, WorkspaceFolderNotFoundException, UserNotFoundException { // ASLSession session = getASLSession(httpSession); - String scope = (String) httpSession.getAttribute(SCOPE); - - if(scope==null){ - scope = httpSession.getServletContext().getInitParameter(SCOPE); - logger.info(SCOPE + "read from context is: "+scope); - } + String scope = getScope(httpSession); //GET CONTEXT logger.info("Setting scope: "+scope); ScopeProvider.instance.set(scope); return HomeLibrary.getHomeManagerFactory().getHomeManager().getGuestLogin().getWorkspace(); } + /** + * Gets the scope. + * + * @return the scope + */ + public static String getScope(HttpSession httpSession){ + String scope = (String) httpSession.getAttribute(SCOPE); + logger.info(SCOPE + " read from httpsession is: "+scope); + + if(scope==null){ + logger.info(SCOPE + " is null reading from context"); + scope = httpSession.getServletContext().getInitParameter(SCOPE); + logger.info(SCOPE + " read from context is: "+scope); + } + + httpSession.setAttribute(SCOPE, scope); + return scope; + } } diff --git a/src/test/java/TestFolderIDDecrypt.java b/src/test/java/TestFolderIDDecrypt.java new file mode 100644 index 0000000..9c55452 --- /dev/null +++ b/src/test/java/TestFolderIDDecrypt.java @@ -0,0 +1,19 @@ + + +/** + * + */ +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Sep 16, 2016 + */ +public class TestFolderIDDecrypt { + + + public static void main(String[] args) { + + + + } +}