Url Parsing in Java

Here’s the Java translation of the URL parsing example:

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

public class URLParsing {
    public static void main(String[] args) {
        // We'll parse this example URL, which includes a
        // scheme, authentication info, host, port, path,
        // query params, and query fragment.
        String s = "postgres://user:pass@host.com:5432/path?k=v#f";

        try {
            // Parse the URL and ensure there are no errors.
            URI uri = new URI(s);

            // Accessing the scheme is straightforward.
            System.out.println(uri.getScheme());

            // UserInfo contains all authentication info; we need to split it
            // to get individual username and password.
            String userInfo = uri.getUserInfo();
            System.out.println(userInfo);
            String[] userInfoParts = userInfo.split(":");
            System.out.println(userInfoParts[0]);  // username
            System.out.println(userInfoParts[1]);  // password

            // The Host contains both the hostname and the port,
            // if present. We can get them separately.
            System.out.println(uri.getHost() + ":" + uri.getPort());
            System.out.println(uri.getHost());
            System.out.println(uri.getPort());

            // Here we extract the path and the fragment after the #.
            System.out.println(uri.getPath());
            System.out.println(uri.getFragment());

            // To get query params in a string of k=v format,
            // use getRawQuery(). You can also parse query params
            // into a map using a custom method.
            System.out.println(uri.getRawQuery());
            Map<String, String> queryParams = parseQuery(uri.getQuery());
            System.out.println(queryParams);
            System.out.println(queryParams.get("k"));

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    // Custom method to parse query parameters into a map
    private static Map<String, String> parseQuery(String query) {
        Map<String, String> queryPairs = new java.util.LinkedHashMap<>();
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            queryPairs.put(java.net.URLDecoder.decode(pair.substring(0, idx), java.nio.charset.StandardCharsets.UTF_8), 
                           java.net.URLDecoder.decode(pair.substring(idx + 1), java.nio.charset.StandardCharsets.UTF_8));
        }
        return queryPairs;
    }
}

This Java program demonstrates URL parsing using the java.net.URI class. Here’s a breakdown of what the code does:

  1. We start by defining a sample URL string that includes various components like scheme, authentication info, host, port, path, query parameters, and fragment.

  2. We create a URI object from this string, which parses the URL.

  3. We then extract various components of the URL using methods provided by the URI class:

    • getScheme() for the scheme
    • getUserInfo() for authentication info
    • getHost() and getPort() for host and port
    • getPath() for the path
    • getFragment() for the fragment
    • getRawQuery() for the raw query string
  4. For parsing query parameters, we’ve implemented a custom parseQuery method that splits the query string and returns a Map of parameter names to values.

  5. We print out each component to demonstrate how to access them.

To run this program:

  1. Save the code in a file named URLParsing.java
  2. Compile it with javac URLParsing.java
  3. Run it with java URLParsing

The output will show the different components of the parsed URL, similar to the original example.

Note that Java’s URI class doesn’t provide direct methods for some operations (like separating username and password), so we’ve implemented those manually. Also, the query parameter parsing is done using a custom method, as Java doesn’t provide a built-in method for this in the URI class.