Skip to Content

Introduction

This document is prepared to give a brief idea that how we can make connect the SAP to JAVA without any Third party tool (PI). The document contains details of “Sending the Files attachments from ABAP to JAVA Web Services through Http” and also shows necessary steps with pre-codes.

Purpose

The SAP system needs to send some text/files to the JAVA service(s) and in response the SAP system will get the output whatever the JAVA calculated.

There are several ways of doing this:

  • PI – By setting the Proxy Settings at the ABAP Side
  • RFC/BAPI – by Creating the RFM under  Registered Server Program
  • Distributing HTTP Requests using the SAP

We have finalized the HTTP Request way to communicate to JAVA, as no other dependencies are there in such case.

From the JAVA side, we have used Rest Web Service ,it could be developed on SOAP as well.

Why HTTP Client is used

The Module is developed in SAP ABAP without using any other third tool like PI or by creating RFC but consumed the Java Web Services (SOAP) with ABAP using HTTP.

There is a class “CL_HTTP_CLIENT” provided by SAP where systems includes more functionality than simple retrieval, including search, front-end update, and annotation. HTTP allows an open-ended set of methods and headers that indicate the purpose of a request.

It builds on the discipline of reference provided by the Uniform Resource Identifier (URI), as a location (URL) , for indicating the resource to which a method is to be applied. Messages are passed in a format similar to that used by Internet mail as defined by the Multipurpose Internet Mail Extensions (MIME).

HTTP is also used as a generic protocol for communication between user agents and proxies/gateways to other Internet systems, including those supported by the SMTP, NNTP, FTP, Gopher, and WAIS protocols. In this way, HTTP allows basic hypermedia access to resources available from diverse applications.

Connection and Data Transfer to the JAVA Web Services

The HTTP protocol is a request/response protocol. A client sends a request to the server in the form of a request method, URI, and protocol version, followed by MIME-like message containing request modifiers, client information, and possible body content over a connection with a server.

The server responds with a status line, including the message’s protocol version and a success or error code, if Success (200 else code could be anything), followed any possible entity-body content (if any), Which SAP has to interpret and manipulate accordingly in the form of Internal tables/Strings.

Establish the connection and Data Transfer (with Code)

  • Consume URL: This is to locate network resources.

http_URL = “http:” “//” host [ “:” port ] [ abs_path

  1. http: is a scheme to locate network path
  2. host: Server Address basically the System IP where the Web service is Created
  3. port: if the Port is empty ,default it will take 80
  4. abs path : is the Web service Name

Example of general URL: http://9.122.28.176:8080/Load/rest/Upload

DATA: l_http_client TYPE REF TO if_http_client,                              

fs_str type string.

CALL METHOD cl_http_client=>create_by_url
EXPORTING
  url = fs_str
IMPORTING
  client = l_http_client
EXCEPTIONS
  argument_not_found = 1
  plugin_not_active      = 2
  internal_error             = 3
  OTHERS                   = 4.

       

  • Data Request:

CALL METHOD l_http_client->request->set_header_field
EXPORTING
name = ‘~request_method’
  value = ‘POST’.

     

  • Protocol Version:

   CALL METHOD l_http_client->request->set_header_field
EXPORTING
  name = ‘~server_protocol’
  value = ‘HTTP/1.1’.

      

CALL METHOD l_http_client->request->set_header_field (
  name = ‘Content-Type’
  value = ‘text/xml; charset=utf-8’).

  • Multipart data:
             CALL METHOD l_http_client->request->if_http_entity~set_content_type
EXPORTING
content_type = 'multipart/form-data'.
   
 
        part = l_http_client->request->if_http_entity~add_multipart ( ).
 
  
            CALL METHOD part->set_header_field
EXPORTING
name = 'content-disposition'
value = ‘form-data; name="content”; filename=testfile.csv’.
 
           Note: Here the testfile.csv is the filename which is having Contents are passing to JAVA.
 
  • Convert the Contents(data)

    Note: the internal table will be created, where the data is stored in X-String Format

     

  • Set Contents: Transfer the Data Contents

CALL METHOD part->set_data
EXPORTING
  data   = it_data
  offset = 0.

  • Send Data:

CALL METHOD l_http_client->send
EXPORTING
  timeout                    = 200
EXCEPTIONS
  http_communication_failure = 1
   http_invalid_state         = 2
   http_processing_failed     = 3
  OTHERS                     = 4. 

                            Note: Timeout is the time (in seconds) ,default set 200

  • Receive:

CALL METHOD l_http_client->receive
EXCEPTIONS
  http_communication_failure = 1
  http_invalid_state         = 2
  http_processing_failed     = 3.

  • Check Status:

CALL METHOD l_http_client->response->get_status
IMPORTING
  code = l_code

.

          Note:  If the l_code (return Status Code) is 200, data successfully posted.

                  Otherwise there are relevant values for other Error Code like.

