You are on page 1of 27

How to Develop Web Services in WebASPart 2

:
Design your own Web services with ABAP HTTP Extensions
By Axel Angeli, logosworld.com Editor's Note: To be honest, I didn't really understand the Web Application Server as well as I wanted to—until I read Axel's SAPtips white paper on the subject. Axel has a knack for making sense of SAP®'s solutions and helping others to understand their value. He believes that SAP's technological evolution is part of the overall trend towards open platforms and universal standards. What some SAP users may not realize is that the “future is now”—SAP has already reached the point where it can provide support for basic, integrated Web services initiatives. But stepping into SAP Web services does require mastery of new buzzwords like SOAP and WDSL. And, some key decisions about development environments must also be made. These choices may depend on which version of SAP is in use, which messaging systems are running, and many other factors. In this excellent how-to article, Axel digs right into all these issues. Building on his last piece, Axel starts with a basic introduction to Web services technologies, and then he takes a closer look at how basic Web services can be designed using WebAS, either with Business Server Pages or with HTTP extensions. Because this technology is still emerging, there are some judgment calls that must be made, such as whether to use traditional RFC/SAP HTTP Protocol, or WebAS's new ABAP Objects/HTTP classes. If this kind of techie talk peaks your interest, you're going to love Axel's opinionated, bird's eye view of SAP's Web services technologies. Chock full of actual coding examples, this article gives the real scoop on the technical capabilities SAP has to offer—capabilities that anyone who is looking to upgrade to the latest versions of SAP will want (and need) to know about. Technical Overview: The first part of this white paper1 uncovered the technical landscape of SAP’s WebAS and outlined how to develop classical server pages through the Business Server Pages (BSP). Web services are the recognized technology that provides automated services via the Internet. Given that fact, and given that a majority of international companies have SAP as their ERP system, it is a natural approach to make use of SAP itself to provide Web services. In this article, we’ll start by guiding you through an introduction to Web services and emerging protocols like SOAP and WDSL. Then, we'll discuss how Web services can be implemented using WebAS, either by means of Business Server Pages, or through SAP’s adaptation of the Java Servlet technology by means of the HTTP extensions super class.

A note on public XML services: The examples for Web services in this document make use of the public services provided by Amazon.com. The developer key in the examples has been invalidated. The examples will still work, but we kindly ask you to register for your own developer token at http://amazon.com/xml, in order to comply with their code of conduct and as a gesture to proper “netiquette.”

Part one of this series is available for SAPtips subscribers in the SAPtips document library. The article is titled "How to Develop Web Services in WebAS – Part 1," by Axel Angeli.
Copyright © 2003 by Klee Associates, Inc. www.SAPtips.com Page 1

1

How to Develop Web Services in WebASPart 2:
Design your own Web services with ABAP HTTP Extensions Consulting Web Services from SAP and Anywhere Accessing SAP from Any Web Server
Nowadays, the SAP ERP suite is the omnipresent software solution for enterprise computing. Many companies rely on SAP to manage their important finance and asset management issues, as well as their sales and distribution process, just to name a couple of the functional areas that SAP encompasses. A common—and definitely not very new—desire of users of information technology is to access the data stored in a centralized ERP system from anywhere in the world; especially now, since we have a feature-rich Internet browser client stored on every computer, including small pervasive devices like the palm-top computers. Under this premise, it appears as a natural choice to access your centralized SAP system via HTTP, the protocol your browser uses. Once you start talking about managing business processes remotely via the Internet, you're talking about the emerging technology that we call “Web services.”

Accessing SAP ERP via HTTP

HTTP Client Application (can be another R/3 system, too)
HTTP

Somewhere in the Internet/Intranet

Any HTTP Server

SAP WebAS ICM Internet Communication Manager HTTP Communication Interface Understands both HTTP and RFC

COM

RFC Function Modules

Figure 1: Accessing an ERP System HTTP Web services are designed to be accessed from another program, although they can generally be consulted from a Web browser as well. The calling program can be any program that understands HTTP, including SAP R/3 starting with release 3.0E. Before we go into details, let’s first learn more about Web services by example.

Copyright © 2003 by Klee Associates, Inc. www.SAPtips.com

RFC

BAPI SAP R/3

RFC RFC
Page 2

which allows XML-based inquiries to their bookstore through automated Web services. logosworld.amazon. To visualize this. The principle of our example is very simple: • • You send an HTTP request to the Web service provider. www.com.com Web Service To make the request string a little better readable.com/onca/xml2?t=webservices-20&tag=logosworldcom&devt=D2H30000000000&AsinSearch=3528057297&type=lite&f=xml. However. See Figure 4 below. You can test this easily by entering the HTTP request shown in Figure 2 in your Web browser: http://xml.SAPtips. Inc.amazon.com Page 3 .com/onca/xml2 ?t=webservices-20 &tag=logosworldcom &dev-t=D2H30000000000 &AsinSearch=3528057297 &type=lite&f=xml.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions Example of a Web Service Let's look at what a typical Web service might look like. Copyright © 2003 by Klee Associates.com).com Web Service Request URL Formatted for Better Readability This request (which asks Amazon. http://xml. we shall have a look at Amazon.com to list the books authored by someone in my company. You receive an XML document with the result. returns a plain XML document with the requested information included. Figure 2: Calling the Amazon. the URL above must be entered as one string without any spaces as shown in Figure 2. we insert some line breaks after each part of the URL (Figure 3). Figure 3: The Amazon.

