+ All Categories
Home > Technology > Telosys Services

Telosys Services

Date post: 21-May-2015
Category:
Upload: laurent-guerin
View: 233 times
Download: 0 times
Share this document with a friend
Description:
AJAX and navigation Services with the Telosys framework
Popular Tags:
23
http://telosys.ow2.org/ Services Laurent Guérin / V 1.3 / 2009 – June ( for Telosys 1.0.0 and + )
Transcript
Page 1: Telosys Services

http://telosys.ow2.org/

Services

Laurent Guérin / V 1.3 / 2009 – June

( for Telosys 1.0.0 and + )

Page 2: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 2

What is a Telosys service ?

� A Telosys service is a Java method( "execute " ) that can be called from the client side with an HTTP request ( GET or POST )

� Service input : � Http request parameters

� Service environment :� User screen session � gives access to :

� all screen contexts

� standard http session

� Application level objects

� Service output :� XML response or full page

A service is not linked with a particular “Screen Context”

( to work in a specific Screen Context usean “exec” action )

Page 3: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 3

Client

What is a Telosys service ?

Server

ScreenSession

request

2

3

1

response

processing

� The service ( Java method ) is called from the client-side ( via AJAX call, link, …)

� The JavaScript framework provides a “Service”object and “Response” object

� A new instance of the service is created for each call => a service is “thread safe”

Javascript

AJAXframework

ScreenService

Response

Service

The service responsecontains the appropriate HTTP headers to avoidbrowser caching effects

Page 4: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 4

Different kinds of services

� “AJAX services” ( RPC services )� Return : XML response

� Without “renderer”� no view in the response (only values)

� With “renderer(s)”� “view(s)” in the XML response

� Usage : “Remote Procedure Call”( to retrieve and use the result in the same page )

� “Navigation services” :� Return : full HTML page ( ScreenMap, any page, …)

� Usage : classical “page navigation”( to replace the current page or open a new window )

Page 5: Telosys Services

AJAX (RPC) services

Page 6: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 6

Client

Telosys service : Request & Response

Server

Call “name.svc”

Response

� Request : � Http GET/POST request with the service name

and a query string (parameters). Example : � http://server/context/MyServiceName.svc?p1=aa&p2=bb

� Response ( “ServiceResponse” class ) :� 0..N Values

� 0..1 Result object ( “Object” )

� 0..1 View ( produced by a "renderer" )

Params

View

Via URL with query string

ret code

Values

“name”

ScreenService

ServiceResponse Result ?

XML

Result

Page 7: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 7

How to create & use a service

� 1 – Create the service ( = create a Java Class )

. init()

. execute()

. getRenderer()

. getName()

. getSessionKey()

ScreenService<< interface >>

. init()

. getRenderer()

. getName()

. getSessionKey()

StandardScreenService<< abstract >>

. execute()

MyService

Standard base class to build services

Service interface

The service Only one method “execute”Parameters :. ScreenSession. ServiceRequest. ServiceResponseReturn type : View

return null; � XML response (AJAX)return view; � HTML response (navigation)

Page 8: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 8

How to create & use a service

public class MyService extends StandardScreenServ ice{

public View execute ( ScreenSession screenSession , ServiceRequest serviceRequest , ServiceResponse serviceResponse

) throws TelosysException{

System.out.println("TestService : execute()");//--- Processing ...

//--- Set response messageserviceResponse.addMessage(

new ScreenMessage("TestService message.", ScreenMess age.INFORMATION));

//--- Set response result serviceResponse.setResult("Result object (here a Str ing)");

//--- Set response valuesserviceResponse.setValue("ResultV1", "V1");serviceResponse.setValue("ResultV2", "V2");

//--- Return : null = no navigation return null ; // return null for AJAX call ( generate an XML re sponse )

}}

� Example :Only one method

to implement

User Session

Input parameters

Response to build

Return � Type of response : . return null : for AJAX call (response in XML). return View : for navigation (forward to the view)

Page 9: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 9

How to create & use a service

� 2 – Register (declare) the service in the “Service Registry”� Symbolic name

� Implementation class

� Renderer (optional)

� Session key (optional)

� Example :

// Parameters :// Name, Class, Renderer, SessionKeyregister(" MyServ ", MyService.class , null , null );register("L002", L002.class, "renderer/L001 .jsp", Const.SVC_L002_RESULT );register("L003", L003.class, "renderer/L001 .jsp", null );register("L002bis", L002.class, null, null );register("T001", T001.class, "renderer/T001 .jsp", null );

« name » � class

Service Registry

Optional :A service can be called by its class name if the class is in a predefined package

registerServicePackage ( "org.samples.service" );registerServicePackage ( "org.samples.other" ) ;

default packages location (for invocation by class name)

Page 10: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 10

How to create & use a service

� 3 – Test the service directly from the browser

Service Call( http request )

Service XMLResponse

A service is called by its "symbolic name"( or class name ) + ".svc"

Page 11: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 11

How to create & use a service

� 4 – Use (call) the service with JavaScript� Example :

//--- Get a service instancevar service = telosys.getService( "MyServ" , showServiceResponse );

function callService(){

var sArg1 = fwkGetValue("arg1");var sArg2 = fwkGetValue("arg2");service . call (sArg1, sArg2); // Call with generic parameters (p1 =..,p2=..)

}

function showServiceResponse (oResponse) // ScreenResponse oResponse{

if ( oResponse.isOK() ){

var sResult = oResponse. getValue ("result");alert ( "OK : Result = " + sResult );fwkSetValue("result", sResult );

}else{

alert( "ERREUR : " + oResponse.getReturnCode() + " - " +oResponse.getReturnMessage() );

}}

Service name Callback method

Method to callthe service

Method to processthe service response

Get a returned value

Put the resultin a field

Page 12: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 12

How to create & use a service

� A service can be called with "anonymous parameters" or with "named parameters" :� service.call() � anonymous parameters

� service.callWithParams() � named parameters

function call(){

var params = new Object();params [ "code" ] = "123" ;params [ "name" ] = "Bart"; service. callWithParams ( params );

}

req.getParameter( "code" );req.getParameter( "name" );

function call(){

var x = "123";service. call ( x, "abcd", 456, "foo" );

}

req.getParameter(1);req.getParameter(2);req.getParameter(3);etc ...

Client side ( JavaScript ) Server side ( Java )

Page 13: Telosys Services

Advanced AJAX (RPC) services

Page 14: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 14

How to produce a “view”

� A service can produce a page fragment, on the server-side, by generating a “view”. The view can be directly print in a part of the user screen

� To build a “view” after a service call � assign a “renderer” (a JSP) to the service

� The built view is transported as a part of the XML response � it can be used with “result values”

ServerCall “name.svc”

Response

Params

View

Via URL with query string

ret code

Values

ScreenService

ServiceResponse

Result

XML

RendererJSP

include

Page 15: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 15

How to use the view on the client side

//--- Get a service instancevar service = telosys.getService( “MyServ" , showServiceResponse );

function callService(){

var sNom = fwkGetValue("Nom");var sPrenom = fwkGetValue("Prenom");service . call (sNom, sPrenom); // Call with generic parameters (p 1=..,p2=..)

}

function showServiceResponse (oResponse) {

fwkShowServiceView("containerId",oResponse);}

Service name Callback method

Method to callthe service

Method to processthe service response

Get the “view”part of the response( produced by theservice renderer )and put it in the

“container” widget

// Parameters :// Name, Class, Renderer, SessionKeyregister("MyServ", MyService.class, "renderer/L001.jsp" , null );

Service Registry

JavaScript

Page 16: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 16

“stateless” or “statefull” services ?

� By default a service is “stateless”(the result is not kept after execution)

� To store a service result …� assign a “session key” to the service� the result and the renderer are stored in the screen session with that key � reusable

Service

ServiceResponse

Renderer

JSP

ServiceResult

ScreenSession

ServiceResultHolder

ServiceProcessor

session key

Page 17: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 17

How to reuse a service result …

� To reuse a service result previously stored in the Screen Session � call the service URL with a single parameter “action=get”� Example : http://…/MyService.svc?action=get

� The result and the renderer retrieved in the session are used to produce the “view” again.

Renderer

JSP

ServiceResult

ScreenSession

ServiceResultHolder

ServiceProcessor

No serviceexecution

session key

View

XML

ServiceResponse

Page 18: Telosys Services

Navigation services

Page 19: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 19

Navigation services

� To navigate … just return the “target view” : public class MyService extends StandardScreenServ ice{

public View execute ( ScreenSession screenSession, ServiceRequest serviceRequest, ServiceResponse serviceResponse

) throws TelosysException{

System.out.println("TestService : execute()");//--- Processing ...

//--- Set response messageserviceResponse.addMessage(

new ScreenMessage("TestService message.", ScreenMess age.INFORMATION));

//--- Set response result serviceResponse.setResult("Result object (here a Str ing)");

//--- Set response valuesserviceResponse.setValue("ResultV1", "V1");serviceResponse.setValue("ResultV2", "V2");

//--- Return View view = new ResourceView ( "/dir/page.jsp" ) ;return view ;

}}

Page 20: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 20

Navigation services ( the “views” )

� Different kinds of views :

. getTarget()

. generate()

View<< interface >>

. ResourceView(String)

ResourceView

View v = new ResourceView("/dir/page.jsp");

. ScreenView(String, int)

. ScreenView(String, int, String)

. getId()

. getName()

. getAction()

. getType()

. getScreenMap()

ScreenView

View v = new ScreenView(“MyScreen", 0 );View v = new ScreenView(“MyScreen", 0 , "html" );View v = new ScreenView(“MyScreen", 0 , “xul" );

Telosys ScreenMap

Standard web resource(page, jsp, servlet, .. )

Page 21: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 21

How to populate Views ?

� If the view (or the renderer) is a JSP (classical JSP or ScreeMap), it can use …� E.L. ( the standard Expression Language )

� Telosys Symbolic Variables

� To provide the useful objects to the view, the service must "expose" them (std request scope)� Example :

Employee emp = ... ;response.expose ( "employee" , emp);response.expose ( "result" , new Integer(123));

<h3>Result = ${ result }</h3>

<t:field ... value="@{ employee .name}" />

Service

View

If the View is a ScreenMap associated with a ScreenContext, it has access to the ScreenContext elements via the Symbolic Variables

Page 22: Telosys Services

Telosys framework - Services ( Laurent Guérin / ver 1.3 ) 22

How to prepare a ScreenContext

� Example :

Screen1

Screen2

Server

ScreenContext

ScreenManager

Call Params

onCreate ()onReuse()

Screen2

ScreenContextManager. openScreenContext(..,..,..,..)OR

ScreenContextManager. useScreenContext(..,..,..,..)……return new ScreenMap(“SCREEN2”, 0);

ScreenService

Openor Use

onCreate ()

onCreate ()oronReuse()

initialize

<t:field …value="@{e.attr}"/>

The context can be usedfor fields initialization( classical MVC pattern )

Page 23: Telosys Services

THE END


Recommended