Example usage for java.net URI getAuthority

List of usage examples for java.net URI getAuthority

Introduction

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

Prototype

public String getAuthority() 

Source Link

Document

Returns the decoded authority component of this URI.

Usage

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

@Test
public void testReplDumpResultSet() throws Exception {
    String tid = TestJdbcWithMiniHS2.class.getCanonicalName().toLowerCase().replace('.', '_') + "_"
            + System.currentTimeMillis();
    String testPathName = System.getProperty("test.warehouse.dir", "/tmp") + Path.SEPARATOR + tid;
    Path testPath = new Path(testPathName);
    FileSystem fs = testPath.getFileSystem(new HiveConf());
    Statement stmt = conDefault.createStatement();
    try {/*w  w w . j  av  a  2  s  .c o  m*/
        stmt.execute("set hive.repl.rootdir = " + testPathName);
        ResultSet rs = stmt.executeQuery("repl dump " + testDbName);
        ResultSetMetaData rsMeta = rs.getMetaData();
        assertEquals(2, rsMeta.getColumnCount());
        int numRows = 0;
        while (rs.next()) {
            numRows++;
            URI uri = new URI(rs.getString(1));
            int notificationId = rs.getInt(2);
            assertNotNull(uri);
            assertEquals(testPath.toUri().getScheme(), uri.getScheme());
            assertEquals(testPath.toUri().getAuthority(), uri.getAuthority());
            // In test setup, we append '/next' to hive.repl.rootdir and use that as the dump location
            assertEquals(testPath.toUri().getPath() + "/next", uri.getPath());
            assertNotNull(notificationId);
        }
        assertEquals(1, numRows);
    } finally {
        // Clean up
        fs.delete(testPath, true);
    }
}

From source file:org.opentravel.schemacompiler.repository.impl.RemoteRepositoryClient.java

/**
 * @see org.opentravel.schemacompiler.repository.Repository#getRepositoryItem(java.lang.String,
 *      java.lang.String)//from w w w .ja  v  a 2 s .c  om
 */
@Override
public RepositoryItem getRepositoryItem(String itemUri, String itemNamespace)
        throws RepositoryException, URISyntaxException {
    URI uri = RepositoryUtils.toRepositoryItemUri(itemUri);
    RepositoryItem item;

    if ((uri.getAuthority() == null) || !uri.getAuthority().equals(id)) {
        throw new RepositoryException(
                "Unable to retrieve the requested item becuase it does not belong to this repository.");
    }
    VersionSchemeFactory vsFactory = VersionSchemeFactory.getInstance();
    String[] uriParts = RepositoryUtils.parseRepositoryItemUri(uri);
    String versionScheme = (uriParts[3] == null) ? vsFactory.getDefaultVersionScheme() : uriParts[3];

    if (itemNamespace == null) {
        itemNamespace = uriParts[1];
    }
    if (itemNamespace == null) {
        throw new RepositoryException(
                "Unable to identify the repository item because its namespace was not specified.");
    }
    try {
        VersionScheme vScheme = vsFactory.getVersionScheme(versionScheme);
        String baseNS = vScheme.getBaseNamespace(itemNamespace);
        String versionIdentifier = vScheme.getVersionIdentifier(itemNamespace);

        downloadContent(baseNS, uriParts[2], versionIdentifier, false);

        LibraryInfoType libraryMetadata = manager.getFileManager().loadLibraryMetadata(baseNS, uriParts[2],
                versionIdentifier);

        if ((libraryMetadata == null) || !libraryMetadata.getOwningRepository().equals(uriParts[0])) {
            throw new RepositoryException("Resource not found in repository: " + uriParts[0]);
        }
        item = newRepositoryItem(libraryMetadata);

    } catch (VersionSchemeException e) {
        throw new RepositoryException("Unknown version scheme specified by URI: " + versionScheme);
    }
    return item;
}

From source file:vitro.vgw.wsiadapter.TCSWSIAdapter.java

