use of org.ow2.proactive.resourcemanager.authentication.Client in project scheduling by ow2-proactive.
the class DataSpaceClient method upload.
@Override
public boolean upload(final ILocalSource source, final IRemoteDestination destination) throws NotConnectedException, PermissionException {
if (log.isDebugEnabled()) {
log.debug("Uploading from " + source + " to " + destination);
}
StringBuffer uriTmpl = (new StringBuffer()).append(restDataspaceUrl).append(destination.getDataspace().value());
ResteasyClient client = new ResteasyClientBuilder().providerFactory(providerFactory).httpEngine(httpEngine).build();
ResteasyWebTarget target = client.target(uriTmpl.toString()).path(destination.getPath());
Response response = null;
try {
response = target.request().header("sessionid", sessionId).put(Entity.entity(new StreamingOutput() {
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
source.writeTo(outputStream);
}
}, new Variant(MediaType.APPLICATION_OCTET_STREAM_TYPE, (Locale) null, source.getEncoding())));
if (response.getStatus() != HttpURLConnection.HTTP_CREATED) {
if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw new NotConnectedException("User not authenticated or session timeout.");
} else {
throw new RuntimeException("File upload failed. Status code:" + response.getStatus());
}
}
if (log.isDebugEnabled()) {
log.debug("Upload from " + source + " to " + destination + " performed with success");
}
return true;
} catch (IOException ioe) {
throw Throwables.propagate(ioe);
} finally {
if (response != null) {
response.close();
}
}
}
use of org.ow2.proactive.resourcemanager.authentication.Client in project scheduling by ow2-proactive.
the class DataTransferTest method testDownloadFile.
@Test
public void testDownloadFile() throws Exception {
String srcDirPath = URI.create(getScheduler().getUserSpaceURIs().get(0)).getPath();
File srcFile = new File(srcDirPath, TEMP_FILE_TMP_NAME);
if (srcFile.exists()) {
assertTrue(srcFile.delete());
}
Files.write(randomFileContents(), srcFile);
File tmpFile = tmpDir.newFile(TEMP_FILE_TMP_NAME);
if (tmpFile.exists()) {
assertTrue(tmpFile.delete());
}
// use standard client
IDataSpaceClient client = clientInstance();
RemoteSource source = new RemoteSource(USER, TEMP_FILE_TMP_NAME);
LocalDestination dest = new LocalDestination(tmpFile);
assertTrue(client.download(source, dest));
assertTrue(Files.equal(srcFile, tmpFile));
// use RemoteSpace API
FileUtils.deleteQuietly(tmpFile);
File downloadedFile = client.getUserSpace().pullFile(TEMP_FILE_TMP_NAME, tmpFile);
assertTrue(Files.equal(srcFile, downloadedFile));
}
use of org.ow2.proactive.resourcemanager.authentication.Client in project scheduling by ow2-proactive.
the class DataTransferTest method testUploadSingleFile.
@Test
public void testUploadSingleFile() throws Exception {
String testFolderName = "testUploadSingleFile";
System.out.println(testFolderName);
File tmpFile = tmpDir.newFile(TEMP_FILE_TMP_NAME);
Files.write(randomFileContents(), tmpFile);
// use standard client
IDataSpaceClient client = clientInstance();
LocalFileSource source = new LocalFileSource(tmpFile);
RemoteDestination dest = new RemoteDestination(USER, testFolderName + "/" + TEMP_FILE_TMP_NAME);
assertTrue(client.upload(source, dest));
String destDirPath = URI.create(getScheduler().getUserSpaceURIs().get(0)).getPath();
File destFile = new File(destDirPath, testFolderName + "/" + TEMP_FILE_TMP_NAME);
assertTrue(Files.equal(tmpFile, destFile));
// use RemoteSpace API
FileUtils.deleteQuietly(destFile);
client.getUserSpace().pushFile(tmpFile, testFolderName + "/" + TEMP_FILE_TMP_NAME);
assertTrue(Files.equal(tmpFile, destFile));
}
use of org.ow2.proactive.resourcemanager.authentication.Client in project scheduling by ow2-proactive.
the class DataTransferTest method testDeleteFile.
@Test
public void testDeleteFile() throws Exception {
URI srcDirPath = URI.create(getScheduler().getUserSpaceURIs().get(0));
File srcFile = new File(new File(srcDirPath), TEMP_FILE_TMP_NAME);
if (srcFile.exists()) {
assertTrue(srcFile.delete());
}
Files.write(randomFileContents(), srcFile);
// use standard client
IDataSpaceClient client = clientInstance();
RemoteSource source = new RemoteSource(USER, TEMP_FILE_TMP_NAME);
assertTrue(client.delete(source));
assertFalse(srcFile.exists());
// use RemoteSpace API
Files.write(randomFileContents(), srcFile);
client.getUserSpace().deleteFile(TEMP_FILE_TMP_NAME);
assertFalse(srcFile.exists());
}
use of org.ow2.proactive.resourcemanager.authentication.Client in project scheduling by ow2-proactive.
the class DataTransferTest method testUploadSelectedFilesUsingFilenames.
@Test
public void testUploadSelectedFilesUsingFilenames() throws Exception {
String testFolderName = "testUploadSelectedFilesUsingFilenames";
System.out.println(testFolderName);
TestFilesToUploadCreator testFiles = new TestFilesToUploadCreator().invoke();
File tempTextFile = testFiles.getTempTextFile();
File tempFile = testFiles.getTempFile();
// use standard client
IDataSpaceClient client = clientInstance();
LocalDirSource source = new LocalDirSource(tmpDir.getRoot());
source.setIncludes("**/" + TEMP_FILE_TMP_NAME);
RemoteDestination dest = new RemoteDestination(USER, testFolderName);
assertTrue(client.upload(source, dest));
String destRootUri = URI.create(getScheduler().getUserSpaceURIs().get(0)).getPath();
File[] destRootFiles = new File(destRootUri, testFolderName).listFiles();
System.out.println("Files in destination: " + Arrays.asList(destRootFiles));
assertEquals(1, destRootFiles.length);
assertTrue(destRootFiles[0].isDirectory());
assertTrue(Files.equal(tempFile, new File(destRootFiles[0], TEMP_FILE_TMP_NAME)));
// use RemoteSpace API
FileUtils.deleteDirectory(new File(destRootUri, testFolderName));
client.getUserSpace().pushFiles(tmpDir.getRoot(), "**/" + TEMP_FILE_TMP_NAME, testFolderName);
destRootFiles = new File(destRootUri, testFolderName).listFiles();
assertEquals(1, destRootFiles.length);
assertTrue(destRootFiles[0].isDirectory());
assertTrue(Files.equal(tempFile, new File(destRootFiles[0], TEMP_FILE_TMP_NAME)));
}
Aggregations