Example usage for org.apache.commons.vfs2.provider.url UrlFileName URLFileName

List of usage examples for org.apache.commons.vfs2.provider.url UrlFileName URLFileName

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.provider.url UrlFileName URLFileName.

Prototype

public URLFileName(final String scheme, final String hostName, final int port, final int defaultPort,
            final String userName, final String password, final String path, final FileType type,
            final String queryString) 

Source Link

Usage

From source file:org.datacleaner.bootstrap.Bootstrap.java

/**
 * Looks up a file, either based on a user requested filename (typically a
 * CLI parameter, may be a URL) or by a relative filename defined in the
 * system-/*from w  w w.  j  a v  a  2 s  .  co  m*/
 * 
 * @param userRequestedFilename
 *            the user requested filename, may be null
 * @param localFilename
 *            the relative filename defined by the system
 * @param userPreferences
 * @return
 * @throws FileSystemException
 */
private FileObject resolveFile(final String userRequestedFilename, final String localFilename,
        UserPreferences userPreferences) throws FileSystemException {
    final File dataCleanerHome = DataCleanerHome.getAsFile();
    if (userRequestedFilename == null) {
        File file = new File(dataCleanerHome, localFilename);
        return VFSUtils.toFileObject(file);
    } else {
        String lowerCaseFilename = userRequestedFilename.toLowerCase();
        if (lowerCaseFilename.startsWith("http://") || lowerCaseFilename.startsWith("https://")) {
            if (!GraphicsEnvironment.isHeadless()) {
                // download to a RAM file.
                final FileObject targetDirectory = VFSUtils.getFileSystemManager()
                        .resolveFile("ram:///datacleaner/temp");
                if (!targetDirectory.exists()) {
                    targetDirectory.createFolder();
                }

                final URI uri;
                try {
                    uri = new URI(userRequestedFilename);
                } catch (URISyntaxException e) {
                    throw new IllegalArgumentException("Illegal URI: " + userRequestedFilename, e);
                }

                final WindowContext windowContext = new SimpleWindowContext();

                MonitorConnection monitorConnection = null;

                // check if URI points to DC monitor. If so, make sure
                // credentials are entered.
                if (userPreferences != null && userPreferences.getMonitorConnection() != null) {
                    monitorConnection = userPreferences.getMonitorConnection();
                    if (monitorConnection.matchesURI(uri)) {
                        if (monitorConnection.isAuthenticationEnabled()) {
                            if (monitorConnection.getEncodedPassword() == null) {
                                final MonitorConnectionDialog dialog = new MonitorConnectionDialog(
                                        windowContext, userPreferences);
                                dialog.openBlocking();
                            }
                            monitorConnection = userPreferences.getMonitorConnection();
                        }
                    }
                }

                try (MonitorHttpClient httpClient = getHttpClient(monitorConnection)) {

                    final String[] urls = new String[] { userRequestedFilename };
                    final String[] targetFilenames = DownloadFilesActionListener.createTargetFilenames(urls);

                    final FileObject[] files = downloadFiles(urls, targetDirectory, targetFilenames,
                            windowContext, httpClient, monitorConnection);

                    assert files.length == 1;

                    final FileObject ramFile = files[0];

                    if (logger.isInfoEnabled()) {
                        final InputStream in = ramFile.getContent().getInputStream();
                        try {
                            final String str = FileHelper
                                    .readInputStreamAsString(ramFile.getContent().getInputStream(), "UTF8");
                            logger.info("Downloaded file contents: {}\n{}", userRequestedFilename, str);
                        } finally {
                            FileHelper.safeClose(in);
                        }
                    }

                    final String scheme = uri.getScheme();
                    final int defaultPort;
                    if ("http".equals(scheme)) {
                        defaultPort = 80;
                    } else {
                        defaultPort = 443;
                    }

                    final UrlFileName fileName = new UrlFileName(scheme, uri.getHost(), uri.getPort(),
                            defaultPort, null, null, uri.getPath(), FileType.FILE, uri.getQuery());

                    AbstractFileSystem fileSystem = (AbstractFileSystem) VFSUtils.getBaseFileSystem();
                    return new DelegateFileObject(fileName, fileSystem, ramFile);
                } finally {
                    userPreferences.setMonitorConnection(monitorConnection);
                }

            }
        }

        return VFSUtils.getFileSystemManager().resolveFile(userRequestedFilename);
    }
}

