• No results found

NetBeans e lo sviluppo di applicazioni Java/JavaFX per Facebook. Corrado De Bari Sun Microsystems Italia

N/A
N/A
Protected

Academic year: 2021

Share "NetBeans e lo sviluppo di applicazioni Java/JavaFX per Facebook. Corrado De Bari Sun Microsystems Italia"

Copied!
38
0
0

Loading.... (view fulltext now)

Full text

(1)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

NetBeans e lo sviluppo di

applicazioni Java/JavaFX per

Facebook

(2)

Goals

Architecture

Samples step by step

References

(3)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Why JavaFX for Facebook

Facebook as a world wide content manager:

Photos and Videos the main assets

No manipulation allowed, just a repository

(4)

JavaFX vision

JavaFX is

Powered by Java

JavaFX is

the

Platform for Creating and Delivering

(5)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

JavaFX 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)

(6)

Facebook developing model

FBML or iFrame

(html/http)

HTML/HTTP

HTTP request

Glassfish

facebook

facebook

REST server

Callback canvas URL

HTTP request

HTTP request

XML or JSON

(7)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma –

30

gennaio

2010

Sun Microsystems Italia Proprietary -

All rights reserved

(8)
(9)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

(10)

Drag: “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);

(11)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Generated 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(); }

%>

(12)

Netbeans Saas generated packages

org.netbeans.saas

RestConnection/RestResponse handler

org.netbeans.saas.facebook

FacebookSocialNetworkingServiceAuthenticator.

sign() service

in FacebookSocialNetworkingService: wrapper

(13)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

setup application on FB

(14)

setup api_key & secret

(15)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

First JavaFX app

Create an FBapp as a JavaFX application

(16)

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();

(17)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Main.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

}

]

}

(18)

Setup arguments to pass JavaFX

In FBapp.html of FBapp, with a copy&past from

“Fbapp” project

Tab: Files

(19)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Setup 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>

(20)

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>

(21)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Add JavaFX app to FBSaas

(22)

Add JavaFX app to FBSaas

(23)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Run

On FBSaas project, right click to run:

(24)
(25)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Result Page

JavaFX applet

(26)

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.

(27)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Restful 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(); }

.... }

(28)

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

(29)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Implement 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;

(30)
(31)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Call 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

(32)

Facebook REST interface API

The Facebook API REST server

http://api.facebook.com/restserver.php

API

(33)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

GetFriend()

“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">

(34)

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

(35)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

Call 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

(36)

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.

(37)

Corrado De Bari – corrado.debari@sun.com – Sun Microsystems Italia

Javaday IV – Roma – 30 gennaio 2010

Sun Microsystems Italia Proprietary -

All rights reserved

(38)

References

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

Facebook apps setup

:

References

Related documents

Tests based on linear rank statistics, in particular the Wilcoxon and the median test, have been suggested repeatedly for robust edge detection in images or time series.

SC: PREVIOUSLY: Under the ALEJANDRINO DOCTRINE, mandamus will not lie against the legislative body, and its officers, to compel the performance of duties purely

material costs (parts, services and supplies) associated with all scheduled and unscheduled work/tasks associated with the maintenance (inspections, preventative

We obtain in Proposition 3.3 a scaling limit result for the joint law of the area and boundary length processes of an arbitrary peeling process on the UIHPQ S analogous to the result

We have implemented the proposed algorithm on a thermal fin model problem using least-squares cost functions with up to 6 variable parameters, and we achieve reductions in the number

In view the effects of mobility on the equilibrium price and wage levels, as analyzed in section , we find that the indroduction of progressive taxation reduces pre-tax wages, as

Spread betting was an ideal medium to take advantage of these markets, as unlike conventional share trading it allows investors to bet on markets..

In those sectors, technology trends (smart grids, the electric car…) demand higher frequency of operation, without detriment of the operational lives of the components. This