use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileManager method setRemoteSource.
/**
* Sets the base URI for remote files, this is attached to any relative URIs
* provided in the get(...) methods
*
* @param uriStr base URI for remote files
* @throws URIxSyntaxException if uriStr is malformed
*/
public void setRemoteSource(String uriStr) throws URIxSyntaxException {
if (uriStr == null) {
remoteSource = null;
return;
}
URIx remote = new URIx(uriStr);
setRemoteSource(remote);
}
use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class MultiViewerTesterBase method loadStanfordBunny.
protected static PolygonalMesh loadStanfordBunny() {
// read Standford bunny directly
String bunnyURL = "http://graphics.stanford.edu/~mdfisher/Data/Meshes/bunny.obj";
// bunny
File bunnyFile = new File("tmp/data/stanford_bunny.obj");
PolygonalMesh bunny = null;
try {
if (!bunnyFile.exists()) {
bunnyFile.getParentFile().mkdirs();
// read file directly from remote
FileCacher cacher = new FileCacher();
cacher.initialize();
cacher.cache(new URIx(bunnyURL), bunnyFile);
cacher.release();
}
WavefrontReader reader = new WavefrontReader(bunnyFile);
bunny = new PolygonalMesh();
reader.readMesh(bunny);
// bunny.computeVertexNormals();
// normalize bunny
double r = bunny.computeRadius();
Vector3d c = new Vector3d();
bunny.computeCentroid(c);
c.negate();
bunny.scale(1.0 / r);
c.z -= 0.5;
bunny.transform(new RigidTransform3d(c, new AxisAngle(1, 0, 0, Math.PI / 2)));
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("Unable to load stanford bunny... requires internet connection");
bunny = null;
}
return bunny;
}
use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileCacher method cache.
public File cache(URIx uri, File cacheFile, FileTransferMonitor monitor) throws FileSystemException {
// For atomic operation, first download to temporary directory
File tmpCacheFile = new File(cacheFile.getAbsolutePath() + TMP_EXTENSION);
URIx cacheURI = new URIx(cacheFile.getAbsoluteFile());
URIx tmpCacheURI = new URIx(tmpCacheFile.getAbsoluteFile());
FileObject localTempFile = manager.resolveFile(tmpCacheURI.toString(true));
FileObject localCacheFile = manager.resolveFile(cacheURI.toString(true));
// will resolve next
FileObject remoteFile = null;
// loop through authenticators until we either succeed or cancel
boolean cancel = false;
while (remoteFile == null && cancel == false) {
remoteFile = resolveRemote(uri);
}
if (remoteFile == null || !remoteFile.exists()) {
throw new FileSystemException("Cannot find remote file <" + uri.toString() + ">", new FileNotFoundException("<" + uri.toString() + ">"));
}
// monitor the file transfer progress
if (monitor != null) {
monitor.monitor(localTempFile, remoteFile, -1, cacheFile.getName());
monitor.start();
monitor.fireStartEvent(localTempFile);
}
// transfer content
try {
if (remoteFile.isFile()) {
localTempFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
} else if (remoteFile.isFolder()) {
// final FileObject fileSystem = manager.createFileSystem(remoteFile);
localTempFile.copyFrom(remoteFile, new AllFileSelector());
// fileSystem.close();
}
if (monitor != null) {
monitor.fireCompleteEvent(localTempFile);
}
} catch (Exception e) {
// try to delete local file
localTempFile.delete();
throw new RuntimeException("Failed to complete transfer of " + remoteFile.getURL() + " to " + localTempFile.getURL(), e);
} finally {
// close files if we need to
localTempFile.close();
remoteFile.close();
if (monitor != null) {
monitor.release(localTempFile);
monitor.stop();
}
}
// now that the copy is complete, do a rename operation
try {
if (tmpCacheFile.isDirectory()) {
SafeFileUtils.moveDirectory(tmpCacheFile, cacheFile);
} else {
SafeFileUtils.moveFile(tmpCacheFile, cacheFile);
}
} catch (Exception e) {
// delete if possible
localCacheFile.delete();
throw new RuntimeException("Failed to atomically move " + "to " + localCacheFile.getURL(), e);
}
return cacheFile;
}
use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileCacher method resolveRemote.
private FileObject resolveRemote(URIx uri) throws FileSystemException {
FileObject remoteFile = null;
// base determines the first protocol
URIx base = uri.getBaseURI();
// clear authenticators
setAuthenticator(fsOpts, null);
setIdentityFactory(fsOpts, null);
// first try to find matching identity
for (URIxMatcher matcher : identMap.keySet()) {
if (matcher.matches(base)) {
// set identity, try to resolve file
myIdFactory.setIdentityRepository(identMap.get(matcher));
setIdentityFactory(fsOpts, myIdFactory);
remoteFile = tryGettingRemote(uri);
if (remoteFile != null) {
return remoteFile;
}
}
}
// then try authenticator
setIdentityFactory(fsOpts, null);
for (URIxMatcher matcher : authMap.keySet()) {
if (matcher.matches(base)) {
// we have found an authenticator
setAuthenticator(fsOpts, authMap.get(matcher));
remoteFile = tryGettingRemote(uri);
if (remoteFile != null) {
return remoteFile;
}
}
}
// try without authentication last (otherwise it seems to resolve, which is bad)
remoteFile = tryGettingRemote(uri);
if (remoteFile != null) {
return remoteFile;
}
return null;
}
use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileManager method put.
/**
* Uploads a file, according to options. Works with absolute paths and
* destination URIs, otherwise combines path and dest URI with downloadDir and
* remoteSource, respectively. If the destination is null or a directory,
* then the filename of source is appended.
*
* If there is any internal problem, (such as failing to obtain a hash, or
* failing to download a file), the function will log the error message and
* continue.
*
* @param source
* the source file to upload
* @param dest
* the remote URI to upload to
* @param options
* set of options, either FORCE_REMOTE or CHECK_HASH
* @throws FileTransferException if the upload fails
*/
public void put(File source, URIx dest, int options) throws FileTransferException {
// default destination if none provided
if (dest == null) {
if (!source.isAbsolute()) {
dest = new URIx(source.getPath());
} else {
dest = new URIx(source.getName());
}
} else if (isDirectory(dest)) {
if (!source.isAbsolute()) {
dest = new URIx(dest, source.getPath());
} else {
dest = new URIx(dest, source.getName());
}
}
// convert to absolute
dest = getAbsoluteURI(dest);
source = getAbsoluteFile(source);
// XXX TODO: check if we need to actually upload file
boolean push = true;
// download file if we need to
if (push) {
putRemote(source, dest);
} else {
logger.debug("File '" + dest + "' exists and does not need to be uploaded.");
}
}
Aggregations