Portable REST using Spark Framework

9 January 2015 Leave a comment

Today, i will write some tiny-tutorial how to create a portable REST using Spark MVC Framework, http://sparkjava.com/. Spark is very lite and would be a good choice -and an alternative of SpringMVC- if you are developing a small project of MVC/REST.

  1. Create your Maven project and add this dependency. Notes here, i’m using Java 6 for my development and i’m too lazy to point to Java 8, so i’m using the version 0.9.9.3-SNAPSHOT of Spark. You are welcomed to use the newest version of Spark. Please refer to http://sparkjava.com/download.html.
    <dependency>
            <groupId>spark</groupId>
    	<artifactId>spark</artifactId>
    	<version>0.9.9.3-SNAPSHOT</version>
    </dependency>

    And this to your repository

    <repository>
    	<id>Spark repository</id>
    	<url>http://www.sparkjava.com/nexus/content/repositories/spark/</url>
    </repository>
  2. Code your application here.Dont forget to put the code inside of your main function, so it could be called from the command prompt.
    package com.namex.spark;
    
    import spark.Request;
    import spark.Response;
    import spark.Route;
    import spark.Spark;
    
    public class SparkHelloWorld {
    	public static void main(String[] args) {
    		Spark.get(new Route("/SparkHelloWorld/:name") {
    
    			@Override
    			public Object handle(Request req, Response res) {
    				// to get the parameter
    				String nameFromParam = req.params(":name");
    				String jsonString = "{\"message\": \"Spark Hello World\", \"name\": \"" + nameFromParam + "\"}";
    				return jsonString;
    			}
    		});
    	}
    }
    
  3. Run the SparkHelloWorld class, and open this url from your web browser http://localhost:4567/SparkHelloWorld/namingexception.

JbNIxZN

 

That’s all. It’s so simple and easy. Even though i have to admit Spark might not suitable for big-scale project, still it’s a good option to be considered.

Categories: Java Tags: , , ,

Dont Compute With Wrapper

27 March 2014 2 comments

As a Java Developer, i got these interview questions a lot… really. What is the difference between int and Integer, which one is better ?

The answer for the difference, can be answered easily, int is a primitive data type and Integer is wrapper object for int, and so on bla bla bla google it.

But, for the better one the answer would be  depends on the purpose of it. For this example, i will put simple demo to show how slow is the Wrapper when we use it for the mathematics computation.

import java.util.Date;
public class Tester {
	public void runInteger() {
		long startTime = new Date().getTime();
		Integer ctr = 0;
		for (int i = 0; i < Integer.MAX_VALUE; i++) {
			ctr += i;
		}
		long endtime = new Date().getTime();
		System.out
				.println("Time for Integer: " + (endtime - startTime) + " ms");

		System.out.println();
	}

	public void runint() {
		long startTime = new Date().getTime();

		int i = 0;
		int ctr = 0;
		for (i = 0; i < Integer.MAX_VALUE; i++) {
			ctr += i;
		}

		long endtime = new Date().getTime();

		System.out.println("Time for int: " + (endtime - startTime) + " ms");

		System.out.println();
	}

	public static void main(String[] args) {
		new Tester().runInteger();
		new Tester().runint();
		new Tester().runInteger();
	}
}

and the output was:

Time for Integer: 7652 ms

Time for int: 5 ms

Time for Integer: 7541 ms

We can see the interval difference between primitive and wrapper on this example is quite huge ~ 7500ms .  But, unfortunately, many developers today didnt and dont aware of this,  because of the auto boxing-unboxing features since Java 1.5, the process is hidden on the background.

Categories: Java

Variable Declaration : Member, Outside or Inside Loop

21 March 2014 1 comment

Well, my colleague and I, were having this discussion before. He was a bit confused with best practice (in performance matter) to declare the variable, whether it’s as a member, inside loop or outside loop.

public class InsideLoop {
	public void test() {

		for (int i = 0; i < 100000; i++) {
			int calc = (int) (i * Math.pow(i, i * 2));
			System.out.println(i);
		}

	}

	public static void main(String []args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new InsideLoop().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Inside Loop Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Inside Loop Time taken: "+ (endTime-startTime) +" ms");
	}

}

 

public class OutsideLoop {
	public void test() {
		int calc = 0;
		for (int i = 0; i < 100000; i++) {
			calc = (int) (i * Math.pow(i, i * 2));

		}

	}

	public static void main(String[] args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new OutsideLoop().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Outside Loop Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Outside Loop Time taken: " + (endTime - startTime) + " ms");
	}

}

 

public class Member {
	int calc;

	public void test() {

		for (int i = 0; i < 100000; i++) {
			calc = (int) (i * Math.pow(i, i * 2));
			System.out.println(i);
		}

	}

	public static void main(String[] args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new Member().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Member Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Member Time taken: " + (endTime - startTime) + " ms");
	}

}

And the results are:

Inside Loop Memory taken: 16044520 bytes
Inside Loop Time taken: 626 ms

Outside Loop Memory taken: 16044264 bytes
Outside Loop Time taken: 638 ms

Member Memory taken: 16044392 bytes
Member Time taken: 610 ms

As we can see, there is no significant difference with where did you place the variable – at least for this test cases. Thanks for the magic of JIT feature that Java has on it, so the optimization can be done at the runtime of the application and made this achieved.

Of course, this cant be taken generally, especially for non-primitive data type, String, Thread, etc. And depends of what operations that you do for that Object.

Categories: Java

Case Study: Factory Design Pattern

17 December 2013 11 comments

I had a job to check our project code quality. And have to report it back to my team leader for any obstacle that i found in the project.  I found a lot of leaks and i think would be good to be discussed on the blog. Not to mock the author, but to learn and improve ourselves together.

Like this code, this is the part that i found in our code.

 public ContactInfoBean(final Reseller resellerInfo) {

        switch(resellerInfo.getType()) {

            case PROGRAM_CONTACT:

                readExecutiveInfo(resellerInfo);

                break;

            case FILE_CONTACT:

                readOperationalInfo(resellerInfo);

                break;

            default:

                break;

        }

    }

The code works fine, and do its job pretty well. But some problem will appear by using this code-style. This class will grow tailing the biz changes, as usual, the bigger one class, the “merrier” to maintain it is. And most likely this class, will be having more than one purpose, can be called low-cohesion.

Better OOP Approach

Well the better approach for the case above would be using the Factory Design Pattern.  We can let the factory of  READER to generate every single instance according to their type. It would be easier to grow the instance type, since we just need to create a new class and do a little modification in the Factory class. The caller class, wont grow and will stand still at its current shape.


public interface InfoReader {

	public void readInfo();

}

public class ExecutiveReader implements InfoReader {

	public void readInfo() {

		// override

	}

}

public class OperationalReader implements InfoReader {

	public void readInfo() {

		// override

	}

}

And The Factory

public enum ReaderType {
PROGRAM_CONTACT, FILE_CONTACT
}

 

public class InfoReaderFactory {

	public static InfoReader getInstance(ReaderType type) {

		InfoReader instance = null;

		switch (type) {

		case PROGRAM_CONTACT:

			instance = new ExecutiveReader();

			break;

		case FILE_CONTACT:

			instance = new OperationalReader();

			break;

		default:

			throw new IllegalArgumentException("Unknown Reseller");

		}

		return instance;

	}

}

And now The Caller


InfoReader reader = InfoReaderFactory.getInstance(resellerInfo.getType());

reader.readInfo();

The Benefits

