Java URI Parse parseRegionName(URI endpoint)

Here you can find the source of parseRegionName(URI endpoint)

Description

parse Region Name

License

Open Source License

Declaration

@Deprecated
public static String parseRegionName(URI endpoint) 

Method Source Code


//package com.java2s;
/*// w w w  .  j ava 2s. com
 * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import com.amazonaws.internal.config.HostRegexToRegionMapping;
import com.amazonaws.internal.config.InternalConfig;

import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern S3_ENDPOINT_PATTERN = Pattern.compile("^(?:.+\\.)?s3[.-]([a-z0-9-]+)$");

    /**
     * @deprecated in favor of {@link #parseRegionName(String, String)}.
     */
    @Deprecated
    public static String parseRegionName(URI endpoint) {
        return parseRegionName(endpoint.getHost(), null);
    }

    /**
     * Attempts to parse the region name from an endpoint based on conventions
     * about the endpoint format.
     *
     * @param host the hostname to parse
     * @param serviceHint an optional hint about the service for the endpoint
     * @return the region parsed from the hostname, or "us-east-1" if
     *         no region information could be found
     */
    public static String parseRegionName(final String host, final String serviceHint) {

        if (host == null) {
            throw new IllegalArgumentException("hostname cannot be null");
        }
        String regionNameInInternalConfig = parseRegionNameByInternalConfig(host);
        if (regionNameInInternalConfig != null) {
            return regionNameInInternalConfig;
        }

        if (host.endsWith(".amazonaws.com")) {
            int index = host.length() - ".amazonaws.com".length();
            return parseStandardRegionName(host.substring(0, index));
        }

        if (serviceHint != null) {
            // If we have a service hint, look for 'service.[region]' or
            // 'service-[region]' in the endpoint's hostname.
            Pattern pattern = Pattern.compile("^(?:.+\\.)?" + Pattern.quote(serviceHint) + "[.-]([a-z0-9-]+)\\.");

            Matcher matcher = pattern.matcher(host);
            if (matcher.find()) {
                return matcher.group(1);
            }
        }

        // Endpoint is totally non-standard; guess us-east-1 for lack of a
        // better option.

        return "us-east-1";
    }

    /**
     * @return the configured region name if the given host name matches any of
     *         the host-to-region mappings in the internal config; otherwise
     *         return null.
     */
    private static String parseRegionNameByInternalConfig(String host) {
        InternalConfig internConfig = InternalConfig.Factory.getInternalConfig();

        for (HostRegexToRegionMapping mapping : internConfig.getHostRegexToRegionMappings()) {
            String hostNameRegex = mapping.getHostNameRegex();
            if (host.matches(hostNameRegex)) {
                return mapping.getRegionName();
            }
        }

        return null;
    }

    /**
     * Parses the region name from a standard (*.amazonaws.com) endpoint.
     *
     * @param fragment the portion of the endpoint excluding
     *            ".amazonaws.com"
     * @return the parsed region name (or "us-east-1" as a best guess
     *         if we can't tell for sure)
     */
    private static String parseStandardRegionName(final String fragment) {

        Matcher matcher = S3_ENDPOINT_PATTERN.matcher(fragment);
        if (matcher.matches()) {
            // host was 'bucket.s3-[region].amazonaws.com'.
            return matcher.group(1);
        }

        int index = fragment.lastIndexOf('.');
        if (index == -1) {
            // host was 'service.amazonaws.com', guess us-east-1
            // for lack of a better option.
            return "us-east-1";
        }

        // host was 'service.[region].amazonaws.com'.
        String region = fragment.substring(index + 1);

        // Special case for iam.us-gov.amazonaws.com, which is actually
        // us-gov-west-1.
        if ("us-gov".equals(region)) {
            region = "us-gov-west-1";
        }

        return region;
    }
}

Related

  1. parseQuery(URI uri)
  2. parseQuery(URI uri)
  3. parseQueryString(final URI uri, final M queryParams)
  4. parseQueryString(URI uri)
  5. parseQueryString(URI uri, M queryParams)
  6. parseRegionName(URI endpoint)
  7. parseRegionName(URI endpoint)
  8. parseRepositoryItemUri(URI uri)
  9. parseServiceName(URI endpoint)