NW ABAP Gateway (OData), SAP ABAP Development

Download ZIP Package through NetWeaver Gateway with Odata

In this blog, I will show you how to download a ZIP package stored on an application server via Odata call in very simple steps.

I have tried not to add any additional logic apart from downloading the ZIP package from Odata service.

1. Create a table where we will have package name stored.

Table

2. Create an Odata service.

Odata service

3. Create an entity with entity set.

Entity

4. Add table name to ABAP structure and also mark Media checkbox as true. Do remember to mark the Media as true.

Add Structure

5. Import Properties/fields from the custom table.

Import Properties

6. Now its time to generate the service.

Generate Service

7. Go to DPC extension class, redefine GET_ENTITYSET method and add logic to get data from the custom table. This is required so that we can fetch data from table to get the package name.

DPC_EXT Fetch Data

8. In the DPC extension, redefine method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM and add the logic to get filename, convert it to xstring and pass it back to Odata.

METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream.

   TYPES:
     "! <p class="shorttext synchronized" lang="en">Media Type Information</p>
     BEGIN OF ty_s_media_resource.
       INCLUDE TYPE /iwbep/if_mgw_core_srv_runtime=>ty_s_media_resource.
   TYPES:END OF ty_s_media_resource .

   TYPES: BEGIN OF ty_package,
            docnr    TYPE zzdocnr,
            filename TYPE zzfilename,
          END OF ty_package.

   DATA:ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
        ls_package TYPE ty_package,
        ls_stream  TYPE ty_s_media_resource,
        lv_docnr   TYPE zzdocnr,
        lo_meco    TYPE REF TO /iwbep/if_message_container,
        ls_lheader TYPE ihttpnvp,
        lv_xstring TYPE xstring.

   READ TABLE it_key_tab INTO ls_key_tab INDEX 1.
   IF sy-subrc EQ 0.
     lv_docnr = ls_key_tab-value.
   ENDIF.

   SELECT SINGLE docnr,
               filename
     FROM zzip_package INTO @ls_package
     WHERE docnr = @lv_docnr.


   DATA(lv_path) = '.\SamplePackage.zip'.
   OPEN DATASET lv_path FOR INPUT IN BINARY MODE.    "#EC CI_USE_WANTED
   IF sy-subrc = 0.
     READ DATASET lv_path INTO lv_xstring.           "#EC CI_USE_WANTED
   ENDIF.

   CLOSE DATASET lv_path.                            "#EC CI_USE_WANTED

   IF sy-subrc = 0.
     ls_stream-mime_type = 'application/x-zip-compressed'. "mime type
     ls_stream-value     = lv_xstring.             "content
     ls_lheader-name     = 'Content-Disposition'(001).

     CONCATENATE 'attachment; filename="'(002)
                 ls_package-filename
                 '";'
            INTO ls_lheader-value.

     set_header( is_header = ls_lheader ).

     ls_lheader-name  = 'Content-Length'(003).

     DATA(lv_len)     = xstrlen( ls_stream-value ).
     ls_lheader-value = lv_len.
     set_header( is_header = ls_lheader ).
     copy_data_to_ref( EXPORTING is_data = ls_stream
                       changing  cr_data = er_stream ).

   ELSE. "error when file not found

     lo_meco = mo_context->get_message_container( ).
     lo_meco->add_message_text_only(
     EXPORTING
     iv_msg_type               =  'E'
     iv_msg_text               =  'File Not Found'(004)  " Message Text
     iv_add_to_response_header = abap_true " Flag for adding or not the message to the response header
     ).

   ENDIF.

 ENDMETHOD.

9. Now redefine the method DEFINE of MPC extension class. Add your Entity name and field which contains the zip package.

METHOD define.
  super->define( ).

  DATA: lo_entity   TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
        lo_property TYPE REF TO /iwbep/if_mgw_odata_property.

  lo_entity = model->get_entity_type( iv_entity_name = 'Package' ).

  IF lo_entity IS BOUND.
    lo_property = lo_entity->get_property( iv_property_name = 'Filename').
    lo_property->set_as_content_type( ).
  ENDIF.

ENDMETHOD.

10. Now it’s time to register the Odata service from /IWFND/MAINT_SERVICE tcode.

11. Once, service is register let’s see if metadata is loaded properly.

Metadata

12. Lets us now upload the zip file to application server.

Upload Package

13. Contents of the package. There are 52 notepad files in it.

Package contents

14. Let us execute the Odata service to get the ZIP Package. Select HTTP method as GET and pass below URI.

/sap/opu/odata/sap/ZZIPPACKAGE_DOWNLOAD_SRV/PackageSet('0001')/$value

15. Below is the HTTP response.

Response

16. Here come the final step, click on the “Response in Browser” and let us see the uploaded ZIP package.

Downloaded ZIP Package