With the Factory Design Pattern to handle this case, we can achieve some benefits,

  • Specifying a class for one task, means, easier to maintain since one class is for one purpose only (modularity/High Cohesion). i.e: Operational Reader is only to read data for Operational only, no other purpose. Just in case, one day in the future we need another Reader (say: NonOperationalReader). We just need create a new Class that extends (or implements) the InfoReader class and then we can override our own readInfo() function. This Caller class will have no impact. We just need to do some modification in the Factory code (and the Enum of course.
public enum ReaderType {
PROGRAM_CONTACT, FILE_CONTACT, NEW_READER
}

 

public class InfoReaderFactory {

	public static InfoReader getInstance(RaederType type) {

		InfoReader instance = null;

		switch (type) {

		case PROGRAM_CONTACT:

			instance = new ExecutiveReader();

			break;

		case FILE_CONTACT:

			instance = new OperationalReader();

			break;

		case NEW_READER:

			instance = new NonOperationalReader();

			break;

		default:

			throw new IllegalArgumentException("Unknown Reseller");

		}

		return instance;

	}

}

  • Higher Reusability of Parent’s Component (Inheritance): Since we have parent class (InfoReader), we can put common functions and thingies inside this InfoReader class, and later all of the derivative classes (ExecutiveReader and OperationalReader) can reuse the common components from InfoReader . Avoid code redundancy and can minimize coding time. Eventhough this one depends on how you do the code and cant be guaranteed 😀

But, It’s Run Perfectly, Should We Change It?

Obviously the answer is big NO. This is only the case study and for your further experience and knowledge. OOP is good, do it anywhere it’s applicable. But the most important thing is, if it’s running, dont change it. It would be ridiculous if you ruin the entire working code just to pursue some OOP approach. Dont be naive also, no one can achieve the perfect code. The most important is we know what is the better approach.

This blog has some modifications and improvements caused by some comments in here. Personally, thanks for great input so i can keep the quality of this post.

Categories: Java, Miscellaneous Tags: , , ,

Calling private methods publicly ?

We Java developers, known 4 access modifiers in Java: private, protected, public, and package. Well, except for the private, the last three, can be called from outside of the class by inheritance, same package or from the instance.

Now, the common question, can private be called publicly (from outside class)? well the answer is NO and YES. No when you use ‘usual’ way to access it, and YES when you ‘hack’ into it using the Reflection API provided by Java itself.

Well okay, now just write the code that we will hack into. I called it as “TheVictim

package com.namex.hack;

public class TheVictim {
	private void hackTest() {
		System.out.println("hackTest called");
	}

	private static void hackTestStatic() {
		System.out.println("hackTestStatic called");
	}

}

Now after that, just follow my code and try to run it. I guarantee that if you followed it right, you will get TheVictim to call both of the hackTest and hackTestStatic. And you can see the output on your screen.

package com.namex.hack;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class HackTest {
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

		Class c = TheVictim.class;

		Method[] ms = c.getDeclaredMethods();

		for (Method each : ms) {
			String methodName = each.getName();
			each.setAccessible(true); // this is the key
			if (Modifier.isPrivate(each.getModifiers())) {
				
				if (Modifier.isStatic(each.getModifiers())) {
					// static doesnt require the instance to call it.
					each.invoke(TheVictim.class, new Object[] {});
				} else {
					each.invoke(new TheVictim(), new Object[] {});
				}
			}
		}

	}
}


Output example:

hackTestStatic called
hackTest called

Okay, this tutorial has met its purpose. Now you know the Reflection API of java is very powerful feature of programming language. And it’s all up to you to modify or even extend it for your own purpose. Have fun with Java 🙂

Categories: Java Tags: ,

Task Driven vs Event Driven on Struts 2 Action

I had a job to design and develop a new framework for my own development team. We made the decision that we will adopt the Struts 2 as my MVC framework.

Yes i see the benefit from Struts 2 very well, easy to implement and has so many features. Until, my team member said “Hey, i hate Struts very much,  Because i have to put many exception-handling and/or debugger inside many events/methods. Can we use another framework? or just use the servlet mixed with the scriptlet?”

Well i know his concern, it would be easy to read and maintain the code with their exception-handling when your code was still small, but along with the time, you will have your own trouble to maintain the code to stay in a good shape, believe  me, i have had this kind of experience before.

So, i think and try to find a better solution and lucky for me, i found the much better solution with the Task-Driven approach. Maybe for some of you this approach will look like a tricky, but no problem as long as my team could gain a benefit from it and keep the code stay in healthy shape :).

Well, as a brief introduction for Struts 2. This Action code,perhaps, is the common way that almost every single Struts 2 developer will do. As you can see, you can have many methods that indicates as an “EVENT”

As you can see, i have the events/methods execute(), save(), fetch(), and delete().

and you can see on the above code, that you have to do the exception-handling four times, one each method.  For me — and team — one is more than enough, easy to read and maintain the code, especially when exception occur.

Now, my approach is to convert the familiarity of the event-driven into the task-driven.  So, i just have to add a new parameter that i gave name task, as a flagging parameter. In here i will have only 4 tasks to subtitute the 4 methods, “execute“, “save“, “fetch” and “delete“.

Implementing the code is easy, just put one extra attribute on the Action code, i named it task.

And on the JSP, only add a minor change, and i will give you the comparison between the event-driven and task-driven.

