Example usage for java.net URI getQuery

List of usage examples for java.net URI getQuery

Introduction

In this page you can find the example usage for java.net URI getQuery.

Prototype

public String getQuery() 

Source Link

Document

Returns the decoded query component of this URI.

Usage

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 . java2s.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 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.apache.hadoop.hive.metastore.HiveMetaStoreClientPreCatalog.java

private void resolveUris() throws MetaException {
    String metastoreUrisString[] = MetastoreConf.getVar(conf, ConfVars.THRIFT_URIS).split(",");

    List<URI> metastoreURIArray = new ArrayList<URI>();
    try {//from   www.j a va2s . com
        int i = 0;
        for (String s : metastoreUrisString) {
            URI tmpUri = new URI(s);
            if (tmpUri.getScheme() == null) {
                throw new IllegalArgumentException("URI: " + s + " does not have a scheme");
            }
            if (uriResolverHook != null) {
                metastoreURIArray.addAll(uriResolverHook.resolveURI(tmpUri));
            } else {
                metastoreURIArray.add(new URI(tmpUri.getScheme(), tmpUri.getUserInfo(),
                        HadoopThriftAuthBridge.getBridge().getCanonicalHostName(tmpUri.getHost()),
                        tmpUri.getPort(), tmpUri.getPath(), tmpUri.getQuery(), tmpUri.getFragment()));
            }
        }
        metastoreUris = new URI[metastoreURIArray.size()];
        for (int j = 0; j < metastoreURIArray.size(); j++) {
            metastoreUris[j] = metastoreURIArray.get(j);
        }

        if (MetastoreConf.getVar(conf, ConfVars.THRIFT_URI_SELECTION).equalsIgnoreCase("RANDOM")) {
            List uriList = Arrays.asList(metastoreUris);
            Collections.shuffle(uriList);
            metastoreUris = (URI[]) uriList.toArray();
        }
    } catch (IllegalArgumentException e) {
        throw (e);
    } catch (Exception e) {
        MetaStoreUtils.logAndThrowMetaException(e);
    }
}

From source file:com.zimbra.client.ZMailbox.java

public static String resolveUrl(String url, boolean isAdmin) throws ZClientException {
    try {//w w w.  ja v a  2  s.c  om
        URI uri = new URI(url);

        if (isAdmin && uri.getPort() == -1) {
            uri = new URI("https", uri.getUserInfo(), uri.getHost(), ADMIN_PORT, uri.getPath(), uri.getQuery(),
                    uri.getFragment());
            url = uri.toString();
        }

        String service = (uri.getPort() == ADMIN_PORT) ? AdminConstants.ADMIN_SERVICE_URI
                : AccountConstants.USER_SERVICE_URI;
        if (uri.getPath() == null || uri.getPath().length() <= 1) {
            if (url.charAt(url.length() - 1) == '/') {
                url = url.substring(0, url.length() - 1) + service;
            } else {
                url = url + service;
            }
        }
        return url;
    } catch (URISyntaxException e) {
        throw ZClientException.CLIENT_ERROR("invalid URL: " + url, e);
    }
}

From source file:org.apache.oozie.action.hadoop.TestJavaActionExecutor.java

