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:com.buaa.cfs.fs.FileSystem.java

/**
 * Returns the FileSystem for this URI's scheme and authority.  The scheme of the URI determines a configuration
 * property name, <tt>fs.<i>scheme</i>.class</tt> whose value names the FileSystem class. The entire URI is passed
 * to the FileSystem instance's initialize method. This always returns a new FileSystem object.
 *//*from  w ww . ja  v a 2s. c  om*/
public static FileSystem newInstance(URI uri, Configuration conf) throws IOException {
    String scheme = uri.getScheme();
    String authority = uri.getAuthority();

    if (scheme == null) { // no scheme: use default FS
        return newInstance(conf);
    }

    if (authority == null) { // no authority
        URI defaultUri = getDefaultUri(conf);
        if (scheme.equals(defaultUri.getScheme()) // if scheme matches default
                && defaultUri.getAuthority() != null) { // & default has authority
            return newInstance(defaultUri, conf); // return default
        }
    }
    return CACHE.getUnique(uri, conf);
}

From source file:com.buaa.cfs.fs.FileSystem.java

/**
 * Returns the FileSystem for this URI's scheme and authority.  The scheme of the URI determines a configuration
 * property name, <tt>fs.<i>scheme</i>.class</tt> whose value names the FileSystem class. The entire URI is passed
 * to the FileSystem instance's initialize method.
 *//*  w w  w  . ja  v a 2 s . c om*/
public static FileSystem get(URI uri, Configuration conf) throws IOException {
    String scheme = uri.getScheme();
    String authority = uri.getAuthority();

    if (scheme == null && authority == null) { // use default FS
        return get(conf);
    }

    if (scheme != null && authority == null) { // no authority
        URI defaultUri = getDefaultUri(conf);
        if (scheme.equals(defaultUri.getScheme()) // if scheme matches default
                && defaultUri.getAuthority() != null) { // & default has authority
            return get(defaultUri, conf); // return default
        }
    }

    String disableCacheName = String.format("fs.%s.impl.disable.cache", scheme);
    if (conf.getBoolean(disableCacheName, false)) {
        return createFileSystem(uri, conf);
    }

    return CACHE.get(uri, conf);
}

From source file:com.sina.cloudstorage.services.scs.SCSClient.java

/**
 * Converts the current endpoint set for this client into virtual addressing
 * style, by placing the name of the specified bucket before the S3 service
 * endpoint./*from www . j  av a 2 s.co m*/
 *
 * @param bucketName
 *            The name of the bucket to use in the virtual addressing style
 *            of the returned URI.
 *
 * @return A new URI, creating from the current service endpoint URI and the
 *         specified bucket.
 */
private URI convertToVirtualHostEndpoint(String bucketName, URI endpoint) {
    try {
        return new URI(endpoint.getScheme() + "://" + bucketName + "." + endpoint.getAuthority());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Invalid bucket name: " + bucketName, e);
    }
}

From source file:core.com.qiniu.http.AmazonHttpClient.java

/**
 * Internal method to execute the HTTP method given.
 *
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler,
 *      HttpResponseHandler)//from  w ww. ja  va 2s  .com
 * @see AmazonHttpClient#execute(Request, HttpResponseHandler,
 *      HttpResponseHandler, ExecutionContext)
 */
<T> Response<T> executeHelper(Request<?> request,
        HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
        HttpResponseHandler<AmazonServiceException> errorResponseHandler, ExecutionContext executionContext)
        throws AmazonClientException, AmazonServiceException {
    /*
     * Depending on which response handler we end up choosing to handle the
     * HTTP response, it might require us to leave the underlying HTTP
     * connection open, depending on whether or not it reads the complete
     * HTTP response stream from the HTTP connection, or if delays reading
     * any of the content until after a response is returned to the caller.
     */
    boolean leaveHttpConnectionOpen = false;
    AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
    /*
     * add the service endpoint to the logs. You can infer service name from
     * service endpoint
     */
    awsRequestMetrics.addProperty(AWSRequestMetrics.Field.ServiceName, request.getServiceName());
    awsRequestMetrics.addProperty(AWSRequestMetrics.Field.ServiceEndpoint, request.getEndpoint());

    // Apply whatever request options we know how to handle, such as
    // user-agent.
    setUserAgent(request);
    request.addHeader(HEADER_SDK_TRANSACTION_ID, UUID.randomUUID().toString());
    int requestCount = 0;
    long lastBackoffDelay = 0;
    URI redirectedURI = null;
    AmazonClientException retriedException = null;

    // Make a copy of the original request params and headers so that we can
    // permute it in this loop and start over with the original every time.
    Map<String, String> originalParameters = new LinkedHashMap<String, String>(request.getParameters());
    Map<String, String> originalHeaders = new HashMap<String, String>(request.getHeaders());
    // mark input stream if supported
    InputStream originalContent = request.getContent();
    if (originalContent != null && originalContent.markSupported()) {
        originalContent.mark(-1);
    }

    final AWSCredentials credentials = executionContext.getCredentials();
    Signer signer = null;
    HttpResponse httpResponse = null;
    HttpRequest httpRequest = null;

    while (true) {
        ++requestCount;
        awsRequestMetrics.setCounter(AWSRequestMetrics.Field.RequestCount, requestCount);
        if (requestCount > 1) { // retry
            request.setParameters(originalParameters);
            request.setHeaders(originalHeaders);
            request.setContent(originalContent);
        }
        if (redirectedURI != null) {
            request.setEndpoint(URI.create(redirectedURI.getScheme() + "://" + redirectedURI.getAuthority()));
            request.setResourcePath(redirectedURI.getPath());
        }

        try {
            if (requestCount > 1) { // retry
                awsRequestMetrics.startEvent(AWSRequestMetrics.Field.RetryPauseTime);
                try {
                    lastBackoffDelay = pauseBeforeNextRetry(request.getOriginalRequest(), retriedException,
                            requestCount, config.getRetryPolicy());
                } finally {
                    awsRequestMetrics.endEvent(AWSRequestMetrics.Field.RetryPauseTime);
                }
                InputStream content = request.getContent();
                if (content != null && content.markSupported()) {
                    content.reset();
                }
            }
            request.addHeader(HEADER_SDK_RETRY_INFO, (requestCount - 1) + "/" + lastBackoffDelay);

            // Sign the request if a signer was provided
            if (signer == null)
                signer = executionContext.getSignerByURI(request.getEndpoint());
            if (signer != null && credentials != null) {
                awsRequestMetrics.startEvent(AWSRequestMetrics.Field.RequestSigningTime);
                try {
                    signer.sign(request, credentials);
                } finally {
                    awsRequestMetrics.endEvent(AWSRequestMetrics.Field.RequestSigningTime);
                }
            }

            if (requestLog.isDebugEnabled()) {
                requestLog.debug("Sending Request: " + request.toString());
            }

            httpRequest = requestFactory.createHttpRequest(request, config, executionContext);

            retriedException = null;
            awsRequestMetrics.startEvent(AWSRequestMetrics.Field.HttpRequestTime);
            try {
                httpResponse = httpClient.execute(httpRequest);
            } finally {
                awsRequestMetrics.endEvent(AWSRequestMetrics.Field.HttpRequestTime);
            }

            if (isRequestSuccessful(httpResponse)) {
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.StatusCode, httpResponse.getStatusCode());
                /*
                 * If we get back any 2xx status code, then we know we
                 * should treat the service call as successful.
                 */
                leaveHttpConnectionOpen = responseHandler.needsConnectionLeftOpen();
                T response = handleResponse(request, responseHandler, httpResponse, executionContext);
                return new Response<T>(response, httpResponse);
            } else if (isTemporaryRedirect(httpResponse)) {
                /*
                 * S3 sends 307 Temporary Redirects if you try to delete an
                 * EU bucket from the US endpoint. If we get a 307, we'll
                 * point the HTTP method to the redirected location, and let
                 * the next retry deliver the request to the right location.
                 */
                String redirectedLocation = httpResponse.getHeaders().get("Location");
                log.debug("Redirecting to: " + redirectedLocation);
                // set redirect uri and retry
                redirectedURI = URI.create(redirectedLocation);
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.StatusCode, httpResponse.getStatusCode());
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.RedirectLocation, redirectedLocation);
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSRequestID, null);
            } else {
                leaveHttpConnectionOpen = errorResponseHandler.needsConnectionLeftOpen();
                AmazonServiceException ase = handleErrorResponse(request, errorResponseHandler, httpResponse);
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSRequestID, ase.getRequestId());
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSErrorCode, ase.getErrorCode());
                awsRequestMetrics.addProperty(AWSRequestMetrics.Field.StatusCode, ase.getStatusCode());

                if (!shouldRetry(request.getOriginalRequest(), httpRequest.getContent(), ase, requestCount,
                        config.getRetryPolicy())) {
                    throw ase;
                }

                // Cache the retryable exception
                retriedException = ase;
                /*
                 * Checking for clock skew error again because we don't want
                 * to set the global time offset for every service
                 * exception.
                 */
                if (RetryUtils.isClockSkewError(ase)) {
                    int timeOffset = parseClockSkewOffset(httpResponse, ase);
                    SDKGlobalConfiguration.setGlobalTimeOffset(timeOffset);
                }
                resetRequestAfterError(request, ase);
            }
        } catch (IOException ioe) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            }
            awsRequestMetrics.incrementCounter(AWSRequestMetrics.Field.Exception);
            awsRequestMetrics.addProperty(AWSRequestMetrics.Field.Exception, ioe);
            awsRequestMetrics.addProperty(AWSRequestMetrics.Field.AWSRequestID, null);

            AmazonClientException ace = new AmazonClientException(
                    "Unable to execute HTTP request: " + ioe.getMessage(), ioe);
            if (!shouldRetry(request.getOriginalRequest(), httpRequest.getContent(), ace, requestCount,
                    config.getRetryPolicy())) {
                throw ace;
            }

            // Cache the retryable exception
            retriedException = ace;
            resetRequestAfterError(request, ioe);
        } catch (RuntimeException e) {
            throw handleUnexpectedFailure(e, awsRequestMetrics);
        } catch (Error e) {
            throw handleUnexpectedFailure(e, awsRequestMetrics);
        } finally {
            /*
             * Some response handlers need to manually manage the HTTP
             * connection and will take care of releasing the connection on
             * their own, but if this response handler doesn't need the
             * connection left open, we go ahead and release the it to free
             * up resources.
             */
            if (!leaveHttpConnectionOpen && httpResponse != null) {
                try {
                    if (httpResponse.getRawContent() != null) {
                        httpResponse.getRawContent().close();
                    }
                } catch (IOException e) {
                    log.warn("Cannot close the response content.", e);
                }
            }
        }
    } /* end while (true) */
}

