Java URI Parse parseServiceName(URI endpoint)

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

Description

Parses the service name from an endpoint.

License

Apache License

Declaration

public static String parseServiceName(URI endpoint) 

Method Source Code

//package com.java2s;
/*//w  ww .  j  av  a 2 s .com
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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 java.net.URI;

import java.util.regex.Pattern;

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

    /**
     * Parses the service name from an endpoint. Can only handle endpoints of
     * the form 'service.[region.]amazonaws.com'.
     * or
     * bucket.s3.[region.]awazonaws.com
     */
    public static String parseServiceName(URI endpoint) {
        String host = endpoint.getHost();

        if (!host.endsWith(".amazonaws.com")
                && !host.endsWith(".amazonaws.com.cn")) {
            return "s3"; // cannot parse name, assume s3
        }

        String serviceAndRegion = host.substring(0,
                host.indexOf(".amazonaws.com"));

        // Special cases for S3 endpoints with bucket names embedded.
        if (serviceAndRegion.endsWith(".s3")
                || S3_ENDPOINT_PATTERN.matcher(serviceAndRegion).matches()) {
            return "s3";
        }

        char separator = '.';

        // If we don't detect a separator between service name and region, then
        // assume that the region is not included in the hostname, and it's only
        // the service name (ex: "http://iam.amazonaws.com").
        if (serviceAndRegion.indexOf(separator) == -1) {
            return serviceAndRegion;
        }

        return serviceAndRegion.substring(0,
                serviceAndRegion.indexOf(separator));
    }
}

Related

  1. parseRegionName(URI endpoint)
  2. parseRegionName(URI endpoint)
  3. parseRegionName(URI endpoint)
  4. parseRepositoryItemUri(URI uri)
  5. parseServiceName(URI endpoint)
  6. parseURI(final String string)
  7. parseURI(final String value)
  8. parseURI(String connectionString, URI defaultURI)
  9. parseUri(String s)