Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GSoC] Integrate NextCloud for Apache Airavata's Data Transfer Use Cases #5

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,21 @@
<artifactId>vecmath</artifactId>
<version>1.6.0-scijava-2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>com.github.lookfirst</groupId>
<artifactId>sardine</artifactId>
<version>5.8</version>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.seagrid.desktop.connectors.NextcloudStorage.Exception;

public class NextcloudApiException extends RuntimeException {
private static final long serialVersionUID = 8088239559973590632L;

public NextcloudApiException(Throwable cause) {
super(cause);
}

public NextcloudApiException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.seagrid.desktop.connectors.NextcloudStorage;

import com.github.sardine.DavResource;
import com.jcraft.jsch.SftpException;
import org.seagrid.desktop.connectors.NextcloudStorage.Exception.NextcloudApiException;
import org.seagrid.desktop.util.SEAGridContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.LinkedList;
import java.util.List;

public class NextCloudFolderdownloadtask extends NextcloudFileTask {

private final static Logger logger = LoggerFactory.getLogger(NextCloudFolderdownloadtask.class);

private String rootpath;
private String remoteDirPath, localDirPath;
private double totalBytesRead = 0, totalSize = 0;

public NextCloudFolderdownloadtask(String remoteDirPath, String localDirPath) throws IOException {
super();
rootpath = (isusehttps ? "https" : "http") +"://"+servername+"/"+basepath+ "/" + SEAGridContext.getInstance().getUserName();
this.remoteDirPath = remoteDirPath;
this.localDirPath = localDirPath;
}

@Override
protected Boolean call() throws Exception {
calculateNextcloudTotalSize(remoteDirPath);
boolean status = downloadFolder(remoteDirPath, localDirPath);
return status;
}

/**
* Downloads the folder at the specified remotepath to the rootdownloadirpath
*
* @param remotepath the path in the nextcloud server with respect to the specific folder
* @param rootdownloadirpath the local path in the system where the folder needs be saved
* @return
* @throws IOException
*/

public boolean downloadFolder(String remotepath, String rootdownloadirpath) throws IOException {
int depth=1;
String newdownloadir = rootdownloadirpath;
File localDir = new File(newdownloadir);

if(!localDir.exists()){
localDir.mkdirs();
}

String rootpathnew = rootpath + remotepath ;

int count = 0;
String filepath;

List<String> retVal= new LinkedList<>();
List<DavResource> resources;
try {
resources = sardine.list(rootpathnew, depth);
} catch (IOException e) {
throw new NextcloudApiException(e);
}

for (DavResource res : resources)
{
if(count != 0) {
if(res.equals(".") || res.equals("..")){
continue;
}

else if(res.isDirectory()) {
String subFoldername = res.getName();
String downloadDirtosend = newdownloadir + "/" + subFoldername;
String pathToSend = remotepath + "/" + subFoldername;
downloadFolder(pathToSend,downloadDirtosend);
}

else {
String filename = res.getName();
filepath = rootpathnew + "/" + filename;
retVal.add(res.getName());
InputStream in = null;
if (sardine.exists(filepath)) {
in = sardine.get(filepath);
byte[] buffer = new byte[in.available()];
File targetFile = new File(newdownloadir + "/" + filename);
OutputStream outStream = new FileOutputStream(targetFile);
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
outStream.write(buffer);
totalBytesRead += bytesRead;
updateMessage("Downloaded " + totalBytesRead + " bytes");
updateProgress(totalBytesRead, totalSize);
}
in.close();
outStream.close();
}
}
}
count ++;
}
return true;
}

private void calculateNextcloudTotalSize(String remoteDirPath) throws SftpException, IOException {
List<DavResource> davResource = listDirectories(remoteDirPath);
int count = 0;
for (DavResource res : davResource) {
if(count != 0) {
if (res.getName().equals(".") || res.getName().equals("..")) {
continue;
}
if (res.isDirectory()) {
String tempRemoteDir = remoteDirPath + "/" + res.getName();
calculateNextcloudTotalSize(tempRemoteDir);
} else {
totalSize += res.getContentLength().intValue();
}
}
count++;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.seagrid.desktop.connectors.NextcloudStorage;

import org.seagrid.desktop.connectors.NextcloudStorage.Exception.NextcloudApiException;
import org.seagrid.desktop.util.SEAGridContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class NextcloudFileDownloadTask extends NextcloudFileTask {

private final static Logger logger = LoggerFactory.getLogger(NextcloudFileDownloadTask.class);

private String remoteFilePath, localFilePath;
private String remoterootpath;

public NextcloudFileDownloadTask(String remoteFilePath, String localFilePath) throws IOException {
super();
this.remoteFilePath = remoteFilePath;
this.localFilePath = localFilePath;
remoterootpath = (isusehttps ? "https" : "http") + "://" + servername + "/" + basepath + "/" + SEAGridContext.getInstance().getUserName();
}

@Override
protected Boolean call() throws Exception {
return downloadFile(localFilePath, remoteFilePath);
}

public boolean downloadFile(String sourceFile, String destFile) {
String path = remoterootpath + destFile;
File downloadFilepath = new File(sourceFile);
if(!downloadFilepath.getParentFile().exists()) {
downloadFilepath.getParentFile().mkdirs();
}
try {
InputStream in = sardine.get(path);
byte[] buffer = new byte[in.available()];
in.read(buffer);
File targetFile = new File(sourceFile);
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
outStream.close();
in.close();
return true;
} catch (IOException e) {
throw new NextcloudApiException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.seagrid.desktop.connectors.NextcloudStorage;

import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import com.github.sardine.SardineFactory;
import javafx.concurrent.Task;
import org.seagrid.desktop.connectors.NextcloudStorage.Exception.NextcloudApiException;
import org.seagrid.desktop.util.SEAGridContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public abstract class NextcloudFileTask extends Task<Boolean> {
private final static Logger logger = LoggerFactory.getLogger(org.seagrid.desktop.connectors.NextcloudStorage.NextcloudFileTask.class);

protected Sardine sardine = SardineFactory.begin();

protected String servername;
protected String basepath;
protected boolean isusehttps;
private String token;
private String rootremotepath;

public NextcloudFileTask() throws IOException {
servername = SEAGridContext.getInstance().getNextcloudServername();
basepath = SEAGridContext.getInstance().getDavBasepath();
isusehttps = SEAGridContext.getInstance().isUseHttps();
rootremotepath = (isusehttps ? "https" : "http") + "://" + servername + "/" + basepath + "/" + SEAGridContext.getInstance().getUserName();
token = SEAGridContext.getInstance().getOAuthToken();
sardine.setCredentials(SEAGridContext.getInstance().getUserName(), token);
sardine.enablePreemptiveAuthentication(SEAGridContext.getInstance().getNextcloudServername());
}

/**
* Create the folder at the specified path
* @param remotepath
*/
public void createFolder(String remotepath){
String path= rootremotepath+remotepath;

try {
sardine.createDirectory(path);
} catch (IOException e) {
throw new NextcloudApiException(e);
}
}

public List<DavResource> listDirectories(String remotepath) throws IOException {
String path = rootremotepath + remotepath;
List<DavResource> resources;
if(sardine.exists(path)) {
try {
resources = sardine.list(path, 1);
return resources;
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.seagrid.desktop.connectors.NextcloudStorage;

import org.seagrid.desktop.connectors.NextcloudStorage.Exception.NextcloudApiException;
import org.seagrid.desktop.util.SEAGridContext;

import java.io.*;
import java.util.Map;

public class NextcloudFileUploadTask extends NextcloudFileTask {

private String remoterootpath;
private Map<String, File> uploadFiles;
/**
* Constructor
*/
public NextcloudFileUploadTask(Map<String, File> uploadFiles) throws IOException {
super();
this.uploadFiles = uploadFiles;
remoterootpath = (isusehttps ? "https" : "http") + "://" + servername + "/" + basepath + "/" + SEAGridContext.getInstance().getUserName();
}

@Override
protected Boolean call() throws Exception {
return uploadToNextcloud();
}

public boolean uploadToNextcloud() throws IOException {
for(String remoteFilePath : uploadFiles.keySet()){
int numberOfFiles = uploadFiles.size();
int index = 1;
remoteFilePath = remoteFilePath.replace("\\","/");
File localFile = uploadFiles.get(remoteFilePath);
String localpath = localFile.getPath();
String remotepath = remoteFilePath;
boolean status = false;
InputStream inputStream = new FileInputStream(localpath);
String path;
try {
//createpath if not exists
String[] segments = remotepath.split("/");
String appendpath="";
for(int i = 1; i < segments.length - 1 ; i++)
{
appendpath = appendpath + "/" + segments[i];
if(!sardine.exists(remoterootpath + "/" + appendpath)) {
createFolder(appendpath);
}
}
path = remoterootpath + remotepath;
long fileSize = localFile.length();
byte[] buffer = new byte[inputStream.available()];
int bytesRead = -1;
int totalBytesRead = 0;
double percentCompleted = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
totalBytesRead += bytesRead;
sardine.put(path, buffer);
percentCompleted = ((double)totalBytesRead) / fileSize * index / numberOfFiles;
updateMessage("Uploaded " + totalBytesRead + " bytes");
updateProgress(percentCompleted, 1);
}
status = true;
} catch (IOException e) {
throw new NextcloudApiException(e);
} finally {
inputStream.close();
return status;
}
}
return true;
}

}
Loading