Java URL Normalize normalize(URL u)

Here you can find the source of normalize(URL u)

Description

normalize an URL,

License

Open Source License

Parameter

Parameter Description

Return

a new URL, the normalized version of the parameter, or the u URL, if something failed in the process

Declaration

public static URL normalize(URL u) 

Method Source Code


//package com.java2s;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    static Method url_defport;

    /**//from  w  ww.  j a  va2s.  c  om
     * normalize an URL, 
     * @param u, the URL to normalize
     * @return a new URL, the normalized version of the parameter, or
     * the u URL, if something failed in the process
     */
    public static URL normalize(URL u) {
        String proto = u.getProtocol().toLowerCase();
        String host = u.getHost().toLowerCase();
        int port = u.getPort();

        if (port != -1) {
            if (url_defport != null) {
                try {
                    int udp;
                    udp = ((Integer) url_defport.invoke(u, (Object[]) null)).intValue();
                    // we have the default, skip the port part
                    if (udp == port) {
                        port = -1;
                    }
                } catch (InvocationTargetException ex) {
                } catch (IllegalAccessException iex) {
                }
            } else {
                switch (port) {
                case 21:
                    if (proto.equals("ftp")) {
                        port = -1;
                    }
                    break;
                case 80:
                    if (proto.equals("http")) {
                        port = -1;
                    }
                    break;
                case 443:
                    if (proto.equals("https")) {
                        port = -1;
                    }
                    break;
                }
            }
        }
        try {
            URL _nu;
            if (port == -1) {
                _nu = new URL(proto, host, u.getFile());
            } else {
                _nu = new URL(proto, host, port, u.getFile());
            }
            return _nu;
        } catch (MalformedURLException ex) {
        }
        return u;
    }
}

Related

  1. normalize(String absoluteURL)
  2. normalize(String url)
  3. normalize(String url)
  4. normalize(String url)
  5. normalize(String url_str)
  6. normalize(URL url)
  7. normalize(URL url)
  8. normalizeBaseUrl(String networkUrl)
  9. normalizeCapabilitiesUrl(String url)