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();
					}

 

 

custom linkedList implementation

Computing systems are concerned with the storage and retrieval of information.For systems to be economical the data must be organized (into data structures) in such a way as to support efficient manipulation (by algorithms).Choosing the wrong algorithms and data structures makes a program slow at best and unmaintainable and insecure at worst.

Lets take an example of purchase order of an e-commerce store. If purchase order implemented as custom linkedlist then we can take benefit of

a) constant-time insertions/deletions from the list
b) maintaining many versions of purchase order without any additional complexity.
c) to insert items in the middle of the list (such as a priority queue)
d) many priority queue based operation
e) In-memory operations

below is one such implementation

 
package com;

import java.util.Iterator;

public class LinkedListDemo {

	public static void main(String[] args) {

		LinkedList lnkls = new LinkedList();
		lnkls.add(11);
		lnkls.add(12);
		lnkls.add(13);
		lnkls.add(14);
		lnkls.add(15);
		lnkls.add(16);
		lnkls.add(17);
		lnkls.add(18);
		System.out.println("Total added items :" + lnkls.size);
		Iterator<LinkedList.Node> it = lnkls.iterator();

		System.out.println("****** Items of LinkedList ******");

		while (it.hasNext()) {
			System.out.println(it.next().objValue);
		}

	}
}

class LinkedList implements Iterable<LinkedList.Node>, Iterator<LinkedList.Node> {

	Node head;
	Node lastNode;
	Node visitorNode;

	int size = 0;
	int visited = 0;

	public LinkedList() {
	}

	public void add(Object o) {
		Node newNode = new Node(o);

		if (this.head == null) {
			this.lastNode = this.head = newNode;
		} else {
			this.lastNode.next = newNode;
			this.lastNode = newNode;
		}
		size++;
		System.out.println("Added " + newNode);
	}

	@Override
	public Iterator<LinkedList.Node> iterator() {
		visitorNode = lastNode = head;
		return this;
	}

	@Override
	public boolean hasNext() {
		if (visited++ < size)
			return true;
		else
			return false;
	}

	@Override
	public Node next() {
		Node visitedNode = lastNode;
		lastNode = lastNode.next;
		return visitedNode;
	}

	class Node {

		Object objValue;
		Node next;

		public Node(Object o) {
			this.objValue = o;
			this.next = null;
		}

		@Override
		public String toString() {
			return objValue.toString();
		}

	}
 // additional operational and supportive APIs for custom linkedlist
}

check your java class locking and classloader concept.

try below code to test your concept of class Locking and classLoader. I will post the
reason of output next week till then you do your finding !!

public class VerifyClassLoadingOnClassLock {
    public static void main(String[] args) {
        show();
    }

    private static void show() {
        synchronized (SharedQ.class) {
            System.out.println(" Method Show() executing from Main() .... ");
        }       
    }
} 



class SharedQ {  
    static {
        System.out.println(" Classloader is loading SharedQ ");
    }

    public static void writeStream() {
        // some multiThread code here
    }
}

use Weak reference for high availability of cache and In-Memory DB operations

We should think about using WeakReference whenever we need a reference to an object, but you don’t want that reference to protect the object from the garbage collector. A classic example is a cache that we want to be garbage collected when memory usage gets too high.

Basically a WeakReference will be GC-d by the JVM eagerly, once the referenced object has no hard references to it. A SoftReferenced object on the other hand, will tend to be left about by the garbage collector until it really needs to reclaim the memory.

Suppose there is a Query level cache and it is persisted and attached to a session. If such queries are very high, certainly they will occupy a huge memory and then they need to be claimed ( GC-d ) as soon as not required. For this, Class Query should be implemented or enqueued through WeakReference.

Sample code for Producer n Consumer Problem

There are at least 5 direct ways to write a Producer n Consumer Problem. The basic is one which use wait() and notify().

// Queue
class CustomerQueue
{

    String sharedCustomerQueue[] = new String[5];
}

// Client
public class ProducerConsumerQ
{

    static int NEED_TO_SERVE_BATCH = 2;

    public static void main(String[] args)
    {

        System.out.println(" Producer will fill SharedQueue with 5 elements in each batch.");
        System.out.println(" Consumer will consume elements available in SharedQueue in each batch.");

        System.out.println(" Total " + NEED_TO_SERVE_BATCH + " will be served.");
        System.out.println(" \n ***************************************************************************** ");
        CustomerQueue cq = new CustomerQueue();
        Thread pro = new producer(cq);
        Thread con = new consumer(cq);
        pro.setName("Producer");
        con.setName("Consumer");
        pro.start();
        con.start();

    }

}

class producer extends Thread
{

    int count = 0;
    CustomerQueue qp;

    public producer(CustomerQueue q)
    {
        qp = q;
    }

    @Override
    public void run()
    {

        while (count < ProducerConsumerQ.NEED_TO_SERVE_BATCH)
        {
            System.out.println("\n");
            synchronized (qp.sharedCustomerQueue)
            {

                // everytime generate 5 random numbers and put in Queue
                for (int p = 0; p < 5; p++)
                {
                    qp.sharedCustomerQueue[p] = (int) ((Math.random() * 10) % 10) + "";
                    System.out.println("Producer : Genereated item is :" + qp.sharedCustomerQueue[p]);
                }
                qp.sharedCustomerQueue.notify();
                try
                {
                    qp.sharedCustomerQueue.wait();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                count++;
            }
        }

    }
}

class consumer extends Thread
{

    int count = 0;
    CustomerQueue qc;

    public consumer(CustomerQueue q)
    {
        qc = q;
    }

    @Override
    public void run()
    {

        while (count < ProducerConsumerQ.NEED_TO_SERVE_BATCH)
        {
            if (qc.sharedCustomerQueue[0] != null) // consume only if queue is filled
            {
                synchronized (qc.sharedCustomerQueue)
                {

                    for (String qItem : qc.sharedCustomerQueue)
                    {
                        System.out.println("Consumer : Consuming item " + qItem);
                    }

                    System.out.println("\n");
                    qc.sharedCustomerQueue.notify();

                    try
                    {
                        qc.sharedCustomerQueue.wait();
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    count++;
                }
            }
        }

    }
}

/*
 Producer will fill SharedQueue with 5 elements in each batch.
 Consumer will consume elements available in SharedQueue in each batch.
 Total 2 will be served.
 
 ***************************************************************************** 
Producer : Genereated item is :3
Producer : Genereated item is :5
Producer : Genereated item is :5
Producer : Genereated item is :1
Producer : Genereated item is :1
Consumer : Consuming item 3
Consumer : Consuming item 5
Consumer : Consuming item 5
Consumer : Consuming item 1
Consumer : Consuming item 1




Producer : Genereated item is :9
Producer : Genereated item is :5
Producer : Genereated item is :8
Producer : Genereated item is :9
Producer : Genereated item is :8
Consumer : Consuming item 9
Consumer : Consuming item 5
Consumer : Consuming item 8
Consumer : Consuming item 9
Consumer : Consuming item 8



*/