293 lines
8.9 KiB
Java
293 lines
8.9 KiB
Java
package org.gcube.portlets.user.statisticalalgorithmsimporter.server.storage;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.InputStream;
|
|
import java.net.URLConnection;
|
|
|
|
import org.gcube.common.homelibrary.home.HomeLibrary;
|
|
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
|
|
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.common.homelibrary.home.workspace.exceptions.InsufficientPrivilegesException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemAlreadyExistException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WrongDestinationException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WrongItemTypeException;
|
|
import org.gcube.contentmanager.storageclient.model.protocol.smp.SMPUrl;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException;
|
|
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 {
|
|
|
|
private static final String STATISTICAL_ALGORITHM_PROJECT_MIMETYPE = "text/xml";
|
|
private static final String STATISTICAL_ALGORITHM_PROJECT_FILE_DESCRIPTION = "Statistical Algorithm Project File";
|
|
private static final String STATISTICAL_ALGORITHM_PROJECT_FILE_NAME = "stat_algo.project";
|
|
|
|
public static final Logger logger = LoggerFactory
|
|
.getLogger(FilesStorage.class);
|
|
|
|
public void copyItemOnFolder(String user, String itemId, String folderId)
|
|
throws StatAlgoImporterServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem workSpaceItem = ws.getItem(folderId);
|
|
if (!workSpaceItem.isFolder()) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Destination is not a folder!");
|
|
}
|
|
|
|
ws.copy(itemId, folderId);
|
|
|
|
return;
|
|
} catch (WrongDestinationException | ItemAlreadyExistException
|
|
| InsufficientPrivilegesException
|
|
| WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException e) {
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
}
|
|
|
|
public void deleteItemOnFolder(String user, String itemId)
|
|
throws StatAlgoImporterServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
ws.removeItems(itemId);
|
|
|
|
return;
|
|
} catch (InsufficientPrivilegesException
|
|
| WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException e) {
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void saveStatisticalAlgorithmProject(String user, InputStream inputStream,
|
|
String folderId) throws StatAlgoImporterServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem workSpaceItem = ws.getItem(folderId);
|
|
if (!workSpaceItem.isFolder()) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Destination is not a folder!");
|
|
}
|
|
|
|
WorkspaceItem projectItem = ws.find(
|
|
STATISTICAL_ALGORITHM_PROJECT_FILE_NAME, folderId);
|
|
|
|
if (projectItem == null) {
|
|
ws.createExternalFile(STATISTICAL_ALGORITHM_PROJECT_FILE_NAME,
|
|
STATISTICAL_ALGORITHM_PROJECT_FILE_DESCRIPTION,
|
|
STATISTICAL_ALGORITHM_PROJECT_MIMETYPE, inputStream,
|
|
folderId);
|
|
} else {
|
|
ws.updateItem(projectItem.getId(), inputStream);
|
|
}
|
|
|
|
return;
|
|
} catch (WrongItemTypeException | WorkspaceFolderNotFoundException
|
|
| InternalErrorException | HomeNotFoundException
|
|
| ItemNotFoundException | InsufficientPrivilegesException
|
|
| ItemAlreadyExistException | WrongDestinationException e) {
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void saveItemOnWorkspace(String user, InputStream inputStream, String name,
|
|
String description, String mimeType,
|
|
String folderId) throws StatAlgoImporterServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem workSpaceItem = ws.getItem(folderId);
|
|
if (!workSpaceItem.isFolder()) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Destination is not a folder!");
|
|
}
|
|
|
|
WorkspaceItem projectItem = ws.find(
|
|
name, folderId);
|
|
|
|
if (projectItem == null) {
|
|
ws.createExternalFile(name,
|
|
description,
|
|
mimeType, inputStream,
|
|
folderId);
|
|
} else {
|
|
ws.updateItem(projectItem.getId(), inputStream);
|
|
}
|
|
|
|
return;
|
|
} catch (WrongItemTypeException | WorkspaceFolderNotFoundException
|
|
| InternalErrorException | HomeNotFoundException
|
|
| ItemNotFoundException | InsufficientPrivilegesException
|
|
| ItemAlreadyExistException | WrongDestinationException e) {
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public InputStream retrieveProjectItemOnWorkspace(String user, String folderId)
|
|
throws StatAlgoImporterServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem workSpaceItem = ws.getItem(folderId);
|
|
if (!workSpaceItem.isFolder()) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Folder is not valid item!");
|
|
}
|
|
|
|
WorkspaceItem projectItem=ws.find(STATISTICAL_ALGORITHM_PROJECT_FILE_NAME, folderId);
|
|
|
|
if(projectItem==null){
|
|
throw new StatAlgoImporterServiceException(
|
|
"No project found in this folder!");
|
|
}
|
|
|
|
return retrieveImputStream(user, projectItem);
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException | WrongItemTypeException e) {
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public InputStream retrieveItemOnWorkspace(String user, String itemId)
|
|
throws StatAlgoImporterServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem workSpaceItem = ws.getItem(itemId);
|
|
if (workSpaceItem.isFolder()) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Folder is not valid item!");
|
|
}
|
|
|
|
return retrieveImputStream(user, workSpaceItem);
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException e) {
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param user
|
|
* User
|
|
* @param remotePath
|
|
* File path on storage
|
|
* @return InputStream back to read the file
|
|
* @throws StatAlgoImporterServiceException
|
|
*/
|
|
public InputStream retrieveImputStream(String user, WorkspaceItem wi)
|
|
throws StatAlgoImporterServiceException {
|
|
InputStream is = null;
|
|
try {
|
|
|
|
org.gcube.common.homelibrary.home.workspace.folder.items.File gcubeItem = ((org.gcube.common.homelibrary.home.workspace.folder.items.File) wi);
|
|
SMPUrl smsHome = new SMPUrl(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);
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(
|
|
"Error retrieving file from storage: "
|
|
+ e.getLocalizedMessage(), e);
|
|
}
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
*
|
|
* @param user User
|
|
* @param itemId Item on workspace
|
|
* @param data String to save
|
|
* @throws StatAlgoImporterServiceException
|
|
*/
|
|
public void saveItem(String user, String itemId, String data)
|
|
throws StatAlgoImporterServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem workSpaceItem = ws.getItem(itemId);
|
|
if (workSpaceItem==null) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"No item retrieved on workspace!");
|
|
} else {
|
|
if (workSpaceItem.isFolder()) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Folder is not valid item!");
|
|
} else {
|
|
|
|
}
|
|
}
|
|
// convert String into InputStream
|
|
InputStream is = new ByteArrayInputStream(data.getBytes());
|
|
|
|
ws.updateItem(itemId, is);
|
|
|
|
return;
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException | InsufficientPrivilegesException | ItemAlreadyExistException | WrongDestinationException e) {
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
|
|
}
|