Difference between revisions of "EDT:Accessing a service"

From Eclipsepedia

Jump to: navigation, search
(Accessing a third-party REST service)
(Accessing a third-party REST service)
Line 258: Line 258:
 
         myResult.text = "At " + statusResult.name + ": " +
 
         myResult.text = "At " + statusResult.name + ": " +
 
                 statusResult.status.reason;
 
                 statusResult.status.reason;
 
        // display the response in JSON format
 
        // SysLib.writeStdOut(myHttp.getResponse().body);
 
       
 
 
     end
 
     end
  

Revision as of 17:32, 30 August 2012

You can access services from a Rich UI application. This page gives code examples. For additional information, see Service bindings.

Accessing a dedicated service

The simplest way to deploy a service written in EGL is to include it with the Rich UI application. The service in that case is known as a dedicated service.

Assume that the service is named MyServiceType and the function of interest is named calculate. Here is a short example of the code necessary to access that service:

   call MyServiceType.calculate(myList) 
        returning to theCallBack
        onException theExceptionHandler;


Accessing an EGL service in either of its two forms

If the logic for a given service is written in EGL, the logic might be accessed as a dedicated service or as an EGL REST-RPC service. In that second case, the service is deployed outside of the Rich UI application, typically on a different server. 

Here is the simplest way to switch between accessing the identical EGL service logic in one way or another:

  1. In your code, declare a binding variable and embed it in a call statement.
  2. In the EGL deployment descriptor, define an entry.  To switch between the two forms of the same service logic, you can vary the entry in the same deployment descriptor or, more likely, can reference a different deployment descriptor with a same-named entry.

/********************************************************
 * Declare the binding variable                         * 
 ********************************************************/
   myBinding IHttp? = Resources.getResource("binding:myBinding");
 
/********************************************************
 * Call the service                                     * 
 ********************************************************/
   call MyServiceType.calculate(myList) 
      using myBinding
      returning to theCallBack 
      onException theExceptionHandler;
 
/********************************************************
 * Example: create a new EGL project for                *
 * "Web 2.0 client application with services". Add the  *
 * the Service type shown next to the server package,   * 
 * and add the Handler type to the client package.      *
 *                                                      *
 * Test the example in the Rich UI Preview tab          * 
 * as follows:                                          * 
 *                                                      *     
 *    1. Type valid input into the first text box;      * 
 *       for example: 5, 12, 4.                         *
 *                                                      * 
 *    2. Click '''Calculate'''.                         *
 ********************************************************/
 
/********************************************************
 * The file with a Service type                         *
 ********************************************************/
package server;
 
service MyServiceType
 
   // variables and constants can be here
   function calculate(myScores Int[] in) returns(Decimal(4, 2))
      numberOfScores, i, mySum Int;
      numberOfScores = myScores.getSize();
 
      for(i from 1 to numberOfScores)
         mySum = myScores[i] + mySum;
      end
 
      return(mySum/numberOfScores);
   end 
end
 
/********************************************************
 * The file with a Handler type                         *
 ********************************************************/
package client;
 
import server.MyServiceType;
import org.eclipse.edt.rui.widgets.GridLayout;
import org.eclipse.edt.rui.widgets.GridLayoutData;
import org.eclipse.edt.rui.widgets.TextField;
import org.eclipse.edt.rui.widgets.TextLabel;
 
import dojo.widgets.DojoButton;
import dojo.widgets.DojoTextField;
 
handler MyHandler type RUIhandler{initialUI =[ui], onConstructionFunction = start, 
                                  cssFile = "css/MyClientAppWithService.css", title = "MyHandler"}
 
   ui GridLayout{columns = 3, rows = 4, cellPadding = 4, children =[myResult, myButton, scores, codeLabel]};
 
   codeLabel TextLabel{layoutdata = new GridLayoutData{row = 2, column = 2}, text = "Numbers:"};                 
 
   scores TextField{layoutData = new GridLayoutData{row = 2, column = 3}};
 
   myButton DojoButton{layoutData = 
      new GridLayoutData{row = 4, column = 2}, text = "Calculate", onClick ::= ui_onClick};
 
   myResult DojoTextField{layoutData = new GridLayoutData{row = 4, column = 3}};
 
   function start()
 
   end
 
   function theExceptionHandler(exp AnyException in)
 
      SysLib.writeStdOut(exp.messageID + " " + exp.message);
 
      if(exp isa ServiceInvocationException)
         SysLib.writeStdOut((exp as ServiceInvocationException).detail1);
         SysLib.writeStdOut((exp as ServiceInvocationException).detail2);
         SysLib.writeStdOut((exp as ServiceInvocationException).detail3);
      end
   end
 
   function theCallBack(retResult decimal(4, 2) in)
      myResult.text = retResult;
   end
 
   function ui_onClick(event Event in)
 
      inputLength int = scores.text.length();
 
      myDelimiters string = ", ";
      myPosition int = 1;
      myToken string;
      myList int[];
 
      while(myPosition < inputLength)
         myToken = StringLib.getNextToken(scores.text, myPosition, myDelimiters);
 
            if(myToken != null)
               myList.appendElement(myToken as int);
            end
      end
 
      /************ Service access statements ***************************/
 
      myBinding IHttp? = Resources.getResource("binding:myBinding");
      call MyServiceType.calculate(myList) 
         using myBinding
         returning to theCallBack 
         onException theExceptionHandler;
 
      /*******************************************************************/
 
   end
end
Incidentally, you can retrieve the details from the HTTP response by adding a parameter of type IHTTP to the callback function:
   function theCallBack(retResult decimal(4, 2) in, myHttp IHTTP in)
      myResult.text = retResult;
 
      /* display the response in JSON format */
      SysLib.writeStdOut(myHttp.getResponse().body);
end

Accessing a third-party REST service

A Rich UI handler can invoke a proxy function, which must be in a package that is generated to JavaScript. The proxy function requires no logic at all: you access the service by setting values in function annotations, and you supplement those values with a deployment-descriptor entry.

The example in this section shows how to invoke a third-party REST service; specifically, the Airport Status service provided by the U.S. Federal Aviation Administration: Airport Status (http://services.faa.gov/docs/tutorial/).

/****************************************************************
 * Declare the binding variable                                 * 
 ****************************************************************/
   myBinding IHttp? = Resources.getResource("binding:USFAA");
 
 
/****************************************************************
 * Call the proxy function                                      * 
 ****************************************************************/
   call getAirportStatus(airportCode.text)
      using myBinding returning to theCallBack
      onException theExceptionHandler;
 
 
/****************************************************************
 * Example:                                                     *
 *                                                              *
 * 1. Create a new EGL project for                              *
 *    "Web 2.0 client application with services".               *
 *                                                              * 
 * 2. Create Record types with a few keystrokes:                *
 *                                                              * 
 *    a. Use a browser to go to the following site:             *
 *                                                              *
 *       http://services.faa.gov/airport/status/IAD?format=xml  *  
 *                                                              *
 *    b. Copy the XML that is displayed in your browser.        * 
 *                                                              * 
 *    c. In your new project, right-click the                   *
 *       common package; click New > Record; at the             *
 *       EGL Record wizard, name the file StatusRecordTypes     *
 *       and double-click Records from XML; and                 *
 *       at the Records from XML page, click Create             *
 *       from a string. Next, paste the XML into the text       * 
 *       area and click Finish.                                 *  
 *                                                              *
 * 2. Copy and paste the code shown later. Specifically,        * 
 *    add the Handler type and proxy-function library           * 
 *    to the client package.                                    * 
 *                                                              *  
 * 3. Create a deployment descriptor entry name USFAA           *
 *    and specify the base URI as follows:                      * 
 *                                                              * 
 *       http://services.faa.gov/airport/status                 *
 *                                                              *
 * 4. Test the example in the Rich UI Preview tab:              * 
 *                                                              *     
 *    a. Type an airport code into the first text box;          * 
 *       for example, LAX.  Other examples are JFK, DFW, RDU.   *
 *                                                              * 
 *    b. Click Retrieve.                                        *
 ****************************************************************/
 
/****************************************************************
 * The file with a Handler type named MyHandlerUSFAA            *
 ****************************************************************/
package client;
 
import common.AirportStatus;
import org.eclipse.edt.rui.widgets.GridLayout;
import org.eclipse.edt.rui.widgets.GridLayoutData;
import org.eclipse.edt.rui.widgets.TextLabel;
import org.eclipse.edt.rui.widgets.TextField;
import dojo.widgets.DojoButton;
import dojo.widgets.DojoTextArea;
 
handler MyHandlerUSFAA type RUIhandler{initialUI =[ui
            ], onConstructionFunction = start, cssFile = "css/MyClientAppWithService.css", title = "MyHandler"}
 
    ui GridLayout{columns = 3, rows = 4, cellPadding = 4, children =[myResult,
                    myButton, airportCode, codeLabel]};
    codeLabel TextLabel{layoutdata = new GridLayoutData{row = 2, column = 1, horizontalSpan = 2}, text = "Airport code: "};
    airportCode TextField{layoutData = new GridLayoutData{row = 2, column = 3}};
    myButton DojoButton{layoutData = new GridLayoutData{row = 4, column = 2}, text = "Retrieve", onClick ::= ui_onClick};
    myResult DojoTextArea{layoutData = new GridLayoutData{row = 4, column = 3}};
    myCommonError string = "Please specify a valid airport code; for example, LAX or DFW";
 
    use ProxyFunctions;
 
    function start()
    end
 
    function theExceptionHandler(exp AnyException in)
        if(exp.messageID == "CRRUI3655E")
            myResult.text = myCommonError;
        else
            myResult.text = exp.messageID + " " + exp.message;
        end
    end
 
    function theCallBack(statusResult AirportStatus in)
 
        myResult.text = "At " + statusResult.name + ": " +
                statusResult.status.reason;
    end
 
    function ui_onClick(event Event in)
 
        if(airportCode.text.trim() != null)
 
            /************ Service access statements ***************************/
 
            myBinding IHttp? = Resources.getResource("binding:USFAA");
 
            call getAirportStatus(airportCode.text) using myBinding
                    returning to theCallBack onException theExceptionHandler;
 
            /*******************************************************************/
        else
            myResult.text = myCommonError;
        end
    end
end
 
 
/****************************************************************
 * The file with a Library type named ProxyFunctions            *
 ****************************************************************/
package client;
 
import common.AirportStatus;
 
library ProxyFunctions
 
   function getAirportStatus(airportCode String) returns (AirportStatus) {
	   @Resource{uri = "binding:US_FAA"},
	   @Rest{method = _GET, uriTemplate = "/{airportCode}?format=application/xml",
	   requestFormat = None, responseFormat = XML}
   }
   end	
end




Code snippets main page