Leveraging Legacy Systems and Modernization using SOA

October 22, 2008

Writing a facebook application using java

October 21, 2008

AT A GLANCE

First and foremost, I’m not sure why facebook decided to drop its support for java.  The community developing in the java space is so widespread and there is such movement in the Open Source world (utilizing this discipline) it boggles the mind.

Rant: One thing that really pisses me off is the fact that you can find how to build these applications in PHP EVERYWHERE ON THE NET!  A few handful of sites offer Java insight.  DAMN!

Before we get into the bowels of this facebook discussion, here are a few things you MUST have.

  • A facebook application developer account
    • API Key
    • Secret Key
  • A server that is reachable to the outside world.  This WILL NOT WORK from your desktop.  There is a way to test your application (using desktop development) but this is beyond the scope, as we’re developing a JEE-compliant application.
  • The willingness to get your hands dirty, coffee and some patience.

Ok, now for a few clarifications:

FBML

This is a proprietary language analogous to JSTL tags.  Basically, these tags have interaction with Facebook’s  internal services that can query user information, render forms, pages, etc.  All this while keeping a uniform look and feel.

It doesn’t matter what you write your business logic in, your presentation code in the canvas must be a combination of HTML and FBML (Facebook Markup Language).

FBML or Facebook API

Developers have the option of manipulating and creating business objects as they would with any other web application, EXCEPT you must enclose the rendering of these objects inside FBML tags.  Confused? No problem.  I’ll give you the code and the instructions afterwards …

For example:

If we have a servlet that sets the value myUserID value like so:

request.setAttribute("myUserID", "your_user_id_here");

You must extract it and present it to Facebook like so:

Hey <fb:name uid="${myUserID}" useyou="false" firstnameonly="true" />, welcome back!

This would return the following message (assuming you replaced your_user_id_here with your actual id.

Callback URL

This is something that I struggled with and something that I’ve seen to be an issue.  In short, the Callback URL is nothing more than a url to the application in your server.

For example:

This is what I use as a callback url: http://www.joshmolina.com/mysimpleapp

In my server I have an application named mysimpleapp.

You can find the source here.  (This file is a fully-compliant Eclipse project) and the web application here.

Briefly:


This is the directory structure

As you can see, there’s NOTHING out of the ordinary here.

However, I’d like to call attention to the web.xml file in the WEB-INF folder:  This file forwards to a page named index.jsp.  This becomes the landing page of your facebook application when you call it.  (more on that later)…

Here’s my web.xml file:

So, so far we’ve got an application (although basic) named mysimpleapp that does NOTHING.

Take a look at WEB-INF/lib as you’ll find all the necessary facebook API files downloaded from here.

The files are:

  • activation-1.1.jar
  • commons-logging-1.1.1.jar
  • facebook-java-api-2.0.1.jar
  • facebook-java-api-schema-2.0.1.jar
  • jaxb-api-2.1.jar
  • jaxb-impl-2.1.3.jar
  • json-20070829.jar
  • log4j-1.2.9.jar
  • stax-api-1.0-2.jar

Ok, deploy your application as shown above and you should be able to access it like this:

http://yourserver.com/mysimpleapp

or in my case: http://joshmolina.com/mysimpleapp

..more to come…