You are on page 1of 13

Creating Mobile Applications on top of SAP, Part 2

Applies to:
SAP ERP 6.0, Netweaver > 7.10. For more information, visit the Mobile homepage.

Summary
Welcome to the second part of our mobile SAP tutorial! Let's quickly sum up the overall goal which is creating so called "instant value mobile applications" in terms of lightweight mobile solutions which do not require a fully blown Sybase stack - of course the Sybase stack can not be replaced based on this simple stack, but as already mentioned, there might be requirements which could be solved in a more efficient manner using a lightweight approach. In the first part of this tutorial series we chalked out how to expose a SAP BAPI as a Web Service. We used the common known FLIGHT BAPI so at the current state of the tutorial we have a Web Service up and running providing a simple method called "Flight Get List". If you should have missed the first part of the tutorial, here's a link to the first part: http://www.resource.ch/userfiles/medien/blog/SAP_mobile01.pdf Please notice as well: if you should have a different Web Service instead of FLIGHT BAPI, no matter, the following steps apply to any SAP Web Service - it's completely up to you whether you wanna show up some flights or some CRM data on your mobile device. Author: Florian Mller

Company: Resource AG Created on: 22 of Dec. 2010

Author Bio
Florian Mller works as a Solution Architect for Resource AG, one of the leading SAP consulting companies within Switzerland. Florian focuses on SAP UI technologies in first place, especially mobile UI technologies are part of his daily SAP business. Furthermore Florian is founder of richability (http://www.richability.com) and frequent author for several magazines and books within the space of Java & UI Technologies.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 1

Creating Mobile Applications on top of SAP, Part 2

Table of Contents
Sum Up ............................................................................................................................................................... 3 Outlook - What We will Achieve ......................................................................................................................... 4 Requirements/ Installations ............................................................................................................................. 5 Create the Java Web Service Stubs ................................................................................................................... 6 Using Java Web Service Stub ...................................................................................................................... 10 Next Steps ........................................................................................................................................................ 11 Related Content ................................................................................................................................................ 12 Disclaimer and Liability Notice .......................................................................................................................... 13

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 2

Creating Mobile Applications on top of SAP, Part 2

Sum Up
Ok, last tutorial has been released couple of weeks before this part, so we will start with a quick sum up in order to get you up to date on what has been achieved as well as what will be achieved today. As already mentioned, we have a simple Web Service up and running providing SAP related data based on several input parameters. This is always kind of basic step before you actually start thinking about the client layout, technology etc.; as already mentioned, you can use any Web Service from your SAP system and do not require our FLIGHT BAPI Web Service. We suggest, you use SOAPUI and test your Web Service before you continue with our implementation, at this state you should have an accessible Web Service which returns some data from your SAP system when executing a dry run within the SOAPUI environment (SOAPUI is free of charge and can be downloaded here: http://www.soapui.org/). Our Web Service for example returns 15 flights when passing in the "LH" (Lufthansa) carrier, see screenshot below:

Ok, so this should have been achieved after the last part of the tutorial so let's switch to the next chapter and see what we will achieve within this second part of the tutorial.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 3

Creating Mobile Applications on top of SAP, Part 2

Outlook - What We will Achieve


Please remind the big picture (see below) - the grey box is up and running for now so let's focus the blue box above: on the one hand the blue box provides UI stuff to our mobile client (this is NOT covered within this 2nd tutorial part), on the other hand the blue box connects the Web Service - which is what we will do today. It is important to connect the Web Services using Java language as the client interface (vaadin) will be created based on Java as well - so if we have the Web Services accessible through Java we can easily use these within our mobile client. The Web Services will be connected via Apache Axis 2 - the cool thing about Axis: Axis will translate the WSDL to Java classes automatically so all you need to know is how to invoke Axis on your Web Service. After generation there will be a Java stack available enabling you to access your Web Services easily.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 4

Creating Mobile Applications on top of SAP, Part 2

Requirements/ Installations In order to generate the Java stubs on top of your Web Service (as well as for the vaadin UI creation in the next part of the tutorial), you require several tools: Eclipse IDE for Java EE developers (3.6): http://www.eclipse.org/downloads/ (!!! MAKE SURE YOU CHOSE THE "IDE FOR JAVA EE DEVELOPERS" !!!!) Apache Axis (2.1.5.2): http://axis.apache.org/axis2/java/core/

Download Eclipse IDE as well as Apache Axis, next fire up Eclipse and your are close to implementing/generating the Web Service stubs. You can create a new workspace (Eclipse will ask you when launching first time), just give it a name such as myMobileSapWorkspace and wait until Eclipse finished loading. There is one important configuration issue which needs to be achieved in order to run Axis2 as Eclipse by default ships with the old Axis1 - using the old Axis would work as well but as there have been huge changes from Axis1 to Axis2 you should link in Axis2: Fire up the Eclipse Preferences (Eclipse Preferences) and switch to the Axis2 Preferences - next enter the path to your downloaded Axis 2 package, see screenshot below...

Ok - now you are ready to generate the Java stub - congrats! We will create a little demo project (without any UI stuff) which will access the FLIGHT BAPI Web Service, get some flights and print the results to the console window - if we can print Flight BAPI results to the console windows it's just a very little step in the third part of the tutorial in order to display results on a mobile device (see blow).

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 5

Creating Mobile Applications on top of SAP, Part 2

Flight BAPI in action

Create the Java Web Service Stubs


Ok, let's create a Java access layer to our Web Service which basically enables us to do sth. similar to:
FlightWebService flightService = new FlightWebService(); List<Flight> flights = flightService.getFlights("LH"); for (int i=0; i<flights.size(); i++) System.out.println("Flight:" +flights.get(i).getName()...

Of course this is a pretty simplified excerpt of what we will do but it chalks out the basic idea of our generation exercise: we want to have a service enabling us to call the SAP layer, and we want to have some objects representing the service call results - because returning plain XML and mixing this XML to objects is nasty and takes a lot of time. Axis does exactly what we require: creation of access Java class creation of Java Objects representing the results returned from the service call. So let's get started! First of all create a new so called "Dynamic Web Project": 1) Creation of "Dynamic Web project"

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 6

Creating Mobile Applications on top of SAP, Part 2

2) Specify project name (e.g. "MySAPService"), and select dynamic web module version 2.5 instead of 3.0 (Axis2 works with 2.5 only!). DONT miss this step of switching web module version to 2.5! Then press Next Next - Next - Finish or Finish directly.

3) Next thing we have to do is adding the Web Service stuff to our project. Select the newly created project within the project tree at the left, select "New - Other"; a popup comes up, select "Web Services - Web Service Client"

4) A Web Service configuration popup comes up, in the first field, drop your Web Service which should be sth. similar to: http://riagsrv.riagdom2.resource.ch:9999/sap/bc/srt/wsdl/bndg_DFE5C490AA519FF18F410050569D2CAD/w sdl11/allinone/ws_policy/document?sap-client=300

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 7

