Showing posts with label uri. Show all posts
Showing posts with label uri. Show all posts

Thursday, 11 November 2021

Java: How to get the domain name from url?

URL stands for Uniform resource locator, it is a special type of uri which tells how to access the resource.

 

Example

https://self-learning-java-tutorial.blogspot.com/search?q=test

 

In the above example.

https -> specific scheme

self-learning-java-tutorial.blogspot.com -> represents the path

/search -> specifies the path

q=test -> specifies query parameter.

 

Example 2

https://www.self-learning-java-tutorial.blogspot.com/search?q=test

 

In the above example, url contains the string www after //.

 

We need to address both the examples while finding the domain name. Below snippet takes an url and return the domain name.

public static String getHostName(String url) throws URISyntaxException {
	URI uri = new URI(url);
	String hostComponent = uri.getHost();
	return hostComponent.startsWith("www.") ? hostComponent.substring(4) : hostComponent;
}

 

Find the below working application.

 

DomainNameDemo.java

 

package com.sample.app.net;

import java.net.URI;
import java.net.URISyntaxException;

public class DomainNameDemo {

	public static String getHostName(String url) throws URISyntaxException {
		URI uri = new URI(url);
		String hostComponent = uri.getHost();
		return hostComponent.startsWith("www.") ? hostComponent.substring(4) : hostComponent;
	}

	public static void main(String args[]) throws URISyntaxException {
		String url1 = "https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html";
		String url2 = "http://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html";
		String url3 = "https://www.self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html";
		String url4 = "http://www.self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html";

		System.out.println("Host name in " + url1 + " is \t" + getHostName(url1));
		System.out.println("Host name in " + url2 + " is \t\t" + getHostName(url2));
		System.out.println("Host name in " + url3 + " is \t" + getHostName(url3));
		System.out.println("Host name in " + url4 + " is \t" + getHostName(url4));
	}

}

 

Output

Host name in https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html is 	self-learning-java-tutorial.blogspot.com
Host name in http://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html is 		self-learning-java-tutorial.blogspot.com
Host name in https://www.self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html is 	self-learning-java-tutorial.blogspot.com
Host name in http://www.self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html is 	self-learning-java-tutorial.blogspot.com

 


 

Previous                                                    Next                                                    Home

URI vs URL

While working with web applications, you may come across the terms URI and URL. Most of the people think that both URI and URL are same, but actually not.

 

In this post, lets’ dig into it and try to understand the difference between URI and URL.

 

What is URI?

A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource.

 

Syntax of URI

scheme:[//authority]path[?query][#fragment]  

Examples

ftp://xyz.ab.com/text/text1808.txt
http://www.abc.xy.org/text/text2396.txt
mailto:ram.krishna@example.com
news:comp.abc.www.servers.unix
tel:+1-987-123-1212
telnet://192.168.2.255:80/


Components of URI syntax

a.   Schema: Every URI begins with a scheme name consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-"). For example http, https, ftp, telnet represent schemes.

b.   Authority: The authority component is preceded by a double slash ("//") and is terminated by the next slash ("/"), question mark ("?"), or number  sign ("#") character, or by the end of the URI. Authority component can contain port number (it is optional).

c.    Path: If a URI contains an authority component, then the path component must either be empty or begin with a slash ("/") character.

d.   Query: Query component is used to represent query parameters and it is preceded by a question mark(?). Query component is optional.

e.   Fragement: It is an optional component, preceded by a hash (#) symbol. The semantics of a fragment identifier are defined by the set of representations that might result from a retrieval action on the primary resource.

 

You can refer below link to get list of registered uri schemes.

http://www.iana.org/assignments/uri-schemes

 

 

What is URL?

Definition 1: URL stands for Uniform resource locator, it is a special type of uri which tells how to access the resource.

 

Definition 2: URL is used to find the location of a resource in the web. URL is a subset of URIs, in addition to identifying the resource, url provide a means of locating the resource by describing its primary access mechanism (e.g., its network "location").

 

In simple terms, URI is a superset of URLs, a URL is a URI, but a URI not necessarily be a URI.





What is URN?

URN stands for "Uniform Resource Name" is a Uniform Resource Identifier (URI) that uses the urn scheme.

 

Examples

 

urn:isbn:0451450523
urn:ietf:rfc:2648
urn:ISSN:0167-6423

 

URI vs URL vs URN

 


 

Reference

https://datatracker.ietf.org/doc/html/rfc3986

https://datatracker.ietf.org/doc/html/rfc1738

https://datatracker.ietf.org/doc/html/rfc2141

https://datatracker.ietf.org/doc/html/rfc3305

https://en.wikipedia.org/wiki/Uniform_Resource_Name

Previous                                                    Next                                                    Home

Sunday, 12 January 2020

Convert uri query parameters to a map of name, value

Below snippet takes a url and convert the query parameters to Map<String, String>.
public static Map<String, String> getQueryParamsMap(URL url) throws UnsupportedEncodingException {

 if (url == null) {
  return Collections.EMPTY_MAP;
 }

 // Get Query part of the url
 String queryPart = url.getQuery();

 if (queryPart == null || queryPart.isEmpty()) {
  return Collections.EMPTY_MAP;
 }

 Map<String, String> queryParams = new HashMap<String, String>();

 String[] pairs = queryPart.split("&");
 for (String pair : pairs) {
  String[] keyValuePair = pair.split("=");

  queryParams.put(URLDecoder.decode(keyValuePair[0], StandardCharsets.UTF_8.name()),
    URLDecoder.decode(keyValuePair[1], StandardCharsets.UTF_8.name()));
 }
 return queryParams;
}

App.java
package com.sample.app;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class App {

 public static Map<String, String> getQueryParamsMap(URL url) throws UnsupportedEncodingException {

  if (url == null) {
   return Collections.EMPTY_MAP;
  }

  // Get Query part of the url
  String queryPart = url.getQuery();

  if (queryPart == null || queryPart.isEmpty()) {
   return Collections.EMPTY_MAP;
  }

  Map<String, String> queryParams = new HashMap<String, String>();

  String[] pairs = queryPart.split("&");
  for (String pair : pairs) {
   String[] keyValuePair = pair.split("=");

   queryParams.put(URLDecoder.decode(keyValuePair[0], StandardCharsets.UTF_8.name()),
     URLDecoder.decode(keyValuePair[1], StandardCharsets.UTF_8.name()));
  }
  return queryParams;
 }

 public static void main(String args[]) throws MalformedURLException, UnsupportedEncodingException {

  String urlStr = "https://abc123.com/questions/?filter=age&page=112&pagesize=50";
  URL url = new URL(urlStr);

  Map<String, String> queryParamsMap = getQueryParamsMap(url);

  for (String key : queryParamsMap.keySet()) {
   System.out.println(key + " = " + queryParamsMap.get(key));
  }

 }

}

Output
filter = age
pagesize = 50
page = 112    


You may like