From source file:org.eobjects.datacleaner.bootstrap.Bootstrap.java

/**
 * Looks up a file, either based on a user requested filename (typically a
 * CLI parameter, may be a URL) or by a relative filename defined in the
 * system-/*from  w  w  w  . j  a  v a 2  s  .  c  o m*/
 * 
 * @param userRequestedFilename
 *            the user requested filename, may be null
 * @param localFilename
 *            the relative filename defined by the system
 * @param userPreferences
 * @return
 * @throws FileSystemException
 */
private FileObject resolveFile(final String userRequestedFilename, final String localFilename,
        UserPreferences userPreferences) throws FileSystemException {
    final FileObject dataCleanerHome = DataCleanerHome.get();
    if (userRequestedFilename == null) {
        return dataCleanerHome.resolveFile(localFilename);
    } else {
        String lowerCaseFilename = userRequestedFilename.toLowerCase();
        if (lowerCaseFilename.startsWith("http://") || lowerCaseFilename.startsWith("https://")) {
            if (!GraphicsEnvironment.isHeadless()) {
                // download to a RAM file.
                final FileObject targetDirectory = VFSUtils.getFileSystemManager()
                        .resolveFile("ram:///datacleaner/temp");
                if (!targetDirectory.exists()) {
                    targetDirectory.createFolder();
                }

                final URI uri;
                try {
                    uri = new URI(userRequestedFilename);
                } catch (URISyntaxException e) {
                    throw new IllegalArgumentException("Illegal URI: " + userRequestedFilename, e);
                }

                final WindowContext windowContext = new SimpleWindowContext();

                @SuppressWarnings("resource")
                MonitorHttpClient httpClient = new SimpleWebServiceHttpClient();
                MonitorConnection monitorConnection = null;

                // check if URI points to DC monitor. If so, make sure
                // credentials are entered.
                if (userPreferences != null && userPreferences.getMonitorConnection() != null) {
                    monitorConnection = userPreferences.getMonitorConnection();
                    if (monitorConnection.matchesURI(uri)) {
                        if (monitorConnection.isAuthenticationEnabled()) {
                            if (monitorConnection.getEncodedPassword() == null) {
                                final MonitorConnectionDialog dialog = new MonitorConnectionDialog(
                                        windowContext, userPreferences);
                                dialog.openBlocking();
                            }
                            monitorConnection = userPreferences.getMonitorConnection();
                            httpClient = monitorConnection.getHttpClient();
                        }
                    }
                }

                try {

                    final String[] urls = new String[] { userRequestedFilename };
                    final String[] targetFilenames = DownloadFilesActionListener.createTargetFilenames(urls);

                    final FileObject[] files = downloadFiles(urls, targetDirectory, targetFilenames,
                            windowContext, httpClient, monitorConnection);

                    assert files.length == 1;

                    final FileObject ramFile = files[0];

                    if (logger.isInfoEnabled()) {
                        final InputStream in = ramFile.getContent().getInputStream();
                        try {
                            final String str = FileHelper
                                    .readInputStreamAsString(ramFile.getContent().getInputStream(), "UTF8");
                            logger.info("Downloaded file contents: {}\n{}", userRequestedFilename, str);
                        } finally {
                            FileHelper.safeClose(in);
                        }
                    }

                    final String scheme = uri.getScheme();
                    final int defaultPort;
                    if ("http".equals(scheme)) {
                        defaultPort = 80;
                    } else {
                        defaultPort = 443;
                    }

                    final UrlFileName fileName = new UrlFileName(scheme, uri.getHost(), uri.getPort(),
                            defaultPort, null, null, uri.getPath(), FileType.FILE, uri.getQuery());

                    AbstractFileSystem fileSystem = (AbstractFileSystem) dataCleanerHome.getFileSystem();
                    return new DelegateFileObject(fileName, fileSystem, ramFile);
                } finally {
                    httpClient.close();

                    userPreferences.setMonitorConnection(monitorConnection);
                }

            }
        }

        return VFSUtils.getFileSystemManager().resolveFile(userRequestedFilename);
    }
}

From source file:org.pentaho.big.data.impl.cluster.NamedClusterImpl.java