Creating Mobile Applications on top of SAP, Part 2

Then, chose the Web Service runtime (Apache Axis by default and set it to Apache Axis 2): (dont mind if there is no server runtime displayed within your project, this entry is not required...)

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 8

Creating Mobile Applications on top of SAP, Part 2

5) Axis2 will start its work immediately and present the service stub detected behind the Web Service in the next screen, you can click Finish at this state.

Have a look into your project (expand the src Folder), you will notice two classes which have been created, these classes will enable you to access your Web Service:

Before we start using and exploring these classes we have to apply a very little change to this generated class manually, since Axis2 by default ignores security credentials which have been placed on top of a Web Service (in our case simple authentication based on username and password). I'm pretty sure there is a way of generating security stuff directly but as we could not figure out directly we decided to present this workaround which is basically adding the required security stuff. 1) Open the File with the "...Stub" extension (Z_get_flight_listStub.java) 2) Search for the following string: "_serviceClient.createClient(_operations[0].getName());" 3) Add the following snippet after the line:
HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 9

Creating Mobile Applications on top of SAP, Part 2

auth.setUsername("username"); auth.setPassword("*******"); _operationClient.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstan ts.AUTHENTICATE, auth);

4) Here's what the whole block should look like;


try{ org.apache.axis2.client.OperationClient _operationClient = _serviceClient.createClient(_operations[0].getName()); HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator(); auth.setUsername("username"); auth.setPassword("********"); _operationClient.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstan ts.AUTHENTICATE, auth); _operationClient.getOptions().setAction("urn:sapcom:document:sap:soap:functions:mc-style:z_get_flight_list:ZFlightGetlistRequest"); _operationClient.getOptions().setExceptionToBeThrownOnSOAPFault(true);