From source file:org.apache.hadoop.mapred.CoronaJobTracker.java

public static String getSystemDir(FileSystem fs, Configuration conf) {
    Path sysDir = new Path(conf.get(SYSTEM_DIR_KEY, DEFAULT_SYSTEM_DIR));
    java.net.URI uri = sysDir.toUri();
    if (uri.getScheme() != null && uri.getAuthority() != null) {
        return sysDir.toString();
    } else {//from  ww w. ja  va2  s.  c  o  m
        return fs.makeQualified(sysDir).toString();
    }
}

From source file:org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.java

/**
 * Checks if the given rawDir belongs to this account/container, and
 * if so returns the canonicalized path for it. Otherwise return null.
 *//* www .  ja  va  2  s .c  o m*/
private String verifyAndConvertToStandardFormat(String rawDir) throws URISyntaxException {
    URI asUri = new URI(rawDir);
    if (asUri.getAuthority() == null || asUri.getAuthority().toLowerCase(Locale.ENGLISH)
            .equalsIgnoreCase(sessionUri.getAuthority().toLowerCase(Locale.ENGLISH))) {
        // Applies to me.
        return trim(asUri.getPath(), "/");
    } else {
        // Doen't apply to me.
        return null;
    }
}

From source file:org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.java

public boolean isKeyForDirectorySet(String key, Set<String> dirSet) {
    String defaultFS = FileSystem.getDefaultUri(sessionConfiguration).toString();
    for (String dir : dirSet) {
        if (dir.isEmpty() || key.startsWith(dir + "/")) {
            return true;
        }// w  w  w.ja  v  a2s  .co  m

        // Allow for blob directories with paths relative to the default file
        // system.
        //
        try {
            URI uriPageBlobDir = new URI(dir);
            if (null == uriPageBlobDir.getAuthority()) {
                // Concatenate the default file system prefix with the relative
                // page blob directory path.
                //
                if (key.startsWith(trim(defaultFS, "/") + "/" + dir + "/")) {
                    return true;
                }
            }
        } catch (URISyntaxException e) {
            LOG.info(String.format("URI syntax error creating URI for %s", dir));
        }
    }
    return false;
}