As you can see the difference between those 2 slices of code. I dont know how about you, but i love the task-driven Struts more than the event-driven. Maybe it’s relative, the answer could be different for another developer, but the most important for me is now my developers are happier to use Struts.

Categories: Java

Tremendous Moment of NamingException

17 March 2012 Leave a comment

These several months, has been tremendous year for me and my NamingException blog. Last year, around October 2011, ive been asked to be contributor of Java Code Geeks, and not only that. According to my private email with JCG founder, it turns out i’m the only and the first Indonesian Contributor at that time.

And now, in March 2012, ive been asked by Curator of Dzone.com for them to repost my blog content at their JavaLobby lounge and got an offer to join their Most Valuable Blogger (MVB) program. Of course, i accepted that offer.

Too bad, i havent updated my NamingException yet for several months, not because i dont want to, and im not too busy too, but honestly i have no idea about new and quality topic. I dont want my blog only act as “reposter” site, just to fulfill my post-target, for me, quantity is important but quality is still at the top.

If you have any idea to be discussed about, please let me know and help me to make this NamingException better and better in quality. I expect no commercial reason in here, all of my post are always and always will free and open-source. Never did, never do and never will about money. Let share about our skill and experience, from community, by community and to community. Let the benefit come to us by itself 🙂

Categories: Miscellaneous

Implementing URL Shortener with Servlet

10 October 2011 17 comments

I bet that most of you already tried Url Shortener Service to make your long link shorter and easier to write and remember. And perhaps you use more than one service, you name it ow.ly, goo.gl, bit.ly and still tons of services like that available freely on the cloud. But, how it works? here i will share its implementation with Servlet. It’s easy to implement, believe me.

First of all, prepare several tools below:

  • MySql Database Server. I use version 5.1.49 Community Edition, you can download newest MySql from http://www.mysql.com/downloads/mysql/. Or you can choose another DBMS implementation, like Oracle, Sql Server, etc.
  • MySql Connector/J, as our JDBC driver for MySql. You can download it from http://www.mysql.com/downloads/connector/j/. Please use suitable JDBC Driver if you choose another DBMS.
  • Servlet Container, here i use Apache Tomcat 6.0.32, you can download ithttp://tomcat.apache.org/. Of course, you can replace it with your own most favourite Web Server; Jetty, Glassfish, JBoss, etc.
  • servlet-api.jar, this is jar of Servlet API. You will need it to create a simple Http Servlet. You can download it from http://www.java2s.com/Code/Jar/STUVWXYZ/Downloadservletapijar.htm just in case you dont have it.
  • Your favourite Java IDE. In this tutorial I’m using Eclipse Helios 3.6, feel free to use similar IDE.

After you have all of these things above, we can start it. In order to make this post shorter, i assume that you have experience in creating new Web Project and adding required jars (servlet-api.jar and Mysql Connector/J ) to the classpath.

Scenario .

Before we started to the code, maybe i have to describe the business scenario first. My scenario is i want to make a simple program that take input of my long url and return a shorter link that formed like http://myserver/{id} , where {id} is my identifier to find the original url and turn my browser to open it.

Create Database

Run this Sql Script on your DBMS engine to create a table that stores our data. Dont forget, the id column has to be auto-increment. This is our primary key and we need it as a short url. You need to modify the Sql if you use other DBMS.

CREATE DATABASE /*!32312 IF NOT EXISTS*/`url_shortener` /*!40100 DEFAULT CHARACTER SET latin1 */;

CREATE TABLE `url_data` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `long_url` text NOT NULL,
  PRIMARY KEY (`id`)
);


Creating Servlet to Insert Long Url.

Now, create a new Java Class that extends javax.servlet.http.HttpServlet object and type below code.

package com.namex.shortener;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Insert extends HttpServlet {

    /**
     * Both POST and GET method are allowed to insert new record
     *
     */

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	doGet(request, response);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	String longUrl = request.getParameter("longUrl");
	request.getSession().setAttribute("a", "a");
	String a = response.encodeURL(request.getRequestURI());
	request.getSession().setAttribute("a", "a");
	System.out.println("->"+request.getRequestURI());
	System.out.println(a);
	System.out.println("shortening " + longUrl);
	String serverName = request.getServerName();
	int port = request.getServerPort();
	String contextPath = request.getContextPath();
	String shortUrl = null;
	try {
	    shortUrl = new Logic().getShort(serverName, port, contextPath,
		    longUrl);
	} catch (Exception e) {

	    e.printStackTrace();
	}
	System.out.println("short url: " + shortUrl);
	request.getSession().setAttribute("shortUrl", shortUrl);
	response.sendRedirect("index.jsp");
    }
}

