Java URL Resolve resolveURL(URL base, String target)

Here you can find the source of resolveURL(URL base, String target)

Description

Resolve relative URL-s and fix a few java.net.URL errors in handling of URLs with embedded params and pure query targets.

License

Apache License

Parameter

Parameter Description
base base url
target target url (may be relative)

Exception

Parameter Description
MalformedURLException an exception

Return

resolved absolute url.

Declaration

public static URL resolveURL(URL base, String target) throws MalformedURLException 

Method Source Code

//package com.java2s;
/**/*from  ww  w .j a  v a2  s  .co m*/
 * Licensed to DigitalPebble Ltd under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * DigitalPebble 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.MalformedURLException;

import java.net.URL;

public class Main {
    /**
     * Resolve relative URL-s and fix a few java.net.URL errors in handling of
     * URLs with embedded params and pure query targets.
     * 
     * @param base
     *            base url
     * @param target
     *            target url (may be relative)
     * @return resolved absolute url.
     * @throws MalformedURLException
     */
    public static URL resolveURL(URL base, String target) throws MalformedURLException {
        target = target.trim();

        if (target.startsWith("?")) {
            return fixPureQueryTargets(base, target);
        }

        return new URL(base, target);
    }

    /** Handle the case in RFC3986 section 5.4.1 example 7, and similar. */
    static URL fixPureQueryTargets(URL base, String target) throws MalformedURLException {
        if (!target.startsWith("?"))
            return new URL(base, target);

        String basePath = base.getPath();
        String baseRightMost = "";
        int baseRightMostIdx = basePath.lastIndexOf("/");
        if (baseRightMostIdx != -1) {
            baseRightMost = basePath.substring(baseRightMostIdx + 1);
        }

        if (target.startsWith("?"))
            target = baseRightMost + target;

        return new URL(base, target);
    }
}

Related

  1. resolveResourceAsURL(Class clazz, String name)
  2. resolveResourceURL(Class base, String resource)
  3. resolveRootUrl(Class knownClass)
  4. resolveUrl(String url, Properties properties)
  5. resolveURL(String urlValue)
  6. resolveURL(URL contextURL, String url)
  7. resolveURL(URL url)
  8. resolveWsURI(String url)
  9. resolveXmlConfigUrl(@Nullable URL defaultXmlConfigUrl)