use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.
the class SchedulerStateRest method pushFile.
/**
* Pushes a file from the local file system into the given DataSpace
*
* @param sessionId
* a valid session id
* @param spaceName
* the name of the DataSpace
* @param filePath
* the path inside the DataSpace where to put the file e.g.
* "/myfolder"
* @param multipart
* the form data containing : - fileName the name of the file
* that will be created on the DataSpace - fileContent the
* content of the file
* @return true if the transfer succeeded
* @see org.ow2.proactive.scheduler.common.SchedulerConstants for spaces
* names
*/
@Override
public boolean pushFile(@HeaderParam("sessionid") String sessionId, @PathParam("spaceName") String spaceName, @PathParam("filePath") String filePath, MultipartFormDataInput multipart) throws IOException, NotConnectedRestException, PermissionRestException {
checkAccess(sessionId, "pushFile");
Session session = dataspaceRestApi.checkSessionValidity(sessionId);
Map<String, List<InputPart>> formDataMap = multipart.getFormDataMap();
List<InputPart> fNL = formDataMap.get("fileName");
if ((fNL == null) || (fNL.size() == 0)) {
throw new IllegalArgumentException("Illegal multipart argument definition (fileName), received " + fNL);
}
String fileName = fNL.get(0).getBody(String.class, null);
List<InputPart> fCL = formDataMap.get("fileContent");
if ((fCL == null) || (fCL.size() == 0)) {
throw new IllegalArgumentException("Illegal multipart argument definition (fileContent), received " + fCL);
}
InputStream fileContent = fCL.get(0).getBody(InputStream.class, null);
if (fileName == null) {
throw new IllegalArgumentException("Wrong file name : " + fileName);
}
filePath = normalizeFilePath(filePath, fileName);
FileObject destfo = dataspaceRestApi.resolveFile(session, spaceName, filePath);
URL targetUrl = destfo.getURL();
logger.info("[pushFile] pushing file to " + targetUrl);
if (!destfo.isWriteable()) {
RuntimeException ex = new IllegalArgumentException("File " + filePath + " is not writable in space " + spaceName);
logger.error(ex);
throw ex;
}
if (destfo.exists()) {
destfo.delete();
}
// used to create the necessary directories if needed
destfo.createFile();
dataspaceRestApi.writeFile(fileContent, destfo, null);
return true;
}
use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.
the class SchedulerStateRest method deleteFile.
/**
* Deletes a file or recursively delete a directory from the given DataSpace
*
* @param sessionId
* a valid session id
* @param spaceName
* the name of the data space involved (GLOBAL or USER)
* @param filePath
* the path to the file or directory which must be deleted
*/
@Override
public boolean deleteFile(@HeaderParam("sessionid") String sessionId, @PathParam("spaceName") String spaceName, @PathParam("filePath") String filePath) throws IOException, NotConnectedRestException, PermissionRestException {
checkAccess(sessionId, "deleteFile");
Session session = dataspaceRestApi.checkSessionValidity(sessionId);
filePath = normalizeFilePath(filePath, null);
FileObject sourcefo = dataspaceRestApi.resolveFile(session, spaceName, filePath);
if (!sourcefo.exists() || !sourcefo.isWriteable()) {
RuntimeException ex = new IllegalArgumentException("File or Folder " + filePath + " does not exist or is not writable in space " + spaceName);
logger.error(ex);
throw ex;
}
if (sourcefo.getType().equals(FileType.FILE)) {
logger.info("[deleteFile] deleting file " + sourcefo.getURL());
sourcefo.delete();
} else if (sourcefo.getType().equals(FileType.FOLDER)) {
logger.info("[deleteFile] deleting folder (and all its descendants) " + sourcefo.getURL());
sourcefo.delete(Selectors.SELECT_ALL);
} else {
RuntimeException ex = new IllegalArgumentException("File " + filePath + " has an unsupported type " + sourcefo.getType());
logger.error(ex);
throw ex;
}
return true;
}
use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.
the class RestDataspaceImpl method delete.
/**
* Delete file(s) from the specified location in the <i>dataspace</i>. The
* format of the DELETE URI is:
* <p>
* {@code http://<rest-server-path>/data/<dataspace>/<path-name>}
* <p>
* Example:
* {@code http://localhost:8080/rest/rest/data/user/my-files/my-text-file.txt}
* <ul>
* <li>dataspace: can have two possible values, 'user' or 'global',
* depending on the target <i>DATASPACE</i></li>
* <li>path-name: location of the file(s) to be deleted.</li>
* </ul>
* <b>Notes:</b>
* <ul>
* <li>Only empty directories can be deleted.</li>
* <li>File names or regular expressions can be used as 'includes' and
* 'excludes' query parameters, in order to select which files to be deleted
* inside the specified directory (path-name).</li>
* </ul>
*/
@DELETE
@Path("/{dataspace}/{path-name:.*}")
public Response delete(@HeaderParam("sessionid") String sessionId, @PathParam("dataspace") String dataspace, @PathParam("path-name") String pathname, @QueryParam("includes") List<String> includes, @QueryParam("excludes") List<String> excludes) throws NotConnectedRestException, PermissionRestException {
Session session = checkSessionValidity(sessionId);
try {
checkPathParams(dataspace, pathname);
FileObject fo = resolveFile(session, dataspace, pathname);
if (!fo.exists()) {
return Response.status(Response.Status.NO_CONTENT).build();
}
if (fo.getType() == FileType.FOLDER) {
logger.debug(String.format("Deleting directory %s in %s", pathname, dataspace));
return deleteDir(fo, includes, excludes);
} else {
logger.debug(String.format("Deleting file %s in %s", pathname, dataspace));
fo.close();
return fo.delete() ? noContentRes() : serverErrorRes("Cannot delete the file: %s", pathname);
}
} catch (Throwable error) {
logger.error(String.format("Cannot delete %s in %s.", pathname, dataspace), error);
throw rethrow(error);
}
}
use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.
the class RestDataspaceImpl method retrieve.
/**
* Retrieves single or multiple files from specified location of the server.
* The format of the GET URI is:
* <P>
* {@code http://<rest-server-path>/data/<dataspace>/<path-name>}
* <p>
* Example:
* <p>
* {@code http://localhost:8080/rest/rest/data/user/my-files/my-text-file.txt}
* <ul>
* <li>dataspace: can have two possible values, 'user' or 'global',
* depending on the target <i>DATASPACE</i></li>
* <li>path-name: location from which the file will be retrieved.</li>
* </ul>
* <b>Notes:</b>
* <ul>
* <li>If 'list' is specified as the 'comp' query parameter, an
* {@link ListFile} type object will be return in JSON format. It will contain a list of files and folder contained in the selected
* path.
* </li>
* <li>If 'recursive' is specified as the 'comp' query parameter, an
* {@link ListFile} type object will be return in JSON format. It will contain a list of files and folder contained in the selected
* path and all subfolders.
* </li>
* <li>If the pathname represents a file its contents will be returned as:
* <ul>
* <li>an octet stream, if its a compressed file or the client doesn't
* accept encoded content</li>
* <li>a 'gzip' encoded stream, if the client accepts 'gzip' encoded content
* </li>
* <li>a 'zip' encoded stream, if the client accepts 'zip' encoded contents</li>
* </ul>
* </li>
* <li>If the pathname represents a directory, its contents will be returned
* as 'zip' encoded stream.</li>
* <li>file names or regular expressions can be used as 'includes' and
* 'excludes' query parameters, in order to select which files to be
* returned can be used to select the files returned.</li>
* </ul>
*/
@GET
@Path("/{dataspace}/{path-name:.*}")
public Response retrieve(@HeaderParam("sessionid") String sessionId, @HeaderParam("Accept-Encoding") String encoding, @PathParam("dataspace") String dataspace, @PathParam("path-name") String pathname, @QueryParam("comp") String component, @QueryParam("includes") List<String> includes, @QueryParam("excludes") List<String> excludes) throws NotConnectedRestException, PermissionRestException {
Session session = checkSessionValidity(sessionId);
try {
checkPathParams(dataspace, pathname);
FileObject fo = resolveFile(session, dataspace, pathname);
if (!fo.exists()) {
return notFoundRes();
}
if (!Strings.isNullOrEmpty(component)) {
return componentResponse(component, fo, includes, excludes);
}
if (fo.getType() == FileType.FILE) {
if (VFSZipper.isZipFile(fo)) {
logger.debug(String.format("Retrieving file %s in %s", pathname, dataspace));
return fileComponentResponse(fo);
} else if (Strings.isNullOrEmpty(encoding) || encoding.contains("*") || encoding.contains("gzip")) {
logger.debug(String.format("Retrieving file %s as gzip in %s", pathname, dataspace));
return gzipComponentResponse(pathname, fo);
} else if (encoding.contains("zip")) {
logger.debug(String.format("Retrieving file %s as zip in %s", pathname, dataspace));
return zipComponentResponse(fo, null, null);
} else {
logger.debug(String.format("Retrieving file %s in %s", pathname, dataspace));
return fileComponentResponse(fo);
}
} else {
// folder
if (Strings.isNullOrEmpty(encoding) || encoding.contains("*") || encoding.contains("zip")) {
logger.debug(String.format("Retrieving folder %s as zip in %s", pathname, dataspace));
return zipComponentResponse(fo, includes, excludes);
} else {
return badRequestRes("Folder retrieval only supported with zip encoding.");
}
}
} catch (Throwable error) {
logger.error(String.format("Cannot retrieve %s in %s.", pathname, dataspace), error);
throw rethrow(error);
}
}
use of org.apache.commons.vfs2.FileObject in project scheduling by ow2-proactive.
the class TestGlobalSpace method testGlobalSpace.
@Test
public void testGlobalSpace() throws Throwable {
File in = tmpFolder.newFolder("input_space");
String inPath = in.getAbsolutePath();
File out = tmpFolder.newFolder("output_space");
String outPath = out.getAbsolutePath();
writeFiles(inFiles, inPath);
TaskFlowJob job = new TaskFlowJob();
job.setName(this.getClass().getSimpleName());
job.setInputSpace(in.toURI().toURL().toString());
job.setOutputSpace(out.toURI().toURL().toString());
JavaTask A = new JavaTask();
A.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
A.setName("A");
for (String[] file : inFiles) {
A.addInputFiles(file[0], InputAccessMode.TransferFromInputSpace);
A.addOutputFiles(file[0] + ".glob.A", OutputAccessMode.TransferToGlobalSpace);
}
A.setPreScript(new SimpleScript(scriptA, "groovy"));
A.setForkEnvironment(new ForkEnvironment());
job.addTask(A);
JavaTask B = new JavaTask();
B.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
B.setName("B");
B.addDependence(A);
for (String[] file : inFiles) {
B.addInputFiles(file[0] + ".glob.A", InputAccessMode.TransferFromGlobalSpace);
B.addOutputFiles(file[0] + ".out", OutputAccessMode.TransferToOutputSpace);
}
B.setPreScript(new SimpleScript(scriptB, "groovy"));
B.setForkEnvironment(new ForkEnvironment());
job.addTask(B);
Scheduler scheduler = schedulerHelper.getSchedulerInterface();
JobId id = scheduler.submit(job);
schedulerHelper.waitForEventJobFinished(id);
assertFalse(schedulerHelper.getJobResult(id).hadException());
/**
* check: inFiles > IN > LOCAL A > GLOBAL > LOCAL B > OUT
*/
for (String[] inFile : inFiles) {
File f = new File(outPath + File.separator + inFile[0] + ".out");
assertTrue("File does not exist: " + f.getAbsolutePath(), f.exists());
Assert.assertEquals("Original and copied files differ", inFile[1], FileUtils.readFileToString(f));
f.delete();
File inf = new File(inPath + File.separator + inFile[0]);
inf.delete();
}
/**
* check that the file produced is accessible in the global user space via the scheduler API
*/
String globalURI = scheduler.getGlobalSpaceURIs().get(0);
assertTrue(globalURI.startsWith("file:"));
String globalPath = new File(new URI(globalURI)).getAbsolutePath();
FileSystemManager fsManager = VFSFactory.createDefaultFileSystemManager();
for (String[] file : inFiles) {
FileObject outFile = fsManager.resolveFile(globalURI + "/" + file[0] + ".glob.A");
log("Checking existence of " + outFile.getURL());
assertTrue(outFile.getURL() + " exists", outFile.exists());
File outFile2 = new File(globalPath, file[0] + ".glob.A");
log("Checking existence of " + outFile2);
assertTrue(outFile2 + " exists", outFile2.exists());
}
}
Aggregations