In my case, i want my program to handle both of POST and GET method, so i need to implement both of doGet and doPost method. Just in case you only want to handle GET method and not the POST, so you only need to override the doGet method.

See the new Logic().getShort(serverName, port, contextPath, longUrl); line. That is our Logic class that handle the whole business-logic problem. So we can see our servlet neater. Below is complete Logic source code, just type the whole thing, including not used yet function. And dont forget to adjust the getConnection according to your DBMS IP Address and credential.

package com.namex.shortener;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Logic {
    public Connection getConnection() throws IllegalAccessException,
	    ClassNotFoundException, SQLException {
	//adjust it
	String url = "jdbc:mysql://localhost/url_shortener";
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = DriverManager.getConnection(url, "root", "root");
	return conn;
    }

    public String getId(String longUrl) throws Exception {
	Connection conn = null;
	ResultSet rs = null;
	Statement st = null;

	String query = "SELECT id FROM url_data WHERE long_url='"
		+ longUrl.trim() + "'";
	String id = null;
	try {
	    try {
		conn = getConnection();
		st = conn.createStatement();
		rs = st.executeQuery(query);
		if (rs.next()) {
		    id = rs.getString("id");
		}
	    } finally {

		if (rs != null) {
		    rs.close();
		}
		if (st != null) {
		    st.close();
		}
		if (conn != null) {
		    conn.close();
		}
	    }
	} catch (Exception e) {
	    throw e;
	}
	return id;
    }

    public String getShort(String serverName, int port, String contextPath,
	    String longUrl) throws Exception {

	Connection conn = null;

	Statement st = null;
	String id = getId(longUrl);// check if URL has been shorten already
	if (id != null) {
	    // if id is not null, this link has been shorten already.
	    // nothing to do

	} else {
	    // at this point id is null, make it shorter
	    String sqlInsert = "INSERT INTO url_data(long_url) VALUES('"
		    + longUrl.trim() + "')";
	    try {
		conn = getConnection();
		st = conn.createStatement();
		st.execute(sqlInsert);
	    } finally {
		if (st != null) {
		    st.close();
		}
		if (conn != null) {
		    conn.close();
		}
	    }
	    // after we insert the record, we obtain the ID as identifier of our
	    // new short link
	    id = getId(longUrl);

	}
	return "http://" + serverName + ":" + port + contextPath + "/" + id;
    }

    public String getLongUrl(String urlId) throws Exception {
	if (urlId.startsWith("/")) {
	    urlId = urlId.replace("/", "");
	}
	String query = "SELECT long_url FROM url_data where id=" + urlId;
	String longUrl = null;
	Connection conn = null;
	ResultSet rs = null;
	Statement st = null;

	try {
	    conn = getConnection();
	    st = conn.createStatement();
	    rs = st.executeQuery(query);
	    if (rs.next()) {
		longUrl = rs.getString("long_url");
	    }
	} finally {

	    if (rs != null) {
		rs.close();
	    }
	    if (st != null) {
		st.close();
	    }
	    if (conn != null) {
		conn.close();
	    }
	}

	return longUrl;
    }

}

Adjusting web.xml for Insert Servlet.

To let the Server identify your new created servlet, adjust your web.xml like this.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>URLShortener</display-name>

	<servlet>
		<servlet-name>Insert</servlet-name>
		<servlet-class>com.namex.shortener.Insert</servlet-class>
		<load-on-startup>-1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Insert</servlet-name>
		<url-pattern>/insert</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

You see the <servlet></servlet> tag, between that tag is our servlet class and we give it a name Insert. So whenever we need to do configuration to this servlet, we use the name Insert. Dont forget to give negative integer to <load-on-startup></load-on-startup> because we wont need it to be loaded when the server is up. We only need the servlet loaded when it called explicitly from our program. You can see more detail about <servlet></servlet> tag here http://download.oracle.com/docs/cd/E13222_01/wls/docs81/webapp/web_xml.html#1039287

The <servlet-mapping></servlet-mapping> identify when the servlet is called by the program. In our case, the Insert servlet will be called when someone make request to /{context-path}/Insert.

