Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedNetBeans e lo sviluppo di
applicazioni Java/JavaFX per
●
Goals
●
Architecture
●
Samples step by step
●
References
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedWhy JavaFX for Facebook
●
Facebook as a world wide content manager:
Photos and Videos the main assets
●
No manipulation allowed, just a repository
JavaFX vision
JavaFX is
Powered by Java
JavaFX is
the
Platform for Creating and Delivering
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedJavaFX architecture
JavaFX Applications and Services
JavaFX Apps Framework
JavaFX Common Profile
Desktop
Extensions
Extensions
Mobile
Java Platform (Java Plug in)
JavaFX
Desktop
Runtime
JavaFX
Mobile
Runtime
JavaFX
TV
Runtime
TV
Extensions
Authoring
Tools
Developer
Tools
(IDE Plug ins, RAD,
Production Suite)
Designer
Tools
(Authoring, Publishing,
Media Encoding)
Facebook developing model
FBML or iFrame
(html/http)
HTML/HTTP
HTTP request
Glassfish
REST server
Callback canvas URL
HTTP request
HTTP request
XML or JSON
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma –
30
gennaio
2010
Sun Microsystems Italia Proprietary -
All rights reservedCorrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedDrag: “usersGetLoggedInUser”
First Facebook Web App
●
Get Netbeans 6.8/6.7.1 with Glassfish 2.1/3
●Create a FBSaas application
●
New Web app application
●
Into
index.jsp
drag the Facebook
“usersGetLoggedInUser” service
●
Add in top JSP:
<%! String sessionKey; %>
●Add under //TODO:
sessionKey = FacebookSocialNetworkingServiceAuthenticator.getSessionKey(request,
response);
out.println("<br>The SaasService returned: " + result.getDataAsString());
out.println("<br>\nThe SaasService session returned: " + sessionKey);
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedGenerated code: org.netbeans.saas.facebook.*
<%@ page import="org.netbeans.saas.*, org.netbeans.saas.facebook.*" %> <%
Try {
String format = null;
RestResponse result = FacebookSocialNetworkingService.usersGetLoggedInUser(request, response, format);
I f (result1.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.UsersGetLoggedInUserResponse.class) instanceof facebook.socialnetworkingservice.facebookresponse.UsersGetLoggedInUserResponse) { facebook.socialnetworkingservice.facebookresponse.UsersGetLoggedInUserResponse result1Obj = result1.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.UsersGetLoggedInUserResponse.class); } els e if (result1.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.ErrorResponse.class) instanceof facebook.socialnetworkingservice.facebookresponse.ErrorResponse) { facebook.socialnetworkingservice.facebookresponse.ErrorResponse result1Obj = result1.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.ErrorResponse.class); }
//TODO - Uncomment the print Statement below to print result.
sessionKey=FacebookSocialNetworkingServiceAuthenticator.getSessionKey(request,response);
out.println("<br>The SaasService returned: " + result1.getDataAsString());
out.println("<br>\nThe SaasService session returned: " + sessionKey);
} catch (Exception ex) {
ex.printStackTrace(); }
%>
Netbeans Saas generated packages
●
org.netbeans.saas
●
RestConnection/RestResponse handler
●
org.netbeans.saas.facebook
●
FacebookSocialNetworkingServiceAuthenticator.
sign() service
●
in FacebookSocialNetworkingService: wrapper
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedsetup application on FB
setup api_key & secret
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedFirst JavaFX app
●
Create an FBapp as a JavaFX application
Main.fx (1/2)
●
In main.fx:
package fbapp;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.io.http.HttpRequest;
import javafx.scene.paint.Color;
var apiKey: String;
var sessionKey: String;
var sessionKeyjnlp: String;
sessionKey = {FX.getArgument("sessionkey")}.toString();
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedMain.fx (2/2)
●
In main.fx:
Stage {
title: "FacebookClient"
width: 500
height: 200
scene: Scene {
fill: Color.GREEN
content: [
Text {
font : Font {
size: 15
}
x: 10, y: 20
content: "SessionID:"
}
Text{
content:
bind sessionKeyjnlp
translateX: 10
translateY: 40
font:Font{
size: 15
}
}
Text {
font : Font {
size: 15
}
x: 10, y: 70
content:
bind sessionKey
}
]
}
Setup arguments to pass JavaFX
●
In FBapp.html of FBapp, with a copy&past from
●
“Fbapp” project
–
Tab: Files
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedSetup arguments to pass JavaFX
●
Past in index.jsp of FBSaas, adding a line:
<script src="http://dl.javafx.com/1.2/dtfx.js"></script>
<script>
javafx(
{
archive: "FBapp.jar",
draggable: true,
width: 500,
height: 200,
code: "fbapp.Main",
name: "FBapp",
sessionkey: "<%=sessionKey%>"
}
);
</script>
Setup arguments to pass JavaFX app
●
Past in index.jsp of FBSaas, adding a line:
<script src="http://dl.javafx.com/1.2/dtfx.js"></script>
<script>
javafx(
{
archive: "FBapp.jar",
draggable: true,
width: 500,
height: 200,
code: "fbapp.Main",
name: "FBapp"
,
sessionkey: "<%=sessionKey%>"
}
);
</script>
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedAdd JavaFX app to FBSaas
Add JavaFX app to FBSaas
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedRun
●
On FBSaas project, right click to run:
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedResult Page
JavaFX applet
Sign Service
Create a Restful wrapper on:
package org.netbeans.saas.facebook
FacebookSocialNetworkingServiceAuthenticator {
static String sign(String[][] params) {
...
}
}
This package has been created with
drag&drop Facebook services
as mentioned before
NOTE: Create a singleton RESTful resource class with GET and PUT methods using Java API for RESTful Web Service (JSR-311). This pattern is useful for creating a simple HelloWorld service and wrapper services for invoking WSDL-based web services.
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedRestful WS skeleton generated
package org.fbsaas.fxhelper; ...
@Path("signer")
public class Signer { @Context
private UriInfo context;
/** Creates a new instance of Signer */ public Signer() {
} /**
* Retrieves representation of an instance of org.fbsaas.fxhelper.Signer * @return an instance of java.lang.String
*/ @GET
@Produces("application/xml")
public String getXml() {
//TODO return proper representation object throw new UnsupportedOperationException(); }
.... }
Implement RESTful service (1/2)
package org.fbsaas.fxhelper;
import ....
import org.netbeans.saas.facebook.FacebookSocialNetworkingServiceAuthenticator;
@Path("signer")
public class Signer {
@Context
private UriInfo context;
public Signer() {
}
@GET
@Produces("application/xml")
public String getSign(
/*@DefaultValue("")*/ @QueryParam("api_key") String api_key,
@QueryParam("session_key") String session_key,
@QueryParam("call_id") String call_id,
@QueryParam("v") String v,
@QueryParam("format") String format,
@QueryParam("flid") String flid,
@QueryParam("method") String method,
@QueryParam("fields") String fields,
@QueryParam("uids") String uids) {
//TODO return proper representation object
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedImplement RESTful service (2/2)
//TODO return proper representation object
String sig = new String("ERROR");
try {
String[][] params = null;
params = new String[9][2];
params[0][0] = "api_key";
params[0][1] = api_key;
params[1][0] = "session_key";
params[1][1] = session_key;
params[2][0] = "call_id";
params[2][1] = call_id;
params[3][0] = "v";
params[3][1] = v;
params[4][0] = "format";
params[4][1] = format;
params[5][0] = "flid";
params[5][1] = flid;
params[6][0] = "method";
params[6][1] = method;
params[7][0] = "fields";
params[7][1] = fields;
params[8][0] = "uids";
params[8][1] = uids;
sig = new String(FacebookSocialNetworkingServiceAuthenticator.sign(params));
} catch (java.io.IOException e) { sig = new String("ERROR"); }String result = new String("<sigs> <sig>" + sig + "</sig> </sigs>");
return result;
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedCall Sign Service from JavaFX
package fbapp;
import javafx.data.pull.PullParser; import javafx.data.pull.Event;
public class SigPullParser {
public function parse(input:
java.io.InputStream
): String {
var sig: String;
// Parse the input data (Photo Metadata) and construct Photo instance
def parser =
PullParser
{
input
: input
onEvent
: function(event:
Event
) {
println("event text: {event.text}");
if (event.text.length()>30) { sig= event.text; }
}
}
parser.parse();
println("Signature got: {sig}"); return sig;
} }
PullParser
class get in
input
an
java.io.InputStream
and
generate
Event
parsing the
input.
Each Event may be
intercepted implementing a
callback function
onEvent
Facebook REST interface API
●
The Facebook API REST server
http://api.facebook.com/restserver.php
●
API
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedGetFriend()
“friends.get”
●
mandatory fields:
●api_key
●
call_id
●
sig ( MD5 hash of the current request and your secret key)
●v version API
●
Return example:
<?xml version="1.0" encoding="UTF-8"?> <friends_get_response
xmlns="http://api.facebook.com/1.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://api.facebook.com/1.0/
http://api.facebook.com/1.0/facebook.xsd" list="true">
Call a Facebook REST API (1)
call_id = String.valueOf(System.currentTimeMillis()); method = "facebook.friends.get";
var requestSign: HttpRequest =
HttpRequest
{
location:
"http://localhost:8080/FBSaas/resources/signer?api_key={apiKey}&session_key={sessionKey} &call_id={call_id}&v={v}&format={format}&flid={flid}&method={method}"
method:
HttpRequest.GET
onInput
: function(input: java.io.InputStream) { var parser = SigPullParser{};signature = (parser.parse(input)).toString();
try { println("bytes of content available: {input.available()}"); } finally { input.close(); } }
onDone:
function() { ...}
requestSign.start();
HttpRequest
class enable an http connection to do
REST call to
signer
services implemented before
location
determine url to call
onInput
define callback function it will be
asyncronously called passing an
java.io.InputStream
to be parsed
A
SigPullParser
instance of will parse the
signature got calling REST service
onDone
will define callback function to be called
on Http stream closed
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedCall a Facebook REST API (2)
...
onDone: function() {
var requestFriend: HttpRequest = HttpRequest {
location: "
http://api.facebook.com/restserver.php?api_key={apiKey}&session_key=
{sessionKey}&sig={signature}&call_id={call_id}&v={v}&format={format}&method={method}"
method: HttpRequest.GET
onInput: function(input: java.io.InputStream) {
var parser =
UsersPullParser
{};
friends = parser.parse(input);
for (x in friends) {insert x.uid into friendsId;}
try {
println("bytes of content available: {input.available()}");
} finally { input.close(); }
}
onDone : function(){
getPhoto(friendsId);
}
}
requestFriend.start();
}
}
In
onDone
callback function it will be
implemented the real Facebook API
call, using the signature got by server
side implemented before
UsersPullParser
is another PullParser
implementation in which a UserList it
will be parsed
GetPhoto()
“users.getInfo”
●mandatory fields:
●api_key
●call_id
●sig
●v
●
uids: (array) List of user IDs. This is a comma-separated list of
user IDs.
●
fields: (array) List of desired fields in return. This is a
comma-separated list of field strings.
It will be specified as field “
pic_square
”: URL of a square section of the
user profile picture, with width 50px and height 50px.
Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia
Javaday IV – Roma – 30 gennaio 2010
Sun Microsystems Italia Proprietary -
All rights reservedReferences
●
Call Facebook rest services by netbeans web apps:
http://www.netbeans.org/kb/61/websvc/facebook.html
●
JavaFX applet reads arguments from html page
http://forums.sun.com/thread.jspa?threadID=5387552
●
A JavaFX app as Facebook app:
http://kaansoral.com/?p=133
●