8 Service Consumption
8.1 OData Toolkit for Java Platform, Standard Edition
8.1.3 Introduction to the OData Java Client Library
System.out.println(e.getMessage());
}
Displayed as: Booking AA 00002161 has already been canceled
Running your JAVA SE Application
You can run and test the generated Java SE application in Eclipse just as you run any Java application created in Eclipse. .
Java SE Application Security
The framework automatically enables your application to make requests that include a challenge token, and ensures that the requests from your application are not coming from another source other than the user.
Java SE Application Support for Cross Site Request Forgery Protection
OData services are protected against Cross-Site Request Forgery (XCSRF) attacks. This protection mechanism appends challenge tokens to each request and associates them with each user’s session. Each token is unique per user session, and per request.
All OData services that are compatible with OData library version 1.0 and above are protected and require XCSRF tokens.
Some services that are compatible with OData library 0.2 also require these tokens.
You can disable the protection using CSRF tokens, by implementing your custom code.
8.1.3 Introduction to the OData Java Client Library
The OData Java Client Library supplies a set of APIs and classes that handle the connectivity and processing of OData for SAP solutions on top of SAP NetWeaver Gateway.
The OData Java Client Library comes with a set of APIs, interfaces, and classes for connecting to SAP NetWeaver Gateway and proocessing of OData for SAP solution. The library parses OData content and fills the appropriate objects with values.
The library provides APIs for:
● Connecting to an SAP NetWeaver Gateway system.
It provides APIs of HTTP client for RESTful calls to an SAP NetWeaver Gateway system, supporting HTTP GET, POST, PUT and DELETE methods. In addition, it supports basic authentication over secure sockets layer (SSL).
● Parsing of OData elements as well as APIs for the construction of XML strings from OData elements encapsulated in an SAP Data Helper class.
To use the OData Java Client Library, you prepare the content of your request in your own application, or one that you have generated in the framework, that creates an OData entry. Then, you call the SAP Data Helper to convert the request to an ODataEntry XML text in the SAP format.
The library also simplifies sending requests to SAP NetWeaver Gateway by providing APIs for creating the request content, sending it and parsing the response into Java entities.
To send a request to an OData services, first make a connection to a specific SAP NetWeaver Gateway, and then set the feed or entry object. You call a library method to convert the object into XML and send it.
8.1.3.1 Getting Started
The following examples show how to use the various APIs in the OData Java Library to interact with OData services. In addition, the examples provide sample code showing how to use the library.
To get started, you can create a custom application using the Starter Application Project wizard of the SAP NetWeaver Gateway Productivity Accelerator. This generates the library into your project.
You must provide the appropriate authentication.
Example
The following is an overview for using the library:
First, send a request to connect to an SAP NetWeaver Gateway host in which the OData service you want to call is available.
For example:
IGatewayConfiguration gateway = new GatewayConfiguration("<host>", "<port>", "",
"<user>", "<password>", false);
You must replace <host>, <port>, <user>, <password> with the appropriate values.
Initialize the REST client based on the SAP NetWeaver Gateway connection details.
RestClient restClient = new RestClient(gateway);
Obtain the response from the connected SAP NetWeaver Gateway host about the OData service.
String response = restClient.get("<collection>");
Where <collection> is the name of the OData service you to call.
For instance, /sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights
Transform the XML response into an ODataCollection object.
ODataCollection collecion = ODataUtil.parseODataCollection(response);
Extract the properties from the ODataCollection object.
ODataEntry[] entries = collecion.getEntries();
for (int i = 0; i < entries.length; i++) { ODataEntry entry = entries[i];
String stringDataValue = entry.getProperty("<property name>");
System.out.println(stringDataValue);
}
} catch (RestClientException e)
8.1.3.2 Calling an OData service
After setting the connection and authentication, you use the RestService API class to form an HTTP request for the specific OData service.
Example
For example, to call an OData service called, RMTSAMPLEFLIGHT.
You must provide the URI of the service.
Initialize the REST client based on the connection details.
RestClient restClient = new RestClient(gateway);
Parse the string XML response into an ODataCollection object.
ODataCollection collection = ODataUtil.parseODataCollection(response);
Loop over the atom entries in the feed.
ODataEntry[] entries = collection.getEntries();
for (int i = 0; i < collection.length; i++) {
ODataEntry entry = collection[i];
String stringDataValue = entry.getProperty("<property name>");
System.out.println(stringDataValue);
} }
catch (RestClientException e) {
To call an SAP NetWeaver Gateway service, proceed as follows:
1. Indicate to the Java library to call and use the RestClient class to obtain the response string from the OData service.
2. Parse the response into Java objects.
3. Call a method to send the request and receive the results.
4. Handle errors and exceptions.
The following is an example for updating data in your SAP backend system through SAP NetWeaver Gateway:
/* * Code example for updating data (PUT) through SAP NetWeaver Gateway
* @param entry with updated data
* @throws MarshalerException
* @throws RestClientException
*/
private static void updateExample(ODataEntry entry) throws MarshalerException, RestClientException
The following is an example for creating new data in your SAP backend system through SAP NetWeaver Gateway:
/** Example code for CREATING new data (POST) through
*SAP NetWeaver Gateway in the SAP backend system.
*/ private static ODataEntry createExample() throws MarshalerException, RestClientException, ParserException
{
//Create HashMap object
HashMap<String, String> propertiesHashMap = new HashMap<String, String>();
//Add key value pairs to HashMap propertiesHashMap.put("carrid","AA");
propertiesHashMap.put("connid","0017");
// Create an entry ODataEntry atomEntry = createAtomEntry(propertiesHashMap);
//Convert to xml string String body = ODataUtil.format(atomEntry);
//Call Gateway
String responseString = restClient.post("/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/
BookingCollection", body);
System.out.println(responseString);
//Parse the response XML string from Gateway
ODataEntry newEntry = ODataUtil.parseODataEntry(responseString);
return newEntry;
}
8.1.3.3 OData Java Library API Reference
The API reference section provides detailed descriptions of the packages, classes, methods and working code examples for the classes, and interfaces. Below are the OData Java Library APIs:
Package Index
provides classes and interfaces for calling an SAP NetWeaver Gateway host.com.sap.nw.gateway.odata.client.connectivity.im pl
The package,
com.sap.nw.gateway.odata.client.connectivity.impl , provides classes and interfaces for implementing connectivity to an SAP NetWeaver Gateway host.
com.sap.nw.gateway.odata.client.connectivity.int erceptors
The package,
com.sap.nw.gateway.odata.client.connectivity.inter ceptors, provides classes and interfaces for intercepting calls to an SAP NetWeaver Gateway host
com.sap.nw.gateway.odata.client.connectivity.se curity
The package,
com.sap.nw.gateway.odata.client.connectivity.secur ity, provides classes and interfaces for security calls to an SAP NetWeaver Gateway host
com.sap.nw.gateway.odata.client.connectivity.va lidators
The package,
com.sap.nw.gateway.odata.client.connectivity.valid ators, provides classes and interfaces for validating calls to an SAP NetWeaver Gateway host
com.sap.nw.gateway.odata.client.exceptions The package,
com.sap.nw.gateway.odata.client.exceptions, provides classes and interfaces for exception calls.
com.sap.nw.gateway.odata.client.jsonsimple The package,
com.sap.nw.gateway.odata.client.exceptions, provides classes and interfaces for JSON client calls.
com.sap.nw.gateway.odata.client.jsonsimple.par ser
The package,
com.sap.nw.gateway.odata.client.exceptions, provides classes and interfaces for parsing JSON calls.
com.sap.nw.gateway.odata.client.nls.messages The package,
com.sap.nw.gateway.odata.client.nls.messages, provides classes and interfaces for handling messages.
com.sap.nw.gateway.odata.client.odatamodel.at om
The package,
com.sap.nw.gateway.odata.client.odatamodel.atom, provides classes and interfaces for ATOMPUB client calls.
Packages Description com.sap.nw.gateway.odata.client.odatamodel.at
om.sap
The package,
com.sap.nw.gateway.odata.client.odatamodel.atom.sa p, provides classes and interfaces for calling ATOMPUB elements provided by SAP.
com.sap.nw.gateway.odata.client.odatamodel.ed mx
The package,
com.sap.nw.gateway.odata.client.odatamodel.edmx, provides classes and interfaces for calling entity data models entries.
com.sap.nw.gateway.odata.client.odatamodel.ed mx.v3
The package,
com.sap.nw.gateway.odata.client.odatamodel.edmx.v3 , provides classes and interfaces for calling entity data model entries that conform to version 3 of the OData Common Schema Defintion Language (CSDL).
com.sap.nw.gateway.odata.client.odatamodel.ed mx.v3.expressions
The package,
com.sap.nw.gateway.odata.client.odatamodel.edmx.v3 .expressions, provides classes and interfaces for calling entity data model entries that conform to version 3 of the CSDL expressions.
com.sap.nw.gateway.odata.client.odatamodel.ed mx.v3.expressions.primitive
The package,
com.sap.nw.gateway.odata.client.odatamodel.edmx.v3 .expressions.primitive , provides classes and interfaces for calling entity data model entries that conform to version 3 of the CSDL primitive types.
com.sap.nw.gateway.odata.client.odatamodel.ge neric
The package,
com.sap.nw.gateway.odata.client.odatamodel.generic , provides classes and interfaces for calling SAP supplied OData elements.
com.sap.nw.gateway.odata.client.odatamodel.ge neric.v3
The package,
com.sap.nw.gateway.odata.client.odatamodel.generic .v3, provides classes and interfaces for calling SAP supplied OData elements that conform to version of CDSL.
com.sap.nw.gateway.odata.client.odatamodel.jso n
The package,
com.sap.nw.gateway.odata.client.odatamodel.json, provides classes and interfaces for implementing JSON formats.
com.sap.nw.gateway.odata.client.parser The package, com.sap.nw.gateway.odata.client.parser, provides classes and interfaces for parsing OData elements.
com.sap.nw.gateway.odata.client.parser.impl The package,
com.sap.nw.gateway.odata.client.exceptions, provides classes and interfaces for exception calls.
com.sap.nw.gateway.odata.client.parser.impl.util The package,
com.sap.nw.gateway.odata.client.parser.impl.util, provides classes and interfaces for utility that implements the parsing.
com.sap.nw.gateway.odata.client.parser.impl.v3 The package,
com.sap.nw.gateway.odata.client.parser.impl.v3, provides classes and interfaces for implementing version of the CDSL.
com.sap.nw.gateway.odata.client.parser.util The package,
com.sap.nw.gateway.odata.client.parser.util, provides classes and interfaces for a utility that implements the parsing.
Packages Description com.sap.nw.gateway.odata.client.proxy.helper The package,
com.sap.nw.gateway.odata.client.proxy.helper, provides classes and interfaces for calling the service proxy.
com.sap.nw.gateway.odata.client.util The package, com.sap.nw.gateway.odata.client.uti, provides classes and interfaces for a utility that implements the APIs.
For detailed information about the APIs, see the Javadocs.