Home | History | Annotate | Download | only in util
      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 
     26 package sun.net.util;
     27 
     28 import java.net.URL;
     29 
     30 /**
     31  * URL Utility class.
     32  */
     33 public class URLUtil {
     34     /**
     35      * Returns a string form of the url suitable for use as a key in HashMap/Sets.
     36      *
     37      * The string form should be behave in the same manner as the URL when
     38      * compared for equality in a HashMap/Set, except that no nameservice
     39      * lookup is done on the hostname (only string comparison), and the fragment
     40      * is not considered.
     41      *
     42      * @see java.net.URLStreamHandler.sameFile(java.net.URL)
     43      */
     44     public static String urlNoFragString(URL url) {
     45         StringBuilder strForm = new StringBuilder();
     46 
     47         String protocol = url.getProtocol();
     48         if (protocol != null) {
     49             /* protocol is compared case-insensitive, so convert to lowercase */
     50             protocol = protocol.toLowerCase();
     51             strForm.append(protocol);
     52             strForm.append("://");
     53         }
     54 
     55         String host = url.getHost();
     56         if (host != null) {
     57             /* host is compared case-insensitive, so convert to lowercase */
     58             host = host.toLowerCase();
     59             strForm.append(host);
     60 
     61             int port = url.getPort();
     62             if (port == -1) {
     63                 /* if no port is specificed then use the protocols
     64                  * default, if there is one */
     65                 port = url.getDefaultPort();
     66             }
     67             if (port != -1) {
     68                 strForm.append(":").append(port);
     69             }
     70         }
     71 
     72         String file = url.getFile();
     73         if (file != null) {
     74             strForm.append(file);
     75         }
     76 
     77         return strForm.toString();
     78     }
     79 
     80     // Android-removed: Android doesn't support SecurityManager, so Permissions logic is dead code.
     81     /*
     82     public static Permission getConnectPermission(URL url) throws IOException {
     83         String urlStringLowerCase = url.toString().toLowerCase();
     84         if (urlStringLowerCase.startsWith("http:") || urlStringLowerCase.startsWith("https:")) {
     85             return getURLConnectPermission(url);
     86         } else if (urlStringLowerCase.startsWith("jar:http:") || urlStringLowerCase.startsWith("jar:https:")) {
     87             String urlString = url.toString();
     88             int bangPos = urlString.indexOf("!/");
     89             urlString = urlString.substring(4, bangPos > -1 ? bangPos : urlString.length());
     90             URL u = new URL(urlString);
     91             return getURLConnectPermission(u);
     92             // If protocol is HTTP or HTTPS than use URLPermission object
     93         } else {
     94             return url.openConnection().getPermission();
     95         }
     96     }
     97 
     98     private static Permission getURLConnectPermission(URL url) {
     99         String urlString = url.getProtocol() + "://" + url.getAuthority() + url.getPath();
    100         return new URLPermission(urlString);
    101     }
    102     */
    103 }
    104 
    105