You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
283 lines
7.8 KiB
Java
283 lines
7.8 KiB
Java
package org.gcube.portlets.user.dataminermanager.server.storage;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URL;
|
|
import java.net.URLConnection;
|
|
import java.net.URLStreamHandler;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.apache.log4j.Logger;
|
|
import org.gcube.common.homelibrary.home.Home;
|
|
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.WorkspaceFolder;
|
|
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.ItemNotFoundException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WrongItemTypeException;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalImage;
|
|
import org.gcube.contentmanager.storageclient.model.protocol.smp.SMPURLConnection;
|
|
import org.gcube.portlets.user.dataminermanager.shared.exception.ServiceException;
|
|
|
|
public class StorageUtil {
|
|
|
|
private static Logger logger = Logger.getLogger(StorageUtil.class);
|
|
|
|
public static InputStream getStorageClientInputStream(String url)
|
|
throws Exception {
|
|
/*
|
|
* try { logger.debug("Get ImputStream from: "+url); SMPUrl smsHome =
|
|
* new SMPUrl(url); logger.debug("smsHome: [host:" + smsHome.getHost() +
|
|
* " path:" + smsHome.getPath() + " ref:" + smsHome.getRef() +
|
|
* " userinfo:" + smsHome.getUserInfo() + " ]"); URLConnection uc =
|
|
* (URLConnection) smsHome.openConnection(); InputStream is =
|
|
* uc.getInputStream(); return is;
|
|
*
|
|
* } catch (Exception e) {
|
|
* logger.error("Error retrieving imput stream from storage: ", e);
|
|
* e.printStackTrace(); throw new Exception(
|
|
* "Error retrieving imput stream from storage: " +
|
|
* e.getLocalizedMessage(), e); }
|
|
*/
|
|
|
|
try {
|
|
|
|
URL u = new URL(null, url, new URLStreamHandler() {
|
|
|
|
@Override
|
|
protected URLConnection openConnection(URL u)
|
|
throws IOException {
|
|
|
|
return new SMPURLConnection(u);
|
|
}
|
|
});
|
|
|
|
return u.openConnection().getInputStream();
|
|
|
|
} catch (Throwable e) {
|
|
logger.error("Error in StorageUtil: " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new Exception(e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static String extractLocation(String url) {
|
|
String[] loc = url.split("//");
|
|
// logger.trace("url extracted: " + loc[1]);
|
|
return loc[1];
|
|
}
|
|
|
|
public static String getFileName(String url) {
|
|
String[] urlParam = url.split("\\?");
|
|
String location = extractLocation(urlParam[0]);
|
|
|
|
try {
|
|
return location.split("/")[1];
|
|
} catch (Exception e) {
|
|
// e.printStackTrace();
|
|
return location;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return
|
|
*/
|
|
public static Map<String, String> getFilesUrlFromFolderUrl(
|
|
String serviceClass, String serviceName, String url,
|
|
String username, String scope) throws Exception {
|
|
|
|
Home home = HomeLibrary.getHomeManagerFactory().getHomeManager()
|
|
.getHome(username);
|
|
Map<String, String> map = new LinkedHashMap<String, String>();
|
|
Workspace ws = home.getWorkspace();
|
|
WorkspaceItem folderItem = ws.getItemByPath(url);
|
|
// logger.trace("Type of workspace item is : "
|
|
// + folderItem.getType().toString());
|
|
WorkspaceFolder folder = (WorkspaceFolder) folderItem;
|
|
List<WorkspaceItem> childrenList = folder.getChildren();
|
|
for (WorkspaceItem item : childrenList) {
|
|
ExternalImage file = (ExternalImage) item;
|
|
String name = item.getName();
|
|
String absoluteUrlFile = file.getPublicLink();
|
|
|
|
map.put(name, absoluteUrlFile);
|
|
}
|
|
|
|
return map;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param user
|
|
* User
|
|
* @param itemId
|
|
* Item id
|
|
* @return Input stream
|
|
* @throws StatAlgoImporterServiceException
|
|
*/
|
|
public static InputStream getInputStreamForItemOnWorkspace(String user,
|
|
String itemId) throws ServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem workSpaceItem = ws.getItem(itemId);
|
|
if (workSpaceItem.isFolder()) {
|
|
throw new ServiceException("Folder is not valid!");
|
|
}
|
|
|
|
return getImputStream(user, workSpaceItem);
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException e) {
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param user
|
|
* User
|
|
* @param wi
|
|
* WorkspaceItem
|
|
* @return InputStream
|
|
* @throws StatAlgoImporterServiceException
|
|
*/
|
|
public static InputStream getImputStream(String user, WorkspaceItem wi)
|
|
throws ServiceException {
|
|
InputStream is = null;
|
|
try {
|
|
|
|
org.gcube.common.homelibrary.home.workspace.folder.items.File gcubeItem = ((org.gcube.common.homelibrary.home.workspace.folder.items.File) wi);
|
|
is = gcubeItem.getData();
|
|
return is;
|
|
|
|
} catch (Throwable e) {
|
|
logger.error("Error retrieving file from storage", e);
|
|
e.printStackTrace();
|
|
throw new ServiceException("Error retrieving file from storage: "
|
|
+ e.getLocalizedMessage(), e);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param user
|
|
* User
|
|
* @param itemId
|
|
* Item id
|
|
* @return Public link
|
|
* @throws StatAlgoImporterServiceException
|
|
*/
|
|
public static String getPublicLink(String user, String itemId)
|
|
throws ServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem workSpaceItem = ws.getItem(itemId);
|
|
if (workSpaceItem.isFolder()) {
|
|
throw new ServiceException("Attention this is a folder!");
|
|
}
|
|
|
|
return workSpaceItem.getPublicLink(true);
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException e) {
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param user
|
|
* @param itemName
|
|
* @return
|
|
* @throws ServiceException
|
|
*/
|
|
public static WorkspaceItem getItemInRootFolderOnWorkspace(String user,
|
|
String itemName) throws ServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem item = ws.find(itemName, ws.getRoot().getId());
|
|
|
|
return item;
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException
|
|
| WrongItemTypeException e) {
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param user
|
|
* @param folderId
|
|
* @param itemName
|
|
* @return
|
|
* @throws ServiceException
|
|
*/
|
|
public static WorkspaceItem getItemInFolderOnWorkspace(String user,
|
|
String folderId, String itemName) throws ServiceException {
|
|
Workspace ws;
|
|
try {
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
WorkspaceItem item = ws.find(itemName, folderId);
|
|
|
|
return item;
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException
|
|
| WrongItemTypeException e) {
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
*
|
|
* @param user
|
|
* @param itemId
|
|
* @throws ServiceException
|
|
*/
|
|
public static void deleteItem(String user, String itemId)
|
|
throws ServiceException {
|
|
Workspace ws;
|
|
try {
|
|
logger.debug("User: "+user+", ItemId:"+itemId);
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
ws.removeItems(itemId);
|
|
|
|
return;
|
|
} catch (InsufficientPrivilegesException
|
|
| WorkspaceFolderNotFoundException | InternalErrorException
|
|
| HomeNotFoundException | ItemNotFoundException e) {
|
|
e.printStackTrace();
|
|
throw new ServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|