Here you can find the source of createAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot)
public static URL createAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot) throws IllegalArgumentException
//package com.java2s; /**// w ww. j av a 2s . c o m * Copyright (C) 2014 4th Line GmbH, Switzerland and others * * The contents of this file are subject to the terms of the * Common Development and Distribution License Version 1 or later * ("CDDL") (collectively, the "License"). You may not use this file * except in compliance with the License. See LICENSE.txt for more * information. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ import java.net.*; public class Main { public static URL createAbsoluteURL(URL base, String uri) throws IllegalArgumentException { return createAbsoluteURL(base, URI.create(uri)); } public static URL createAbsoluteURL(URL base, URI relativeOrNot) throws IllegalArgumentException { if (base == null && !relativeOrNot.isAbsolute()) { throw new IllegalArgumentException("Base URL is null and given URI is not absolute"); } else if (base == null && relativeOrNot.isAbsolute()) { try { return relativeOrNot.toURL(); } catch (Exception ex) { throw new IllegalArgumentException("Base URL was null and given URI can't be converted to URL"); } } else { try { assert base != null; URI baseURI = base.toURI(); URI absoluteURI = createAbsoluteURI(baseURI, relativeOrNot); return absoluteURI.toURL(); } catch (Exception ex) { throw new IllegalArgumentException("Base URL is not an URI, or can't create absolute URI (null?), " + "or absolute URI can not be converted to URL", ex); } } } public static URL createAbsoluteURL(URI base, URI relativeOrNot) throws IllegalArgumentException { try { return createAbsoluteURI(base, relativeOrNot).toURL(); } catch (Exception ex) { throw new IllegalArgumentException("Absolute URI can not be converted to URL", ex); } } public static URL createAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot) throws IllegalArgumentException { try { if (address instanceof Inet6Address) { return createAbsoluteURL(new URL("http://[" + address.getHostAddress() + "]:" + localStreamPort), relativeOrNot); } else if (address instanceof Inet4Address) { return createAbsoluteURL(new URL("http://" + address.getHostAddress() + ":" + localStreamPort), relativeOrNot); } else { throw new IllegalArgumentException("InetAddress is neither IPv4 nor IPv6: " + address); } } catch (Exception ex) { throw new IllegalArgumentException("Address, port, and URI can not be converted to URL", ex); } } public static URL toURL(URI uri) { if (uri == null) return null; try { return uri.toURL(); } catch (MalformedURLException ex) { throw new RuntimeException(ex); } } public static URI toURI(URL url) { if (url == null) return null; try { return url.toURI(); } catch (URISyntaxException ex) { throw new RuntimeException(ex); } } /** * Guarantees that the returned URI is absolute, no matter what the argument is. * * @param base An absolute base URI, can be null! * @param uri A string that either represents a relative or an already absolute URI * @return An absolute URI * @throws IllegalArgumentException If the base URI is null and the given URI string is not absolute */ public static URI createAbsoluteURI(URI base, String uri) throws IllegalArgumentException { return createAbsoluteURI(base, URI.create(uri)); } public static URI createAbsoluteURI(URI base, URI relativeOrNot) throws IllegalArgumentException { if (base == null && !relativeOrNot.isAbsolute()) { throw new IllegalArgumentException("Base URI is null and given URI is not absolute"); } else if (base == null && relativeOrNot.isAbsolute()) { return relativeOrNot; } else { assert base != null; // If the given base URI has no path we give it a root path if (base.getPath().length() == 0) { try { base = new URI(base.getScheme(), base.getAuthority(), "/", base.getQuery(), base.getFragment()); } catch (Exception ex) { throw new IllegalArgumentException(ex); } } return base.resolve(relativeOrNot); } } }