From source file:org.apache.hadoop.fs.azure.NativeAzureFileSystem.java

@Override
public void initialize(URI uri, Configuration conf) throws IOException, IllegalArgumentException {
    // Check authority for the URI to guarantee that it is non-null.
    uri = reconstructAuthorityIfNeeded(uri, conf);
    if (null == uri.getAuthority()) {
        final String errMsg = String
                .format("Cannot initialize WASB file system, URI authority not recognized.");
        throw new IllegalArgumentException(errMsg);
    }//from  ww w.ja  va  2s  . com
    super.initialize(uri, conf);

    if (store == null) {
        store = createDefaultStore(conf);
    }

    instrumentation = new AzureFileSystemInstrumentation(conf);
    if (!conf.getBoolean(SKIP_AZURE_METRICS_PROPERTY_NAME, false)) {
        // Make sure the metrics system is available before interacting with Azure
        AzureFileSystemMetricsSystem.fileSystemStarted();
        metricsSourceName = newMetricsSourceName();
        String sourceDesc = "Azure Storage Volume File System metrics";
        AzureFileSystemMetricsSystem.registerSource(metricsSourceName, sourceDesc, instrumentation);
    }

    store.initialize(uri, conf, instrumentation);
    setConf(conf);
    this.uri = URI.create(uri.getScheme() + "://" + uri.getAuthority());
    this.workingDir = new Path("/user", UserGroupInformation.getCurrentUser().getShortUserName())
            .makeQualified(getUri(), getWorkingDirectory());
    this.blockSize = conf.getLong(AZURE_BLOCK_SIZE_PROPERTY_NAME, MAX_AZURE_BLOCK_SIZE);

    if (LOG.isDebugEnabled()) {
        LOG.debug("NativeAzureFileSystem. Initializing.");
        LOG.debug("  blockSize  = " + conf.getLong(AZURE_BLOCK_SIZE_PROPERTY_NAME, MAX_AZURE_BLOCK_SIZE));
    }
}

From source file:fr.cls.atoll.motu.library.misc.data.CatalogData.java

/**
 * get NCSS server Url (this is optional, only OpenDAP is mandatory).
 * /*  w  w  w  .j  a va 2 s . c om*/
 * @param product the product
 * @param catalogXml Xml TDS catalog
 * @param datasetType dataset from which one's search url
 * 
 * @return Opendap Server Url.
 * 
 * @throws MotuException the motu exception
 */
