From c213f62adaaf4e9528480061178aa51413e01545 Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Thu, 14 Nov 2013 18:21:17 +0000 Subject: [PATCH] Fixed storage access git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@85511 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gwtservice/client/rpc/TDGWTService.java | 21 +++-- .../client/rpc/TDGWTServiceAsync.java | 15 ++-- .../gwtservice/server/TDGWTServiceImpl.java | 84 +++++++++++++++---- .../server/storage/FilesStorage.java | 51 ++++++++++- .../shared/TDGWTServiceException.java | 8 +- .../shared/csv/CSVImportSession.java | 24 +++--- 6 files changed, 160 insertions(+), 43 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTService.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTService.java index f2b0a50..2344332 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTService.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTService.java @@ -58,7 +58,7 @@ public interface TDGWTService extends RemoteService { * @param tr * @throws TDGWTServiceException */ - public void setTabResource(TabResource tr) throws TDGWTServiceException; + public void setTabResource(TabResource tabResource) throws TDGWTServiceException; /** * Creates a TabResource on Service @@ -86,7 +86,7 @@ public interface TDGWTService extends RemoteService { * @param s * @throws TDGWTServiceException */ - public void setTDOpenSession(TDOpenSession s) throws TDGWTServiceException; + public void setTDOpenSession(TDOpenSession tdOpenSession) throws TDGWTServiceException; // Import SDMX @@ -120,7 +120,7 @@ public interface TDGWTService extends RemoteService { * @param s * @throws TDGWTServiceException */ - public void setSDMXSession(SDMXImportSession s) + public void setSDMXSession(SDMXImportSession sdmxImportSession) throws TDGWTServiceException; @@ -131,7 +131,7 @@ public interface TDGWTService extends RemoteService { * @param s * @throws TDGWTServiceException */ - public void startSDMXImport(SDMXImportSession s) + public void startSDMXImport(SDMXImportSession sdmxImportSession) throws TDGWTServiceException; @@ -159,8 +159,17 @@ public interface TDGWTService extends RemoteService { * @param s * @throws TDGWTServiceException */ - public void setCSVSession(CSVImportSession s) throws TDGWTServiceException; + public void setCSVSession(CSVImportSession csvImportSession) throws TDGWTServiceException; + + /** + * + * @param s + * @throws TDGWTServiceException + */ + public void getFileFromWorkspace(CSVImportSession csvImportSession) throws TDGWTServiceException; + + /** * * @return @@ -197,7 +206,7 @@ public interface TDGWTService extends RemoteService { * @param s * @throws TDGWTServiceException */ - public void startCSVImport(CSVImportSession s) throws TDGWTServiceException; + public void startCSVImport(CSVImportSession csvImportSession) throws TDGWTServiceException; /** * Get Operation Monitor during the CSV Import operation diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTServiceAsync.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTServiceAsync.java index 8523a03..39b0021 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTServiceAsync.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/client/rpc/TDGWTServiceAsync.java @@ -44,7 +44,7 @@ public interface TDGWTServiceAsync { void getTabularResources(AsyncCallback> callback); //Open - void setTDOpenSession(TDOpenSession s, AsyncCallback callback); + void setTDOpenSession(TDOpenSession tdOpenSession, AsyncCallback callback); //Import SDMX void getCodelists(AsyncCallback> callback); @@ -53,27 +53,28 @@ public interface TDGWTServiceAsync { void getAgencies(AsyncCallback> callback); - void setSDMXSession(SDMXImportSession s, AsyncCallback callback); + void setSDMXSession(SDMXImportSession sdmxImportSession, AsyncCallback callback); void getSDMXImportMonitor(AsyncCallback callback); - void startSDMXImport(SDMXImportSession s,AsyncCallback callback); + void startSDMXImport(SDMXImportSession sdmxImportSession,AsyncCallback callback); //Import CSV void getFileUploadMonitor(AsyncCallback callback); void getCSVImportMonitor(AsyncCallback callback); - void setCSVSession(CSVImportSession s, AsyncCallback callback); - - + void setCSVSession(CSVImportSession csvImportSession, AsyncCallback callback); + + void getFileFromWorkspace(CSVImportSession csvImportSession, AsyncCallback callback); + void getAvailableCharset(AsyncCallback callback); void configureCSVParser(String encoding, HeaderPresence headerPresence, char delimiter, char comment, AsyncCallback> callback); void checkCSV(long errorsLimit, AsyncCallback > callback); - void startCSVImport(CSVImportSession s, AsyncCallback callback); + void startCSVImport(CSVImportSession csvImportSession, AsyncCallback callback); } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/TDGWTServiceImpl.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/TDGWTServiceImpl.java index 4371f70..438cf70 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/TDGWTServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/TDGWTServiceImpl.java @@ -3,6 +3,7 @@ */ package org.gcube.portlets.user.td.gwtservice.server; +import java.io.File; import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -15,6 +16,12 @@ import java.util.Map; import javax.servlet.http.HttpSession; import org.gcube.application.framework.core.session.ASLSession; +import org.gcube.common.homelibrary.home.Home; +import org.gcube.common.homelibrary.home.HomeLibrary; +import org.gcube.common.homelibrary.home.HomeManager; +import org.gcube.common.homelibrary.home.HomeManagerFactory; +import org.gcube.common.homelibrary.home.workspace.Workspace; +import org.gcube.common.homelibrary.home.workspace.WorkspaceItem; import org.gcube.data.analysis.tabulardata.metadata.NoSuchMetadataException; import org.gcube.data.analysis.tabulardata.model.table.Table; import org.gcube.data.analysis.tabulardata.model.table.TableId; @@ -556,18 +563,18 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements throw new TDGWTServiceException( "Error in importSDMX Status null"); } else { - logger.debug("Service Task.getStatus(): "+task.getStatus()); + logger.debug("Service Task.getStatus(): " + + task.getStatus()); importMonitor.setStatus(task.getStatus().ordinal()); switch (importMonitor.getStatus()) { case SUCCEDED: importMonitor.setProgress(task.getProgress()); - logger.debug("Task Result: "+task.getResult()); - trId.setTableId(String.valueOf(task - .getResult().getOutput().getId() - .getValue())); + logger.debug("Task Result: " + task.getResult()); + trId.setTableId(String.valueOf(task.getResult() + .getOutput().getId().getValue())); sdmxImportTabResource.setTrId(trId); - SessionUtil.setSDMXImportTabResource( - session, sdmxImportTabResource); + SessionUtil.setSDMXImportTabResource(session, + sdmxImportTabResource); SessionUtil.setTabResource(session, sdmxImportTabResource); @@ -575,9 +582,12 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements break; case FAILED: if (task.getResult() != null) { - logger.debug("Task exception:"+task.getResult().getException()); - importMonitor.setError(new Throwable(task.getResult() - .getException().toString())); + logger.debug("Task exception:" + + task.getResult().getException()); + importMonitor + .setError(new Throwable(task + .getResult().getException() + .toString())); } else { logger.debug("Task exception: Error In Import"); importMonitor.setError(new Throwable( @@ -911,14 +921,17 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements throw new TDGWTServiceException( "Error in importCSV Status null"); } else { - logger.debug("Status: "+task.getStatus()); + logger.debug("Status: " + task.getStatus()); importMonitor.setStatus(task.getStatus().ordinal()); switch (importMonitor.getStatus()) { case FAILED: if (task.getResult() != null) { - logger.debug("Task exception:"+task.getResult().getException()); - importMonitor.setError(new Throwable(task.getResult() - .getException().toString())); + logger.debug("Task exception:" + + task.getResult().getException()); + importMonitor + .setError(new Throwable(task + .getResult().getException() + .toString())); } else { logger.debug("Task exception: Error In Import"); importMonitor.setError(new Throwable( @@ -927,10 +940,10 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements importMonitor.setProgress(task.getProgress()); break; case SUCCEDED: - logger.debug("Task Result:"+task.getResult()); + logger.debug("Task Result:" + task.getResult()); importMonitor.setProgress(task.getProgress()); Table table = task.getResult().getOutput(); - + logger.debug("Table retrived: " + table.toString()); trId.setTableId(String.valueOf(table.getId() .getValue())); @@ -971,4 +984,43 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements } + @Override + public void getFileFromWorkspace(CSVImportSession csvImportSession) + throws TDGWTServiceException { + // TODO Auto-generated method stub + session = this.getThreadLocalRequest().getSession(); + aslSession = SessionUtil.getAslSession(session); + + try { + HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory(); + + HomeManager manager = factory.getHomeManager(); + + Home home = manager.getHome(aslSession.getUsername()); + + Workspace w=home.getWorkspace(); + WorkspaceItem wi=w.getItem(csvImportSession.getItemId()); + + if(wi==null){ + logger.error("error retrieving the item"+csvImportSession.getItemId()); + throw new TDGWTServiceException( + "error retrieving the item"+csvImportSession.getItemId()); + } + File file=new File("ciao"); + FilesStorage filesStorage=new FilesStorage(); + filesStorage.retriveFile(aslSession.getUsername(), wi.getRemotePath(), file); + + + + } catch (Exception e) { + e.printStackTrace(); + throw new TDGWTServiceException( + "Error in importCSV getFileFromWorkspace: " + + e.getLocalizedMessage(),e); + } + return; + } + + + } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/storage/FilesStorage.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/storage/FilesStorage.java index 9ec2139..a9308d9 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/storage/FilesStorage.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/storage/FilesStorage.java @@ -1,20 +1,67 @@ package org.gcube.portlets.user.td.gwtservice.server.storage; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import org.gcube.contentmanagement.blobstorage.service.IClient; import org.gcube.contentmanager.storageclient.wrapper.AccessType; import org.gcube.contentmanager.storageclient.wrapper.MemoryType; import org.gcube.contentmanager.storageclient.wrapper.StorageClient; +import org.gcube.portlets.user.td.gwtservice.shared.TDGWTServiceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class FilesStorage { - public String storageCSVTempFile(String user, File file) { + public static final Logger logger = LoggerFactory + .getLogger(FilesStorage.class); + + public String storageCSVTempFile(String user, File file) { IClient client = new StorageClient(user, AccessType.PUBLIC, MemoryType.VOLATILE).getClient(); String remotePath = "/CSVimport/" + file.getName(); client.put(true).LFile(file.getAbsolutePath()).RFile(remotePath); - String url=client.getUrl().RFile(remotePath); + String url = client.getUrl().RFile(remotePath); return url; } + + public void retriveFile(String user, String remotePath, File file) throws TDGWTServiceException { + IClient client = new StorageClient(user, AccessType.PUBLIC, + MemoryType.VOLATILE).getClient(); + + InputStream is = null; + + try { + is = client.get().RFileAsInputStream(remotePath); + + } catch (Exception e) { + logger.error("No payload found in Storage for remotePath: " + + remotePath,e); + throw new TDGWTServiceException("No payload found in Storage for remotePath: " + + remotePath,e); + } + + try { + BufferedInputStream bis = new BufferedInputStream(is); + FileOutputStream os = new FileOutputStream(file); + BufferedOutputStream bos = new BufferedOutputStream(os); + byte[] buffer = new byte[1024]; + int readCount; + while ((readCount = bis.read(buffer)) > 0) { + bos.write(buffer, 0, readCount); + } + bos.close(); + } catch (IOException e) { + logger.error("Error trasferring file from storage: " + + e.getLocalizedMessage(),e); + throw new TDGWTServiceException("Error trasferring file from storage: " + + e.getLocalizedMessage(),e); + + } + } + } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/TDGWTServiceException.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/TDGWTServiceException.java index 62b01c9..a16dcf5 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/TDGWTServiceException.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/TDGWTServiceException.java @@ -4,7 +4,9 @@ package org.gcube.portlets.user.td.gwtservice.shared; /** - * @author "Federico De Faveri defaveri@isti.cnr.it" + * + * @author "Giancarlo Panichi" + * g.panichi@isti.cnr.it * */ public class TDGWTServiceException extends Exception { @@ -26,5 +28,9 @@ public class TDGWTServiceException extends Exception { } + public TDGWTServiceException(String message,Throwable t) { + super(message,t); + } + } diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/csv/CSVImportSession.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/csv/CSVImportSession.java index 307076c..efaddbf 100644 --- a/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/csv/CSVImportSession.java +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/shared/csv/CSVImportSession.java @@ -34,7 +34,7 @@ public class CSVImportSession implements Serializable { protected boolean[] columnToImportMask; protected String localFileName; - protected String workspaceItemPath; + protected String itemId; /*protected long fileLenght; protected String serverlFileName; @@ -46,6 +46,7 @@ public class CSVImportSession implements Serializable { */ + public String getId() { return id; } @@ -88,7 +89,12 @@ public class CSVImportSession implements Serializable { public void setLocalFileName(String localFileName) { this.localFileName = localFileName; } - + public String getItemId() { + return itemId; + } + public void setItemId(String itemId) { + this.itemId = itemId; + } /* public long getFileLenght() { return fileLenght; @@ -127,23 +133,19 @@ public class CSVImportSession implements Serializable { this.csvRights = csvRights; } */ - public String getWorkspaceItemPath() { - return workspaceItemPath; - } - public void setWorkspaceItemPath(String workspaceItemPath) { - this.workspaceItemPath = workspaceItemPath; - } - @Override public String toString() { return "CSVImportSession [id=" + id + ", source=" + source + ", tabResource=" + tabResource + ", headers=" + headers + ", skipInvalidLines=" + skipInvalidLines + ", columnToImportMask=" + Arrays.toString(columnToImportMask) - + ", localFileName=" + localFileName + ", workspaceItemPath=" - + workspaceItemPath + "]"; + + ", localFileName=" + localFileName + ", itemId=" + itemId + + "]"; } + + + }