This adds security credentials to the request before the request is actually being send to the Web Service, if you have no security on top of your Web Service you can skip this step. Using Java Web Service Stub Ok, the whole Web Service stub is ready for usage right now - which means, we can create a very simple demo app (simply add new Class to your src directoy), the class could look like this:
public class TestService public static void main(String[] args) throws Exception onSearch("FRANKFURT", "TOKYO"); private static void onSearch(String fromCity,String toCity) throws Exception Z_get_flight_listStub stub = new Z_get_flight_listStub(); Z_get_flight_listStub.ZFlightGetlist request = new Z_get_flight_listStub.ZFlightGetlist(); // from Char20 from = new Char20(); from.setChar20(fromCity); request.setImwCityFrom(from); // to Char20 to = new Char20(); to.setChar20(toCity); request.setImwCityTo(to); // fire request final Z_get_flight_listStub.ZFlightGetlistResponse resp = stub.zFlightGetlist(request); // extract results... int numberOfItems = resp.getExtFlights().getItem().length; System.out.println("Found flights: " +numberOfItems);

You are done! Test the service by simply running this class - in our case the service returns 15 flights from Frankfurt to Tokyo which have been looked up within the SAP system.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 10

Creating Mobile Applications on top of SAP, Part 2

Let's sum up what we are doing within this class: 1) Instantiate sup and create request on top of stub:
Z_get_flight_listStub stub = new Z_get_flight_listStub(); Z_get_flight_listStub.ZFlightGetlist request = new Z_get_flight_listStub.ZFlightGetlist();

2) Use the generated datatype classes and populate these - datatype classes? you might wonder...Axis is pretty clever - Axis checks automatically the datatypes required by your Web Service and creates datatype classes - this ensures you do ot push String longer than expected/allowed to the Web Service, it's a nice and efficient manner to prevent you from checking "what dataytpe and which length is expected at the service...?". (Char20 means datatype Char, maxlength 20)
// from Char20 from = new Char20(); from.setChar20(fromCity); request.setImwCityFrom(from); // to Char20 to = new Char20(); to.setChar20(toCity); request.setImwCityTo(to); 3) fire request and process results...: // fire request final Z_get_flight_listStub.ZFlightGetlistResponse resp = stub.zFlightGetlist(request); // extract results... int numberOfItems = resp.getExtFlights().getItem().length;

3) done! you just accessed you SAP Web Service through a Java Axis layer, this Axis layer now can connect to any Java related mobile UI technology - which is great and enables us to create a nice vaadin iPhone UI on top of this stub within the next tutorial...!

Next Steps
The next tutorial will get you in touch with development of mobile UI based on vaadin. As vaadin is plain Java there will be no difficulties to connect the stubs created within this tutorial part. Basically the "boiler plate stuff" is done at this point of the tutorial, next thing is the really exciting (and funny!) part , which is creating the iPhone UI...!

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 11

Creating Mobile Applications on top of SAP, Part 2

Related Content
Creating Mobile Applications on Top of SAP, Part 1 For more information, visit the Mobile homepage.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 12

Creating Mobile Applications on top of SAP, Part 2

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

SAP COMMUNITY NETWORK 2010 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com 13

You might also like