Search in sources :

Example 6 with URIx

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);
}
Also used : URIx(maspack.fileutil.uri.URIx)

Example 7 with URIx

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;
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader) RigidTransform3d(maspack.matrix.RigidTransform3d) AxisAngle(maspack.matrix.AxisAngle) Vector3d(maspack.matrix.Vector3d) FileCacher(maspack.fileutil.FileCacher) URIx(maspack.fileutil.uri.URIx) IOException(java.io.IOException) File(java.io.File) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 8 with URIx

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;
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileNotFoundException(java.io.FileNotFoundException) URIx(maspack.fileutil.uri.URIx) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileNotFoundException(java.io.FileNotFoundException) JSchException(com.jcraft.jsch.JSchException)

Example 9 with URIx

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;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) URIx(maspack.fileutil.uri.URIx) URIxMatcher(maspack.fileutil.uri.URIxMatcher)

Example 10 with URIx

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.");
    }
}
Also used : URIx(maspack.fileutil.uri.URIx)

Aggregations

URIx (maspack.fileutil.uri.URIx)17 File (java.io.File)11 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 IOException (java.io.IOException)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 FileObject (org.apache.commons.vfs2.FileObject)2 JSchException (com.jcraft.jsch.JSchException)1 FileNotFoundException (java.io.FileNotFoundException)1 FileCacher (maspack.fileutil.FileCacher)1 URIxMatcher (maspack.fileutil.uri.URIxMatcher)1 PolygonalMesh (maspack.geometry.PolygonalMesh)1 WavefrontReader (maspack.geometry.io.WavefrontReader)1 AxisAngle (maspack.matrix.AxisAngle)1 RigidTransform3d (maspack.matrix.RigidTransform3d)1 Vector3d (maspack.matrix.Vector3d)1 AllFileSelector (org.apache.commons.vfs2.AllFileSelector)1