Note: The current release is NetBeans IDE 6.5. If you are using NetBeans IDE 6.5, see the latest version of this tutorial.
NetBeans IDE 6.1 provides a Web Services Manager that supports Software as a Service (SaaS) applications. SaaS refers to a software application delivery model where a software vendor develops a web-native software application and hosts and operates the application for use by its customers over the Internet. SaaS is an increasingly popular model for providing software functionality as it is economical in terms of both cost and customer hardware resources. The IDE now makes it easy for Java developers to access all the popular SaaS services on the web.
The Facebook Platform is a standards-based web service with methods for accessing and contributing Facebook data. The Facebook Platform is documented on the Facebook Developers Wiki.
Facebook secures web service communication with the User Login mechanism. The application developer needs to register the application with Facebook to get an API key and a secret key. Every method call to Facebook needs to be signed using the secret key. During the login process, the application first calls Facebook to request a token using the API key. Next, the user is redirected to a login URL constructed from the API key and the token. This URL takes the user to a login page on Facebook. After the user logs into Facebook and authorizes the application to call Facebook on his/her behalf, the application sends back a session key and a session secret key. From them on, all API calls must pass the session key and be signed using the session secret key.
In this tutorial, you use the simple friends_get operation inserted into a PSP file to return a list of the numeric IDs of all a user's Facebook friends. The tutorial focuses on showing you how to set up the complicated Facebook User Login authentication. By analogy this should help you with other User Login services, such as Flickr's.
Contents
To follow this tutorial, you need the following software and resources.
The IDE lets you insert SaaS operations into servlets, existing RESTful services, JSP pages, or plain Java objects (POJOs). In this tutorial, you create a web application project and add the service into the project's default JSP page.
To create the project:
Left-click the friends_get operation. Hold the left mouse button down and drag the operation into the body of index.jsp. The Customize GET SaaS dialog opens, in which you can set initial parameter values. Leave the default values and click OK. The IDE inserts the friends_get operation into the body of index.jsp, which now looks like this:
<body>
<h1>JSP Page</h1>
<%@ page import="org.netbeans.saas.*, org.netbeans.saas.facebook.*" %>
<%
try {
String format = null;
String flid = null;
RestResponse result = FacebookSocialNetworkingService.friendsGet(request, response, format, flid);
facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse resultObj = result.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse.class);
//TODO - Uncomment the print Statement below to print result.
//out.println("The SaasService returned: "+result.getDataAsString());
} catch (Exception ex) {
ex.printStackTrace();
}
%>
</body>
If the compiler does not recognize the FriendsGetResponse class, you need to install the latest patches to the NetBeans IDE. This patch includes support for JAXB FriendsGetResponse.
You can use the FriendsGetResponse JAXB object to access the underlying XML document. Add an empty line after the facebook.socialnetworkingservice... line and type resultObj.. A pop-up window appears with all the methods that extend resultObj and that you can use to access the underlying XML.

The IDE also adds classes and the security key property file in the packages org.netbeans.saas and org.netbeans.saas.facebook. The complete project structure is shown in the Projects window.

Register the application with Facebook to get an API key and a secret key. You need to have a Facebook account. When you get the keys, copy them out of your browser and paste them into org.netbeans.saas.facebook.facebooksocialnetworkingserviceauthenticator.properties.
To register your project:
Leave all other fields with default values. Click Submit. The browser refreshes with the FacebookSaas application home page. Newly generated API and secret keys are displayed. In the IDE, go to the Projects window and open facebooksocialnetworkingserviceauthenticator.properties, in org.netbeans.saas.facebook. Copy the API key and the secret key from the browser to this file. Save the file.
After you have retrieved the API and secret keys from Facebook, you can run your project.
To run the project:
Here are a few more ideas for you to explore:
For more information about using NetBeans IDE to develop RESTful web services, SaaS, and other Java EE applications, see the following resources:
To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.
You are viewing a mobilized version of this site...
View original page here