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.apache.nifi.cluster.coordination.http.replication.ThreadPoolRequestReplicator.java

private URI createURI(final URI exampleUri, final NodeIdentifier nodeId) {
    return createURI(exampleUri.getScheme(), nodeId.getApiAddress(), nodeId.getApiPort(), exampleUri.getPath(),
            exampleUri.getQuery());
}

From source file:com.maestrodev.maestrocontinuumplugin.ContinuumWorker.java

private String getPomUrlWithCredentials() throws URISyntaxException {
    String pomUrl = getPomUrl();/*w  w  w . ja  v  a2  s .  c o m*/
    String pomUsername = getPomUsername();
    if (pomUsername != null) {
        URI u = new URI(pomUrl);
        pomUrl = new URI(u.getScheme(), pomUsername + ":" + getPomPassword(), u.getHost(), u.getPort(),
                u.getPath(), u.getQuery(), u.getFragment()).toString();
    }
    return pomUrl;
}

From source file:eu.stratosphere.nephele.fs.s3.S3FileSystem.java

static Path extendPath(final Path parent, final String extension) throws IOException {

    final URI parentUri = parent.toUri();

    if (extension.isEmpty()) {
        return parent;
    }/*from w w  w  . j  av  a2 s .  com*/

    final String path = parentUri.getPath();
    String extendedPath;
    if (path.isEmpty()) {
        if (extension.charAt(0) == Path.SEPARATOR_CHAR) {
            extendedPath = extension;
        } else {
            extendedPath = Path.SEPARATOR + extension;
        }
    } else {
        if (path.charAt(path.length() - 1) == Path.SEPARATOR_CHAR) {
            if (extension.charAt(0) == Path.SEPARATOR_CHAR) {
                if (extension.length() > 1) {
                    extendedPath = path + extension.substring(1);
                } else {
                    extendedPath = path;
                }
            } else {
                extendedPath = path + extension;
            }
        } else {
            if (extension.charAt(0) == Path.SEPARATOR_CHAR) {
                extendedPath = path + extension;
            } else {
                extendedPath = path + Path.SEPARATOR + extension;
            }
        }
    }

    try {
        final URI extendedUri = new URI(parentUri.getScheme(),
                ((parentUri.getAuthority() != null) ? parentUri.getAuthority() : ""), extendedPath,
                parentUri.getQuery(), parentUri.getFragment());
        return new Path(extendedUri);
    } catch (URISyntaxException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}

From source file:org.eclipse.orion.server.git.servlets.GitCommitHandlerV1.java

private boolean identifyNewCommitResource(HttpServletRequest request, HttpServletResponse response,
        Repository db, String newCommit) throws ServletException {
    try {/*from  w ww . j  a  va2s .  c o  m*/
        URI u = getURI(request);
        IPath p = new Path(u.getPath());
        IPath np = new Path("/"); //$NON-NLS-1$
        for (int i = 0; i < p.segmentCount(); i++) {
            String s = p.segment(i);
            if (i == 2) {
                s += ".." + newCommit; //$NON-NLS-1$
            }
            np = np.append(s);
        }
        if (p.hasTrailingSeparator())
            np = np.addTrailingSeparator();
        URI nu = new URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), np.toString(), u.getQuery(),
                u.getFragment());
        JSONObject result = new JSONObject();
        result.put(ProtocolConstants.KEY_LOCATION, nu);
        OrionServlet.writeJSONResponse(request, response, result);
        response.setHeader(ProtocolConstants.HEADER_LOCATION, resovleOrionURI(request, nu).toString());
        return true;
    } catch (Exception e) {
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "An error occured when identifying a new Commit resource.", e));
    }
}

From source file:org.apache.hive.jdbc.Utils.java

