Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, November 30, 2007

HttpMethod Delegation in Jersey

I came across a new use case for the JSR 311 (Jersey) implementation. Here's the juicy details:
  • Have a collection resource and a specific resource that is delegated to (ie: UsersResource and UserResource where UsersResource delegates to UserResource if a username is specified)
  • Want to load up a User object for each UserResource so that the duplication is not done and if you request information for a User that doesn't exist, you get an error
  • However, I would like to create a User using the same style (obviously just a different http method)
I was trying to make this happen, but there appears to be something in Jersey that makes it automagically delegate to the specific resource rather than look for an exact match to the method I really want to invoke.

Here's a taste of the code:

UsersResource:
@UriTemplate("{username}")
public UserResource getUser(@UriParam("username") String username) {
User user = RealmManager.instance().getUser(username);
if (null == user) {
throw new WebApplicationException(new RuntimeException("user '" + username + "' does not exist."), 404);
}

return new UserResource(formatterFactory, uriInfo, user);
}


Tried this in UsersResource, but this was not executed:
@UriTemplate("{username}")
@HttpMethod("PUT")
public void addUser(@UriParam("username") String username) {
RealmManager.instance().createUser(username);
}


So there you have it...anyone have any ideas?

Saturday, October 20, 2007

fb_sig parameters

I've been working away at building my own java library for Facebook applications (Java-Spring-Book) and came across something today that was a little different.

For Facebook API calls, you need to have a session_key as one of your parameters. You get this key by asking for it with authToken. Nothing special here. So, my original plan was to store this session_key so that I didn't have to keep requesting one (and I'm sure many other people would do the same). For simplicity, I decided to store the key in the user's session.

As I started working away, I noticed that I wasn't able to store the key in the session (it was always blank on subsequent calls). Without going into the session more, I decided to try simple http cookies. They may not be the greatest, but they should do the job. Again, they weren't working. Cookies didn't seem to persist across subsequent calls. Strange, no?

From the looks of it, my code was working totally fine. There really wasn't any information in subsequent sessions or cookies. And from what I can tell, that's all by design. Here's my theory as to why:
  • In your application setup, you can tell Facebook whether you want to use FBML or an iFrame to display your content (FBML is a bunch of custom tags that make building apps easier)
  • In order to properly render your page inside of Facebook, a request from the user goes to Facebook to show an app. Facebook then sends a seperate, distinct request to my server to get my content. However, this request is different then the user's request, and appears to be blank each time (no persisted info). The result? No cookies for you!
So how can I go about "storing" a session_key? I don't think I have to. From the looks of it, when Facebook makes a request for my page it appends a bunch of "fb_sig_" parameters (including one for session_key). It looks like I can use these parameters in my calls.

I took a quick look through the Facebook docs and couldn't find anything. Anybody know where this might be at?

Building Custom Facebook Apps

Facebook...what a great thing. Taking a simple idea of friends, Mark has been able to create a company that is probably worth something in the billions. Not bad for someone whose my age. So now that I am in fourth year at UW for Systems Design, my friends and I decided that a design project around Facebook might become profitable (not to mention fun).

Now that Facebook has opened its developer platform up, anyone can make their own applications that users can add to their profile, and for the lucky ones, make a little money. There's even companies (such as Slide) that are based specifically around building custom apps.

Our goal? Well...something that makes something about building apps a little easier. And quite obviously, we have no idea what that something is.

So, now 2 months later, we are well on our way. The 2 other group members are looking at a fairly well-developed PHP library that Facebook provides. They seem to be having a lot of fun and finding it rather simple. And me? Not as much fun. I decided to take the Java approach. And let's just say it hasn't been the most enjoyable or easy experience of my life. But I am learning a lot.

Why isn't it so fun? Well, the Java library Facebook provides is for desktop applications, not web based apps...so some subtlety's cause some problems. But, me, being the try-hard that I am (ha!) has decided to build my own Java library...no small feat mind you.

But I did try something I've never done before (and have been silently inspired by my boss): I've started an open source project around it: Java Spring Book. Right now, it's in a very early stage...I'm still trying to figure out the details. Hopefully, someday it will be in a position to offer some help to new developers (who are like I was when I started).

Monday, February 26, 2007

CGLibbing CGLib

So for my first couple of posts, I figured I would go back and look at some of the many problems I've encountered in my daily life of Java development.

The first problem I came across was trying to CGLib an already CGLibbed object (yea...sounds a little shady to me too).

Anyways, here's the background:
- We use Hibernate (3.1.3 if you want to get specific)
- We wanted the ability to dynamically add getters and setters to objects on the fly (and these getters/setters would actually get/put information from/to another member object)

So, myself and another work colleague set out to see what we could do. First step was to actually figure out how CGLib works. This definately was a little painful since I found the documentation on CGLib to be a little poor (at best!). After a couple hours, we finally got through the basics (read: Enhancer is the class to pay attention to) and were merrily running down our unit testing path.

How far did we actually get? Quite far actually. Dynamic getters/setters? Check. Getters and setters that mapped to other member objects? Check. Being able to save these objects? BOOM!

It seems that Hibernate didn't like what we were doing. What exactly was the problem? The object we were giving Hibernate back was one it couldn't understand. So it threw one of its many exceptions (don't recall the exact one) and we were stuck. But we weren't licked yet! We decided to dive a little deeper...

So how exactly did Hibernate know that the object we were giving it wasn't one it could understand? It appears that Hibernate was calling getClass() and comparing the result against its mapping files. And our CGLibbing process meant that the getClass() wasn't really what we had (I believe it returned the Enhancer class). So further on we trudged!

(Note: The rest of the story is a little hazy at best, so I apologize now)
The short and sweet of the finale is that we investigated modifying the code that did the getClass() calling. I do believe it involved some Hibernate Interceptors, however, we felt we were jumping into a pit of problems (as I usually feel when I start mucking around the code of real professionals). Adding in the fact that we had spent several days trying out variations of our approach with no success, we decided to go back to the drawing board and try again.

So there you have it! Our CGLibbing adventure cut short as we couldn't CGLib and a CGLibbed object. But that doesn't mean its impossible! Does anyone have any success stories? I know I skimmed a bunch of blogs and the CGLib mailing list with no success, but hopefully others have had some.