Normalize an URL : URL « Network Protocol « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Network Protocol » URLScreenshots 
Normalize an URL
  
// URLUtils.java
// $Id: URLUtils.java,v 1.3 2007/02/11 18:39:47 ylafon Exp $
// (c) COPYRIGHT ERCIM, Keio and MIT, 2003.
// Please first read the full copyright statement in file COPYRIGHT.html

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;

public class URLUtils {

  static Method url_defport;

  static {
    try {
      Class c = java.net.URL.class;
      url_defport = c.getMethod("getDefaultPort"(Class[]) null);
    catch (NoSuchMethodException ex) {
      // not using a recent jdk...
      url_defport = null;
    }
  }

  /**
   * Checks that the protocol://host:port part of two URLs are equal.
   
   @param u1,
   *          the first URL to check
   @param u2,
   *          the second URL to check
   @return a boolean, true if the protocol://host:port part of the URL are
   *         equals, false otherwise
   */
  public static boolean equalsProtocolHostPort(URL u1, URL u2) {
    if ((u1 == null|| (u2 == null)) {
      return false;
    }
    // check that the protocol are the same (as it impacts the
    // default port check
    if (!u1.getProtocol().equalsIgnoreCase(u2.getProtocol())) {
      return false;
    }
    // check that both hostnames are equal
    if (!u1.getHost().equalsIgnoreCase(u2.getHost())) {
      return false;
    }
    int u1p = u1.getPort();
    int u2p = u2.getPort();
    // if port is ok, it's good!
    if (u1p == u2p) {
      return true;
    else if ((u1p > 0&& (u2p > 0)) {
      return false;
    }
    // otherwise, the painful comparison of -1 and such
    if (url_defport != null) {
      if (u1p == -1) {
        try {
          int u1dp;
          u1dp = ((Integerurl_defport.invoke(u1, (Object[]) null)).intValue();
          return (u2p == u1dp);
        catch (InvocationTargetException ex) {
        catch (IllegalAccessException iex) {
        }
      else {
        try {
          int u2dp;
          u2dp = ((Integerurl_defport.invoke(u2, (Object[]) null)).intValue();
          return (u1p == u2dp);
        catch (InvocationTargetException ex) {
        catch (IllegalAccessException iex) {
        }
      }
    }
    // no JDK 1.4 this is becoming painful...
    if (u1p == -1) {
      String s = u1.getProtocol();
      int u1dp = 0;
      if (s.equalsIgnoreCase("http")) {
        u1dp = 80;
      else if (s.equalsIgnoreCase("https")) {
        u1dp = 443;
      // FIXME do others?
      return (u2p == u1dp);
    else {
      String s = u2.getProtocol();
      int u2dp = 0;
      if (s.equalsIgnoreCase("http")) {
        u2dp = 80;
      else if (s.equalsIgnoreCase("https")) {
        u2dp = 443;
      // FIXME do others?
      return (u1p == u2dp);
    }
  }

  /**
   * 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 = ((Integerurl_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 examples in the same category
1. Creating a URL with a single string.
2. Creating a URL With components
3. Converting Between a Filename Path and a URL
4. URL Constructor Test
5. URL Encode Test
6. Get URL Content
7. Get URL Parts
8. Read from a URL
9. Convert a URL to a URI
10. Converting Between a URL and a URI
11. Convert an absolute URI to a URL
12. URL Equality
13. Parsing a URL
14. URL Request
15. URL Get
16. A URL Retrieval Example
17. URL Reader
18. URL Connection ReaderURL Connection Reader
19. Using URLConnection
20. Parse URLParse URL
21. Resolve a relative URL
22. sends e-mail using a mailto: URLsends e-mail using a mailto: URL
23. Convert the absolute URI to a URL object
24. Convert URI to URL
25. Get parts of a url
26. Checks, whether the URL uses a file based protocol.
27. Add Parameter to URL
28. Returns the anchor value of the given URL
29. Extracts the file name from the URL.
30. Creates a relative url by stripping the common parts of the the url.
31. Checks, whether the URL points to the same service. A service is equal if the protocol, host and port are equal.
32. Extracts the base URL from the given URL by stripping the query and anchor part.
33. Returns true if the URL represents a path, and false otherwise.
34. Parse Port
35. Parse Host
36. Given a URL check if its a jar url(jar:!/archive) and if it is, extract the archive entry into the given dest directory and return a file URL to its location
37. check the validity of url pattern according to the spec.
38. A collection of File, URL and filename utility methods
39. Build Relative URL Path
40. Checks that the protocol://host:port part of two URLs are equal
41. Create valid URL from a system id
42. Extract URL File Name
43. Extract the URL page name from the given path
44. Get Domain Name
45. Get Locale From String
46. Get URL Last Modified
47. Get the name of the parent of the given URL path
48. Get the parent of the given URL path
49. Has URLContent Changed
50. Is URL a local file
51. Normalizes an URL
52. Resolve a relative URL string against an absolute URL string
53. ResourceBundle String manager
54. Save URL contents to a file
55. URL Path: standardize the creation of mutation of path-like structures
56. Utility class for building URLs
57. Add Default Port to a URL If Missing
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.