Create User Interface.

Now it’s time to provide some user interface, i make a simple one with only 1 text-field and a simple scriptlet to view our recently shorted link. You can beautify it if you want. Put it on top of WEB-INF folder as index.jsp, because we want it to be the first page of our program.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
	<form action="/url/insert" method="GET">
		Long URL: <input type="text" name="longUrl" size="100" /> <input
			type="submit" value="Get Short !" />

	</form>
	</p>
	<%
	    if (session.getAttribute("shortUrl") != null) {
	%>
	Hi, your short url is:
	<br />
	<br />
	<%=session.getAttribute("shortUrl")%>

	<%
	    }
	%>
</body>
</html>

url short 001

Testing Shortening

After we did all the steps above, now we can try it in action. Compile and make a WAR of your recently create application and deploy it on Tomcat. And open the program using your browser. Input the long URL, press the button and you can see the short URL generated below.

url short 002

the result would be

url short 003

IF, remember the BIG IF everything runs perfectly, our task isnt done yet. We still have to make a redirector that transforming the short url to the original url.

Creating Servlet to Transform Short Url.

Similar proses like here. Just type the Retrieve servlet, and be aware of line new Logic().getLongUrl(urlId);. We have had created this before and if you did what i told you before to code the Logic class completely, with all of its functions, the Retrieve servlet code should be perfect.

package com.namex.shortener;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Retrieve extends HttpServlet {

    private static final long serialVersionUID = 1293961717469276130L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	String urlId = request.getServletPath();

	String longUrl = null;
	if (urlId != null && !"".equals(urlId)) {
	    try {
		longUrl = new Logic().getLongUrl(urlId);
	    } catch (Exception e) {
		// handling exception here
		e.printStackTrace();
	    }
	}

	if (longUrl == null) {
	    // if long url not found, send to index.jsp
	    System.out.println("long url not found, back to index.jsp");
	    response.sendRedirect("index.jsp");
	} else {
	    //if long url found, so redirect the browser
	    System.out.println("redirecting to "+longUrl );
	    response.sendRedirect(longUrl);
	}
    }

}

Adjusting web.xml for Retrieve Servlet.

Dont forget we have to adjust the web.xml setting to let the Tomcat identify our servlet. The whole web.xml would be like this.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>URLShortener</display-name>

	<servlet>
		<servlet-name>Insert</servlet-name>
		<servlet-class>com.namex.shortener.Insert</servlet-class>
		<load-on-startup>-1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Insert</servlet-name>
		<url-pattern>/insert</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>Retrieve</servlet-name>
		<servlet-class>com.namex.shortener.Retrieve</servlet-class>
		<load-on-startup>-1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Retrieve</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Test the Whole Shortener.

This is our last part, now you can test the program completely. Run your Tomcat and open our program. Insert the long url and open the short url in your browser, see if it return your page or not.

Now, our Url Shortening service has been created. There are still many ways to implement it using various techniques, we can use SOAP, REST or even EJB to handle the shortening and retrieving url. Now it’s your turn to improve it and implement it for your own need.

Categories: Java

How Easy to Make Your Own Twitter Client Using Java

12 September 2011 53 comments

Got inspired by my friend, whom built his own Twitter client for his company’s client. So, i tried to build one using Java. And im surprised that how easy to make Twitter client, of course i still use third-party API to make my job easier.

This simple client will only have 2 purposes: reading timeline and post status. Dont worry, you can expand this application later, it’s simple and easy once you have your app got authorized by Twitter.

First of all, you have to go to official Twitter Developer Registration at https://dev.twitter.com/apps/new , and register your application detail there. For this blog purpose, i will create a new application that called “Namex Tweet for Demo“. It’s simple, just fill in some required data and voila it’s done in seconds.

After you passed this registration step, dont forget the most important things in here are these Consumer and Consumer Secret key. Just say, it’s a signature to let Twitter knows your application. These things will be hardcoded at your application. In here, my Consumer key is DXjHgk9BHPmekJ2r7OnDg and my Consumer Secret key is u36Xuak99M9tf9Jfms8syFjf1k2LLH9XKJTrAbftE0 . Dont use these keys in your application, it’s useless because i will turn off the application as short as this blogging purpose done.

consumer key

And after registration step dont forget to visit Setting page and adjust setting for your application access.

tab setting

app type

Choose Read, Write and Access direct messages to get your application at full functional. you now can download additional java API for twitter, im using Twitter4J . Here, you have to download several jars,

  • twitter4j-async-<a.b.c>
  • twitter4j-core-<a.b.c>
  • twitter4j-media-support-<a.b.c>
  • twitter4j-stream-<a.b.c>

Notes: Dont use twitter4j-appengine.jar, it will cause your application thrown to exception on authorizing process.

In my version a is 2, b is 2 and c is 4. So it would look like twitter4j-async-2.2.4 etc. After these jars being downloaded at your machine, our downloading job has not done yet. We still have to download Apache Commons Codec as Twitter4J dependencies. After all of the jars downloaded, now we can start to code. Open your fave IDE and start it with me.

package com.namex.tweet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

public class NamexTweet {
    private final static String CONSUMER_KEY = "DXjHgk9BHPmekJ2r7OnDg";
    private final static String CONSUMER_KEY_SECRET = "u36Xuak99M9tf9Jfms8syFjf1k2LLH9XKJTrAbftE0";

    public void start() throws TwitterException, IOException {

	Twitter twitter = new TwitterFactory().getInstance();
	twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);
	RequestToken requestToken = twitter.getOAuthRequestToken();
	System.out.println("Authorization URL: \n"
		+ requestToken.getAuthorizationURL());

	AccessToken accessToken = null;

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	while (null == accessToken) {
	    try {
		System.out.print("Input PIN here: ");
		String pin = br.readLine();

		accessToken = twitter.getOAuthAccessToken(requestToken, pin);

	    } catch (TwitterException te) {

		System.out.println("Failed to get access token, caused by: "
			+ te.getMessage());

		System.out.println("Retry input PIN");

	    }
	}

	System.out.println("Access Token: " + accessToken.getToken());
	System.out.println("Access Token Secret: "
		+ accessToken.getTokenSecret());

	twitter.updateStatus("hi.. im updating this using Namex Tweet for Demo");

    }

    public static void main(String[] args) throws Exception {
	new NamexTweet().start();// run the Twitter client
    }
}

Compile and run the code, it will create permission for “Namex Tweet for Demo” to be linked with your Twitter account. Just open the “Authorization URL” shown at the screen and input the PIN shown by the website. Your application will send back the pin to Twitter, if it’s match your account will be linked with this new application and you can see you just posted a new status using “Namex Tweet for Demo “. Congratulation!

Notes: Authorization URL and PIN will generated differently each time it’s run.

auth url

auth pin

update tweet

In here you can see, we input no username and password of Twitter account but we can use our account within application. Yeah it’s possible because of OAuth . It “transformed” password-input-process to sending-receive-token. So dont worry, Third-party Twitter client application cant read and store no password of your Twitter account. In simple, it’s safer and prevent password thieving.

Now we still have a tiny problem, at this point, your program still need to open Twitter’s website and input pin back to the application. So, maybe you are asking on the cloud, do i need this annoying authorization on the future ? well, gladly the answer is NO. At the time your app being authorized by Twitter, you have no use to re-authorize it again — with a simple note you have to save the Access Token and Secret Access Token . What the hell is that, how could i get that. Well, you have it already, see the image below, i put it in a big red rectangle so it will be more eye-catchy. In here, our token is and our secret token is. These 2 tokens have to be saved somewhere, you can choose your own method to save it: Persistence, CSV, DBMS, etc. It’s all up to you.

access token

So, i saved the tokens! How do i reuse it? It’s simple, see below code. It’s how to use your tokens, so you wont have the re-authorization process again. Try to post and read your timeline now.

package com.namex.tweet;

import java.io.IOException;

import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;

public class NamexTweet {
    private final static String CONSUMER_KEY = "DXjHgk9BHPmekJ2r7OnDg";
    private final static String CONSUMER_KEY_SECRET = "u36Xuak99M9tf9Jfms8syFjf1k2LLH9XKJTrAbftE0";

    public void start() throws TwitterException, IOException {

	Twitter twitter = new TwitterFactory().getInstance();
	twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);

	// here's the difference
	String accessToken = getSavedAccessToken();
	String accessTokenSecret = getSavedAccessTokenSecret();
	AccessToken oathAccessToken = new AccessToken(accessToken,
		accessTokenSecret);

	twitter.setOAuthAccessToken(oathAccessToken);
	// end of difference

	twitter.updateStatus("Hi, im updating status again from Namex Tweet for Demo");

	System.out.println("\nMy Timeline:");

	// I'm reading your timeline
	ResponseList list = twitter.getHomeTimeline();
	for (Status each : list) {

	    System.out.println("Sent by: @" + each.getUser().getScreenName()
		    + " - " + each.getUser().getName() + "\n" + each.getText()
		    + "\n");
	}

    }

    private String getSavedAccessTokenSecret() {
	// consider this is method to get your previously saved Access Token
	// Secret
	return "oC8tImRFL6i8TuRkTEaIcWsF8oY4SL5iTGNkG9O0Q";
    }

    private String getSavedAccessToken() {
	// consider this is method to get your previously saved Access Token
	return "102333999-M4W1Jtp8y8QY8RH7OxGWbM5Len5xOeeTUuG7QfcY";
    }

    public static void main(String[] args) throws Exception {
	new NamexTweet().start();
    }

}

post again

tl

Now our simple Twitter application has been -could be- done, we can read and post to Twitter. Of course, many things still on the task list if you want to make it professionally and -perhaps- sell it. A nice UI, reading and sending Direct Message, Searching Users, Follow and Unfollow. I put these jobs on your shoulder, cause i just want to share it’s easy to make a Twitter client and i hope this short tutorial can help you in developing Twitter client using Java.

Happy Code All !

Categories: Java, Miscellaneous Tags: , ,

Learn fundamental of Java ? Use ancient JDK

Many Java developers are categorized as a good one in developing program using Java. But, i met so many of them are weak and less in java fundamentals itself. They can develop new program easily. But when i asked, what happening in background of java itself. Only few of them can explain it goodly and in appropriate way. Start to investigate what happening with them, reflecting they are not a newbie as Java Developer, 2 or 3 years in field experience.

After some research about situation and background of few developers. I can pull out my own conclusion, they are facing some fundamental problems because they are started to code using JDK 1.5, i classified this version as modern JDK. In 1.5 and later there are boxing-unboxing feature to make easier relationship between primitive and its wrapper, foreach loop statement to make looping process simpler. We wouldn’t see those things in 1.4. In 1.4 we have to use primitive data types and the wrapper explicitly and we still have to do explicit type-casting instead of using foreach. In my simple words: “Almost everything run by write it!”.

I’m not saying here that manual is the best way to do everything, but i found many cases during my professional experience, that with manual ways, we can learn how one process in a code could be done and doing analytic about whats happening with the code.

For the simplest example, look at between 2 pieces of code.

  • int myNumber = new Integer(10);
  • int mySecondNumber = 10;

On JDK 1,5 those codes will produce exact results. Nothing error and nothing produce runtime  exception. I give simple question to some people that declared as Java Developer. Only few of them can give me satisfaction answer.

Now with JDK 1.4 one of those 2 codes will produce error. The first one will produce compilation error. Meanwhile with 1.5, the first one will run perfectly but the code will take some unnecessary processes, means the code will use some space of memory for useless thing.

And yet another example,

  • List<SomeClass> myList= new ArrayList<SomeClass>();
  • List mySecondList= new ArrayList();

Whats the difference between both lists? well the most common answer i got only, “You can use foreach for myList but not to mySecondList”, well the answer is right, i wont hesitate about that. But still, the answer i got wasn’t the answer that i looking for. I’m looking for deeper answer, fundamentally.

For the ancient one, i still give 1.4 my humble recommendation. Maybe it’s a little bias because i started my java code using 1.4, forgive me for this. And please dont take me wrong, i’m not saying that the latest JDK is bad or automatic process is bad. I believe in thought that Java Architects have done very good job by providing simplicity and easiness in use of Java. But still, responsibility to know Java in fundamental way itself still be ours.

So, if you want to know better about Java Fundamental, maybe you should try ancient JDK. Watch and learn the process. And spread your own knowledge.

Well, i wouldn’t give you the answer for the differences of my codes above. Let it be your tasks, considered this is your homework 🙂

And for the last, this article based on my truly experience so maybe the content wont fit you at all. Please share your own or give me correction if you are thinking my article is too cheesy, old-style or maybe too idealist. Im glad to read from you soon 🙂

Categories: Java, Miscellaneous