ref #11879: TDM - Update to StorageHUB

https://support.d4science.org/issues/#11879

Updated to StorageHub



git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@171374 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2018-09-10 16:58:25 +00:00
parent 309ea09453
commit f840ffae53
3 changed files with 61 additions and 53 deletions

View File

@ -227,7 +227,7 @@
<groupId>org.gcube.common</groupId>
<artifactId>home-library-model</artifactId>
<scope>provided</scope>
</dependency>
</dependency>
<!-- Service Client -->
<dependency>

View File

@ -7434,7 +7434,7 @@ public class TDGWTServiceImpl extends RemoteServiceServlet implements TDGWTServi
try {
FilesStorage filesStorage = new FilesStorage();
InputStream is = filesStorage.retrieveInputStream(serviceCredentials.getUserName(), wi);
FileUtil.setImportFileCodelistMapping(fileUploadSession, is, wi.getName(), Constants.FILE_XML_MIMETYPE);
} catch (Throwable e) {
FileUploadMonitor fum = SessionUtil.getFileUploadMonitor(httpRequest, serviceCredentials);

View File

@ -1,24 +1,17 @@
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.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.WorkspaceSharedFolder;
import org.gcube.common.homelibrary.util.WorkspaceUtil;
import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.dsl.FileContainer;
import org.gcube.common.storagehub.client.dsl.FolderContainer;
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.common.storagehub.model.items.AbstractFileItem;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
@ -52,6 +45,8 @@ public class FilesStorage {
String remotePath = "/CSVimport/" + file.getName();
logger.debug("remotePath: " + remotePath);
String id = client.put(true).LFile(file.getAbsolutePath()).RFile(remotePath);
client.close();
return id;
} catch (Throwable e) {
@ -75,6 +70,7 @@ public class FilesStorage {
String remotePath = "/CodelistMappingImport/" + file.getName();
logger.debug("remotePath: " + remotePath);
String id = client.put(true).LFile(file.getAbsolutePath()).RFile(remotePath);
client.close();
return id;
} catch (Throwable e) {
logger.error("Error no codelist mapping file loaded on storage" + e.getLocalizedMessage());
@ -104,6 +100,7 @@ public class FilesStorage {
String remotePath = "/CodelistMappingImport/" + address.getFile();
logger.debug("remotePath: " + remotePath);
id = client.put(true).LFile(is).RFile(remotePath);
client.close();
}
return id;
@ -115,7 +112,9 @@ public class FilesStorage {
}
/**
* user
*
*
*
*
* @param user
* User
@ -126,35 +125,35 @@ public class FilesStorage {
* @throws TDGWTServiceException
* Exception
*/
public void retrieveFile(String user, WorkspaceItem wi, File file) throws TDGWTServiceException {
try {
logger.debug("retrieveFile: [user=" + user + ", item=" + wi + "]");
StorageHubClient shc = new StorageHubClient();
FileContainer fileContainer = shc.open(wi.getId()).asFile();
StreamDescriptor streamDescriptor = fileContainer.download();
try (BufferedInputStream bis = new BufferedInputStream(streamDescriptor.getStream())) {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
byte[] buffer = new byte[1024];
int readCount;
while ((readCount = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readCount);
}
} catch (Throwable e) {
logger.error("Error trasferring file from storage: " + e.getLocalizedMessage(), e);
throw new TDGWTServiceException("Error trasferring file from storage: " + e.getLocalizedMessage(),
e);
}
}
} catch (Throwable e) {
logger.error("Error retrieving file from StorageHub", e);
throw new TDGWTServiceException("Error retrieving file from StorageHub: " + e.getLocalizedMessage(), e);
}
}
/*
* public void retrieveFile(String user, WorkspaceItem wi, File file) throws
* TDGWTServiceException { try { logger.debug("retrieveFile: [user=" + user
* + ", item=" + wi + "]"); StorageHubClient shc = new StorageHubClient();
* FileContainer fileContainer = shc.open(wi.getId()).asFile();
* StreamDescriptor streamDescriptor = fileContainer.download(); try
* (BufferedInputStream bis = new
* BufferedInputStream(streamDescriptor.getStream())) { try
* (BufferedOutputStream bos = new BufferedOutputStream(new
* FileOutputStream(file))) {
*
* byte[] buffer = new byte[1024]; int readCount; while ((readCount =
* bis.read(buffer)) > 0) { bos.write(buffer, 0, readCount); } } catch
* (Throwable e) { logger.error("Error trasferring file from storage: " +
* e.getLocalizedMessage(), e); throw new
* TDGWTServiceException("Error trasferring file from storage: " +
* e.getLocalizedMessage(), e); } }
*
* } catch (Throwable e) {
* logger.error("Error retrieving file from StorageHub", e); throw new
* TDGWTServiceException("Error retrieving file from StorageHub: " +
* e.getLocalizedMessage(), e); }
*
* }
*/
/**
*
*
*
* @param user
* User
@ -165,6 +164,7 @@ public class FilesStorage {
* @throws TDGWTServiceException
* Exception
*/
public InputStream retrieveInputStream(String user, WorkspaceItem wi) throws TDGWTServiceException {
InputStream is = null;
try {
@ -173,7 +173,7 @@ public class FilesStorage {
FileContainer fileContainer = shc.open(wi.getId()).asFile();
StreamDescriptor streamDescriptor = fileContainer.download();
is = streamDescriptor.getStream();
} catch (Throwable e) {
logger.error("Error retrieving file from StorageHub", e);
throw new TDGWTServiceException("Error retrieving file from StorageHub: " + e.getLocalizedMessage(), e);
@ -186,7 +186,7 @@ public class FilesStorage {
/**
*
* @param uri
* Usi
* Uri
* @return Input stream
* @throws TDGWTServiceException
* Exception
@ -259,6 +259,7 @@ public class FilesStorage {
}
/**
*
*
* @param storageId
* Storage id
@ -275,19 +276,22 @@ public class FilesStorage {
* @throws TDGWTServiceException
* Exception
*/
public void createItemOnWorkspaceByStorageId(String storageId, String user, String itemName,
String itemDescription, String itemMimetype, String itemFolder) throws TDGWTServiceException {
public void createItemOnWorkspaceByStorageId(String storageId, String user, String itemName, String itemDescription,
String itemMimetype, String itemFolder) throws TDGWTServiceException {
try {
IClient client = new StorageClient(user, AccessType.PUBLIC, MemoryType.VOLATILE).getClient();
Workspace ws = HomeLibrary.getUserWorkspace(user);
WorkspaceFolder folder = (WorkspaceFolder) ws.getItem(itemFolder);
String uniqueName = WorkspaceUtil.getUniqueName(itemName, folder);
try (InputStream is = client.get().RFileAsInputStream(storageId)) {
logger.debug("ws.createExternalFile [folder: " + folder + ", uniqueName: " + uniqueName + ", description: "
+ itemDescription + ", mimetype: " + itemMimetype + ", StorageId: " + storageId + "]");
WorkspaceUtil.createExternalFile(folder, uniqueName, itemDescription, itemMimetype, storageId);
StorageHubClient shc = new StorageHubClient();
FolderContainer folderContainer = shc.open(itemFolder).asFolder();
FileContainer fileContainer = folderContainer.uploadFile(is, itemName, itemDescription);
AbstractFileItem afi = fileContainer.get();
logger.debug("Created file on workspace: " + afi.getId());
}
client.close();
} catch (Throwable e) {
logger.error("Error creating item on workspace: " + e.getLocalizedMessage(), e);
@ -297,6 +301,7 @@ public class FilesStorage {
}
/**
*
*
* @param user
* User
@ -309,12 +314,15 @@ public class FilesStorage {
public String getVREFolderIdByScope(String user, String scope) throws TDGWTServiceException {
try {
StorageHubClient shc = new StorageHubClient();
FolderContainer folderContainer = shc.openVREFolder();
FolderItem vreFolder = folderContainer.get();
Workspace ws = HomeLibrary.getUserWorkspace(user);
WorkspaceSharedFolder folder = ws.getVREFolderByScope(scope);
if (folder != null) {
return folder.getId();
if (vreFolder != null) {
logger.debug("VRE folder id: " + vreFolder.getId());
return vreFolder.getId();
} else {
logger.debug("VRE folder id: " + vreFolder);
return null;
}