USing StorageHubClient in place of Home Library Webapp HTTP calls
This commit is contained in:
parent
b26baa781a
commit
e6a4dbc3a7
17
pom.xml
17
pom.xml
|
@ -55,6 +55,23 @@
|
|||
<version>2.7.11</version>
|
||||
</dependency>
|
||||
<!-- END Document Store and Accounting libraries -->
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>storagehub-client-library</artifactId>
|
||||
<version>[1.0.0, 2.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
<!-- Home Library dependencies MUST be removed when the storagehub add missing features -->
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>home-library-jcr</artifactId>
|
||||
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>home-library</artifactId>
|
||||
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
<!-- END of Home Library dependencies -->
|
||||
<!-- Test Dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
|
|
@ -1,22 +1,23 @@
|
|||
package org.gcube.accounting.aggregator.directory;
|
||||
|
||||
import org.gcube.accounting.aggregator.workspace.WorkSpaceManagement;
|
||||
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class WorkSpaceDirectoryStructure extends DirectoryStructure<String>{
|
||||
public class WorkSpaceDirectoryStructure extends DirectoryStructure<FolderContainer>{
|
||||
|
||||
private static final String BACKUP_FOLDER_DESCRIPTION = "Accouting Aggregator Plugin Backup Folder";
|
||||
|
||||
@Override
|
||||
protected String getRoot() throws Exception {
|
||||
return WorkSpaceManagement.getHome();
|
||||
protected FolderContainer getRoot() throws Exception {
|
||||
return WorkSpaceManagement.getInstance().getWorkspaceRoot();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createDirectory(String parent, String name) throws Exception {
|
||||
return WorkSpaceManagement.createFolder(parent, name, BACKUP_FOLDER_DESCRIPTION);
|
||||
protected FolderContainer createDirectory(FolderContainer parent, String name) throws Exception {
|
||||
return WorkSpaceManagement.getInstance().createFolder(parent, name, BACKUP_FOLDER_DESCRIPTION);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.gcube.accounting.aggregator.status.AggregationState;
|
|||
import org.gcube.accounting.aggregator.status.AggregationStatus;
|
||||
import org.gcube.accounting.aggregator.utility.Utility;
|
||||
import org.gcube.accounting.aggregator.workspace.WorkSpaceManagement;
|
||||
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -89,14 +90,14 @@ public class Persist {
|
|||
if(AggregationState.canContinue(aggregationStatus.getAggregationState(),AggregationState.ADDED)){
|
||||
Calendar now = Utility.getUTCCalendarInstance();
|
||||
WorkSpaceDirectoryStructure workspaceDirectoryStructure = new WorkSpaceDirectoryStructure();
|
||||
String targetFolder = workspaceDirectoryStructure.getTargetFolder(aggregationStatus.getAggregationInfo().getAggregationType(), aggregationStatus.getAggregationInfo().getAggregationStartDate());
|
||||
FolderContainer targetFolder = workspaceDirectoryStructure.getTargetFolder(aggregationStatus.getAggregationInfo().getAggregationType(), aggregationStatus.getAggregationInfo().getAggregationStartDate());
|
||||
|
||||
File malformedRecords = Utility.getMalformatedFile(aggregateRecordsBackupFile);
|
||||
if(malformedRecords.exists()){
|
||||
WorkSpaceManagement.zipAndBackupFiles(targetFolder,
|
||||
WorkSpaceManagement.getInstance().zipAndBackupFiles(targetFolder,
|
||||
originalRecordsbackupFile.getName().replace(Elaborator.ORIGINAL_SUFFIX, "-with-malformed"), originalRecordsbackupFile, aggregateRecordsBackupFile, malformedRecords);
|
||||
}else{
|
||||
WorkSpaceManagement.zipAndBackupFiles(targetFolder,
|
||||
WorkSpaceManagement.getInstance().zipAndBackupFiles(targetFolder,
|
||||
originalRecordsbackupFile.getName().replace(Elaborator.ORIGINAL_SUFFIX, ""), originalRecordsbackupFile, aggregateRecordsBackupFile);
|
||||
}
|
||||
|
||||
|
|
|
@ -163,6 +163,12 @@ public class Utility {
|
|||
return calendarToSanitize.getTime();
|
||||
}
|
||||
|
||||
public static Calendar getEndCalendarFromStartCalendar(AggregationType aggregationType, Calendar aggregationStartCalendar, int offset) {
|
||||
Calendar aggregationEndDate = getUTCCalendarInstance();
|
||||
aggregationEndDate.setTimeInMillis(aggregationStartCalendar.getTimeInMillis());
|
||||
aggregationEndDate.add(aggregationType.getCalendarField(), offset);
|
||||
return aggregationEndDate;
|
||||
}
|
||||
|
||||
public static Date getEndDateFromStartDate(AggregationType aggregationType, Date aggregationStartDate, int offset) {
|
||||
Calendar aggregationEndDate = getUTCCalendarInstance();
|
||||
|
|
|
@ -1,337 +0,0 @@
|
|||
package org.gcube.accounting.aggregator.workspace;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class HTTPCall {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(HTTPCall.class);
|
||||
|
||||
public static final String CONTENT_TYPE_APPLICATION_JSON_CHARSET_UTF_8 = "application/json;charset=UTF-8";
|
||||
public static final String CONTENT_TYPE_APPLICATION_XML_CHARSET_UTF_8 = "application/xml;charset=UTF-8";
|
||||
public static final String CONTENT_TYPE_TEXT_PLAIN = "text/plain;charset=UTF-8";
|
||||
|
||||
public enum HTTPMETHOD {
|
||||
HEAD, GET, POST, PUT, DELETE;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name();
|
||||
}
|
||||
}
|
||||
|
||||
public static final String PATH_SEPARATOR = "/";
|
||||
public static final String PARAM_STARTER = "?";
|
||||
public static final String PARAM_EQUALS = "=";
|
||||
public static final String PARAM_SEPARATOR = "&";
|
||||
public static final String UTF8 = "UTF-8";
|
||||
|
||||
protected final String address;
|
||||
protected final String userAgent;
|
||||
|
||||
public HTTPCall(String address, String userAgent) {
|
||||
this.address = address;
|
||||
this.userAgent = userAgent;
|
||||
}
|
||||
|
||||
protected String getParametersDataString(
|
||||
Map<String, String> parameters)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
if (parameters == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (String key : parameters.keySet()) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
result.append(PARAM_SEPARATOR);
|
||||
}
|
||||
|
||||
result.append(URLEncoder.encode(key, UTF8));
|
||||
result.append(PARAM_EQUALS);
|
||||
result.append(URLEncoder.encode(parameters.get(key), UTF8));
|
||||
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
protected URL getURL(String address, String path, String urlParameters) throws MalformedURLException {
|
||||
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
stringWriter.append(address);
|
||||
|
||||
if(address.endsWith(PATH_SEPARATOR)){
|
||||
if(path.startsWith(PATH_SEPARATOR)){
|
||||
path = path.substring(1);
|
||||
}
|
||||
}else{
|
||||
if(path.compareTo("")!=0 && !path.startsWith(PATH_SEPARATOR)){
|
||||
stringWriter.append(PATH_SEPARATOR);
|
||||
}
|
||||
}
|
||||
|
||||
stringWriter.append(path);
|
||||
|
||||
if(urlParameters!=null){
|
||||
stringWriter.append(PARAM_STARTER);
|
||||
stringWriter.append(urlParameters);
|
||||
}
|
||||
|
||||
return getURL(stringWriter.toString());
|
||||
}
|
||||
|
||||
|
||||
protected URL getURL(String urlString) throws MalformedURLException{
|
||||
URL url = new URL(urlString);
|
||||
if(url.getProtocol().compareTo("https")==0){
|
||||
url = new URL(url.getProtocol(), url.getHost(), url.getDefaultPort(), url.getFile());
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
protected HttpURLConnection getConnection(String path, String urlParameters, HTTPMETHOD method, String body, String contentType)
|
||||
throws Exception {
|
||||
URL url = getURL(address, path, urlParameters);
|
||||
return getConnection(url, method, body, contentType);
|
||||
}
|
||||
|
||||
protected HttpURLConnection getConnection(String path, String urlParameters, HTTPMETHOD method, InputStream inputStream, String contentType)
|
||||
throws Exception {
|
||||
URL url = getURL(address, path, urlParameters);
|
||||
return getConnection(url, method, inputStream, contentType);
|
||||
}
|
||||
|
||||
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method, InputStream inputStream, String contentType) throws Exception {
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
if (SecurityTokenProvider.instance.get() == null) {
|
||||
if (ScopeProvider.instance.get() == null) {
|
||||
throw new RuntimeException(
|
||||
"Null Token and Scope. Please set your token first.");
|
||||
}
|
||||
connection.setRequestProperty("gcube-scope",
|
||||
ScopeProvider.instance.get());
|
||||
} else {
|
||||
connection.setRequestProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY,
|
||||
SecurityTokenProvider.instance.get());
|
||||
}
|
||||
|
||||
connection.setDoOutput(true);
|
||||
|
||||
connection.setRequestProperty("Content-type", contentType);
|
||||
connection.setRequestProperty("User-Agent", userAgent);
|
||||
|
||||
connection.setRequestMethod(method.toString());
|
||||
|
||||
|
||||
if (inputStream != null
|
||||
&& (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
|
||||
|
||||
DataOutputStream wr = new DataOutputStream(
|
||||
connection.getOutputStream());
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
int len;
|
||||
while ((len = inputStream.read(buffer)) > 0) {
|
||||
wr.write(buffer, 0, len);
|
||||
}
|
||||
wr.flush();
|
||||
wr.close();
|
||||
}
|
||||
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
logger.trace("{} {} : {} - {}",
|
||||
method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
|
||||
responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
|
||||
responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
|
||||
URL redirectURL = getURL(connection.getHeaderField("Location"));
|
||||
|
||||
logger.trace("{} is going to be redirect to {}", url.toString(), redirectURL.toString());
|
||||
|
||||
connection = getConnection(redirectURL, method, inputStream, contentType);
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
||||
protected HttpURLConnection getConnection(URL url, HTTPMETHOD method, String body, String contentType) throws Exception {
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
if (SecurityTokenProvider.instance.get() == null) {
|
||||
if (ScopeProvider.instance.get() == null) {
|
||||
throw new RuntimeException(
|
||||
"Null Token and Scope. Please set your token first.");
|
||||
}
|
||||
connection.setRequestProperty("gcube-scope",
|
||||
ScopeProvider.instance.get());
|
||||
} else {
|
||||
connection.setRequestProperty(org.gcube.common.authorization.client.Constants.TOKEN_HEADER_ENTRY,
|
||||
SecurityTokenProvider.instance.get());
|
||||
}
|
||||
|
||||
connection.setDoOutput(true);
|
||||
|
||||
connection.setRequestProperty("Content-type", contentType);
|
||||
connection.setRequestProperty("User-Agent", userAgent);
|
||||
|
||||
connection.setRequestMethod(method.toString());
|
||||
|
||||
|
||||
if (body != null
|
||||
&& (method == HTTPMETHOD.POST || method == HTTPMETHOD.PUT)) {
|
||||
|
||||
DataOutputStream wr = new DataOutputStream(
|
||||
connection.getOutputStream());
|
||||
wr.writeBytes(body);
|
||||
wr.flush();
|
||||
wr.close();
|
||||
}
|
||||
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
logger.trace("{} {} : {} - {}",
|
||||
method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
|
||||
responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
|
||||
responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
|
||||
|
||||
URL redirectURL = getURL(connection.getHeaderField("Location"));
|
||||
|
||||
logger.trace("{} is going to be redirect to {}", url.toString(), redirectURL.toString());
|
||||
|
||||
connection = getConnection(redirectURL, method, body, contentType);
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
protected StringBuilder getStringBuilder(InputStream inputStream) throws IOException{
|
||||
StringBuilder result = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
result.append(line);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void call(String path, HTTPMETHOD method, Map<String, String> parameters, String contentType) throws Exception {
|
||||
call(path, method, parameters, null, contentType);
|
||||
}
|
||||
|
||||
public void call(String path, HTTPMETHOD method, String body, String contentType) throws Exception {
|
||||
call(path, method, null, body, contentType);
|
||||
}
|
||||
|
||||
protected void call(String path, HTTPMETHOD method, Map<String, String> parameters, String body, String contentType) throws Exception {
|
||||
|
||||
String urlParameters = getParametersDataString(parameters);
|
||||
|
||||
HttpURLConnection connection = getConnection(path, urlParameters, method, body, contentType);
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
|
||||
logger.info("{} {} : {} - {}",
|
||||
method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if(method == HTTPMETHOD.HEAD){
|
||||
if(responseCode == HttpURLConnection.HTTP_NO_CONTENT){
|
||||
throw new Exception(responseMessage);
|
||||
}
|
||||
if(responseCode == HttpURLConnection.HTTP_NOT_FOUND){
|
||||
throw new Exception(responseMessage);
|
||||
}
|
||||
if(responseCode == HttpURLConnection.HTTP_FORBIDDEN){
|
||||
throw new Exception(responseMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
|
||||
InputStream inputStream = connection.getErrorStream();
|
||||
StringBuilder result = getStringBuilder(inputStream);
|
||||
String res = result.toString();
|
||||
throw new Exception(res);
|
||||
}
|
||||
|
||||
StringBuilder result = getStringBuilder(connection.getInputStream());
|
||||
String res = result.toString();
|
||||
logger.trace("Server returned content : {}", res);
|
||||
|
||||
connection.disconnect();
|
||||
|
||||
}
|
||||
|
||||
public void call(String path, HTTPMETHOD method, InputStream inputStream, Map<String, String> parameters, String contentType) throws Exception {
|
||||
String urlParameters = getParametersDataString(parameters);
|
||||
|
||||
HttpURLConnection connection = getConnection(path, urlParameters, method, inputStream, contentType);
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
String responseMessage = connection.getResponseMessage();
|
||||
|
||||
logger.info("{} {} : {} - {}",
|
||||
method, connection.getURL(), responseCode, responseMessage);
|
||||
|
||||
if(method == HTTPMETHOD.HEAD){
|
||||
if(responseCode == HttpURLConnection.HTTP_NO_CONTENT){
|
||||
throw new Exception(responseMessage);
|
||||
}
|
||||
if(responseCode == HttpURLConnection.HTTP_NOT_FOUND){
|
||||
throw new Exception(responseMessage);
|
||||
}
|
||||
if(responseCode == HttpURLConnection.HTTP_FORBIDDEN){
|
||||
throw new Exception(responseMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
|
||||
InputStream errorStream = connection.getErrorStream();
|
||||
StringBuilder result = getStringBuilder(errorStream);
|
||||
String res = result.toString();
|
||||
throw new Exception(res);
|
||||
}
|
||||
|
||||
StringBuilder result = getStringBuilder(connection.getInputStream());
|
||||
String res = result.toString();
|
||||
logger.trace("Server returned content : {}", res);
|
||||
|
||||
connection.disconnect();
|
||||
}
|
||||
|
||||
}
|
|
@ -4,26 +4,26 @@ import java.io.File;
|
|||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.gcube.accounting.aggregator.plugin.AccountingAggregatorPluginDeclaration;
|
||||
import org.gcube.accounting.aggregator.utility.Utility;
|
||||
import org.gcube.accounting.aggregator.workspace.HTTPCall.HTTPMETHOD;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint;
|
||||
import org.gcube.common.resources.gcore.utils.Group;
|
||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||
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.User;
|
||||
import org.gcube.common.homelibrary.home.workspace.Workspace;
|
||||
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
||||
import org.gcube.common.storagehub.client.dsl.ItemContainer;
|
||||
import org.gcube.common.storagehub.client.dsl.ListResolverTyped;
|
||||
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
||||
import org.gcube.common.storagehub.model.items.Item;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Alessandro Pieve (ISTI - CNR)
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class WorkSpaceManagement {
|
||||
|
@ -34,47 +34,17 @@ public class WorkSpaceManagement {
|
|||
private static final String ZIP_FILE_DESCRIPTION = "Backup of original records deleted and aggregtaed records inserted.";
|
||||
private static final String ZIP_MIMETYPE = "application/zip, application/octet-stream";
|
||||
|
||||
protected static final GCoreEndpoint gCoreEndpoint;
|
||||
protected static final Map<String, String> restEndpointMap;
|
||||
protected StorageHubClient storageHubClient;
|
||||
|
||||
protected static final String CLASS_FORMAT = "$resource/Profile/ServiceClass/text() eq '%1s'";
|
||||
protected static final String NAME_FORMAT = "$resource/Profile/ServiceName/text() eq '%1s'";
|
||||
protected static final String STATUS_FORMAT = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
|
||||
|
||||
protected static final String SERVICE_CLASS = "DataAccess";
|
||||
protected static final String SERVICE_NAME = "HomeLibraryWebapp";
|
||||
|
||||
public static final String USER_AGENT = AccountingAggregatorPluginDeclaration.NAME;
|
||||
|
||||
protected static SimpleQuery queryForHomeLibraryGCoreEndpoint(){
|
||||
return ICFactory.queryFor(GCoreEndpoint.class)
|
||||
.addCondition(String.format(CLASS_FORMAT, SERVICE_CLASS))
|
||||
.addCondition(String.format(NAME_FORMAT, SERVICE_NAME))
|
||||
.addCondition(String.format(STATUS_FORMAT))
|
||||
.setResult("$resource");
|
||||
public static WorkSpaceManagement getInstance() {
|
||||
return new WorkSpaceManagement();
|
||||
}
|
||||
|
||||
protected static GCoreEndpoint getHomeLibraryGCoreEndpoint(){
|
||||
SimpleQuery query = queryForHomeLibraryGCoreEndpoint();
|
||||
DiscoveryClient<GCoreEndpoint> client = ICFactory.clientFor(GCoreEndpoint.class);
|
||||
List<GCoreEndpoint> gCoreEndpoints = client.submit(query);
|
||||
return gCoreEndpoints.get(0);
|
||||
protected WorkSpaceManagement() {
|
||||
storageHubClient = new StorageHubClient();
|
||||
}
|
||||
|
||||
static {
|
||||
gCoreEndpoint = getHomeLibraryGCoreEndpoint();
|
||||
Group<Endpoint> endpoints = gCoreEndpoint.profile().endpoints();
|
||||
restEndpointMap = new HashMap<>();
|
||||
for(Endpoint endpoint : endpoints){
|
||||
String endpointName = endpoint.name();
|
||||
String endpointURI = endpoint.uri().toString();
|
||||
if(endpointURI.contains("rest")){
|
||||
restEndpointMap.put(endpointName, endpointURI);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void addToZipFile(ZipOutputStream zos, File file) throws Exception {
|
||||
public void addToZipFile(ZipOutputStream zos, File file) throws Exception {
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
|
@ -90,12 +60,12 @@ public class WorkSpaceManagement {
|
|||
in.close();
|
||||
}
|
||||
|
||||
private static String getZipFileName(String name) throws Exception {
|
||||
private String getZipFileName(String name) throws Exception {
|
||||
String zipFileName = String.format("%s%s", name, ZIP_SUFFIX);
|
||||
return zipFileName;
|
||||
}
|
||||
|
||||
public static boolean zipAndBackupFiles(String targetFolder, String name, File... files) throws Exception {
|
||||
public boolean zipAndBackupFiles(FolderContainer targetFolder, String name, File... files) throws Exception {
|
||||
|
||||
try {
|
||||
String zipFileName = getZipFileName(name);
|
||||
|
@ -116,7 +86,7 @@ public class WorkSpaceManagement {
|
|||
|
||||
FileInputStream zipFileStream = new FileInputStream(zipFile);
|
||||
|
||||
WorkSpaceManagement.uploadFile(zipFileStream, zipFileName, ZIP_FILE_DESCRIPTION,
|
||||
WorkSpaceManagement.getInstance().uploadFile(zipFileStream, zipFileName, ZIP_FILE_DESCRIPTION,
|
||||
ZIP_MIMETYPE, targetFolder);
|
||||
|
||||
zipFile.delete();
|
||||
|
@ -128,13 +98,55 @@ public class WorkSpaceManagement {
|
|||
}
|
||||
}
|
||||
|
||||
public static String getHome() throws Exception {
|
||||
public FolderContainer getWorkspaceRoot() throws Exception {
|
||||
try {
|
||||
return storageHubClient.getWSRoot();
|
||||
} catch(Exception e) {
|
||||
String username = Utility.getUsername();
|
||||
return String.format("/Home/%s/Workspace", username);
|
||||
logger.info("Unable to obtain the Workspace Root for {}. Going to create it.", username);
|
||||
try {
|
||||
HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory();
|
||||
HomeManager manager = factory.getHomeManager();
|
||||
User user = manager.createUser(username);
|
||||
@SuppressWarnings("deprecation")
|
||||
Home home = manager.getHome(user);
|
||||
Workspace ws = home.getWorkspace();
|
||||
ws.getRoot();
|
||||
return storageHubClient.getWSRoot();
|
||||
} catch(Exception ex) {
|
||||
logger.info("Unable to create the Workspace Root for {}.", username);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String createFolder(String parentPath, String folderName, String folderDescription)
|
||||
protected FolderContainer getOrCreateFolder(FolderContainer parent, String name, String description, boolean hidden)
|
||||
throws Exception {
|
||||
FolderContainer destinationFolder = null;
|
||||
ListResolverTyped listResolverTyped = parent.list();
|
||||
List<ItemContainer<? extends Item>> containers = listResolverTyped.includeHidden().getContainers();
|
||||
for(ItemContainer<? extends Item> itemContainer : containers) {
|
||||
if(itemContainer instanceof FolderContainer) {
|
||||
if(itemContainer.get().getName().compareTo(name) == 0) {
|
||||
destinationFolder = (FolderContainer) itemContainer;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(destinationFolder == null) {
|
||||
if(hidden) {
|
||||
destinationFolder = parent.newHiddenFolder(name, description);
|
||||
} else {
|
||||
destinationFolder = parent.newFolder(name, description);
|
||||
}
|
||||
}
|
||||
return destinationFolder;
|
||||
}
|
||||
|
||||
public FolderContainer createFolder(FolderContainer parentPath, String folderName, String folderDescription)
|
||||
throws Exception {
|
||||
// TODO
|
||||
|
||||
/*
|
||||
try {
|
||||
HTTPCall httpCall = new HTTPCall(restEndpointMap.get("CreateFolder"), USER_AGENT);
|
||||
Map<String, String> parameters = new HashMap<>();
|
||||
|
@ -147,13 +159,19 @@ public class WorkSpaceManagement {
|
|||
logger.error("Error while creating folder ", e);
|
||||
throw e;
|
||||
}
|
||||
*/
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void uploadFile(InputStream inputStream, String name, String description, String mimeType,
|
||||
String parentPath) throws Exception {
|
||||
public void uploadFile(InputStream inputStream, String name, String description, String mimeType,
|
||||
FolderContainer parentPath) throws Exception {
|
||||
try {
|
||||
logger.trace("Going to upload file on WorkSpace name:{}, description:{}, mimetype:{}, parentPath:{}", name,
|
||||
description, mimeType, parentPath);
|
||||
// TODO
|
||||
|
||||
/*
|
||||
HTTPCall httpCall = new HTTPCall(restEndpointMap.get("Upload"), USER_AGENT);
|
||||
Map<String, String> parameters = new HashMap<>();
|
||||
parameters.put("name", name);
|
||||
|
@ -161,6 +179,7 @@ public class WorkSpaceManagement {
|
|||
parameters.put("parentPath", parentPath);
|
||||
|
||||
httpCall.call("", HTTPMETHOD.POST, inputStream, parameters, HTTPCall.CONTENT_TYPE_TEXT_PLAIN);
|
||||
*/
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error while uploading file on WorkSpace", e);
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.gcube.accounting.aggregator.plugin.ContextTest;
|
|||
import org.gcube.accounting.aggregator.utility.Constant;
|
||||
import org.gcube.accounting.aggregator.utility.Utility;
|
||||
import org.gcube.accounting.aggregator.workspace.WorkSpaceManagement;
|
||||
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -25,10 +26,10 @@ public class WorkSpaceDirectoryStructureTest extends ContextTest {
|
|||
Date date = Utility.getAggregationStartCalendar(2015, Calendar.JANUARY, 1).getTime();
|
||||
|
||||
WorkSpaceDirectoryStructure workSpaceDirectoryStructure = new WorkSpaceDirectoryStructure();
|
||||
String targetFolder = workSpaceDirectoryStructure.getTargetFolder(aggregationType, date);
|
||||
FolderContainer targetFolder = workSpaceDirectoryStructure.getTargetFolder(aggregationType, date);
|
||||
|
||||
File file = new File(Constant.ROOT_DIRECTORY, "aux.txt");
|
||||
|
||||
WorkSpaceManagement.zipAndBackupFiles(targetFolder, "Test", file);
|
||||
WorkSpaceManagement.getInstance().zipAndBackupFiles(targetFolder, "Test", file);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ public class AccountingAggregatorPluginTest extends ContextTest {
|
|||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountingAggregatorPluginTest.class);
|
||||
|
||||
private static final String ROOT_PROD = "/d4science.research-infrastructures.eu";
|
||||
// private static final String ROOT_PROD = "/d4science.research-infrastructures.eu";
|
||||
|
||||
@Test
|
||||
public void aggregate() throws Exception {
|
||||
|
@ -44,7 +44,7 @@ public class AccountingAggregatorPluginTest extends ContextTest {
|
|||
inputs.put(AccountingAggregatorPlugin.FORCE_RERUN, false);
|
||||
inputs.put(AccountingAggregatorPlugin.FORCE_RESTART, true);
|
||||
|
||||
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2016, Calendar.OCTOBER, 1);
|
||||
Calendar aggregationStartCalendar = Utility.getAggregationStartCalendar(2017, Calendar.JUNE, 1);
|
||||
String aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(aggregationStartCalendar.getTime());
|
||||
logger.trace("{} : {}", AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
|
||||
inputs.put(AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
|
||||
|
@ -59,10 +59,14 @@ public class AccountingAggregatorPluginTest extends ContextTest {
|
|||
AccountingAggregatorPlugin plugin = new AccountingAggregatorPlugin(null);
|
||||
logger.debug("Going to launch {} with inputs {}", AccountingAggregatorPluginDeclaration.NAME, inputs);
|
||||
|
||||
while(aggregationStartCalendar.get(Calendar.YEAR)!=2018) {
|
||||
aggregationStartCalendar.add(Calendar.MONTH, 1);
|
||||
inputs.put(AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
|
||||
// Calendar end = Utility.getEndCalendarFromStartCalendar(AggregationType.MONTHLY, aggregationStartCalendar, 1);
|
||||
Calendar end = Utility.getAggregationStartCalendar(2018, Calendar.JANUARY, 1);
|
||||
|
||||
while(aggregationStartCalendar.before(end)) {
|
||||
plugin.launch(inputs);
|
||||
aggregationStartCalendar.add(Calendar.MONTH, 1);
|
||||
aggregationStartDate = AccountingAggregatorPlugin.AGGREGATION_START_DATE_DATE_FORMAT.format(aggregationStartCalendar.getTime());
|
||||
inputs.put(AccountingAggregatorPlugin.AGGREGATION_START_DATE_INPUT_PARAMETER, aggregationStartDate);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
package org.gcube.accounting.aggregator.workspace;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.accounting.aggregator.plugin.ContextTest;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint.Profile;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint;
|
||||
import org.gcube.common.resources.gcore.utils.Group;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class WorkSpaceManagementTest extends ContextTest {
|
||||
|
||||
public static Logger logger = LoggerFactory.getLogger(WorkSpaceManagementTest.class);
|
||||
|
||||
@Test
|
||||
public void endpointTest(){
|
||||
Profile profile = WorkSpaceManagement.gCoreEndpoint.profile();
|
||||
Group<Endpoint> endpoints = profile.endpoints();
|
||||
Map<String, String> restEndpointMap = new HashMap<>();
|
||||
for(Endpoint endpoint : endpoints){
|
||||
String endpointName = endpoint.name();
|
||||
String endpointURI = endpoint.uri().toString();
|
||||
if(endpointURI.contains("rest")){
|
||||
restEndpointMap.put(endpointName, endpointURI);
|
||||
}
|
||||
}
|
||||
logger.debug("{}", restEndpointMap);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue