Terence hacks

Life as a wannabe programmer

2011 in review

The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.

Here’s an excerpt:

A San Francisco cable car holds 60 people. This blog was viewed about 1,700 times in 2011. If it were a cable car, it would take about 28 trips to carry that many people.

Click here to see the complete report.

Facebook Image Publisher Tracker

Hi there! It’s been a while since I made an article. I got pretty busy and kinda lazy over the past few months so I completely forgot that I have a blog going on.

So the last article that I made was about using Google App Engine. I stopped that project because it got pretty complicated the moment I tried using the Datastore. So now, I decided to build something simple, something that doesn’t require a database to run.

Recently, I found out that you can trace a Facebook image back to its publisher based on the image’s URL. Obviously, the image should come from Facebook’s content deliver network or CDN as people usually describe it. It’s no secret, really. I found out how immediately after my first Google search. I’m pretty sure revealing how is kind of unethical and on a moral gray area, but hey, the information is already out there. It’s not like I can make it any worse by posting it here. Besides, I trust that any reader of this blog (if there’s really anyone reading this) would be responsible enough not to use it for evil.

Let’s say, for example, I posted this Facebook picture of me somewhere on the Internet. At the 3rd level of the image link, there’s a set of numbers separated by an underscore (63442_472293192932_636557932_5746749_150701_n.jpg). These numbers are actually time stamps, but among those time stamps is the profile ID of the publisher of the image. What you want to do is to get the 3rd one (636557932), which is the profile ID, and type the link in your browser (http://facebook.com/profile.php?id=<insert profile ID here>/). That wasn’t so hard right? I’m pretty sure anyone persistent enough would be able to figure it out on their own.

After I learned how to do that, I thought it would be fun to make a simple tool to automate the process since I’m too lazy to type it in the address bar every time. Then I realized that it’s pretty easy to make it into a Google App Engine app, so I did.

Since I didn’t really want this article to be lengthy and cause people to get bored, I decided to skip with the code discussion and just get on with showing you the actual software itself. The tool can be found here and the source code for the whole project can be found here.

A noob’s attempt on Google App Engine using Java part 2

In my last entry, I modified my Maven project so it can be deployed to Google App Engine. This time, I’m going to work on my actual application so I can finally have something to show to other people.

Right now, my Spring web application works fine on Google App Engine. Let me go over the code that I have so far. I first made a bean called Manufacturer:

package com.jenanderic.domain;

public class Manufacturer {
	private int id;
	private String name;
	private String address;
	
	// Constructors
	
	// Getters and Setters
}

Next, I made an implementation class of a service interface for the bean that I just made:

package com.jenanderic.service;

// Imports

public class ManufacturerServiceImpl implements ManufacturerService {
	
	@Autowired
	private List<Manufacturer> manufacturers;
	
	// Getters and Setters
}

I made it autowired so Spring will do the injection for me. I then made the controller to handle the requests:

package com.jenanderic.web;

// Imports

@Controller
@RequestMapping("/manufacturer")
public class ManufacturerController {

	private ManufacturerService manufacturerService;
	
	@Autowired
	public void setManufacturerService(ManufacturerService manufacturerService) {
		this.manufacturerService = manufacturerService;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String getManufacturerList(Model model) {
		model.addAttribute("manufacturers", manufacturerService.getAllManufacturers());
		return "manufacturer_list";
	}
}

Then, I made the template page since the controller returns a page called manufacturer_list.jsp:

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<html>
	<head>
		<title><spring:message code="manufacturer.title" /></title>
	</head>
	<body>
		<h2><spring:message code="manufacturer.heading" /></h2>
		<c:forEach items="${manufacturers}" var="manufacturer">
			<p><c:out value="${manufacturer.id}"/> - 
			<c:out value="${manufacturer.name}"/> - <c:out value="${manufacturer.address}"/></p>
		</c:forEach>
	</body>
</html>

Now that everything’s almost done, it’s time to make an actual bean to inject into the controller. I placed it in the Application’s context:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<bean id="manufacturerService" class="com.jenanderic.service.ManufacturerServiceImpl">
		<property name="manufacturers">
			<list>
				<ref local="manufacturer1" />
				<ref local="manufacturer2" />
				<ref local="manufacturer3" />
			</list>
		</property>
	</bean>
	
	<bean id="manufacturer1" class="com.jenanderic.domain.Manufacturer">
		<property name="id" value="1" />
		<property name="name" value="Manufacturer #1" />
		<property name="address" value="Address1" />
	</bean>
	
	<bean id="manufacturer2" class="com.jenanderic.domain.Manufacturer">
		<property name="id" value="2" />
		<property name="name" value="Manufacturer #2" />
		<property name="address" value="Address2" />
	</bean>
	
	<bean id="manufacturer3" class="com.jenanderic.domain.Manufacturer">
		<property name="id" value="3" />
		<property name="name" value="Manufacturer #3" />
		<property name="address" value="Address3" />
	</bean>
	
</beans>

Since I have it autowired, I don’t need to explicitly inject it into the controller. Pretty cool, right?

Anyway, if I build and run the web app in GAE’s development server, it will run smoothly with no problems, so I’m sure that my web app runs 100% fine at this point.

Of course, I need to add persistence in the application since web apps are mostly about persistence. This is where it gets complicated. Even though I have plenty of experience working with database-driven applications, Google App Engine makes it seem otherwise.

Google App Engine works in a manner very different from traditional platforms. The most noticeable is the way it handles persistence; it doesn’t support relational databases. Google App Engine uses a non-relational database called the datastore. At the moment, I don’t have a full grasp of the pros and cons of not being able to use a relational database, but I’m pretty sure it has a great impact with the way you will build your application. Working with a non-relational database means that you have to manually maintain indexes with hand-written code. Aside from that, you have to manually write code for merging the results of multiple queries. Bummer.