SAPtips. but this is a very ambiguous expression. At one end is a client. a communication consists of two partners. Inc. so that the Internet framework can route the request to a listener that runs on a server and is able to retrieve the content from the point of content. In order to locate a point of content. the partners at either end of a communication line are called end points. so we better avoid it. End points are also known as terminals. or requester.w3.95</OurPrice> </Details> </ProductInfo> Tree representation of above XML ProductInfo └─ Details ├─ Asin │ └─ 3528057297 ├─ ProductName │ └─ The SAP R/3 Guide to EDI and Interfaces ├─ Authors │ └─ Author │ ├─ Axel Angeli │ ├─ Ulrich Streit │ └─ Robi Gonfalonieri └─ OurPrice └─ $50.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions <?xml version="1.95 Figure 4: XML-Response from Amazon.0" encoding="UTF-8"?> <ProductInfo xmlns:xsi=http://www.amazon. the requester specifies three items: Copyright © 2003 by Klee Associates. the point of content is a file. In simple cases. Typically. the requester needs to specify sufficient information about the requested content. URIs and Points of Content In the theory of electronic communication.com Page 4 .com About URLs. and at the other end is a point of content located on a server.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation ="http://xml. www. Typically.xsd"> <Details> <Asin>3528057297</Asin> <ProductName>The SAP R/3 Guide to EDI and Interfaces</ProductName> <Authors> <Author>Axel Angeli</Author> <Author>Ulrich Streit</Author> <Author>Robi Gonfalonieri</Author> </Authors> <Manufacturer>Friedrick Vieweg &amp. Son</Manufacturer> <OurPrice>$50.com/schemas2/dev-lite.

but it can be any string that is understood by the server.php. alternately. Parts of a URI Resource type Resource address Resource parameters ter e me me am na ra N le ol ain er &fi oc -Pa GI om rot Serv ath C p D P http: //www. a URL references a file on a specific server. and instead of returning a simple file.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions • • • The public name of the server and the domain. the Web server captures the URL. .e.. . .. logosworld. this is simply the filename of the requested document.de /asp/testdrive.. There are a wide variety of protocols. In cases of Web pages. it calls a program to return a computed result.htm ?user=micky&pass=mausi Resource address This is an identifying name of the requested ressource. smtp: Simple Mail Transfer Protocol Most important protocol for eMail distribution. while URL is reserved to address a Web page. Usually the server tells from the suffix (e.txt. Inc. it can specify the server’s unique IP address. where a URL is generally recognized as a subset or special variant of a URI. ftp: File Transfer Protocol Protocol specially for file transfers. www.htm.g. The assembly of these three information items is called a URI – Uniform Resource Indicator.com Page 5 . Ressource type Tells the server which protocol to use. they are also known as URL – Uniform Resource Locator.asp.g. Traditionally. . parameters to pass further details about the request are provided. Nowadays a URL may point to any kind of electronic resource that can be reached via HTTP. The most important protocols are the following ones: HyperText Transfer Protocol Tells the server that the requester (i.cgi). a Uniform Resource Identifier. the browser) wants to receive general Unicode-coded documents (e.SAPtips. . Figure 5 shows how a URI is constructed. The name of the resource in a format that it is understood by the listening port on the server Optionally. In these cases. HTML). Usually. So we can say that a reference to a general point of content within the Internet is called a URI. Figure 5: Elements of a URI http: Copyright © 2003 by Klee Associates.

and transported down to Kansas on a train. That is why we could pass all the search parameters as part of the requesting URI string.. we sent all the information that is needed by the server to build the proper result in the browser line. we recommend taking a look at http://whatis. both the customer and vendor have a vested interest in regularly inspecting the status and location of each shipment.com Copyright © 2003 by Klee Associates. Inc. packing 4. Issuing an HTTP request without sending any additional data is known as an “HTTP Get request”. Web services need to be easily accessible without any restrictive documentation. order receipt 2. they must abide by some guidelines: 1. We could also have sent the parameters as an attachment to the HTTP request within the HTTP document body. Of course.com is a relatively simply one.com. Many shipping companies offer these kinds of services already through the Internet. www. imagine that deliveries are transported from Sweden down to the Frankfurt airport. so that the data no longer needs to be passed via paper notes. this crucial information is now available in “quasi real-time”. the commodities are passed through several hands. or processing points. delivered by Techtarget. shipping Such tracking systems are already established by many shipping companies. The history of this parameter-passing convention goes back to the long established CGI protocol2. and manual re-entry. As a natural extension. the individual “legs”. Via a basic Web service. can report their status to the same Web service interface.SAPtips. Furthermore. Web services are typically comprised of self-service applications and automated workflows. A typical process flow may involve the following steps: 1. i.com Page 6 . and that is what we did. The request for a search at Amazon. is called a canonical URL. this “online tracking” can be formalized as a Web service by presenting the data in a computer-readable format that can be easily interpreted by a client program. We did this by adding the parameters behind the actual URL. 2 If readers are looking for more explanation of the technical terms involved in Web services.e. by separating parameters with ampersands (&). production start and end 3. then flown “over the pond” to Chicago. A Detailed View on Web Services There is no precise. and in the course of order fulfillment. This obviously takes time.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions Sending Parameters via HTTP In the previous example (Figure 2). Because Web services are designed to smoothly cooperate with other programs. you cannot simply load a program on your Web server and wait for anyone to call it up from anywhere within the reach of your network. Web services are programs that can be accessed via an Internet connection. where the authorized parties can check the status and location of an order in process. faxes. or even an official definition of “Web service.” Roughly speaking. The form we chose to pass the parameters. Consider this typical self-service application: picture an order tracking service where a company provides an interface through the Internet that lets a client query the actual status of an order. In this case. Given that there are so many uncertainties during the order flow.