void doRequest(String method, URI uri) {

    Request request = newRequest(method);
    URI reqUri = uri;/*  ww w.  j a v a 2s. c o m*/

    //      if(useCoapProxy) {
    //         System.out.println("Setting proxy for request");
    //         request.setOption(new Option(uri.toString(), OptionNumberRegistry.PROXY_URI));
    //      }

    if (method.equals("OBSERVE")) {
        request.setOption(new Option(0, OptionNumberRegistry.OBSERVE));
    }

    // set request URI
    if (method.equals("DISCOVER")
            && (uri.getPath() == null || uri.getPath().isEmpty() || uri.getPath().equals("/"))) {
        // add discovery resource path to URI
        try {
            reqUri = new URI(uri.getScheme(), uri.getAuthority(), DISCOVERY_RESOURCE, uri.getQuery());

        } catch (URISyntaxException e) {
            System.err.println("Failed to parse URI: " + e.getMessage());
            System.exit(ERR_BAD_URI);
        }

    }

    if (useCoapProxy) {
        System.out.println("Setting proxy for request");
        request.setOption(new Option(reqUri.toString(), OptionNumberRegistry.PROXY_URI));
        //         request.setURI(reqUri);
        // override peer address with proxy address
        EndpointAddress a = new EndpointAddress(proxyUri);
        request.setPeerAddress(a);
    } else {
        System.out.println("No proxy for request");
        request.setURI(reqUri);
    }
    request.setPayload("");
    request.setToken(TokenManager.getInstance().acquireToken());
    System.out.println("Ici???s");

    ResponseHandler respHandler = new ResponseHandler() {

        public void handleResponse(Response response) {
            System.out.println("On arrive ici");
            doHandleResponse(response);
        }
    };
    request.registerResponseHandler(respHandler);
    System.out.println("response handler registered");
    // enable response queue in order to use blocking I/O
    //   request.enableResponseQueue(true);

    //
    request.prettyPrint();

    pendingRequests.add(request);
    System.out.println("request stored");
    // execute request
    try {
        request.execute();
        System.out.println("request executed");
    } catch (UnknownHostException e) {
        System.err.println("Unknown host: " + e.getMessage());
        System.exit(ERR_REQUEST_FAILED);
    } catch (IOException e) {
        System.err.println("Failed to execute request: " + e.getMessage());
        System.exit(ERR_REQUEST_FAILED);
    }
}

From source file:org.apache.jxtadoop.fs.FileSystem.java