/**
 * Parse JDBC connection URL/* ww  w .j  a va2  s .co  m*/
 * The new format of the URL is:
 * jdbc:hive2://<host1>:<port1>,<host2>:<port2>/dbName;sess_var_list?hive_conf_list#hive_var_list
 * where the optional sess, conf and var lists are semicolon separated <key>=<val> pairs.
 * For utilizing dynamic service discovery with HiveServer2 multiple comma separated host:port pairs can
 * be specified as shown above.
 * The JDBC driver resolves the list of uris and picks a specific server instance to connect to.
 * Currently, dynamic service discovery using ZooKeeper is supported, in which case the host:port pairs represent a ZooKeeper ensemble.
 *
 * As before, if the host/port is not specified, it the driver runs an embedded hive.
 * examples -
 *  jdbc:hive2://ubuntu:11000/db2?hive.cli.conf.printheader=true;hive.exec.mode.local.auto.inputbytes.max=9999#stab=salesTable;icol=customerID
 *  jdbc:hive2://?hive.cli.conf.printheader=true;hive.exec.mode.local.auto.inputbytes.max=9999#stab=salesTable;icol=customerID
 *  jdbc:hive2://ubuntu:11000/db2;user=foo;password=bar
 *
 *  Connect to http://server:10001/hs2, with specified basicAuth credentials and initial database:
 *  jdbc:hive2://server:10001/db;user=foo;password=bar?hive.server2.transport.mode=http;hive.server2.thrift.http.path=hs2
 *
 * @param uri
 * @return
 * @throws SQLException
 */
public static JdbcConnectionParams parseURL(String uri)
        throws JdbcUriParseException, SQLException, ZooKeeperHiveClientException {
    JdbcConnectionParams connParams = new JdbcConnectionParams();

    if (!uri.startsWith(URL_PREFIX)) {
        throw new JdbcUriParseException("Bad URL format: Missing prefix " + URL_PREFIX);
    }

    // For URLs with no other configuration
    // Don't parse them, but set embedded mode as true
    if (uri.equalsIgnoreCase(URL_PREFIX)) {
        connParams.setEmbeddedMode(true);
        return connParams;
    }

    // The JDBC URI now supports specifying multiple host:port if dynamic service discovery is
    // configured on HiveServer2 (like: host1:port1,host2:port2,host3:port3)
    // We'll extract the authorities (host:port combo) from the URI, extract session vars, hive
    // confs & hive vars by parsing it as a Java URI.
    // To parse the intermediate URI as a Java URI, we'll give a dummy authority(dummy:00000).
    // Later, we'll substitute the dummy authority for a resolved authority.
    String dummyAuthorityString = "dummyhost:00000";
    String suppliedAuthorities = getAuthorities(uri, connParams);
    if ((suppliedAuthorities == null) || (suppliedAuthorities.isEmpty())) {
        // Given uri of the form:
        // jdbc:hive2:///dbName;sess_var_list?hive_conf_list#hive_var_list
        connParams.setEmbeddedMode(true);
    } else {
        LOG.info("Supplied authorities: " + suppliedAuthorities);
        String[] authorityList = suppliedAuthorities.split(",");
        connParams.setSuppliedAuthorityList(authorityList);
        uri = uri.replace(suppliedAuthorities, dummyAuthorityString);
    }

    // Now parse the connection uri with dummy authority
    URI jdbcURI = URI.create(uri.substring(URI_JDBC_PREFIX.length()));

    // key=value pattern
    Pattern pattern = Pattern.compile("([^;]*)=([^;]*)[;]?");

    // dbname and session settings
    String sessVars = jdbcURI.getPath();
    if ((sessVars != null) && !sessVars.isEmpty()) {
        String dbName = "";
        // removing leading '/' returned by getPath()
        sessVars = sessVars.substring(1);
        if (!sessVars.contains(";")) {
            // only dbname is provided
            dbName = sessVars;
        } else {
            // we have dbname followed by session parameters
            dbName = sessVars.substring(0, sessVars.indexOf(';'));
            sessVars = sessVars.substring(sessVars.indexOf(';') + 1);
            if (sessVars != null) {
                Matcher sessMatcher = pattern.matcher(sessVars);
                while (sessMatcher.find()) {
                    if (connParams.getSessionVars().put(sessMatcher.group(1), sessMatcher.group(2)) != null) {
                        throw new JdbcUriParseException(
                                "Bad URL format: Multiple values for property " + sessMatcher.group(1));
                    }
                }
            }
        }
        if (!dbName.isEmpty()) {
            connParams.setDbName(dbName);
        }
    }

    // parse hive conf settings
    String confStr = jdbcURI.getQuery();
    if (confStr != null) {
        Matcher confMatcher = pattern.matcher(confStr);
        while (confMatcher.find()) {
            connParams.getHiveConfs().put(confMatcher.group(1), confMatcher.group(2));
        }
    }

    // parse hive var settings
    String varStr = jdbcURI.getFragment();
    if (varStr != null) {
        Matcher varMatcher = pattern.matcher(varStr);
        while (varMatcher.find()) {
            connParams.getHiveVars().put(varMatcher.group(1), varMatcher.group(2));
        }
    }

    // Handle all deprecations here:
    String newUsage;
    String usageUrlBase = "jdbc:hive2://<host>:<port>/dbName;";
    // Handle deprecation of AUTH_QOP_DEPRECATED
    newUsage = usageUrlBase + JdbcConnectionParams.AUTH_QOP + "=<qop_value>";
    handleParamDeprecation(connParams.getSessionVars(), connParams.getSessionVars(),
            JdbcConnectionParams.AUTH_QOP_DEPRECATED, JdbcConnectionParams.AUTH_QOP, newUsage);

    // Handle deprecation of TRANSPORT_MODE_DEPRECATED
    newUsage = usageUrlBase + JdbcConnectionParams.TRANSPORT_MODE + "=<transport_mode_value>";
    handleParamDeprecation(connParams.getHiveConfs(), connParams.getSessionVars(),
            JdbcConnectionParams.TRANSPORT_MODE_DEPRECATED, JdbcConnectionParams.TRANSPORT_MODE, newUsage);

    // Handle deprecation of HTTP_PATH_DEPRECATED
    newUsage = usageUrlBase + JdbcConnectionParams.HTTP_PATH + "=<http_path_value>";
    handleParamDeprecation(connParams.getHiveConfs(), connParams.getSessionVars(),
            JdbcConnectionParams.HTTP_PATH_DEPRECATED, JdbcConnectionParams.HTTP_PATH, newUsage);

    // Extract host, port
    if (connParams.isEmbeddedMode()) {
        // In case of embedded mode we were supplied with an empty authority.
        // So we never substituted the authority with a dummy one.
        connParams.setHost(jdbcURI.getHost());
        connParams.setPort(jdbcURI.getPort());
    } else {
        // Else substitute the dummy authority with a resolved one.
        // In case of dynamic service discovery using ZooKeeper, it picks a server uri from ZooKeeper
        String resolvedAuthorityString = resolveAuthority(connParams);
        LOG.info("Resolved authority: " + resolvedAuthorityString);
        uri = uri.replace(dummyAuthorityString, resolvedAuthorityString);
        connParams.setJdbcUriString(uri);
        // Create a Java URI from the resolved URI for extracting the host/port
        URI resolvedAuthorityURI = null;
        try {
            resolvedAuthorityURI = new URI(null, resolvedAuthorityString, null, null, null);
        } catch (URISyntaxException e) {
            throw new JdbcUriParseException("Bad URL format: ", e);
        }
        connParams.setHost(resolvedAuthorityURI.getHost());
        connParams.setPort(resolvedAuthorityURI.getPort());
    }

    return connParams;
}