private String getUrlNCSSFromTds(DatasetType datasetType,
        fr.cls.atoll.motu.library.misc.tds.server.Catalog catalogXml, Product product) throws MotuException {
    String tdsServiceName = "";
    String xmlNamespace = ReflectionUtils.getXmlSchemaNamespace(datasetType.getClass());
    StringBuffer xPath = new StringBuffer();
    xPath.append("//threddsMetadataGroup[name='{");
    xPath.append(xmlNamespace);
    xPath.append("}serviceName']/value");
    List<Object> listServiceNameObject = CatalogData.findJaxbElementUsingJXPath(datasetType, xPath.toString());

    for (Object objectElt : listServiceNameObject) {
        if (!(objectElt instanceof String)) {
            continue;
        }
        tdsServiceName = (String) objectElt;
        break;
    }

    if (tdsServiceName.equals("")) {
        throw new MotuException(String.format(
                "Error in getUrlNCSSFromTds - No TDS service found in TDS catalog for dataset '%s' ",
                datasetType.getName()));
    }

    // Search for opendap service, dods if not found
    fr.cls.atoll.motu.library.misc.tds.server.Service tdsService = findTdsService(tdsServiceName,
            TDS_NCSS_SERVICE, catalogXml.getService());
    if (tdsService == null) {
        return ""; // It is not a mandatory service
    }

    // Gather NCSS URLS from TDS
    String relativeUrl = tdsService.getBase();
    URI uri = URI.create(urlSite);
    URI opendapUri = null;
    try {
        opendapUri = new URI(uri.getScheme(), uri.getAuthority(), relativeUrl, null, null);
    } catch (URISyntaxException e) {
        throw new MotuException(String.format(
                "Error in getUrlNCSSFromTds - Uri creation: scheme='%s', authority='%s', path='%s'",
                uri.getScheme(), uri.getAuthority(), relativeUrl), e);
    }

    // Store metadata in Product
    StringBuffer locationDataNCSS = new StringBuffer();
    locationDataNCSS.append(opendapUri.toString());
    locationDataNCSS.append(datasetType.getUrlPath());
    product.setLocationDataNCSS(locationDataNCSS.toString());

    return opendapUri.toString();
}

From source file:fr.cls.atoll.motu.library.misc.data.CatalogData.java

/**
 * get Opendap (or Dods) server Url./*from w ww .  ja  v a2  s.  c  o m*/
 * 
 * @param product the product
 * @param catalogXml Xml TDS catalog
 * @param datasetType dataset from which one's search url
 * 
 * @return Opendap Server Url.
 * 
 * @throws MotuException the motu exception
 */
private String getUrlOpendapFromTds(DatasetType datasetType,
        fr.cls.atoll.motu.library.misc.tds.server.Catalog catalogXml, Product product) throws MotuException {

    String tdsServiceName = "";
    String xmlNamespace = ReflectionUtils.getXmlSchemaNamespace(datasetType.getClass());
    StringBuffer xPath = new StringBuffer();
    xPath.append("//threddsMetadataGroup[name='{");
    xPath.append(xmlNamespace);
    xPath.append("}serviceName']/value");
    List<Object> listServiceNameObject = CatalogData.findJaxbElementUsingJXPath(datasetType, xPath.toString());

    for (Object objectElt : listServiceNameObject) {
        if (!(objectElt instanceof String)) {
            continue;
        }
        tdsServiceName = (String) objectElt;
        break;
    }

    if (tdsServiceName.equals("")) {
        throw new MotuException(String.format(
                "Error in getUrlOpendapFromTds - No TDS service found in TDS catalog for dataset '%s' ",
                datasetType.getName()));
    }

    // Search for opendap service, dods if not found
    fr.cls.atoll.motu.library.misc.tds.server.Service tdsService = findTdsService(tdsServiceName,
            TDS_OPENDAP_SERVICE, catalogXml.getService());
    if (tdsService == null) {
        tdsService = findTdsService(tdsServiceName, TDS_DODS_SERVICE, catalogXml.getService());
    }

    // One of the two is mandatory
    if (tdsService == null) {
        throw new MotuException(String.format(
                "Error in getUrlOpendapFromTds - TDS service '%s' found in TDS catalog for dataset '%s' has neither 'opendap' nor 'dods' service type",
                tdsServiceName, datasetType.getName()));
    }

    // Gather URLS from TDS
    String relativeUrl = tdsService.getBase();
    URI uri = URI.create(urlSite);
    URI opendapUri = null;
    try {
        opendapUri = new URI(uri.getScheme(), uri.getAuthority(), relativeUrl, null, null);
    } catch (URISyntaxException e) {
        throw new MotuException(String.format(
                "Error in getUrlOpendapFromTds - Uri creation: scheme='%s', authority='%s', path='%s'",
                uri.getScheme(), uri.getAuthority(), relativeUrl), e);
    }

    // Store metadata in Product
    StringBuffer locationData = new StringBuffer();
    locationData.append(opendapUri.toString());
    locationData.append(datasetType.getUrlPath());
    product.setLocationData(locationData.toString());
    product.setTdsServiceType(tdsService.getServiceType().toLowerCase());

    return opendapUri.toString();
}