/** Check that a Path belongs to this FileSystem. */
protected void checkPath(Path path) {
    URI uri = path.toUri();
    if (uri.getScheme() == null) // fs is relative 
        return;//  ww w . ja  va  2s . com
    String thisScheme = this.getUri().getScheme();
    String thatScheme = uri.getScheme();
    String thisAuthority = this.getUri().getAuthority();
    String thatAuthority = uri.getAuthority();
    //authority and scheme are not case sensitive
    if (thisScheme.equalsIgnoreCase(thatScheme)) {// schemes match
        if (thisAuthority == thatAuthority || // & authorities match
                (thisAuthority != null && thisAuthority.equalsIgnoreCase(thatAuthority)))
            return;

        if (thatAuthority == null && // path's authority is null
                thisAuthority != null) { // fs has an authority
            URI defaultUri = getDefaultUri(getConf()); // & is the conf default 
            if (thisScheme.equalsIgnoreCase(defaultUri.getScheme())
                    && thisAuthority.equalsIgnoreCase(defaultUri.getAuthority()))
                return;
            try { // or the default fs's uri
                defaultUri = get(getConf()).getUri();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (thisScheme.equalsIgnoreCase(defaultUri.getScheme())
                    && thisAuthority.equalsIgnoreCase(defaultUri.getAuthority()))
                return;
        }
    }
    throw new IllegalArgumentException("Wrong FS: " + path + ", expected: " + this.getUri());
}

From source file:org.openconcerto.openoffice.ODSingleXMLDocument.java

/**
 * Prefix a path.//from   w  w w. j  a  v  a 2  s . co  m
 * 
 * @param href a path inside the pkg, eg "./Object 1/content.xml".
 * @return the prefixed path or <code>null</code> if href is external, eg "./3_Object
 *         1/content.xml".
 */
private String prefixPath(final String href) {
    if (this.getVersion().equals(XMLVersion.OOo)) {
        // in OOo 1.x inPKG is denoted by a #
        final boolean sharp = href.startsWith("#");
        if (sharp)
            // eg #Pictures/100000000000006C000000ABCC02339E.png
            return "#" + this.prefix(href.substring(1));
        else
            // eg ../../../../Program%20Files/OpenOffice.org1.1.5/share/gallery/apples.gif
            return null;
    } else {
        URI uri;
        try {
            uri = new URI(href);
        } catch (URISyntaxException e) {
            // OO doesn't escape characters for files
            uri = null;
        }
        // section 17.5
        final boolean inPKGFile = uri == null
                || uri.getScheme() == null && uri.getAuthority() == null && uri.getPath().charAt(0) != '/';
        if (inPKGFile) {
            final String dotSlash = "./";
            if (href.startsWith(dotSlash))
                return dotSlash + this.prefix(href.substring(dotSlash.length()));
            else
                return this.prefix(href);
        } else
            return null;
    }
}

From source file:org.apache.hadoop.fs.FileSystem.java

/** Check that a Path belongs to this FileSystem. */
protected void checkPath(Path path) {
    URI uri = path.toUri();/*ww w  . jav a 2  s  . c  om*/
    String thatScheme = uri.getScheme();
    if (thatScheme == null) // fs is relative 
        return;
    URI thisUri = getCanonicalUri();
    String thisScheme = thisUri.getScheme();
    //authority and scheme are not case sensitive
    if (thisScheme.equalsIgnoreCase(thatScheme)) {// schemes match
        String thisAuthority = thisUri.getAuthority();
        String thatAuthority = uri.getAuthority();
        if (thatAuthority == null && // path's authority is null
                thisAuthority != null) { // fs has an authority
            URI defaultUri = getDefaultUri(getConf());
            if (thisScheme.equalsIgnoreCase(defaultUri.getScheme())) {
                uri = defaultUri; // schemes match, so use this uri instead
            } else {
                uri = null; // can't determine auth of the path
            }
        }
        if (uri != null) {
            // canonicalize uri before comparing with this fs
            uri = NetUtils.getCanonicalUri(uri, getDefaultPort());
            thatAuthority = uri.getAuthority();
            if (thisAuthority == thatAuthority || // authorities match
                    (thisAuthority != null && thisAuthority.equalsIgnoreCase(thatAuthority)))
                return;
        }
    }
    throw new IllegalArgumentException("Wrong FS: " + path + ", expected: " + this.getUri());
}

From source file:org.apache.hive.beeline.HiveSchemaTool.java

/**
 * Check if the location is valid for the given entity
 * @param entity          the entity to represent a database, partition or table
 * @param entityLocation  the location/*  w  w w  .  j  a va  2 s .  co  m*/
 * @param defaultServers  a list of the servers that the location needs to match.
 *                        The location host needs to match one of the given servers.
 *                        If empty, then no check against such list.
 * @return true if the location is valid
 */
private boolean checkLocation(String entity, String entityLocation, URI[] defaultServers) {
    boolean isValid = true;
    if (entityLocation == null) {
        System.err.println(entity + ", Error: empty location");
        isValid = false;
    } else {
        try {
            URI currentUri = new Path(entityLocation).toUri();
            String scheme = currentUri.getScheme();
            String path = currentUri.getPath();
            if (StringUtils.isEmpty(scheme)) {
                System.err.println(
                        entity + ", Location: " + entityLocation + ", Error: missing location scheme.");
                isValid = false;
            } else if (StringUtils.isEmpty(path)) {
                System.err
                        .println(entity + ", Location: " + entityLocation + ", Error: missing location path.");
                isValid = false;
            } else if (ArrayUtils.isNotEmpty(defaultServers) && currentUri.getAuthority() != null) {
                String authority = currentUri.getAuthority();
                boolean matchServer = false;
                for (URI server : defaultServers) {
                    if (StringUtils.equalsIgnoreCase(server.getScheme(), scheme)
                            && StringUtils.equalsIgnoreCase(server.getAuthority(), authority)) {
                        matchServer = true;
                        break;
                    }
                }
                if (!matchServer) {
                    System.err
                            .println(entity + ", Location: " + entityLocation + ", Error: mismatched server.");
                    isValid = false;
                }
            }

            // if there is no path element other than "/", report it but not fail
            if (isValid && StringUtils.containsOnly(path, "/")) {
                System.err.println(entity + ", Location: " + entityLocation
                        + ", Warn: location set to root, not a recommended config.");
            }
        } catch (Exception pe) {
            System.err.println(entity + ", Error: invalid location - " + pe.getMessage());
            isValid = false;
        }
    }

    return isValid;
}

From source file:org.opentravel.schemacompiler.repository.RepositoryManager.java

/**
 * @see org.opentravel.schemacompiler.repository.Repository#getRepositoryItem(java.lang.String)
 *//* w  w  w . j a  v a  2  s.  c  o m*/
@Override
public RepositoryItem getRepositoryItem(String itemUri) throws RepositoryException, URISyntaxException {
    URI uri = RepositoryUtils.toRepositoryItemUri(itemUri);
    Repository repository = getRepository(uri.getAuthority());
    RepositoryItem item;

    if (repository == null) {
        throw new RepositoryException("Unknown repository ID: " + uri.getAuthority());
    }
    if (repository == this) {
        item = null;
    } else {
        item = repository.getRepositoryItem(itemUri, null);
    }
    return item;
}

From source file:org.opentravel.schemacompiler.repository.RepositoryManager.java

/**
 * @see org.opentravel.schemacompiler.repository.Repository#getRepositoryItem(java.lang.String,
 *      java.lang.String)//from  w w  w.  ja v a2s  .c om
 */
@Override
public RepositoryItem getRepositoryItem(String itemUri, String itemNamespace)
        throws RepositoryException, URISyntaxException {
    URI uri = RepositoryUtils.toRepositoryItemUri(itemUri);
    Repository repository = getRepository(uri.getAuthority());
    RepositoryItem item;

    if (repository == null) {
        throw new RepositoryException("Unknown repository ID: " + uri.getAuthority());
    }
    if (repository == this) {
        VersionSchemeFactory vsFactory = VersionSchemeFactory.getInstance();
        String[] uriParts = RepositoryUtils.parseRepositoryItemUri(uri);
        String versionScheme = (uriParts[3] == null) ? vsFactory.getDefaultVersionScheme() : uriParts[3];

        if (itemNamespace == null) {
            itemNamespace = uriParts[1];
        }
        if (itemNamespace == null) {
            throw new RepositoryException(
                    "Unable to identify the repository item because its namespace was not specified.");
        }
        try {
            VersionScheme vScheme = vsFactory.getVersionScheme(versionScheme);
            String baseNS = vScheme.getBaseNamespace(itemNamespace);
            String versionIdentifier = vScheme.getVersionIdentifier(itemNamespace);

            LibraryInfoType libraryMetadata = fileManager.loadLibraryMetadata(baseNS, uriParts[2],
                    versionIdentifier);

            if ((libraryMetadata == null) || !libraryMetadata.getOwningRepository().equals(uriParts[0])) {
                throw new RepositoryException("Resource not found in repository: " + uriParts[0]);
            }
            RepositoryItemImpl itemImpl = RepositoryUtils.createRepositoryItem(this, libraryMetadata);
            RepositoryUtils.checkItemState(itemImpl, this);
            item = itemImpl;

        } catch (VersionSchemeException e) {
            throw new RepositoryException("Unknown version scheme specified by URI: " + versionScheme);
        }
    } else {
        item = repository.getRepositoryItem(itemUri, itemNamespace);
    }
    return item;
}