From source file:com.github.brandtg.pantopod.crawler.CrawlingEventHandler.java

private URI getNextUri(URI url, String href, String chroot) throws Exception {
    //    if (href.contains("..")) {
    ///*from  w w w . j a  va 2  s .c o m*/
    //      throw new IllegalArgumentException("Relative URI not allowed: " + href);
    //    }

    URI hrefUri = URI.create(href.trim().replaceAll(" ", "+"));

    URIBuilder builder = new URIBuilder();

    builder.setScheme(hrefUri.getScheme() == null ? url.getScheme() : hrefUri.getScheme());
    builder.setHost(hrefUri.getHost() == null ? url.getHost() : hrefUri.getHost());
    builder.setPort(hrefUri.getPort() == -1 ? url.getPort() : hrefUri.getPort());

    if (hrefUri.getPath() != null) {
        StringBuilder path = new StringBuilder();
        if (hrefUri.getHost() == null && chroot != null) {
            path.append(chroot);
        }

        // Ensure no two slashes
        if (hrefUri.getPath() != null && hrefUri.getPath().length() > 0 && hrefUri.getPath().charAt(0) == '/'
                && path.length() > 0 && path.charAt(path.length() - 1) == '/') {
            path.setLength(path.length() - 1);
        }

        path.append(hrefUri.getPath());

        builder.setPath(chroot == null ? "" : chroot + hrefUri.getPath());
    }
    if (hrefUri.getQuery() != null) {
        builder.setCustomQuery(hrefUri.getQuery());
    }
    if (hrefUri.getFragment() != null) {
        builder.setFragment(hrefUri.getFragment());
    }

    return builder.build();
}