/**
 * This method generates the URL from the specific NamedCluster using the specified scheme.
 *
 * @param scheme the name of the scheme to use to create the URL
 * @return the generated URL from the specific NamedCluster or null if an error occurs
 *//*from  w  w w  .j  av a  2s. c o m*/
@VisibleForTesting
String generateURL(String scheme, IMetaStore metastore, VariableSpace variableSpace) {
    String clusterURL = null;
    try {
        if (!Utils.isEmpty(scheme)) {
            String ncHostname = getHdfsHost() != null ? getHdfsHost() : "";
            String ncPort = getHdfsPort() != null ? getHdfsPort() : "";
            String ncUsername = getHdfsUsername() != null ? getHdfsUsername() : "";
            String ncPassword = getHdfsPassword() != null ? getHdfsPassword() : "";

            if (variableSpace != null) {
                variableSpace.initializeVariablesFrom(getParentVariableSpace());
                if (StringUtil.isVariable(scheme)) {
                    scheme = variableSpace.getVariable(StringUtil.getVariableName(scheme)) != null
                            ? variableSpace.environmentSubstitute(scheme)
                            : null;
                }
                if (StringUtil.isVariable(ncHostname)) {
                    ncHostname = variableSpace.getVariable(StringUtil.getVariableName(ncHostname)) != null
                            ? variableSpace.environmentSubstitute(ncHostname)
                            : null;
                }
                if (StringUtil.isVariable(ncPort)) {
                    ncPort = variableSpace.getVariable(StringUtil.getVariableName(ncPort)) != null
                            ? variableSpace.environmentSubstitute(ncPort)
                            : null;
                }
                if (StringUtil.isVariable(ncUsername)) {
                    ncUsername = variableSpace.getVariable(StringUtil.getVariableName(ncUsername)) != null
                            ? variableSpace.environmentSubstitute(ncUsername)
                            : null;
                }
                if (StringUtil.isVariable(ncPassword)) {
                    ncPassword = variableSpace.getVariable(StringUtil.getVariableName(ncPassword)) != null
                            ? variableSpace.environmentSubstitute(ncPassword)
                            : null;
                }
            }

            ncHostname = ncHostname != null ? ncHostname.trim() : "";
            if (ncPort == null) {
                ncPort = "-1";
            } else {
                ncPort = ncPort.trim();
                if (Utils.isEmpty(ncPort)) {
                    ncPort = "-1";
                }
            }
            ncUsername = ncUsername != null ? ncUsername.trim() : "";
            ncPassword = ncPassword != null ? ncPassword.trim() : "";

            UrlFileName file = new UrlFileName(scheme, ncHostname, Integer.parseInt(ncPort), -1, ncUsername,
                    ncPassword, null, null, null);
            clusterURL = file.getURI();
            if (clusterURL.endsWith("/")) {
                clusterURL = clusterURL.substring(0, clusterURL.lastIndexOf("/"));
            }
        }
    } catch (Exception e) {
        clusterURL = null;
    }
    return clusterURL;
}

From source file:org.pentaho.big.data.impl.vfs.hdfs.nc.NamedClusterProviderTest.java

@Test
public void testDoCreateFileSystem()
        throws FileSystemException, MetaStoreException, ClusterInitializationException {
    when(metastoreLocator.getMetastore()).thenReturn(metastore);

    UrlFileName name = new UrlFileName("hc", ncName, 0, 0, null, null, path, null, null);
    NamedClusterProvider provider = new NamedClusterProvider(hdfsLocator, ncService, fileSystemManager,
            fileNameParser, scheme, metastoreLocator);
    FileSystem fs = provider.doCreateFileSystem(name, null);
    assertTrue(fs instanceof HDFSFileSystem);

    HDFSFileSystem hdfsFS = (HDFSFileSystem) fs;
    assertEquals(hfs, hdfsFS.getHDFSFileSystem());

    verify(nc).processURLsubstitution(anyString(), eq(metastore), any(Variables.class));
    verify(hdfsLocator).getHadoopFilesystem(eq(nc), any(URI.class));
}

From source file:org.pentaho.big.data.impl.vfs.hdfs.nc.NamedClusterProviderTest.java

