[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
This commit is contained in:
Francesco Mangiacrapa 2016-09-19 10:19:49 +00:00
parent 2b61da9626
commit cc88c81a83
10 changed files with 246 additions and 65 deletions

15
pom.xml
View File

@ -163,6 +163,21 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<scope>compile</scope>
<version>1.8</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.gcube.portlets.widgets</groupId> -->
<!-- <artifactId>workspace-explorer</artifactId> -->

View File

@ -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);
}
}

View File

@ -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<String>() {
@Override
public void run() {
public void onSuccess(String folderId) {
//TODO //IS A TEST REMOVE
ArrayList<Item> tests = new ArrayList<Item>();
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<Item> tests = new ArrayList<Item>();
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};
}
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -166,4 +166,14 @@ public interface WorkspaceExplorerAppServiceAsync {
*/
void getPublicLinkForItemId(String itemId, AsyncCallback<String> 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<String> callback);
}

View File

@ -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;
}
}
}

View File

@ -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");
}
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,19 @@
/**
*
*/
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 16, 2016
*/
public class TestFolderIDDecrypt {
public static void main(String[] args) {
}
}