HTTP and the sister protocols—like the secure HTTPS option—are the undisputed standard as Internet protocol.) Web services via the SOAP-XML Standard As the request itself becomes more complicated. many EDI projects failed primarily because the chosen formats were proprietary and had been disclosed only to a pre-selected. using the canonical URL as introduced by the CGI. As a point of comparison. Due to the text-based nature of XML documents. this is Copyright © 2003 by Klee Associates. • Web service results are either called by: • • passing the requested data to an agent as an XML document. Characteristics of a Web Service From what is offered as Web services today. Web services look largely the same. automated document-distribution system. This is an important characteristic that defines the essential difference between a classic Web page and a Web service. as will Adobe Framemaker. With the use of XML. • Web services deliver their results as an XML data stream.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions One great feature of the “human readable XML format” is that it usually allows an easy discovery of its contents and the document structure without the need for external documentation. Sooner or later all those acknowledged EDI standards like EDIFACT/UN or ANSI X. etc. the response data must provide sufficient information so that a program can formally derive the requested information with as little effort as possible. the next version of Microsoft Office (Word. No matter the content. XML documents have become so ubiquitous that it is an easy bet to say that XML will be the exclusive data exchange format for any kind of online. EDIFACT documentation is only accessible by paying dues to the organization).SAPtips. XML has become the de facto standard for Internet data exchange. it is possible to parse documents for necessary information. inflexible format.G. It is already obvious that the EDI world will soon be dominated by XML. Inc. • Web services results are structured data—data that can be easily interpreted by another program. 2. or passing parameters as part of the URI.) will be XML-enabled. (We'll elaborate on canonical URLs on the next page.g.12 will go the way of the dinosaurs. and the only Internet protocol that is understood by all modern computers. or adding XML interfaces for external programs. e. However. the URI(URL) used to issue the request would become lengthy. instead of mapping to a strict. Therefore. the results are naturally readable as well— another helpful XML feature. we can generally characterize Web services as having the following attributes: • Web services are invoked via an HTTP request. Web services are designed to be consulted primarily by other programs. Web services should be easily adaptable to a range of clients and operating systems. exclusive audience (E. Given this flexibility. PowerPoint. Many document processing applications are transitioning from proprietary formats to storing data as XML data. the request can also be handed over to HTTP as a data block.com Page 7 . www. The “Alpha and Omega” of Web services is “compatibility through convention”. a handler program can react smoothly on changes and additions in the sent documents..

In addition to having the same features as a plain XML document. therefore it is not that easy to test this variant in your browser.com accepts requests from an HTTP Post as well and. e. however. Let's take a closer look at how Web services function within various SAP releases. a valid RFC destination Copyright © 2003 by Klee Associates. which is found both in the “/bin/exe” directory of the SAP application server (“/usr/sap/SID/SYS/bin/exe”) and as “SAPHTTP. SOAP defines restrictions on how the document must be structured and how the XML tags have to be named. While the WebAS (SAP 4. we have seen how a Web service works in principle. we show what a SOAP request might look like... Amazon. the older releases make use of an SAP-provided utility called SAPHTTP. SOAP is the acronym for the Simple Object Access Protocol and is itself an XML document. but in Figure 6. SOAP was designed out of the simple need to provide a standard format for the multiple ways you could structure a response with respect to naming your tags and laying out your response tree. SOAP has been designed to be used out of programs. a program within SAP R/3. but another program.SAPtips. Before you can use SAPHTTP. The typical client of a Web service.g. in this case.g.7 and higher) can act generically as an HTTP client. <SOAP-ENV:Envelope> <SOAP-ENV:Body> <AsinSearchRequest> <AsinSearchRequest> <asin>3528057297</asin> <tag>logosworldcom</tag> <type>lite</type> <dev-tag>12345678901234</dev-tag> </AsinSearchRequest> </typens:AsinSearchRequest> </SOAP-ENV:Body> </SOAP-ENV:Envelope> Tree representation of above XML AsinSearchRequest ├─ asin │ └─ 3528057297 ├─ tag │ └─ logosworldcom ├─ type │ └─ lite └─ dev-tag └─ 12345678901234 Figure 6: SOAP Request for Amazon. www.EXE” as part of the SAPGUI installation package.com SAP as Web Client So far. is not an individual user.com Page 8 .How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions then called an “HTTP Post request”. There are many uses of HTTP Post requests. an HTTP Post request is sent to a server if you fill out an online HTML form in your browser and submit the form data. e. it uses the SOAP protocol standard. Inc.

ABSOLUTE_URI = 'http://xml.amazon. using our Amazon. and function HTTP_POST with the destination of SAPHTTP. SAPHTTP will be called via the SAPGUI on the workstation.com Page 9 .com book query example. data: RESPONSE_ENTITY_BODY(120) occurs 0 with header line.64:8080' TABLES * REQUEST_ENTITY_BODY = RESPONSE_ENTITY_BODY = RESPONSE_ENTITY_BODY RESPONSE_HEADERS = RESPONSE_HEADERS * REQUEST_HEADERS = EXCEPTIONS OTHERS = 8. Inc. WRITE: / response_entity_body. Using SAPHTTP. Figure 7: SAP as Web Service Client with ABAP via SAPHTTP Utility Copyright © 2003 by Klee Associates.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions must be defined with transaction SM59 in SAP (Figure 8). you call the following function modules: • • function HTTP_GET.com/onca/xml2?t=webservices-20' & '&tag=logosworldcom&dev-t=D2H30000000000' & '&AsinSearch=3528057297&type=lite&f=xml'.SAPtips. ENDLOOP. pointing to SAPHTTP either on the server or on the PC that has SAPGUI on. See Figure 7 for an example of accessing Web services in SAP. LOOP AT response_entity_body. www.168. data: response_headers(80) occurs 0 with header line.69. DATA: ABSOLUTE_URI(128) type c. In the latter case. CALL FUNCTION 'HTTP_GET' EXPORTING ABSOLUTE_URI = ABSOLUTE_URI RFC_DESTINATION = 'SAPHTTPA' PROXY = '192.

Inc. Not only will it keep your development backward-compatible with older SAP releases. Standard SAP WebAS distribution includes the report SXSLT_TEST.SAPtips. proposes to use the IF_HTTP_CLIENT interface class instead. www. and cannot currently satisfy the needs of a modern developer. Unfortunately. ABAP Workbench.com Page 10 . Intermezzo (and key SAP RFC tip): I recommend continuing to use the traditional method via the RFC destination to SAPHTTP whenever possible. but the new access method. is still in a state of evolution. via the ABAP Object syntax. whose form LOAD_HTTP gives an example of how to request an HTTP page. In Figure 9 we constructed another example to demonstrate the essential. with its generic HTTP interface.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions Figure 8: Definition of RFC Destination SAPHTTP in Transaction SM59 Calling a Web Services with the SAP WebAS HTTP Extension Class WebAS. Copyright © 2003 by Klee Associates.

The ABAP editor does also not support ABAP/Objects in a convenient way. which basically does HTTP forwarding to your proxy. it must be said that a large part of the coding is realized on the kernel level. this is not much of an advantage.com. while continuing to use SAPHTTP for outbound Internet access and BSP for inbound HTTP calls. the best practice is to ignore the HTTP classes of WebAS. but from an experienced software architect’s and software auditor’s point of view. I will author an update to SAPtips readers. this way in which HTTP services are currently implemented in WebAS cannot be endorsed at this time. But when you take into account that real life implementations of Web clients from SAP will forcibly access the Internet via a proxy anyway. I will keep an eye on the evolving ABAP/Objects and HTTP classes of WebAS. With respect to the HTTP classes that ship with WebAS. ambition. as opposed to SAPHTTP. which submits a query to Amazon. are either completely missing or awkward to use.com Page 11 . like Intellisense. Copyright © 2003 by Klee Associates. www. Inc. I decidedly state that. For a detailed look at the WebAS HTTP client classes. see Figure 9 below. which makes it nearly impossible to debug there. Therefore. as of this writing. Editing aids. The only advantage of the HTTP classes over the traditional SAPHTTP is the direct implementation of all HTTP features. which displays methods and attributes for an object while you type.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions the traditional SAP development environment. and professional obsession to object oriented development will heavily object. and at the point where these new technologies surpass the traditional SAP HTTP protocols. The core tool of every modern development workbench. does not allow the display of essential information about ABAP/Objects and the current setting of attributes. the debugger. I am aware that a large community of developers.SAPtips. only has very basic support for ABAP/Objects. who dedicate their time.

* Capture the result data response = client->response->get_cdata( ). send & receive client->request->set_header_field( name = value = client->request->set_header_field( name = value = * send & receive client->send( ). DATA: g_ixml TYPE REF TO if_ixml. proxy_port = '8080'. DATA: client TYPE REF TO if_http_client. www. START-OF-SELECTION. host TYPE string.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions REPORT ZAXXTEST_CLHTTP. DATA: g_stream_factory TYPE REF TO if_ixml_stream_factory. . Copyright © 2003 by Klee Associates. port = '80'. client->receive( ). '~request_method' 'GET' ). proxy_host TYPE string.amazon. Setting the parameters needed by the object host = 'http://xml. g_ixml = cl_ixml=>create( ). proxy_host = '192. scheme = 1. path TYPE string.168. proxy_port TYPE string. CLASS cl_ixml DEFINITION LOAD. DATA: headfields TYPE tihttpnvp.com Page 12 . scheme TYPE i. * Create HTTP client object. '~request_uri' path ). "1=HTTP 2=HTTPS * Create HTTP client object CALL METHOD cl_http_client=>create EXPORTING host = host service = port proxy_host = proxy_host proxy_service = proxy_port scheme = scheme IMPORTING client = client.SAPtips. Inc. During program load we create factory (helper) classes for XML handling LOAD-OF-PROGRAM. DATA: response TYPE string.64'. path = '/onca/xml2?t=webservices-20' & '&tag=logosworldcom&dev-t=D2H30000000000' & '&AsinSearch=3528057297&type=lite&f=xml'.69.com'. g_stream_factory = g_ixml->create_stream_factory( ). port TYPE string.

The WSDL format for Amazon. Needless to say.com Page 13 . and you would already know the information you needed to access it. which is upheld by a consortium mainly driven by IBM. in advance. www. it is not clear how to find out how the SOAP requests need to be formatted. these directories are of interest only for a certain niche group of clients. we need some sort of documentation. There appear to be some political issues in play here. However. the next step is to publish the WSDL in a public directory. The required documentation for the form of a Web service can in turn be easily represented as a formal XML document. the use of a Web service is not typically established on an ad hoc basis. The WSDL tells you how the tags of the request and response have to be named and what tree layout the XML documents will have. Discovery and Integration). including the WSDL. WRITE: / response COLOR COL_NEGATIVE. without having to consult the UDDI. This key point should not go unmentioned: if you search for WSDL in SAP’s Interface Repository (http://ifr. One of the popular Web service directories is the UDDI (Universal Description. In practice. Inc.sap. These formal descriptions of SOAP requests are done in a language called WSDL. like the yellow pages. competing Web services standards only slow down the corporate acceptance of Web services. Instead of WDSL.com is illustrated in Figure 10.SAPtips. because while WSCI may not be worse than WSDL. so we can only hope that the political issues will be set aside in the interests of universal interoperability. To do this. just as you publish a telephone number in the yellow pages. you may not find what you expect. you would know exactly what kind of service you wanted to use. as the directories do not indicate anything about the quality of a service. WSDL is mainly driven by IBM and Microsoft and it already has the status of a recommendation by the W3C (World Wide Web Consortium). SAP has not yet committed itself to any standards like WSDL. SAP has affiliated itself with a competing standard called the Web Service Choreography Interface (WSCI). If your Web service is to be accessed by many foreign sources. an initiative mainly driven by SUN Microsystems and BEA. the Web Services Description Language. Figure 9: WebAS as HTTP Client Discovering a Web Service with WSDL and UDDI While the previous examples are certainly clear enough to understand the idea behind sending Web service requests and interpreting the responses. Copyright © 2003 by Klee Associates. Rather. it isn't better either.com).How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions * Capture the header fields from the result CALL METHOD client->response->get_header_fields CHANGING fields = headfields.

Such a program is known as a servlet. There are basically two different approaches to outline here.NET. Copyright © 2003 by Klee Associates. Let us explore this by showing how it is done in Java. what we really want to see is how we can implement our own services.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions <definitions targetNamespace= "urn:PI/DevCentral/SoapService" name="AmazonSearch"> <types> <xsd:schema> <xsd:complexType name="AsinRequest"> <xsd:all> <xsd:element name="asin" /> <xsd:element name="tag" /> <xsd:element name="type" /> <xsd:element name="devtag" /> <xsd:element name="offer" /> <xsd:element name="offerpage" minOccurs="0"/> </xsd:all> </xsd:complexType> </xsd:schema> </types> <message name="AsinSearchRequest"> <part name="AsinSearchRequest" type="typens:AsinRequest"/> </message> <message name="AsinSearchResponse"> <part name="return" type="typens:ProductInfo"/> </message> </definitions> Figure 10: WSDL-Definition of an Amazon Search by Article Number (ASIN) Implementing Your Own Web Services Now that we have seen how we can make use of other people’s Web services. the servlet concept differs in some essential ways. The alternative is to set up a program that is called directly by the URL through an interface shim and returns the resulting data stream. is nearly identical for all Web servers. Inc. and manipulate it to return an XML document instead of an HTML page. One option is to create the page by defining a template page that outlines the structure of the response document and by generating some code that evaluates the response data. www. WebAS. we defined a BSP that calls the BAPI to return the actual currency exchange rates as they are stored in the SAP system. The result was inserted within HTML tags to display an HTML table. our goal will be to create an XML result page. and Microsoft. Web Services Through a BSP Let us start by taking the example of a Business Server Page (BSP) from the previous article I wrote on WebAS. which we'll explore shortly. The way that servlets are called and parameters are passed to them is derived from the old CGI standard. In the previous article. While the concept of server pages. which allowed for the calling of executable programs from a Web server. In both cases. with its predefined templates.com Page 14 .SAPtips.

texch_rate_list TYPE STANDARD TABLE OF bapi1093_0. <%@page language="abap"%> <% DATA: exch_rate_list type bapi1093_0.sap. we could also enhance the template from the IFR and easily construct a SOAP document.Response xmlns:doc="urn:sapcom:document:sap:business"> <!--List of Exchange Rates per Exch.GetCurrentRates. www.com.5--> <%= exch_rate_list-exch_rate %> </EXCH_RATE> </item> <% ENDLOOP. DATA: DATA: DATA: DATA: tfrom_curr_range TYPE STANDARD TABLE OF bapi1093_3. %> <item> <!--BAPI exchange rate table--> <FROM_CURR><!--From currency--><!--CUKY--> <%= exch_rate_list-from_curr %> </FROM_CURR> <TO_CURRNCY><!--To-currency--><!--CUKY--> <%= exch_rate_list-to_currncy %> </TO_CURRNCY> <VALID_FROM><!--Date from Which Entry Is Valid--><!--DATS--> <%= exch_rate_list-valid_from %> </VALID_FROM> <EXCH_RATE><!--Direct Quoted Exchange Rate--><!--DEC 9.GetCurrentRates. The format of the resulting XML document is taken from the template defined in the interface repository http://ifr. which differs only in some named tags.com Page 15 . tto_currncy_range TYPE STANDARD TABLE OF bapi1093_4.SAPtips.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions We’ll now vary this example by inserting some useful XML tags to represent a proper request response (see Figure 11). As another variation. We'll do this later in this paper. %> </ExchRateList> </doc:ExchangeRate. Inc.Response> Figure 11: XML Service Defined Through a BSP Copyright © 2003 by Klee Associates. CALL FUNCTION 'BAPI_EXCHRATE_GETCURRENTRATES' DESTINATION 'NONE' EXPORTING date = sy-datum TABLES from_curr_range = tfrom_curr_range to_currncy_range = tto_currncy_range exch_rate_list = texch_rate_list return = treturn. treturn TYPE STANDARD TABLE OF bapiret1.Rate Type and Date--> <ExchRateList> <!--Export: List of exchange rates--> <% LOOP AT texch_rate_list into exch_rate_list. %> <doc:ExchangeRate.

Inc.NET or Java servlets resolve the parameters immediately into the request object. we demonstrate their use in Figure 12.com Page 16 .How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions Passing Parameters to the Web Service The previous example shown in Figure 11 is missing an important feature. www. the passing of parameters to the Web service. they are accessed through header variables in WebAS. Copyright © 2003 by Klee Associates. Because this feature is poorly documented in the current WebAS release.SAPtips. While ASP.

com Page 17 . DATA: querystring TYPE string. name TYPE string. field TYPE LINE OF tihttpnvp. Inc./sap/public/bc/bsp/styles/sapbsp.. *********** URI Helper Class ************************************** * Initialize the helper class to better deal with query strings URI * need a dummy prefix to make the class work properly CONCATENATE 'dummy?' querystring INTO querystring. DATA: query TYPE REF TO cl_swlwp_uri.css"> <title>Simulation of AMAZON Web Services through BSP</title> <% DATA: BEGIN OF parm. query->get_query_parameter im_name = parm-name ex_value = parm-value. **** Here we read all information pairs of the HTTP request ******** CALL METHOD request->get_header_fields CHANGING fields = tfields. %> Copyright © 2003 by Klee Associates./. DATA: xml TYPE string. CALL METHOD cl_swlwp_uri=>create_from_string EXPORTING im_string = querystring IMPORTING ex_uri = query. **** This one reads the query_string explicitely ******** CALL METHOD request->get_header_field EXPORTING name = '~query_string' RECEIVING value = querystring. value TYPE string. DATA: request_uri TYPE string. DATA: tfields TYPE tihttpnvp.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions <%@page language="abap"%> <html> <head> <link rel="stylesheet" href=". www. ******************************************************* %> </head> <body class="bspBody1"> <H1>Simulation of AMAZON Web Services through BSP</H1> <HR>QueryString: <% = querystring %> <br>PARAM: Name=<% = parm-name %> Value=<% = parm-value %> RC= <% = parm-rc %> <HR><table> <% LOOP AT tfields INTO field. *********** parm-name = CALL METHOD EXPORTING IMPORTING Extract a single parameter **************** 'ASIN'.SAPtips.. rc TYPE i. **** Here we better call a function module to do our service. END OF parm. * CALL FUNCTION 'Z_AXX_XML_BOOKSTORE' * EXPORTING querystring = querystring * IMPORTING XML = XML.

SAPtips.servlet.org/2001/XMLSchema-instance"> <QueryString> <% = querystring %> </QueryString> <PARAM><NAME><% = parm-name %> </NAME><VALUE><% = parm-value %></VALUE></PARAM> <return> <% LOOP AT tfields INTO field. %> </return> </ProductInfo> Figure 13: Returning the Query String Data as XML Stream in the Amazon Fashion Same as in previous example WebAS Servlets Through HTTP Extensions The previous WebAS examples returned the both HTML and XML result through a Business Server Page. the WebAS extends3 the ABAP class IF_HTTP_EXTENSION (the prefix IF stands for interface). assuming that this is sufficient guidance to add the necessary code to return the desired result data in the resulting XML stream. <%@page language="abap"%> <% . %> </table> </body></html> Figure 12: Catching the Query String Parameters to Simulate the Amazon Service In Figure 13 we show the way to simulate the Amazon. Since there is no bookstore available in the WebAS basic version. as they use an approach similar to the Java servlets. the newly developed class inherits the IF_HTTP_EXTENSION and overwrites one distinct method: HANDLE_REQUEST ()..com Page 18 . The request handler itself can access the HTTP objects through a 3 Extending a class is also known as inheriting a class..w3.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions <tr><td><% = field-name %></td><td><% = field-value %></td></tr> <% ENDLOOP. we simply echo the received parameters in the result string. Inc. %> <Details> <Name><% = field-name %></Name> <Value><% = field-value %></Value> </Details> <% ENDLOOP. While Java servlets extend the base class javax. The example returns an XML data stream in the way it is done by Amazon. depending on which terminology of object oriented programming is used Copyright © 2003 by Klee Associates.com article number search. www. which we will call WebAS servlets.*. %> <?xml version="1.0" encoding="UTF-8"?> <ProductInfo xmlns:xsi="http://www.com. Extending the class is also known as inheriting the class. In the case of Web services. Another way to produce the result string from the WebAS is through the use of the HTTP extensions.

server->response->set_header_field( name = 'Content-Type' value = 'text/html' ). So the response header is set with the method Copyright © 2003 by Klee Associates. result = '<html><body>'(001) & 'Connection to server successful'(101) & 'SystemID: &SYSID&'(102) & '</body></html'(999).com Page 19 . Inc. pingservice is the public name that has been assigned to the implementing class in transaction SICF. Hence. See Figure 15. The IF_HTTP_EXTENSION class implements all important methods and attributes that are needed for handling the response and request parts of an HTTP communication. In our example. and it should implement a method with the fixed name HANDLE_REQUEST().g. an arbitrary ABAP Object code can be executed. method IF_HTTP_EXTENSION~HANDLE_REQUEST. according to the requirements of the requesting HTTP client. www.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions series of helper classes. In the pingservice example. e. it calls the method HANDLE_REQUEST() of the named class. replace '&SYSID&' with sy-sysid into result. Finally. with an up and running WebAS. IF_HTTP_RESPONSE and IF_HTTP_REQUEST.. data: result type string. which are inherited via the parent class IF_HTTP_EXTENSION. The class has been mapped with transaction SICF to the service name pingservice. server->response->set_cdata( data = result ). http://linux:8080/pingservice Figure 14: Sample URL to Call a Web Service of the WebAS Request Handling with Method IF_HTTP_EXTENSION~HANDLE_REQUEST When the HTTP service request is received by the WebAS. you could call the service by entering a URL like the one below in your browser.SAPtips. for an HTTP service we shall inspect the predefined class CL_HTTP_EXT_PING. Figure 15: Sample Implementation of a Pingservice Class Within the HANDLE_REQUEST method. which can be reviewed with the class builder (SE24). we assume that the request expects a simple standard HTML response. endmethod. The implementing class should be an extension of the default handler class IF_HTTP_EXTENSION. where ”linux:8080” is the HTTP address of the WebAS. As a simple first example. the response object must be furnished with the response data.

so consequently a servlet must support and implement the standard methods: • • doGet() doPost() Copyright © 2003 by Klee Associates. replace '&SYSID&' with sy-sysid into result. Inc. This is “best practice” because it reduces the amount of code within an interface handler to a minimum. When a servlet is written. A calling Web client may expect that a servlet handles at least the basic HTTP commands Get and Post.2. the header would at least provide information about the content of the document data that is sent in the document body. www. which actually can be an arbitrary text. For every HTTP command. doGet for HTTP Get and doPost for HTTP Post. Other than in Java. In Java.. where Xxx is to be replaced by the name of the handled HTTP command. a program that can be called via HTTP is called a servlet. Basically. you might be interested to see how the same code looks when it is written in Java. which is sometimes mandatory and sometimes only useful. the result is assigned to the HTTP body with the set_cdata method. but the servlet container explicitly calls some methods that match the name of the HTTP request. we build the result string. we need to set the HTTP header information. Doing so. such as GET or POST. In our case. the program in Java looks the same as in ABAP.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions server->response->set_header_field accordingly to text/html and the result data block is set to the response data with method server->response->set_cdata( data = ) . e. Web Services Through Java Servlets With the upcoming hype for Java J2EE support as part of the WebAS starting with release 6. As a best practice rule. In addition to returning the data. you would insert a call to a function module that returns the final result data string. the request handler is used as a wrapper for the application and as a jumping board to call the actual processing only: result = '<html><body>'(001) & 'Connection to server successful'(101) & 'SystemID: &SYSID&'(102) & '</body></html'(999).SAPtips. The valid content descriptors are defined by the W3C MIME recommendation.com Page 20 .g. there is a formal base class that carries the HTTP extensions. server->response->set_header_field( name = 'Content-Type' value = 'text/html' ). Typically. Finally. Again. server->response->set_cdata( data = result ). This class is inherited by any servlet class that you develop and deploy. there has to be a method implemented whose name is built by prefixing the HTTP command with “do” as follows: • doXxx(). the servlet does not work on top of a general request handler. As a next step. we tell the receiver of the document that the body contains HTML data. it is deployed in a formal structure called the servlet container.

e.println("<html><body>Hello World from JSP</body></html>"). Copyright © 2003 by Klee Associates.println("<html><body>Welcome to Hello World for JSP</body></html>").g. Inc.com Page 21 . the servlet class must be deployed in the Web server’s servlet container. In Figure 17. it can be appended to the URL separated by a question mark (?) to build a canonical URI. there is an example that is also found in a similar fashion in the demo section of the WebAS.out.. If the class requires parameter information. and the response of each method is a string that is returned in the body of HTTP response. } public static void main(String[] args) { System. The key thing to understand is that the method soapdoc->add_parameter converts the given ABAP data structure into a SOAP-compliant XML sub tree.SAPtips.” The example may look cryptic at the first glance. creating a proper SOAP response document. When this is successfully done. which we are already familiar with. } } Figure 16: Implementation of Servlet Class HelloWorldExample in Java SAP XML and SOAP Utilities SAP also provides for a number of helper classes to allow for easier (I don't dare say easy) creation of the resulting XML documents. Before we can use the servlet. public class HelloJSP { public static void doGet(String[] args) { System.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions Every method will be called exclusively from the matching context. See Figure 16. it can be called via an HTTP Get or HTTP Post request by specifying nothing more than the class name in the URL.out. www. and is called the “info service.

isoapdoc = soapdoc. clear xdata. xdata type xstring. catch CSoapExceptionInternal into exIntrn. catch CSoapExceptionUsage into exUsage. call method isoapdoc->serialize changing document = xdata. exResrc type ref to CSoapExceptionResource. if xdata is initial. data type string.set header field call method server->response->set_header_field( name = 'Content-Type' "#EC NOTEXT value = 'text/xml' ). call function 'RFC_SYSTEM_INFO' importing rfcsi_export = l_rfcsi. catch CSoapExceptionResource into exResrc. try. endif. www. get reference of l_rfcsi into dref. call method soapdoc->set_method exporting name = 'RFC_SYSTEM_INFO. "-. * parameters: * [in] if_http_server& server * Call through browser: http://linux:8080/sap/public/info data: data: data: data: data: data: data: data: data: l_rfcsi type rfcsi.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions method if_http_extension~handle_request. clear xdata.SAPtips. create object soapdoc. call method server->response->set_header_field( Copyright © 2003 by Klee Associates. call method soapdoc->add_parameter exporting name = 'RFCSI' value = dref direction = CSoapConstants=>ic_param_in.Response' nsprefix = 'rfc' nsvalue = CSoapConstants=>sc_rfc_function_ns. Inc. exUsage type ref to CSoapExceptionUsage. exIntrn type ref to CSoapExceptionInternal.com Page 22 . endtry. soapdoc type ref to CSoapDocument.don't care clear xdata. dref type ref to data. * -. clear xdata. isoapdoc type ref to ISoapSerialize. if not soapdoc is initial.

www. you will find that the SOAP document is nearly identical with the template provided by the SAP interface repository. call method server->response->set_data( data = xdata ).How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions name = 'Content-Type' "#EC NOTEXT value = 'text/html' ). else. If you look closer. but with one small enhancement: the template is embraced by the SOAPENV envelope tags. the resulting SOAP document looks as follows in Figure 18.com Page 23 . Inc. endif. Figure 17: Producing a SOAP Document Out of a Function Call Result In my system. endmethod. * Bild an HTML framework to properly display it in a browser concatenate '<html>' '<body>' l_rfcsi '</body>' '</html>' into data. call method server->response->set_cdata( data = data ).SAPtips. Copyright © 2003 by Klee Associates.

if you designed a solution that delivered information from your ERP system to an Internet requester. it is certainly clear that there is no need to upgrade to WebAS or SAP Enterprise (4. If you want to give your HTTP-based SAP clients real-time access to all the wealth of data inside of SAP. Inc. Realistically.SAPtips. Copyright © 2003 by Klee Associates. and access your SAP ERP system via RFC calls from Java or Windows DCOM calls.7) for the sole purpose of integrating your SAP system with the Web.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions Figure 18: SOAP Document Produced with Aid of the Soapdoc add_parameter Method Do You Need WebAS or SAP Enterprise for Web Services? Calling SAP via RFC or via HTTP From what we have seen so far. www. you can use any modern Web server. Exposing your ERP system directly to the Internet would be like depositing the safe with the Queen’s crown jewels in the garden of Buckingham palace. for the simple reason of security.com Page 24 . you would not even think of plugging in your Internet connection directly to your SAP box.

having SAP function like any HTTP server. • Smooth HTTP interaction can provoke a nominal return of investment when you have a complex heterogeneous server landscape. What may look like an easy five-minute task to your development team suddenly turns out to become an elaborate ceremony requiring weeks of planning.com Page 25 . calling dedicated libraries. So. Installing drivers on a productive machine is always critical. it is only up to whoever is handling the firewall to open or deny the access to your SAP ERP system. as of this writing. for all the reasons I've outlined in this paper. as it does in SAP Enterprise. and it's available for most common operating platforms (including Windows. you will benefit from the alreadyinstalled TCP/IP tools that are universally installed on every modern computer from mainframes down to pervasive palm-top clients. This allows access to SAP from every computer within reach of your network. best of all. Inc. based on the strengths of the WebAS and Enterprise 4. Once your SAP talks HTTP. that specifically when submitting a SOAP request to the SAP system. Secondly. the use of WebAS allows you to access your SAP ERP system in the same manner and use the same utilities as you would to access any other online location or Web server. Another benefit of SAP HTTP: your development and deployment team will also avoid endless discussions with the Basis and security administration team. and being dependent on SAP-provided utilities and documentation during development. www. until the HTTP classes and ABAP/Objects technologies mature. allows a smoother communication with other internal TCP/IP applications.7: • SAP Enterprise will allow you to easily design browser access to SAP for Intranet solutions. But.SAPtips. without any special preparation of the client. you can still continue to use SAP HTTP and traditional RFC methods. Linux. Even if you're running on WebAS.7.7 is the ideal platform for SAP Web services. I recommend continuing to use SAP HTTP and RFC. you do not need to be running WebAS or 4. not the HTTP classes of WebAS. When you decide to activate HTTP communication with SAP. because they don't involve deploying SAPGUI on every workstation. including your own Internet Web server. Every proprietary standard used in your server landscape will likely explode your training and planning costs. Copyright © 2003 by Klee Associates. These solutions are becoming more and more popular. Although programming RFC access is relatively easy. what is my final assessment in terms of the best approach to Web services? My current recommendation.7 to start using Web services in your application environment. keep in mind. This takes the discussion away from technical complexities and allows for a more strategic focus. And. You have to consider: • • • • deployment and registering of the drivers rebooting the box after installation allowing for the proper security mechanisms hat testing your application may interfere with existing applications and even bring them down. deploying an RFC application requires that the client computer has the necessary RFC drivers installed. and UNIX). is that WebAS and Enterprise 4. there are other arguments for upgrading your SAP ERP system to SAP Enterprise 4. Whereas having RFC access means deploying the necessary drivers on the client computer.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions However. Java on Windows.

and explain why the “Clash of the Frameworks” will end in favor of the newcomer SAP WebAS and ABAP—at least for SAP users.NET. and sophisticated change and deployment management. Thanks to the strong transaction management. In the next white paper. the database integration. we will have to clearly distinguish between the SAP ERP suite—which is a class of its own anyway— and the SAP kernel technology. has matured into one of the leading competitors in the enterprise application server market. and SAP WebAS. SAP WebAS has already become a serious contender to the established virtual machine frameworks for enterprise application servers. From now on. www. Inc. with the addition of HTTP service to its kernel. Microsoft.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions SAP As the Universal Development Platform It is already pretty obvious that SAP. namely WebSphere J2EE.SAPtips. Copyright © 2003 by Klee Associates. currently marketed as the NetWeaver WebAS. I shall assess the strengths and weaknesses of the three major development and runtime environments.com Page 26 . the fully integrated development workbench.

Axel has been nicknamed by his colleagues as the “Red Adair” of SAP projects. Axel's SAP experience stems back from the good old R/2 days and he is an expert on SAP’s NetWeaver technology and any kind of ABAP development. SAP AG software referenced on this site is furnished under license agreements between SAP AG and its customers and can be used only within the terms of such agreements. Klee Associates. SAP AG and mySAP are registered trademarks of SAP AG. Inc. A speaker of several languages.com Page 27 .SAPtips. and this publication are not affiliated with or endorsed by SAP AG. Any use of this documentation is at the risk of the user. Known for his intensive and successful trouble-shooting experience. he has always worked with cutting edge technologies. this document may include technical or other inaccuracies or typographical errors. a Germanbased enterprise specializing in coaching SAP and EAI project teams and advising IT management on implementation issues. Axel has been in the IT business since 1984.How to Develop Web Services in WebASPart 2: Design your own Web services with ABAP HTTP Extensions Axel Angeli is a senior SAP and EAI advisor and principal of logosworld. www.” The information in our publications and on our Website is the copyrighted work of Klee Associates. Axel specializes in coaching and leading large multi-national teams on complex projects with heterogeneous platforms and international rollouts. Copyright © 2003 by Klee Associates. Inc. IDocs.com. reserves the right to make changes without prior notice. NO AFFILIATION: Klee Associates. ALE and Interfaces. NO WARRANTY: This documentation is delivered as is. He is the author of the best-selling tutorial “The SAP R/3 Guide to EDI. All other company and product names used herein may be trademarks or registered trademarks of their respective owners. makes no warranty as to its accuracy or use. Inc. Inc. Inc. and is owned by Klee Associates. and Klee Associates. Inc. and throughout his career. Although we make every good faith effort to ensure accuracy.