@Test
public void testDoCreateFileSystem_NCTemplate()
        throws FileSystemException, MetaStoreException, ClusterInitializationException {
    UrlFileName name = new UrlFileName("hc", ncName, 0, 0, null, null, path, null, null);
    NamedClusterProvider provider = new NamedClusterProvider(hdfsLocator, ncService, fileSystemManager,
            fileNameParser, scheme, metastoreLocator);
    FileSystem fs = provider.doCreateFileSystem(name, null);
    assertTrue(fs instanceof HDFSFileSystem);

    HDFSFileSystem hdfsFS = (HDFSFileSystem) fs;
    assertEquals(hfs, hdfsFS.getHDFSFileSystem());

    verify(ncService, never()).read(eq(ncName), eq(metastore));
    verify(hdfsLocator).getHadoopFilesystem(eq(ncTemplate), any(URI.class));
}

From source file:org.pentaho.di.core.namedcluster.NamedClusterManager.java

/**
 * This method generates the URL from the specific NamedCluster using the specified scheme.
 *
 * @param scheme//from ww w  .java 2s . c  o m
 *          the name of the scheme to use to create the URL
 * @param clusterName
 *          the name of the NamedCluster to use to create the URL
 * @return the generated URL from the specific NamedCluster or null if an error occurs
 */
@VisibleForTesting
String generateURL(String scheme, String clusterName, IMetaStore metastore, VariableSpace variableSpace) {
    String clusterURL = null;
    try {
        if (!Utils.isEmpty(scheme) && !Utils.isEmpty(clusterName) && metastore != null) {
            NamedCluster namedCluster = read(clusterName, metastore);
            if (namedCluster != null) {
                String ncHostname = namedCluster.getHdfsHost() != null ? namedCluster.getHdfsHost() : "";
                String ncPort = namedCluster.getHdfsPort() != null ? namedCluster.getHdfsPort() : "";
                String ncUsername = namedCluster.getHdfsUsername() != null ? namedCluster.getHdfsUsername()
                        : "";
                String ncPassword = namedCluster.getHdfsPassword() != null ? namedCluster.getHdfsPassword()
                        : "";

                if (variableSpace != null) {
                    variableSpace.initializeVariablesFrom(namedCluster.getParentVariableSpace());
                    if (StringUtil.isVariable(scheme)) {
                        scheme = variableSpace.getVariable(StringUtil.getVariableName(scheme)) != null
                                ? variableSpace.environmentSubstitute(scheme)
                                : null;
                    }
                    if (StringUtil.isVariable(ncHostname)) {
                        ncHostname = variableSpace.getVariable(StringUtil.getVariableName(ncHostname)) != null
                                ? variableSpace.environmentSubstitute(ncHostname)
                                : null;
                    }
                    if (StringUtil.isVariable(ncPort)) {
                        ncPort = variableSpace.getVariable(StringUtil.getVariableName(ncPort)) != null
                                ? variableSpace.environmentSubstitute(ncPort)
                                : null;
                    }
                    if (StringUtil.isVariable(ncUsername)) {
                        ncUsername = variableSpace.getVariable(StringUtil.getVariableName(ncUsername)) != null
                                ? variableSpace.environmentSubstitute(ncUsername)
                                : null;
                    }
                    if (StringUtil.isVariable(ncPassword)) {
                        ncPassword = variableSpace.getVariable(StringUtil.getVariableName(ncPassword)) != null
                                ? variableSpace.environmentSubstitute(ncPassword)
                                : null;
                    }
                }

                ncHostname = ncHostname != null ? ncHostname.trim() : "";
                if (ncPort == null) {
                    ncPort = "-1";
                } else {
                    ncPort = ncPort.trim();
                    if (Utils.isEmpty(ncPort)) {
                        ncPort = "-1";
                    }
                }
                ncUsername = ncUsername != null ? ncUsername.trim() : "";
                ncPassword = ncPassword != null ? ncPassword.trim() : "";

                UrlFileName file = new UrlFileName(scheme, ncHostname, Integer.parseInt(ncPort), -1, ncUsername,
                        ncPassword, null, null, null);
                clusterURL = file.getURI();
                if (clusterURL.endsWith("/")) {
                    clusterURL = clusterURL.substring(0, clusterURL.lastIndexOf("/"));
                }
            }
        }
    } catch (Exception e) {
        clusterURL = null;
    }
    return clusterURL;
}