Fixed csv file import from workspace
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@85548 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
c213f62ada
commit
4e01ff9458
|
@ -112,21 +112,6 @@ public class LocalUploadServlet extends HttpServlet {
|
|||
|
||||
logger.trace("ContentType: "+contentType);
|
||||
|
||||
/* TODO is necessary?
|
||||
if (contentType.equals("application/octet-stream")){
|
||||
|
||||
logger.trace("We try to guess the content using the extension");
|
||||
String name = uploadItem.getName();
|
||||
|
||||
int lastIndex = name.lastIndexOf('.');
|
||||
if (lastIndex>=0){
|
||||
String extension = name.substring(lastIndex+1);
|
||||
logger.trace("extension "+extension);
|
||||
contentType = MimeTypeUtil.getMimeType(extension);
|
||||
logger.trace("new contenttype: "+contentType);
|
||||
} else logger.trace("No extensions found");
|
||||
}
|
||||
*/
|
||||
|
||||
try {
|
||||
FileUtil.setImportFile(fileUploadSession, uploadItem.getInputStream(), uploadItem.getName(), contentType);
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
*/
|
||||
package org.gcube.portlets.user.td.gwtservice.server;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
|
@ -20,6 +20,7 @@ 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.exceptions.InternalErrorException;
|
||||
import org.gcube.common.homelibrary.home.workspace.Workspace;
|
||||
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
||||
import org.gcube.data.analysis.tabulardata.metadata.NoSuchMetadataException;
|
||||
|
@ -47,6 +48,7 @@ import org.gcube.datapublishing.sdmx.api.model.SDMXRegistryInterfaceType;
|
|||
import org.gcube.datapublishing.sdmx.impl.model.GCubeSDMXRegistryDescriptor;
|
||||
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTService;
|
||||
import org.gcube.portlets.user.td.gwtservice.server.file.FileUploadSession;
|
||||
import org.gcube.portlets.user.td.gwtservice.server.file.FileUtil;
|
||||
import org.gcube.portlets.user.td.gwtservice.server.storage.FilesStorage;
|
||||
import org.gcube.portlets.user.td.gwtservice.server.trservice.OperationsId;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.Agencies;
|
||||
|
@ -63,6 +65,7 @@ import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVImportSession;
|
|||
import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVParserConfiguration;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVRowError;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadMonitor;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.file.FileUploadState;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.file.HeaderPresence;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportMonitor;
|
||||
import org.gcube.portlets.user.td.gwtservice.shared.sdmx.SDMXImportSession;
|
||||
|
@ -990,37 +993,77 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements
|
|||
// TODO Auto-generated method stub
|
||||
session = this.getThreadLocalRequest().getSession();
|
||||
aslSession = SessionUtil.getAslSession(session);
|
||||
|
||||
Workspace w = null;
|
||||
WorkspaceItem wi = null;
|
||||
|
||||
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);
|
||||
|
||||
|
||||
|
||||
|
||||
w = home.getWorkspace();
|
||||
wi = w.getItem(csvImportSession.getItemId());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new TDGWTServiceException(
|
||||
"Error in importCSV getFileFromWorkspace: "
|
||||
+ e.getLocalizedMessage(),e);
|
||||
"Error in importCSV getFileFromWorkspace accessing the workspace: "
|
||||
+ e.getLocalizedMessage(), e);
|
||||
}
|
||||
return;
|
||||
|
||||
if (wi == null) {
|
||||
logger.error("Error retrieving the item on workspace"
|
||||
+ csvImportSession.getItemId());
|
||||
throw new TDGWTServiceException(
|
||||
"Error retrieving the item on workspace"
|
||||
+ csvImportSession.getItemId());
|
||||
}
|
||||
|
||||
try {
|
||||
logger.debug("WorkspaceItem [id:"+wi.getId()+" name:"+wi.getName()+" remotePath:"+wi.getRemotePath()+"]");
|
||||
} catch (InternalErrorException e1) {
|
||||
e1.printStackTrace();
|
||||
throw new TDGWTServiceException(
|
||||
"Error retrieving the item on workspace"
|
||||
+ wi);
|
||||
}
|
||||
|
||||
|
||||
FileUploadSession fileUploadSession = new FileUploadSession();
|
||||
CSVImportMonitor csvImportMonitor = new CSVImportMonitor();
|
||||
FileUploadMonitor fileUploadMonitor = new FileUploadMonitor();
|
||||
|
||||
fileUploadSession.setId(session.getId());
|
||||
fileUploadSession.setFileUploadState(FileUploadState.STARTED);
|
||||
fileUploadSession.setCsvImportMonitor(csvImportMonitor);
|
||||
fileUploadSession.setFileUploadMonitor(fileUploadMonitor);
|
||||
|
||||
SessionUtil.setFileUploadSession(session, fileUploadSession);
|
||||
|
||||
try {
|
||||
FilesStorage filesStorage = new FilesStorage();
|
||||
InputStream is = filesStorage.retriveImputStream(
|
||||
aslSession.getUsername(), wi);
|
||||
|
||||
FileUtil.setImportFile(fileUploadSession, is, wi.getName(),
|
||||
"text/csv");
|
||||
} catch (Exception e) {
|
||||
fileUploadSession.getFileUploadMonitor().setFailed(
|
||||
"An error occured elaborating the file",
|
||||
FileUtil.exceptionDetailMessage(e));
|
||||
fileUploadSession.setFileUploadState(FileUploadState.FAILED);
|
||||
SessionUtil.setFileUploadSession(session, fileUploadSession);
|
||||
logger.error("Error elaborating the imput stream", e);
|
||||
throw new TDGWTServiceException(
|
||||
"Error in importCSV getFileFromWorkspace: "
|
||||
+ e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
logger.trace("changing state");
|
||||
fileUploadSession.getFileUploadMonitor().setState(
|
||||
FileUploadState.COMPLETED);
|
||||
SessionUtil.setFileUploadSession(session, fileUploadSession);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -6,8 +6,14 @@ import java.io.File;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLStreamHandler;
|
||||
|
||||
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
||||
import org.gcube.contentmanagement.blobstorage.service.IClient;
|
||||
import org.gcube.contentmanager.storageclient.model.protocol.smp.Handler;
|
||||
import org.gcube.contentmanager.storageclient.model.protocol.smp.SMPURLConnection;
|
||||
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
|
||||
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
|
||||
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
|
||||
|
@ -15,34 +21,59 @@ import org.gcube.portlets.user.td.gwtservice.shared.TDGWTServiceException;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author "Giancarlo Panichi" <a
|
||||
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
||||
*
|
||||
*/
|
||||
public class FilesStorage {
|
||||
|
||||
public static final Logger logger = LoggerFactory
|
||||
.getLogger(FilesStorage.class);
|
||||
|
||||
public String storageCSVTempFile(String user, File file) {
|
||||
public String storageCSVTempFile(String user, File file) {
|
||||
IClient client = new StorageClient(user, AccessType.PUBLIC,
|
||||
MemoryType.VOLATILE).getClient();
|
||||
String remotePath = "/CSVimport/" + file.getName();
|
||||
logger.debug("remotePath: "+remotePath);
|
||||
client.put(true).LFile(file.getAbsolutePath()).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();
|
||||
|
||||
/**
|
||||
* user
|
||||
*
|
||||
* @param user
|
||||
* User
|
||||
* @param remotePath
|
||||
* File path on storage
|
||||
* @param file
|
||||
* Destination file
|
||||
* @throws TDGWTServiceException
|
||||
*/
|
||||
public void retriveFile(String user, WorkspaceItem wi, File file)
|
||||
throws TDGWTServiceException {
|
||||
InputStream is = null;
|
||||
|
||||
try {
|
||||
is = client.get().RFileAsInputStream(remotePath);
|
||||
|
||||
org.gcube.common.homelibrary.home.workspace.folder.items.File gcubeItem = ((org.gcube.common.homelibrary.home.workspace.folder.items.File) wi);
|
||||
Handler.activateProtocol();
|
||||
URL smsHome = null;
|
||||
smsHome = new URL(gcubeItem.getPublicLink());
|
||||
logger.debug("smsHome: [host:" + smsHome.getHost() + " path:"
|
||||
+ smsHome.getPath() + " ref:" + smsHome.getRef()
|
||||
+ " userinfo:" + smsHome.getUserInfo() + " ]");
|
||||
URLConnection uc = null;
|
||||
uc = (URLConnection) smsHome.openConnection();
|
||||
is = uc.getInputStream();
|
||||
|
||||
} 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);
|
||||
logger.error("Error retrieving file from storage", e);
|
||||
throw new TDGWTServiceException(
|
||||
"Error retrieving file from storage: "
|
||||
+ e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -56,12 +87,61 @@ public class FilesStorage {
|
|||
}
|
||||
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);
|
||||
|
||||
logger.error(
|
||||
"Error trasferring file from storage: "
|
||||
+ e.getLocalizedMessage(), e);
|
||||
throw new TDGWTServiceException(
|
||||
"Error trasferring file from storage: "
|
||||
+ e.getLocalizedMessage(), e);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param user
|
||||
* User
|
||||
* @param remotePath
|
||||
* File path on storage
|
||||
* @return InputStream back to read the file
|
||||
* @throws TDGWTServiceException
|
||||
*/
|
||||
public InputStream retriveImputStream(String user, WorkspaceItem wi)
|
||||
throws TDGWTServiceException {
|
||||
InputStream is = null;
|
||||
try {
|
||||
|
||||
org.gcube.common.homelibrary.home.workspace.folder.items.File gcubeItem = ((org.gcube.common.homelibrary.home.workspace.folder.items.File) wi);
|
||||
Handler.activateProtocol();
|
||||
URL smsHome = null;
|
||||
smsHome = new URL(null, gcubeItem.getPublicLink(), new URLStreamHandler() {
|
||||
|
||||
@Override
|
||||
protected URLConnection openConnection(URL u) throws IOException {
|
||||
return new SMPURLConnection(u);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
// new URL(gcubeItem.getPublicLink());
|
||||
logger.debug("smsHome: [host:" + smsHome.getHost() + " path:"
|
||||
+ smsHome.getPath() + " ref:" + smsHome.getRef()
|
||||
+ " userinfo:" + smsHome.getUserInfo() + " ]");
|
||||
URLConnection uc = null;
|
||||
uc = (URLConnection) smsHome.openConnection();
|
||||
is = uc.getInputStream();
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error retrieving file from storage", e);
|
||||
throw new TDGWTServiceException(
|
||||
"Error retrieving file from storage: "
|
||||
+ e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
return is;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue