Java URL Create buildUrl(String repositoryUrl, String resourceUrl)

Here you can find the source of buildUrl(String repositoryUrl, String resourceUrl)

Description

build Url

License

Open Source License

Declaration

public static String buildUrl(String repositoryUrl, String resourceUrl) throws URISyntaxException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2011 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final Map<String, String> protocolMap = new HashMap<String, String>();

    public static String buildUrl(String repositoryUrl, String resourceUrl) throws URISyntaxException {
        int idx = repositoryUrl.indexOf("://");
        if (idx < 0) {
            return repositoryUrl + resourceUrl;
        }//from   w ww.  j  a  v  a2s . com

        String protocol = repositoryUrl.substring(0, idx);
        protocol = normalizeProtocol(protocol);

        int authorityIdx = idx + 3;
        String authority;
        String path;
        idx = repositoryUrl.indexOf('/', authorityIdx);
        if (idx < 0) {
            authority = repositoryUrl.substring(authorityIdx);
            path = "/";
        } else {
            authority = repositoryUrl.substring(authorityIdx, idx);
            path = repositoryUrl.substring(idx);
        }

        if (!path.endsWith("/")) {
            path += '/';
        }

        if (resourceUrl.startsWith("/")) {
            path += resourceUrl.substring(1);
        } else {
            path += resourceUrl;
        }

        return new URI(protocol, authority, path, null, null).toASCIIString();
    }

    public static String normalizeProtocol(String protocol) {
        if (protocolMap.containsKey(protocol)) {
            return protocolMap.get(protocol);
        } else {
            return protocol;
        }
    }
}

Related

  1. buildUrl(String context, String... relocations)
  2. buildUrl(String host, int port, String path, Map parameters)
  3. buildUrl(String host, int port, String path, Map parameters)
  4. buildURL(String host, String path, Map params)
  5. buildURL(String id)
  6. buildURL(String spec)
  7. buildURL(String spec)
  8. buildURL(String spec, boolean trailingSlash)
  9. buildUrl(String url)