An Introduction to REST and a High Level Design of a REST API

What is REST and Why is it called Representational State Transfer?

REpresentational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web. REST has emerged as a predominant Web service design model.
The Web is comprised of resources. A resource is any item of interest. For example, the Jet Aircraft Corp may define a 747 resource. Clients may access that resource with this URL:
http://www.jet.com/aircraft/747
A representation of the resource is returned (e.g., Jet747.html). The representation places the client application in a state. The result of the client traversing a hyperlink in Jet747.html is another resource is accessed. The new representation places the client application into yet another state. Thus, the client application changes (transfers) state with each resource representation –> Representational State Transfer!

Below is one sample high level REST API design that I had designed some year back. REST’s new learner can take reference of it.

Sample search REST API High level design

SparkJava multipart/form-data fileUpload

Spark Framework is a simple and lightweight Java web framework built for rapid development. With Spark it’s possible to start a REST web server with a few lines of code.It is the Java porting of Sinatra: famous micro-framework written in Ruby.The purpose of this post is to explain how to work with Spark  for multipart/form-data fileUpload requirement.In different dev community I have seen people searching fix of fileUpload failure of SparkJava framework, so I thought to share the fix that I have encountered and fixed recently.


Spark.post("/files/upload/:userName", "multipart/form-data", new Route() {
			@Override
			public Object handle(Request request, Response response) {
				// process request
				String userID = request.params("userName");
				if (isValidUser(userID)) {
// These two lines work as fix.
MultipartConfigElement multipartConfigElement = new MultipartConfigElement("data/tmp");
request.raw().setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement);

					Collection<Part> parts = null;
					try {
						parts = request.raw().getParts();
					} catch (IOException | ServletException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					for (Part part : parts) {
						System.out.println("Name:" + part.getName());
						System.out.println("Size: " + part.getSize());
						System.out.println("Filename:" + part.getSubmittedFileName());
					}
					String fName = null;
					Part file = null;
					try {
						file = request.raw().getPart("fileToBeUploaded");
						fName = request.raw().getPart("fileToBeUploaded").getSubmittedFileName();
					} catch (IOException | ServletException e1) {
						e1.printStackTrace();
					}

 

 

Spark Exceptions

List of exceptions and troubleshooting steps that I have encountered till now using Spark in my project.

Issue 1 # Exception in thread “main” org.apache.spark.SparkException: A master URL must be set in your  configuration

Fix :   It means you simply forgot to specify the master URL.
SparkConf configuration = new SparkConf() .setAppName(“Your Application Name”) .setMaster(“local”);

Issue 2 # <Next_Exception_will_be_listed_soon>

Kafka Exceptions

Kafka is super buzzword nowadays in Bigdata space, so I thought to share some of exceptions and troubleshooting steps that I have encountered till now using Kafka in my project.

Issue 1 # Error while fetching metadata with correlation id 0 : {Visitor=LEADER_NOT_AVAILABLE (org.apache.kafka.clients.NetworkClient)

Fix :  There could be many reason of this failure but I resolved it by updating
                           host.name=localhost
                           advertised.host.name=localhost
        in $Kafka_home/config/server.properties where localhost is kafka’s server                   hostname. Basically it was happening due to incorrect network binding of my               laptop’s wireless interface.

Issue 2 # <Next_Exception_will_be_listed_soon>

Effective use of IdentityHashMap and Flyweight Pattern

Imagine a online finance dashboard where a Finance consultant and his Client discuss, do some calculation , presentation, template and query filling like activities, and finally ends session of discussion. Such dashboard can have various tools and repetitive actions and forms which can be used through discussion. Lets take a case of 1000 customers at any time on dashboard, 100 tasks and forms , again 40 conditions for each task and form. So at least
1000*100*40= 4000000 Objects ( or near to it if consider some static design ) and corresponding GC() cycle we have to support to meet our requirement.

By using proper design and data structure we can reduce this problem and can speed up Dashboard loading and tool performance etc… use of Flyweight pattern and IdentityHashMap is perfect combination for use-case like above.

The Flyweight pattern is suitable for a context free and frequently used large object creations.
IdentityHashMap – This class implements the Map interface with a hash table, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)

Here is one sample code that you can refactor for your own similar use-case.


import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map.Entry;

public class HashedIdentityDashBoard {

	public static void main(String[] args) {
		HashMap<String, DashBoard> hm = new HashMap<String, DashBoard>();
		IdentityHashMap<DashBoard, String> idenHM = new IdentityHashMap<DashBoard, String>();

		hm.put("PendingTasks", new PendingTasks());
		hm.put("DailyTasks", new DailyTasks());
		hm.put("EscalatedTasks", new EscalatedTasks());
		// and 100 more repetitive tasks

		// identity map for repetitive tasks and their Templates and Query Forms
		idenHM.put(hm.get("PendingTasks"), "40 Templates and Query form of PendingTasks");
		idenHM.put(hm.get("DailyTasks"), "40 Templates and Query form of DailyTasks");
		idenHM.put(hm.get("EscalatedTasks"), "40 Templates and Query form of EscalatedTasks");

		// only for 1000 users we need 1000*100*40 = 4000000 Objects

		for (Entry<DashBoard, String> entry : idenHM.entrySet()) {
			DashBoard dashBoard = entry.getKey();
			System.out.println("Key : " + dashBoard + " : Value : " + idenHM.get(dashBoard));
		}

	}

}

interface DashBoard {

	public void setTitle(String widgetTitle);

}

abstract class AbstractUserDashBoard implements DashBoard {
	public String dashBoardName;

	abstract public void setTitle(String widgetTitle);

	@Override
	public String toString() {
		return "Title of DashBoard is :" + this.dashBoardName + " and hashCode is " + this.hashCode();
	}
}

class DailyTasks extends AbstractUserDashBoard {

	public DailyTasks() {
		setTitle("DailyTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}
}

class PendingTasks extends AbstractUserDashBoard {
	public PendingTasks() {
		setTitle("PendingTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}

}

class EscalatedTasks extends AbstractUserDashBoard {
	public EscalatedTasks() {
		setTitle("EscalatedTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}

}

Shared queue processing with Pub and Sub Threads

import java.util.Random;

public class PubSubDemo {

	public static void main(String[] args) {
		SharedQ sharedQ = new SharedQ();

		new Thread(new Consumer(sharedQ), "Consumer").start();
		// Delay publishing
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		new Thread(new Producer(sharedQ), "Producer").start();
	}

}

class Producer implements Runnable {
	SharedQ sharedQPro = null;

	public Producer(SharedQ sharedQ) {
		this.sharedQPro = sharedQ;
	}

	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			produce();
		}
	}

	public void produce() {
		synchronized (sharedQPro.sharedQueue) {

			for (int i = 0; i < 3; i++) {
				sharedQPro.sharedQueue[i] = (new Random().nextInt(20));
				System.out.println(" ### Produced ### " + sharedQPro.sharedQueue[i]);
			}
			sharedQPro.sharedQueueIsEmpty = false;
			sharedQPro.sharedQueue.notify();
			try {
				sharedQPro.sharedQueue.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

class Consumer implements Runnable {
	SharedQ sharedQCon = null;

	public Consumer(SharedQ sharedQ) {
		this.sharedQCon = sharedQ;
	}

	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			consume();
		}
	}

	public void consume() {
		synchronized (sharedQCon.sharedQueue) {
			if (sharedQCon.sharedQueueIsEmpty) {
				try {
					sharedQCon.sharedQueue.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			System.out.println();

			for (int i = 0; i < 3; i++) {
				System.out.println(" *** Consumed *** " + sharedQCon.sharedQueue[i]);
				sharedQCon.sharedQueue[i] = 0;
			}
			sharedQCon.sharedQueue.notify();
			System.out.println(" --------------------------");
			try {
				sharedQCon.sharedQueue.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}

class SharedQ {

	int[] sharedQueue = new int[3];
	boolean sharedQueueIsEmpty = true;

}

/*
Output of pub sub :

 ### Produced ### 1
 ### Produced ### 0
 ### Produced ### 3

 *** Consumed *** 1
 *** Consumed *** 0
 *** Consumed *** 3
 --------------------------
 ### Produced ### 12
 ### Produced ### 0
 ### Produced ### 14

 *** Consumed *** 12
 *** Consumed *** 0
 *** Consumed *** 14
 --------------------------
 ### Produced ### 13
 ### Produced ### 8
 ### Produced ### 17

 *** Consumed *** 13
 *** Consumed *** 8
 *** Consumed *** 17
 --------------------------
*/

Sequence generator tasks by Threads

Many times we face scenario where we need to execute two sequential tasks to perform a operation, like Tagging / Stamping serial Coupons , Producing serial Tickets at different Vending Machines. Below code ( generating Odd and Even in sequence) can be used for such requirement.

PS: This is a basic implementation, the same can be done using other Threading concepts.

				
package multithreading;

// print ODD and Even number alternatively

public class SquenceGeneratorTasks {

	public static boolean oddPrinted = false;

	public static void main(String[] args) {
		 Object lock =new Object();
		Thread oddThread = new Thread(new Runnable() {

			@Override
			public void run() {
				int i = 1;
				while (i <= 20) {

					synchronized (lock) {
						if (!oddPrinted) { // make sure printing starting with Odd number and each Odd is followed with a Even number
							System.out.println(i);
							i = i + 2;
							oddPrinted = true;
							lock.notify();
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}

				}
			}
		});

		
		Thread evenThread = new Thread(new Runnable() {

			@Override
			public void run() {
				
				System.out.println(" Though you have delayed Odd thread but printing gonna start with Odd number only !!");
				
				int i = 2;
				while (i <= 20) {
					synchronized (lock) {
						if (oddPrinted) {
							System.out.println(i);
							i = i + 2;
							oddPrinted = false;
							lock.notify();
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		});

		evenThread.start();
		
		// even if We apply some delay to Odd thread printing will start with ODD number only and 
		// Sequence of Odd-Even-Odd maintained
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		oddThread.start();

	}

}						
/*
Though you have delayed Odd thread but printing gonna start with Odd number only !!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 */

You can download the same code from : https://github.com/SkilledMinds/easyNuts/tree/master/code-25-09-2015