2013-10-24 12:51:21 +02:00
|
|
|
package org.gcube.portlets.user.td.gwtservice.server.storage;
|
2013-10-24 12:21:28 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
2013-11-14 19:21:17 +01:00
|
|
|
import java.io.InputStream;
|
2015-05-29 15:54:58 +02:00
|
|
|
import java.net.URL;
|
2013-11-15 17:05:09 +01:00
|
|
|
import java.net.URLConnection;
|
2013-10-24 12:21:28 +02:00
|
|
|
|
2018-06-29 18:04:59 +02:00
|
|
|
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;
|
2018-09-10 18:58:25 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.FolderItem;
|
2013-10-24 12:21:28 +02:00
|
|
|
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;
|
2013-11-21 10:21:20 +01:00
|
|
|
import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTServiceException;
|
2018-09-14 12:50:04 +02:00
|
|
|
import org.gcube.portlets.user.td.gwtservice.shared.workspace.ItemDescription;
|
2013-11-14 19:21:17 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2013-10-24 12:21:28 +02:00
|
|
|
|
2013-11-15 17:05:09 +01:00
|
|
|
/**
|
|
|
|
*
|
2017-05-16 16:09:16 +02:00
|
|
|
* @author Giancarlo Panichi
|
|
|
|
*
|
2013-11-15 17:05:09 +01:00
|
|
|
*
|
|
|
|
*/
|
2015-04-17 18:52:25 +02:00
|
|
|
|
2013-10-24 12:21:28 +02:00
|
|
|
public class FilesStorage {
|
|
|
|
|
2017-05-16 16:09:16 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(FilesStorage.class);
|
2015-05-29 15:54:58 +02:00
|
|
|
|
2016-06-23 16:57:54 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param user
|
2017-05-16 16:09:16 +02:00
|
|
|
* User
|
2016-06-23 16:57:54 +02:00
|
|
|
* @param file
|
2017-05-16 16:09:16 +02:00
|
|
|
* File
|
|
|
|
* @return File id
|
2016-06-23 16:57:54 +02:00
|
|
|
*/
|
2013-11-15 17:05:09 +01:00
|
|
|
public String storageCSVTempFile(String user, File file) {
|
2014-05-13 14:35:27 +02:00
|
|
|
try {
|
2017-05-16 16:09:16 +02:00
|
|
|
IClient client = new StorageClient(user, AccessType.PUBLIC, MemoryType.VOLATILE).getClient();
|
2014-05-13 14:35:48 +02:00
|
|
|
String remotePath = "/CSVimport/" + file.getName();
|
|
|
|
logger.debug("remotePath: " + remotePath);
|
2017-05-16 16:09:16 +02:00
|
|
|
String id = client.put(true).LFile(file.getAbsolutePath()).RFile(remotePath);
|
2018-09-10 18:58:25 +02:00
|
|
|
client.close();
|
2018-09-14 12:50:04 +02:00
|
|
|
|
2015-05-29 15:54:58 +02:00
|
|
|
return id;
|
|
|
|
|
2014-05-13 14:35:48 +02:00
|
|
|
} catch (Throwable e) {
|
2017-05-16 16:09:16 +02:00
|
|
|
logger.error("Error no csv file loaded on storage" + e.getLocalizedMessage());
|
2014-12-17 13:03:06 +01:00
|
|
|
throw e;
|
2014-05-13 14:35:27 +02:00
|
|
|
}
|
2014-05-13 14:35:48 +02:00
|
|
|
|
2013-10-24 12:21:28 +02:00
|
|
|
}
|
2014-06-06 16:09:46 +02:00
|
|
|
|
2016-06-23 16:57:54 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param user
|
2017-05-16 16:09:16 +02:00
|
|
|
* User
|
2016-06-23 16:57:54 +02:00
|
|
|
* @param file
|
2017-05-16 16:09:16 +02:00
|
|
|
* File
|
|
|
|
* @return File id
|
2016-06-23 16:57:54 +02:00
|
|
|
*/
|
2014-06-04 14:31:43 +02:00
|
|
|
public String storageCodelistMappingTempFile(String user, File file) {
|
|
|
|
try {
|
2017-05-16 16:09:16 +02:00
|
|
|
IClient client = new StorageClient(user, AccessType.PUBLIC, MemoryType.VOLATILE).getClient();
|
2014-06-04 14:31:43 +02:00
|
|
|
String remotePath = "/CodelistMappingImport/" + file.getName();
|
|
|
|
logger.debug("remotePath: " + remotePath);
|
2017-05-16 16:09:16 +02:00
|
|
|
String id = client.put(true).LFile(file.getAbsolutePath()).RFile(remotePath);
|
2018-09-10 18:58:25 +02:00
|
|
|
client.close();
|
2015-05-29 15:54:58 +02:00
|
|
|
return id;
|
|
|
|
} catch (Throwable e) {
|
2017-05-16 16:09:16 +02:00
|
|
|
logger.error("Error no codelist mapping file loaded on storage" + e.getLocalizedMessage());
|
2015-05-29 15:54:58 +02:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-23 16:57:54 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param user
|
2017-05-16 16:09:16 +02:00
|
|
|
* User
|
2016-06-23 16:57:54 +02:00
|
|
|
* @param url
|
2017-05-16 16:09:16 +02:00
|
|
|
* Url
|
|
|
|
* @return File id
|
2016-06-23 16:57:54 +02:00
|
|
|
* @throws TDGWTServiceException
|
2017-05-16 16:09:16 +02:00
|
|
|
* Exception
|
2016-06-23 16:57:54 +02:00
|
|
|
*/
|
2017-05-16 16:09:16 +02:00
|
|
|
public String storageCodelistMappingTempFile(String user, String url) throws TDGWTServiceException {
|
2015-05-29 15:54:58 +02:00
|
|
|
try {
|
2018-06-29 18:04:59 +02:00
|
|
|
String id = null;
|
|
|
|
|
2015-05-29 15:54:58 +02:00
|
|
|
URL address = new URL(url);
|
2018-06-29 18:04:59 +02:00
|
|
|
try (InputStream is = address.openStream()) {
|
2015-06-09 19:05:03 +02:00
|
|
|
|
2018-06-29 18:04:59 +02:00
|
|
|
IClient client = new StorageClient(user, AccessType.PUBLIC, MemoryType.VOLATILE).getClient();
|
|
|
|
String remotePath = "/CodelistMappingImport/" + address.getFile();
|
|
|
|
logger.debug("remotePath: " + remotePath);
|
|
|
|
id = client.put(true).LFile(is).RFile(remotePath);
|
2018-09-10 18:58:25 +02:00
|
|
|
client.close();
|
2018-06-29 18:04:59 +02:00
|
|
|
}
|
2015-05-29 15:54:58 +02:00
|
|
|
|
|
|
|
return id;
|
2018-06-29 18:04:59 +02:00
|
|
|
} catch (Throwable e) {
|
2017-05-16 16:09:16 +02:00
|
|
|
logger.error("Error no codelist mapping file loaded on storage" + e.getLocalizedMessage());
|
2015-05-29 15:54:58 +02:00
|
|
|
throw new TDGWTServiceException(
|
2017-05-16 16:09:16 +02:00
|
|
|
"Error no codelist mapping file loaded on storage" + e.getLocalizedMessage());
|
2014-06-04 14:31:43 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-14 19:21:17 +01:00
|
|
|
|
2013-11-15 17:05:09 +01:00
|
|
|
/**
|
2018-09-10 18:58:25 +02:00
|
|
|
*
|
|
|
|
*
|
2013-11-15 17:05:09 +01:00
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* User
|
2017-05-16 16:09:16 +02:00
|
|
|
*
|
2018-09-14 12:50:04 +02:00
|
|
|
* @param itemId
|
|
|
|
* Workspace item id
|
2017-05-16 16:09:16 +02:00
|
|
|
* @return Input stream
|
2013-11-15 17:05:09 +01:00
|
|
|
* @throws TDGWTServiceException
|
2017-05-16 16:09:16 +02:00
|
|
|
* Exception
|
2013-11-15 17:05:09 +01:00
|
|
|
*/
|
2018-09-10 18:58:25 +02:00
|
|
|
|
2018-09-14 12:50:04 +02:00
|
|
|
public InputStream retrieveInputStream(String user, String itemId) throws TDGWTServiceException {
|
2013-11-15 17:05:09 +01:00
|
|
|
InputStream is = null;
|
|
|
|
try {
|
2018-09-14 12:50:04 +02:00
|
|
|
logger.debug("retrieveFile: [user=" + user + ", itemId=" + itemId + "]");
|
2018-06-29 18:04:59 +02:00
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-09-14 12:50:04 +02:00
|
|
|
FileContainer fileContainer = shc.open(itemId).asFile();
|
2018-06-29 18:04:59 +02:00
|
|
|
StreamDescriptor streamDescriptor = fileContainer.download();
|
|
|
|
is = streamDescriptor.getStream();
|
2018-09-14 12:50:04 +02:00
|
|
|
|
2016-11-22 17:51:26 +01:00
|
|
|
} catch (Throwable e) {
|
2018-06-29 18:04:59 +02:00
|
|
|
logger.error("Error retrieving file from StorageHub", e);
|
|
|
|
throw new TDGWTServiceException("Error retrieving file from StorageHub: " + e.getLocalizedMessage(), e);
|
2013-11-15 17:05:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return is;
|
|
|
|
|
|
|
|
}
|
2013-12-04 20:01:00 +01:00
|
|
|
|
2013-12-02 17:06:40 +01:00
|
|
|
/**
|
|
|
|
*
|
2017-05-16 16:09:16 +02:00
|
|
|
* @param uri
|
2018-09-10 18:58:25 +02:00
|
|
|
* Uri
|
2017-05-16 16:09:16 +02:00
|
|
|
* @return Input stream
|
2013-12-02 17:06:40 +01:00
|
|
|
* @throws TDGWTServiceException
|
2017-05-16 16:09:16 +02:00
|
|
|
* Exception
|
2013-12-02 17:06:40 +01:00
|
|
|
*/
|
2017-05-16 16:09:16 +02:00
|
|
|
public InputStream retrieveInputStream(String uri) throws TDGWTServiceException {
|
2013-12-02 17:06:40 +01:00
|
|
|
InputStream is = null;
|
|
|
|
try {
|
2013-12-04 20:01:00 +01:00
|
|
|
|
2016-09-29 15:41:53 +02:00
|
|
|
// SMPUrl smsHome = new SMPUrl(uri);
|
|
|
|
URL url = new URL(uri);
|
|
|
|
|
2017-05-16 16:09:16 +02:00
|
|
|
logger.debug("smsHome: [host:" + url.getHost() + " path:" + url.getPath() + " ref:" + url.getRef()
|
|
|
|
+ " userinfo:" + url.getUserInfo() + " ]");
|
2013-12-02 17:06:40 +01:00
|
|
|
URLConnection uc = null;
|
2016-06-23 16:57:54 +02:00
|
|
|
uc = (URLConnection) url.openConnection();
|
2013-12-02 17:06:40 +01:00
|
|
|
is = uc.getInputStream();
|
|
|
|
|
2016-11-22 17:51:26 +01:00
|
|
|
} catch (Throwable e) {
|
2013-12-02 17:06:40 +01:00
|
|
|
logger.error("Error retrieving file from storage", e);
|
2017-05-16 16:09:16 +02:00
|
|
|
throw new TDGWTServiceException("Error retrieving file from storage: " + e.getLocalizedMessage(), e);
|
2013-12-02 17:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return is;
|
|
|
|
|
|
|
|
}
|
2013-12-04 20:01:00 +01:00
|
|
|
|
2013-12-02 17:06:40 +01:00
|
|
|
/**
|
|
|
|
*
|
2017-05-16 16:09:16 +02:00
|
|
|
* @param uri
|
|
|
|
* Uri
|
2013-12-02 17:06:40 +01:00
|
|
|
* @param user
|
|
|
|
* User
|
2018-09-14 12:50:04 +02:00
|
|
|
* @param name
|
2017-05-16 16:09:16 +02:00
|
|
|
* Item name
|
2018-09-14 12:50:04 +02:00
|
|
|
* @param description
|
2017-05-16 16:09:16 +02:00
|
|
|
* Item description
|
2018-09-14 12:50:04 +02:00
|
|
|
* @param mimetype
|
2017-05-16 16:09:16 +02:00
|
|
|
* Item mime type
|
2018-06-29 18:04:59 +02:00
|
|
|
* @param folderId
|
2017-05-16 16:09:16 +02:00
|
|
|
* Destination folder
|
2013-12-02 17:06:40 +01:00
|
|
|
* @throws TDGWTServiceException
|
2017-05-16 16:09:16 +02:00
|
|
|
* Exception
|
2013-12-02 17:06:40 +01:00
|
|
|
*/
|
2018-09-14 12:50:04 +02:00
|
|
|
public void createItemOnWorkspace(String uri, String user, String name, String description, String mimetype,
|
|
|
|
String folderId) throws TDGWTServiceException {
|
2018-06-29 18:04:59 +02:00
|
|
|
|
2013-12-02 17:06:40 +01:00
|
|
|
try {
|
|
|
|
|
2016-09-29 15:41:53 +02:00
|
|
|
URL url = new URL(uri);
|
|
|
|
|
2017-05-16 16:09:16 +02:00
|
|
|
logger.debug("smsHome: [host:" + url.getHost() + " path:" + url.getPath() + " ref:" + url.getRef()
|
|
|
|
+ " userinfo:" + url.getUserInfo() + " ]");
|
2015-05-29 15:54:58 +02:00
|
|
|
|
2013-12-02 17:06:40 +01:00
|
|
|
URLConnection uc = null;
|
2016-06-23 16:57:54 +02:00
|
|
|
uc = (URLConnection) url.openConnection();
|
2018-06-29 18:04:59 +02:00
|
|
|
try (InputStream is = uc.getInputStream()) {
|
2014-06-06 16:09:46 +02:00
|
|
|
|
2018-06-29 18:04:59 +02:00
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
FolderContainer folderContainer = shc.open(folderId).asFolder();
|
2018-09-14 12:50:04 +02:00
|
|
|
FileContainer fileContainer = folderContainer.uploadFile(is, name, description);
|
2018-06-29 18:04:59 +02:00
|
|
|
AbstractFileItem afi = fileContainer.get();
|
|
|
|
logger.debug("Created file on workspace: " + afi.getId());
|
|
|
|
}
|
2013-12-04 20:01:00 +01:00
|
|
|
|
2016-11-22 17:51:26 +01:00
|
|
|
} catch (Throwable e) {
|
2013-12-02 17:06:40 +01:00
|
|
|
logger.error("Error creating item on workspace", e);
|
2017-05-16 16:09:16 +02:00
|
|
|
throw new TDGWTServiceException("Error creating item on workspace: " + e.getLocalizedMessage(), e);
|
2013-12-02 17:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-06-09 19:05:03 +02:00
|
|
|
|
|
|
|
/**
|
2018-09-10 18:58:25 +02:00
|
|
|
*
|
2015-06-09 19:05:03 +02:00
|
|
|
*
|
|
|
|
* @param storageId
|
2017-05-16 16:09:16 +02:00
|
|
|
* Storage id
|
2015-06-09 19:05:03 +02:00
|
|
|
* @param user
|
2017-05-16 16:09:16 +02:00
|
|
|
* User
|
2018-09-14 12:50:04 +02:00
|
|
|
* @param name
|
2017-05-16 16:09:16 +02:00
|
|
|
* Item name
|
2018-09-14 12:50:04 +02:00
|
|
|
* @param description
|
2017-05-16 16:09:16 +02:00
|
|
|
* Item description
|
2018-09-14 12:50:04 +02:00
|
|
|
* @param mimetype
|
2017-05-16 16:09:16 +02:00
|
|
|
* Item mime type
|
2018-09-14 12:50:04 +02:00
|
|
|
* @param folderId
|
2017-05-16 16:09:16 +02:00
|
|
|
* Destination Folder
|
2015-06-09 19:05:03 +02:00
|
|
|
* @throws TDGWTServiceException
|
2017-05-16 16:09:16 +02:00
|
|
|
* Exception
|
2015-06-09 19:05:03 +02:00
|
|
|
*/
|
2018-09-14 12:50:04 +02:00
|
|
|
public void createItemOnWorkspaceByStorageId(String storageId, String user, String name, String description,
|
|
|
|
String mimetype, String folderId) throws TDGWTServiceException {
|
2015-06-09 19:05:03 +02:00
|
|
|
|
|
|
|
try {
|
2018-09-14 12:50:04 +02:00
|
|
|
logger.debug("CreateItemOnWorkspaceByStorageId: [storageId=" + storageId + ", user=" + user + ", name="
|
|
|
|
+ name + ", description=" + description + ", mimeType=" + mimetype + ", folderId=" + folderId
|
|
|
|
+ "]");
|
2018-09-10 18:58:25 +02:00
|
|
|
IClient client = new StorageClient(user, AccessType.PUBLIC, MemoryType.VOLATILE).getClient();
|
2015-06-09 19:05:03 +02:00
|
|
|
|
2018-09-10 18:58:25 +02:00
|
|
|
try (InputStream is = client.get().RFileAsInputStream(storageId)) {
|
2018-09-14 16:04:00 +02:00
|
|
|
if(is==null){
|
|
|
|
logger.error("Error in storage input stream is null for storageId :"+storageId);
|
|
|
|
throw new TDGWTServiceException("Error in storage input stream is null for storageId :"+storageId);
|
|
|
|
}
|
2018-09-10 18:58:25 +02:00
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-09-14 12:50:04 +02:00
|
|
|
FolderContainer folderContainer = shc.open(folderId).asFolder();
|
|
|
|
FileContainer fileContainer = folderContainer.uploadFile(is, name, description);
|
2018-09-10 18:58:25 +02:00
|
|
|
AbstractFileItem afi = fileContainer.get();
|
|
|
|
logger.debug("Created file on workspace: " + afi.getId());
|
|
|
|
}
|
|
|
|
client.close();
|
2015-06-09 19:05:03 +02:00
|
|
|
|
2016-11-22 17:51:26 +01:00
|
|
|
} catch (Throwable e) {
|
2018-06-29 18:04:59 +02:00
|
|
|
logger.error("Error creating item on workspace: " + e.getLocalizedMessage(), e);
|
2017-05-16 16:09:16 +02:00
|
|
|
throw new TDGWTServiceException("Error creating item on workspace: " + e.getLocalizedMessage(), e);
|
2015-06-09 19:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-29 15:41:53 +02:00
|
|
|
/**
|
2018-09-10 18:58:25 +02:00
|
|
|
*
|
2016-09-29 15:41:53 +02:00
|
|
|
*
|
|
|
|
* @param user
|
2017-05-16 16:09:16 +02:00
|
|
|
* User
|
2016-09-29 15:41:53 +02:00
|
|
|
* @param scope
|
2017-05-16 16:09:16 +02:00
|
|
|
* Scope
|
|
|
|
* @return Folder id
|
2016-09-29 15:41:53 +02:00
|
|
|
* @throws TDGWTServiceException
|
2017-05-16 16:09:16 +02:00
|
|
|
* Exception
|
2016-09-29 15:41:53 +02:00
|
|
|
*/
|
2017-05-16 16:09:16 +02:00
|
|
|
public String getVREFolderIdByScope(String user, String scope) throws TDGWTServiceException {
|
2016-09-29 15:41:53 +02:00
|
|
|
|
|
|
|
try {
|
2018-09-14 12:50:04 +02:00
|
|
|
logger.debug("GetVREFolderIdByScope: [user=" + user + ", scope=" + scope + "]");
|
2018-09-10 18:58:25 +02:00
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
FolderContainer folderContainer = shc.openVREFolder();
|
|
|
|
FolderItem vreFolder = folderContainer.get();
|
2016-09-29 15:41:53 +02:00
|
|
|
|
2018-09-10 18:58:25 +02:00
|
|
|
if (vreFolder != null) {
|
|
|
|
logger.debug("VRE folder id: " + vreFolder.getId());
|
|
|
|
return vreFolder.getId();
|
2016-09-29 15:41:53 +02:00
|
|
|
} else {
|
2018-09-10 18:58:25 +02:00
|
|
|
logger.debug("VRE folder id: " + vreFolder);
|
2016-09-29 15:41:53 +02:00
|
|
|
return null;
|
|
|
|
}
|
2017-05-16 16:09:16 +02:00
|
|
|
|
2016-11-22 17:51:26 +01:00
|
|
|
} catch (Throwable e) {
|
2018-06-29 18:04:59 +02:00
|
|
|
logger.error("Error retrieving VRE folder by scope! " + e.getLocalizedMessage(), e);
|
2017-05-16 16:09:16 +02:00
|
|
|
throw new TDGWTServiceException("Error retrieving VRE folder by scope: " + e.getLocalizedMessage(), e);
|
2016-09-29 15:41:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-14 12:50:04 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* User
|
|
|
|
* @param itemId
|
|
|
|
* Item Id
|
|
|
|
* @return Item Description
|
|
|
|
* @throws TDGWTServiceException
|
|
|
|
* Error
|
|
|
|
*/
|
|
|
|
public ItemDescription getItem(String user, String itemId) throws TDGWTServiceException {
|
|
|
|
try {
|
|
|
|
logger.debug("getItem: [user=" + user + ", itemId=" + itemId + "]");
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
FileContainer fileContainer = shc.open(itemId).asFile();
|
|
|
|
AbstractFileItem item = fileContainer.get();
|
|
|
|
ItemDescription itemDescription = new ItemDescription(item.getId(), item.getName(), item.getOwner(),
|
|
|
|
item.getPath(), item.getClass().getSimpleName());
|
|
|
|
logger.debug("Item retrieved: " + itemDescription);
|
|
|
|
|
|
|
|
return itemDescription;
|
|
|
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
logger.error("Error retrieving Item from StorageHub: " + e.getLocalizedMessage(), e);
|
|
|
|
throw new TDGWTServiceException("Error retrieving Item from StorageHub: " + e.getLocalizedMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 12:21:28 +02:00
|
|
|
}
|