1/* 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package sun.net.util; 27 28import java.io.IOException; 29import java.net.URL; 30import java.net.URLPermission; 31import java.security.Permission; 32 33/** 34 * URL Utility class. 35 */ 36public class URLUtil { 37 /** 38 * Returns a string form of the url suitable for use as a key in HashMap/Sets. 39 * 40 * The string form should be behave in the same manner as the URL when 41 * compared for equality in a HashMap/Set, except that no nameservice 42 * lookup is done on the hostname (only string comparison), and the fragment 43 * is not considered. 44 * 45 * @see java.net.URLStreamHandler.sameFile(java.net.URL) 46 */ 47 public static String urlNoFragString(URL url) { 48 StringBuilder strForm = new StringBuilder(); 49 50 String protocol = url.getProtocol(); 51 if (protocol != null) { 52 /* protocol is compared case-insensitive, so convert to lowercase */ 53 protocol = protocol.toLowerCase(); 54 strForm.append(protocol); 55 strForm.append("://"); 56 } 57 58 String host = url.getHost(); 59 if (host != null) { 60 /* host is compared case-insensitive, so convert to lowercase */ 61 host = host.toLowerCase(); 62 strForm.append(host); 63 64 int port = url.getPort(); 65 if (port == -1) { 66 /* if no port is specificed then use the protocols 67 * default, if there is one */ 68 port = url.getDefaultPort(); 69 } 70 if (port != -1) { 71 strForm.append(":").append(port); 72 } 73 } 74 75 String file = url.getFile(); 76 if (file != null) { 77 strForm.append(file); 78 } 79 80 return strForm.toString(); 81 } 82 83 public static Permission getConnectPermission(URL url) throws IOException { 84 String urlStringLowerCase = url.toString().toLowerCase(); 85 if (urlStringLowerCase.startsWith("http:") || urlStringLowerCase.startsWith("https:")) { 86 return getURLConnectPermission(url); 87 } else if (urlStringLowerCase.startsWith("jar:http:") || urlStringLowerCase.startsWith("jar:https:")) { 88 String urlString = url.toString(); 89 int bangPos = urlString.indexOf("!/"); 90 urlString = urlString.substring(4, bangPos > -1 ? bangPos : urlString.length()); 91 URL u = new URL(urlString); 92 return getURLConnectPermission(u); 93 // If protocol is HTTP or HTTPS than use URLPermission object 94 } else { 95 return url.openConnection().getPermission(); 96 } 97 } 98 99 private static Permission getURLConnectPermission(URL url) { 100 String urlString = url.getProtocol() + "://" + url.getAuthority() + url.getPath(); 101 return new URLPermission(urlString); 102 } 103} 104 105