201-Created, 202-Accpeted, 400-Bad Request, 401-unauthorized, 403-Forbidden, 405-Method not   allowed, 500-Internal Server Error, 503-Service Unavailable, and 505-Http Version not supported

Fetch the data from the JAVA Web Server

Once the Data Transfer is done successfully and we got the response 200 (means OK).Its time to read the Contents which Java Server is sending may be it’s an algorithm performed result or just a message that entirely depends on the Business Requirement.

There are two ways of fetching the results:

  1. Synchronous
  2. Asynchronous
  1. Synchronous: If while designing the System, its decided the JAVA will send the result immediately without any delay then we have to follow this step:

Output = l_http_client->response->get_cdata ( ).

This will get the Data into the string Output, which can be further use, manipulate into tables, display as the ALV report etc.

  1. Asynchronous: While designing the Module only, if it has decided the format is going to be asynchronous means the File/data will  be sending to the Java and later my other calls sometimes later ,the Output file will be fetched from the Java Server by:
  • Popping the Server using FM Gui_download
  • Reading it by another web service which is provided by the Java

Example of Code

An Example:

An Example where user has to display only selected records from one File.

*——————————————————————–*
*& Report Z_Data_Post_TO_JAVA

*&
*& ————————————————————————–*
& Program Name: Posting Data to Java

*& Program Desc: It will fetch the details on the basis of selection screen
*&                and send the details to the JAVA. *& Programmer   : Namita Sharma Panchal
*& Creation Date: 01.09.2012
*& Project      : XXXXXXXXXXXXXXXXXXXXXXXXXXX
*&———————————————————————*

REPORT  _Data_Post_TO_JAVA NO STANDARD PAGE HEADING LINE-SIZE 165 LINE-COUNT 30
MESSAGE-ID  zps.
data: fs_str type string,
fs_cnt type string.
data: l_http_Client type ref to if_http_client.
data:cnt type i.
*
MOVE ‘http://9.184.169.173:8080/Test123/fileupload‘ TO fs_str.

*  move l_http_client TO fs_cnt.

CALL METHOD cl_http_client=>create_by_url
EXPORTING
url                = fs_str
IMPORTING
client             = l_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active  = 2
internal_error     = 3
OTHERS             = 4.

CALL METHOD l_http_client->request->set_header_field
EXPORTING
name  = ‘~request_method’
value = ‘POST’.

*** Set SERVER PROTOCOL
CALL METHOD l_http_client->request->set_header_field
EXPORTING
name  = ‘~server_protocol’
value = ‘HTTP/1.1’.

*** Set CONTENT-TYPE

CALL METHOD l_http_client->request->set_header_field (
name = ‘Content-Type’
value = ‘text/xml; charset=utf-8’).

DATA: file_name TYPE string.
*** SEND TO JAVA

file_name = ‘C:\Des_test_file.csv’.

  DATA: part TYPE REF TO if_http_entity.
DATA: l_value TYPE string, l_val TYPE string.

CALL METHOD l_http_client->request->if_http_entity~set_content_type
EXPORTING
content_type = ‘multipart/form-data’.

part = l_http_client->request->if_http_entity~add_multipart( ).
CALL METHOD part->set_header_field
EXPORTING
name =’content-disposition’
value      =’form-data; name=”event”‘.

CALL METHOD part->append_cdata
EXPORTING
data = ‘XXXXX’.

part = l_http_client->request->if_http_entity~add_multipart( ).

CONCATENATE ‘form-data; name=”content”;’
‘filename=’ INTO l_value SEPARATED BY space.

MOVE   ‘ Des_test_file.csv’ TO l_val.
CONCATENATE l_value l_val INTO l_value.

CALL METHOD part->set_header_field
EXPORTING
name  = ‘content-disposition’
value = l_value.

data: len type i,
it_fin1 TYPE w3mimetabtype.

CALL FUNCTION ‘GUI_UPLOAD’
EXPORTING
filename   = file_name
filetype   = ‘BIN’
IMPORTING
filelength = len
TABLES
data_tab   = it_fin1.
IF sy-subrc <> 0.
ENDIF.

data: it_data TYPE xstring.
CALL FUNCTION ‘SCMS_BINARY_TO_XSTRING’
EXPORTING
input_length = len
IMPORTING
buffer       = it_data
TABLES
binary_tab   = it_fin1.

CALL METHOD part->set_data
EXPORTING
data   = it_data
offset = 0.

CALL METHOD l_http_client->send
EXPORTING
timeout                    = 200
EXCEPTIONS
http_communication_failure = 1
http_invalid_state         = 2
http_processing_failed     = 3
OTHERS                     = 4.

CALL METHOD l_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state         = 2
http_processing_failed     = 3.

data:l_Code type sy-subrc.

  CALL METHOD l_http_client->response->get_status
IMPORTING
code = l_code.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply