Consuming SAP NetWeaver Gateway Services from Flex Applications

5 downloads 8594 Views 1MB Size Report
Sep 27, 2011 ... Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks ... Xcelsius, and other Business Objects products and services ...
Consuming SAP NetWeaver Gateway Services from Flex Applications

Copyright © Copyright 2011 SAP AG. All rights reserved. SAP Library document classification: PUBLIC No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, StreamWork, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company. Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase, Inc. Sybase is an SAP company. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

Copyright/Trademark

Table of Contents Consuming SAP NetWeaver Gateway Services on Flex Applications ......................................................... 4 Prerequisites ................................................................................................................................................................. 5 Building a Flex Web Application ................................................................................................................................ 6 Function Import Example ....................................................................................................................................... 6 Read Example ......................................................................................................................................................... 9 Create Example ..................................................................................................................................................... 11 Creation of Request Body for Create Method:.................................................................................................. 13 Authentication Handling ............................................................................................................................................ 14 Appendix 1 - RMTSAMPLEFLIGHT Service Metadata Description ...............................................................15 Appendix 2 – Code Snippets ...............................................................................................................................21 Read - Get Carrier by Carrier ID.............................................................................................................................. 21 Function Import – Get available Flights .................................................................................................................. 22 Create - Booking a Flight .......................................................................................................................................... 23

Copyright/Trademark

CONSUMING SAP NETWEAVER GATEWAY SERVICES ON FLEX APPLICATIONS SAP NetWeaver Gateway technology provides a simple way to interact with SAP applications through variety of devices, environments and platforms based on market standards. The framework enables development of innovative, people-centric solutions that bring the power of SAP business software into new experiences, such as: social networking and collaboration environments; mobile and tablet devices; and rich internet applications. The framework supports rapid innovation while ensuring security, integrity, management and optimized maintenance of the core SAP systems. Completely flexible, the software offers connectivity to SAP applications using any programming language or model without the need for SAP knowledge by taking advantage of REST services and OData/ATOM protocols. This guide provides information on how to consume SAP NetWeaver Gateway services from Flex web applications based on the following scenario:    

The user wants to fly from New York to San Francisco. He goes online and searches for the different flights available. He enters his search parameters: carrier city of origin, destination city, departure date, and arrival date (Import Function). From the SAP NetWeaver Gateway, he receives the information for the option(s) that meet(s) his search parameters (Get Function). He can now select the one that suits him the most and book his flight (Create Function).

The links to access the service document and metadata document are provided the in table below. Service Document Metadata Document

http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/ http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/ $metadata?$format=xml

Each service URL should contain the following information:   

Gateway host Port SAP Client – no need to specify client if connecting to the default client.

Example: http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT?sap-client=100

To obtain information on the selected service (its properties, function imports, associations, etc.), refer to Appendix 1, were we show the Metadata description of the RMTSAMPLEFLIGHT service. To view the code snippets in a format that can be copied into new code, refer to Appendix 2.

Copyright/Trademark

The Flight service contains the following collections: Collection

URL

FlightCollection (list of flights)

http://:/sap/opu/sdata/ iwfnd/RMTSAMPLEFLIGHT/FlightCollection

CarrierCollection (list of carriers)

http://:/sap/opu/sdata/ iwfnd/RMTSAMPLEFLIGHT/CarrierCollection

BookingsCollection (list of Bookings)

http://:/sap/opu/sdata/ iwfnd/RMTSAMPLEFLIGHT/BookingCollection

The operation GetAvailableFlights on FlightCollection is used to get a list of flights. The following is the URL for the same: http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights Each collection supports some or all of the operations listed below. The table below provides the list of operations supported and the methods that each of this operations map to respectively: Operation

Method

Create

HTTP POST

Read

GET

Update

PUT

Delete

DELETE

Prerequisites The following tools are required for developing a Flex web application: Software

Version

SAP NetWeaver Gateway

2.0

Flash Builder

4.5

Flex SDK

4.5.1

Contact your system administrator for landscape details to connect to the SAP NetWeaver Gateway system. Make sure you have performed the following: 

Create a Flex project using the Flash Builder, and choose Web as the application type (which means the application runs in Flash Player plug-in on your browser). Copyright/Trademark

Building a Flex Web Application Function Import Example To get a list of the available flights, based on filter criteria, trigger the “GetAvailableFlights” function import with cityFrom, cityTo, fromDate and toDate parameters. The response will contain a list of flights that meet the criteria. Note: For more information on Function Import (also called Service Operation), refer to OData documentation at http://www.odata.org/developers/protocols/uri-conventions under the Addressing Service Operations section. Refer to the code snippet below for details on how to do it from a Flex web application: 1. Initialize an HTTPService object. var service:HTTPService = new HTTPService();

2. Set the request’s URL attribute with a query calling the GetAvailableFlights function import with the following parameters: flight departure date, arrival date, and cities. public static const FLIGHT_URL:String = "http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT/"; service.url = FLIGHT_URL + "GetAvailableFlights?cityfrom=NEW+YORK &cityto=SAN+FRANCISCO&fromdate=20110105&todate=20110728";

3. Set the request’s HTTP method, result format, and proxy attributes. service.useProxy = false; //Define proxy according to your environment service.method = "GET"; service.resultFormat = "e4x";

4. Set the request’s callback methods to handle the service response for both success and failure scenarios. service.addEventListener("result", httpResult); //Handle success response service.addEventListener("fault", httpFault); //Handle failure response

Note: The referred callback methods should be defined appropriately: public function httpResult(event:ResultEvent):void { //TODO: Use event.result.toString() to handle the result of the service call } public function httpFault(event:FaultEvent):void { //TODO: Use event.fault.faultString to handle the error of the service call }

5. Send the request. service.send();

Copyright/Trademark

Response - http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollection FlightCollection 2011-09-27T06:32:42Z - - - AA 0017 2011-01-05T00:00:00 889.00 USD 747-400 385 367 185742.73 31 22 21 21 - US new york JFK US SAN FRANCISCO SFO 361 PT11H00M00S PT14H01M00S 2574.0000 SMI 0 http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollection(carrid='AA',conn id='0017',fldate='20110105') Flight 2011-09-27T06:32:42Z - - - AA 0017 2011-03-16T00:00:00 422.94

Copyright/Trademark

USD 747-400 385 371 192129.22 31 30 21 20 - US new york JFK US SAN FRANCISCO SFO 361 PT11H00M00S PT14H01M00S 2574.0000 SMI 0 http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollection(carrid='AA',conn id='0017',fldate='20110316') Flight 2011-09-27T06:32:42Z

Copyright/Trademark

Read Example To read the carrier details, you must call the “CarrierCollection” using the Carrier ID parameter. Refer to the code snippet below for details on how to do it from a Flex web application: 1. Initialize an HTTPService object. var service:HTTPService = new HTTPService();

2. Set the request’s URL attribute with the query below to retrieve the carrier with a specified carrier ID. public static const FLIGHT_URL:String = "http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT/"; service.url = FLIGHT_URL + "CarrierCollection('AA')";

Note: In this example, the specified carrier ID is ‘AA’ (American Airlines) 3. Set the request’s HTTP method, result format and proxy attributes. service.useProxy = false; //Define proxy according to your environment service.method = "GET"; service.resultFormat = "e4x";

4. Set the request’s callback methods to handle the service response for both success and failure scenarios. service.addEventListener("result", httpResult); //Handle success response service.addEventListener("fault", httpFault); //Handle failure response

Note: The referred callback methods should be defined appropriately: public function httpResult(event:ResultEvent):void { //TODO: Use event.result.toString() to handle the result of the service call } public function httpFault(event:FaultEvent):void { //TODO: Use event.fault.faultString to handle the error of the service call }

5. Send the request. service.send();

Copyright/Trademark

Response - - AA American Airlines USD http://www.aa.com :/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA') Carrier 2011-09-27T10:00:38Z

Copyright/Trademark

Create Example To book a Flight, trigger an HTTP POST method on the Booking Collection (with the Booking details in the request body). Check for the HTTP response code. If the response code is 201, then the HTTP-POST is successful. The response contains the newly created Booking ID in the bookid field, as well as the entire information that was part of the request body. Refer to the code snippet below for details on how to invoke a create request from a Flex web application: 1.

Create the POST request body with the desired Booking entry details. var body:String = ""+ ""+ ""+ ""+ ""+ ""+ "2011-08-17T14:09:29.608Z"+ ""+ ""+ ""+ "00000325"+ ""+ "Y"+ "00000000"+ "00004617"+ "P"+ "879.82"+ "USD"+ ""+ "803.58"+ "USD"+ "14.4"+ "2011-05-22T00:00:00 "+ "1990-10-10T00:00:00"+ "1234567"+ "Joe Smith"+ ""+ ""+ "KG"+ ""+

Copyright/Trademark

"AA"+ "0017"+ "2011-12-21T00:00:00"+ ""+ ""+ "";

2. Initialize an HTTPService object. var service:HTTPService = new HTTPService();

3. Set the request’s URL attribute with the service’s Booking Collection address. The new entry should be created there. public static const FLIGHT_URL:String = "http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT/"; service.url = FLIGHT_URL + "BookingCollection";

4. Set the request’s HTTP method, content type, result format, and proxy attributes. service.useProxy = false; //Define proxy according to your environment service.method = "POST"; service.contentType = "application/xml"; service.resultFormat = "e4x";

5. Add the XMLHttpRequest http header needed for POST requests. service.headers["X-Requested-With"] = "XMLHttpRequest";

6.

Set the request’s callback methods to handle the service response both for success and failure scenarios. service.addEventListener(ResultEvent.RESULT, httpResult); //Handle success response service.addEventListener(FaultEvent.FAULT, httpFault); //Handle failure response

Note: The referred callback methods should be defined appropriately: public function httpResult(event:ResultEvent):void { //TODO: Use event.result.toString() to handle the result of the service call } public function httpFault(event:FaultEvent):void { //TODO: Use event.fault.faultString to handle the error of the service call }

7. Send the request with the previously created body. service.send(body);

Copyright/Trademark

Creation of Request Body for Create Method: The Request body can be easily created by looking at the tags in the collection’s metadata. For example, the metadata for the Booking Collection provides information about the various fields (tags) that are needed for creating a booking:

Copyright/Trademark

Authentication Handling The code snippets above do not include any code for handling authentication methods or user credentials. Therefore, the Flash Player browser plug-in will handle that as follows: 

Basic Authentication A pop-up displays where the user must enter username and password.



X.509 Certificate Authentication The appropriate CA Certificate and X.509 Client Certificate is used for the authentication process. A pop-up may also be presented to the user for selecting the certificate to use.

Note: For all sent requests, make sure you use the appropriate service URL according to the desired authentication method.

Copyright/Trademark

Appendix 1 - RMTSAMPLEFLIGHT Service Metadata Description

Copyright/Trademark



Copyright/Trademark



Copyright/Trademark



Copyright/Trademark

Airline Flight Number Date

Copyright/Trademark

Date Date Depart.city Arrival city

Copyright/Trademark

Appendix 2 – Code Snippets Read - Get Carrier by Carrier ID
public static const FLIGHT_URL:String = "http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT/";

public function httpResult(event:ResultEvent):void { //TODO: Use event.result.toString() to handle the result of the service call } public function httpFault(event:FaultEvent):void { //TODO: Use event.fault.faultString to handle the error of the service call } protected function btnGetFlight_clickHandler(event:MouseEvent):void { var service:HTTPService = new HTTPService(); service.useProxy = false; //Define proxy according to your environment service.url = FLIGHT_URL + "CarrierCollection('AA')"; service.method = "GET"; service.resultFormat = "e4x"; service.addEventListener("result", httpResult); service.addEventListener("fault", httpFault); service.send(); } ]]>

Copyright/Trademark

Function Import – Get available Flights
public static const FLIGHT_URL:String = "http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT/";

public function httpResult(event:ResultEvent):void { //TODO: Use event.result.toString() to handle the result of the service call } public function httpFault(event:FaultEvent):void { //TODO: Use event.fault.faultString to handle the error of the service call } protected function btnGetAvailable_clickHandler(event:MouseEvent):void { var service:HTTPService = new HTTPService(); service.useProxy = false; //Define proxy according to your environment service.url = FLIGHT_URL + "GetAvailableFlights?cityfrom=NEW+YORK &cityto=SAN+FRANCISCO&fromdate=20110105&todate=20110728"; service.method = "GET"; service.resultFormat = "e4x"; service.addEventListener("result", httpResult); service.addEventListener("fault", httpFault); service.send(); } ]]>

Copyright/Trademark

Create - Booking a Flight
public static const FLIGHT_URL:String = "http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT/"; public function httpResult(event:ResultEvent):void { //TODO: Use event.result.toString() to handle the result of the service call } public function httpFault(event:FaultEvent):void { //TODO: Use event.fault.faultString to handle the error of the service call } protected function btnBookFlight_clickHandler(event:MouseEvent):void { var body:String = ""+ ""+ ""+ ""+ ""+ ""+ "2011-08-17T14:09:29.608Z"+ ""+ ""+ ""+

Copyright/Trademark

"00000325"+ ""+ "Y"+ "00000000"+ "00004617"+ "P"+ "879.82"+ "USD"+ ""+ "803.58"+ "USD"+ "14.4"+ "2011-05-22T00:00:00 "+ "1990-10-10T00:00:00"+ "1234567"+ "Joe Smith"+ ""+ ""+ "KG"+ ""+ "AA"+ "0017"+ "2011-12-21T00:00:00"+ ""+ ""+ "";

var service:HTTPService = new HTTPService(); service.useProxy = false; //Define proxy according to your environment service.url = FLIGHT_URL + "BookingCollection";

Copyright/Trademark

service.method = "POST"; service.contentType = "application/xml"; service.headers["X-Requested-With"] = "XMLHttpRequest"; service.resultFormat = "e4x"; service.addEventListener(ResultEvent.RESULT, httpResult); service.addEventListener(FaultEvent.FAULT, httpFault); service.send(body); } ]]>

Copyright/Trademark