From source file:org.paxle.crawler.smb.impl.SmbCrawler.java

public ICrawlerDocument request(URI requestUri) {
    if (requestUri == null)
        throw new NullPointerException("URL was null");
    this.logger.info(String.format("Crawling URL '%s' ...", requestUri));

    ICrawlerDocument crawlerDoc = null;//from   w ww.  ja va  2 s .  c om
    InputStream input = null;
    try {
        final ICrawlerContext ctx = this.contextLocal.getCurrentContext();

        // creating an empty crawler-document
        crawlerDoc = ctx.createDocument();
        crawlerDoc.setCrawlerDate(new Date());
        crawlerDoc.setLocation(requestUri);

        /* 
         * Create a temp URI to ensure that the port is set properly
         * This is required otherwise jcifs throws an exception.
         */
        URI temp = new URI(requestUri.getScheme(), requestUri.getUserInfo(), requestUri.getHost(),
                (requestUri.getPort() == -1) ? 445 : requestUri.getPort(), requestUri.getPath(),
                requestUri.getQuery(), requestUri.getFragment());

        SmbFile smbFile = new SmbFile(temp.toURL());
        if (!smbFile.exists()) {
            crawlerDoc.setStatus(Status.NOT_FOUND, "The resource does not exist");
            this.logger.info(String.format("The resource '%s' does not exit.", requestUri));
            return crawlerDoc;
        } else if (!smbFile.canRead()) {
            crawlerDoc.setStatus(Status.NOT_FOUND, "The resource can not be read.");
            this.logger.info(String.format("The resource '%s' can not be read.", requestUri));
            return crawlerDoc;
        }

        final ICrawlerTools crawlerTools = ctx.getCrawlerTools();
        if (smbFile.isDirectory()) {
            /* Append '/' if necessary. Otherwise we will get:
             * jcifs.smb.SmbException: smb://srver/dir directory must end with '/'
             */
            // XXX still needed with the SmbFile(URL)-constructor?
            String uriString = requestUri.toASCIIString();
            if (!uriString.endsWith("/")) {
                uriString += "/";
                smbFile = new SmbFile(uriString);
            }

            // set the mimetype accordingly
            crawlerDoc.setMimeType("text/html");

            // using the dir creation date as last-mod date
            long creationTimeStamp = smbFile.createTime();
            if (creationTimeStamp != 0) {
                crawlerDoc.setLastModDate(new Date(creationTimeStamp));
            }

            // getting the content of the directory
            SmbFile[] smbFiles = smbFile.listFiles();
            final Iterator<DirlistEntry> dirlistIt = new DirlistIterator(smbFiles, false);

            // generate & save dir listing
            crawlerTools.saveListing(crawlerDoc, dirlistIt, true, smbFiles.length > 50 // if more than 50 files, use compression
            );
        } else if (smbFile.isFile()) {
            // last modified timestamp
            long modTimeStamp = smbFile.getLastModified();
            if (modTimeStamp != 0) {
                crawlerDoc.setLastModDate(new Date(modTimeStamp));
            }

            // get file content
            input = smbFile.getInputStream();
        }

        if (input != null) {
            // copy data into file
            crawlerTools.saveInto(crawlerDoc, input);

            // finished
            crawlerDoc.setStatus(Status.OK);
        } else {
            crawlerDoc.setStatus(Status.UNKNOWN_FAILURE, "Unable to determine the smb-file type");
        }
    } catch (Throwable e) {
        crawlerDoc.setStatus(Status.UNKNOWN_FAILURE, "Unexpected Exception: " + e.getMessage());

        this.logger.warn(String.format("Unexpected '%s' while trying to crawl resource '%s'.",
                e.getClass().getName(), requestUri), e);
    } finally {
        if (input != null)
            try {
                input.close();
            } catch (Exception e) {
                /* ignore this */}
    }

    return crawlerDoc;
}