public void testAddToCache() throws Exception {
    JavaActionExecutor ae = new JavaActionExecutor();
    Configuration conf = new XConfiguration();

    Path appPath = new Path(getFsTestCaseDir(), "wf");
    URI appUri = appPath.toUri();

    // test archive without fragment
    Path archivePath = new Path("test.jar");
    Path archiveFullPath = new Path(appPath, archivePath);
    ae.addToCache(conf, appPath, archiveFullPath.toString(), true);
    assertTrue(conf.get("mapred.cache.archives").contains(archiveFullPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test archive with fragment
    Path archiveFragmentPath = new Path("test.jar#a.jar");
    Path archiveFragmentFullPath = new Path(appPath, archiveFragmentPath);
    conf.clear();/*from  w w  w .ja  va2s  .c o  m*/
    ae.addToCache(conf, appPath, archiveFragmentFullPath.toString(), true);
    assertTrue(conf.get("mapred.cache.archives").contains(archiveFragmentFullPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test .so without fragment
    Path appSoPath = new Path("lib/a.so");
    Path appSoFullPath = new Path(appPath, appSoPath);
    conf.clear();
    ae.addToCache(conf, appPath, appSoFullPath.toString(), false);
    assertTrue(conf.get("mapred.cache.files").contains(appSoFullPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test .so with fragment
    Path appSoFragmentPath = new Path("lib/a.so#a.so");
    Path appSoFragmentFullPath = new Path(appPath, appSoFragmentPath);
    conf.clear();
    ae.addToCache(conf, appPath, appSoFragmentFullPath.toString(), false);
    assertTrue(conf.get("mapred.cache.files").contains(appSoFragmentFullPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test .jar without fragment where app path is on same cluster as jar path
    Path appJarPath = new Path("lib/a.jar");
    Path appJarFullPath = new Path(appPath, appJarPath);
    conf = new Configuration();
    conf.set(WorkflowAppService.HADOOP_USER, getTestUser());
    ae.addToCache(conf, appPath, appJarFullPath.toString(), false);
    // assert that mapred.cache.files contains jar URI path (full on Hadoop-2)
    Path jarPath = HadoopShims.isYARN() ? new Path(appJarFullPath.toUri())
            : new Path(appJarFullPath.toUri().getPath());
    assertTrue(conf.get("mapred.cache.files").contains(jarPath.toString()));
    // assert that dist cache classpath contains jar URI path
    Path[] paths = DistributedCache.getFileClassPaths(conf);
    boolean pathFound = false;
    for (Path path : paths) {
        if (path.equals(jarPath)) {
            pathFound = true;
            break;
        }
    }
    assertTrue(pathFound);
    assertTrue(DistributedCache.getSymlink(conf));

    // test .jar without fragment where app path is on a different cluster than jar path
    appJarPath = new Path("lib/a.jar");
    appJarFullPath = new Path(appPath, appJarPath);
    Path appDifferentClusterPath = new Path(new URI(appUri.getScheme(), null, appUri.getHost() + "x",
            appUri.getPort(), appUri.getPath(), appUri.getQuery(), appUri.getFragment()));
    conf.clear();
    conf.set(WorkflowAppService.HADOOP_USER, getTestUser());
    ae.addToCache(conf, appDifferentClusterPath, appJarFullPath.toString(), false);
    // assert that mapred.cache.files contains absolute jar URI
    assertTrue(conf.get("mapred.cache.files").contains(appJarFullPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test .jar with fragment
    Path appJarFragmentPath = new Path("lib/a.jar#a.jar");
    Path appJarFragmentFullPath = new Path(appPath, appJarFragmentPath);
    conf.clear();
    conf.set(WorkflowAppService.HADOOP_USER, getTestUser());
    ae.addToCache(conf, appPath, appJarFragmentFullPath.toString(), false);
    assertTrue(conf.get("mapred.cache.files").contains(appJarFragmentFullPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test regular file without fragment
    Path appFilePath = new Path("lib/a.txt");
    Path appFileFullPath = new Path(appPath, appFilePath);
    conf.clear();
    ae.addToCache(conf, appPath, appFileFullPath.toString(), false);
    assertTrue(conf.get("mapred.cache.files").contains(appFileFullPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test regular file with fragment
    Path appFileFragmentPath = new Path("lib/a.txt#a.txt");
    Path appFileFragmentFullPath = new Path(appPath, appFileFragmentPath);
    conf.clear();
    ae.addToCache(conf, appPath, appFileFragmentFullPath.toString(), false);
    assertTrue(conf.get("mapred.cache.files").contains(appFileFragmentFullPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test path starting with "/" for archive
    Path testPath = new Path("/tmp/testpath/a.jar#a.jar");
    conf.clear();
    ae.addToCache(conf, appPath, testPath.toString(), true);
    assertTrue(conf.get("mapred.cache.archives").contains(testPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test path starting with "/" for cache.file
    conf.clear();
    ae.addToCache(conf, appPath, testPath.toString(), false);
    assertTrue(conf.get("mapred.cache.files").contains(testPath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test absolute path for archive
    Path testAbsolutePath = new Path("hftp://namenode.test.com:8020/tmp/testpath/a.jar#a.jar");
    conf.clear();
    ae.addToCache(conf, appPath, testAbsolutePath.toString(), true);
    assertTrue(conf.get("mapred.cache.archives").contains(testAbsolutePath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test absolute path for cache files
    conf.clear();
    ae.addToCache(conf, appPath, testAbsolutePath.toString(), false);
    assertTrue(conf.get("mapred.cache.files").contains(testAbsolutePath.toString()));
    assertTrue(DistributedCache.getSymlink(conf));

    // test relative path for archive
    conf.clear();
    ae.addToCache(conf, appPath, "lib/a.jar#a.jar", true);
    assertTrue(conf.get("mapred.cache.archives").contains(appUri.getPath() + "/lib/a.jar#a.jar"));
    assertTrue(DistributedCache.getSymlink(conf));

    // test relative path for cache files
    conf.clear();
    ae.addToCache(conf, appPath, "lib/a.jar#a.jar", false);
    assertTrue(conf.get("mapred.cache.files").contains(appUri.getPath() + "/lib/a.jar#a.jar"));
    assertTrue(DistributedCache.getSymlink(conf));
}