Thursday, September 3, 2020

Enable SAP to communicate via https to AWS

 1. Get Client Certificate from https://www.amazontrust.com/repository/

    e.g. CN=Amazon Root CA 1,O=Amazon,C=US

    Show the PEM-Format and save it as amazon.pse

2. Open transaction STRUST and switch to change mode

   Double-click node "SSL-Client..." in tree

   Use Button "Import Certificate" (at the bottom) to import amazon.pse (saved in 1.)

   Use Button "In Liste aufnehmen" and save.


Test with:

REPORT zjso_http_request.

CONSTANTS:
  gc_proxy_host    TYPE string VALUE 'http-proxy.xxx.xxx',
  gc_proxy_service TYPE string VALUE '1234'.

CLASS lcl_test DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS do.
ENDCLASS.

START-OF-SELECTION.
  lcl_test=>do).

CLASS lcl_test IMPLEMENTATION.
  METHOD do.

    DATA:
      l_url           TYPE string,
      l_client        TYPE REF TO if_http_client,
      l_http_status   TYPE i,
      l_status_text   TYPE string,
      lt_cookie       TYPE tihttpcki,
      ls_cookie       TYPE ihttpcki,
      lt_header_field TYPE tihttpnvp,
      ls_header_field TYPE ihttpnvp,
      l_str_msg       TYPE string,
      l_raw_message   TYPE xstring,
      o_conv_r        TYPE REF TO cl_abap_conv_in_ce.

  
    l_url `http://dummy.restapiexample.com/api/v1/employees`.

    TRY.
        cl_http_client=>create_by_url(
          EXPORTING
            url           l_url
            proxy_host    gc_proxy_host
            proxy_service gc_proxy_service
          IMPORTING client  l_client
        ).
        CHECK l_client IS BOUND.

        " Anmeldedaten übermitteln
        " l_client->authenticate( username = 'abc' password = 'def' ).

        " Logon-Popup ein- bzw. ausschalten
        l_client->propertytype_logon_popup l_client->co_enabled.

        " HTTP-Prtotokoll-Version
        l_client->request->set_versionversion if_http_request=>co_protocol_version_1_1 ).

        " HTTP-Method
        l_client->request->set_methodif_http_request=>co_request_method_get ).

        " Header-Felder explizit setzen
*      l_client->request->set_header_field( name  = '~request_method' value = 'GET' ).
*      l_client->request->set_header_field( name  = 'Content-Type' value = 'text/xml; charset=utf-8' ).
*      l_client->request->set_header_field( name  = 'Accept' value = 'text/xml, text/html' ).

        " Cookies akzeptieren
        l_client->propertytype_accept_cookie if_http_client=>co_enabled.
        " Kompression akzeptieren
        l_client->propertytype_accept_compress if_http_client=>co_enabled.

        " HTTP GET senden, evtl. Timeout angeben
        l_client->send(
          EXCEPTIONS
            http_communication_failure  1
            http_invalid_state          2
            http_processing_failed      3
            OTHERS                      4
        ).
        IF sy-subrc <> 0.
          MESSAGE 'Exception nach send' TYPE 'E'.
        ENDIF.
        WRITE'Request send to'l_url.
        SKIP.

        " Response lesen
        l_client->receive(
         EXCEPTIONS
           http_communication_failure 1
           http_invalid_state         2
           http_processing_failed     3
           OTHERS                     4
        ).
        IF sy-subrc <> 0.
          WRITE 'Exception nach Receive' COLOR COL_NEGATIVE.
        ENDIF.

        " HTTP Return Code holen
        l_client->response->get_status(
          IMPORTING
            code   l_http_status
            reason l_status_text
        ).
        WRITE:
          / 'HTTP_STATUS_CODE:'l_http_status,
          / 'STATUS_TEXT:'l_status_text.
        SKIP.

        " Header-Daten der Response
        WRITE'HEADER FIELDS' COLOR COL_HEADING.
        l_client->response->get_header_fieldsCHANGING fields lt_header_field ).
        LOOP AT lt_header_field INTO ls_header_field.
          WRITE'  ['ls_header_field-name']  'ls_header_field-value.
        ENDLOOP.
        SKIP.

        " Cookies holen
        WRITE'COOKIES' COLOR COL_HEADING.
        l_client->response->get_cookiesCHANGING cookies lt_cookie ).
        LOOP AT lt_cookie INTO ls_cookie.
          WRITE'['ls_cookie-name']  'ls_cookie-valuels_cookie-xdomainls_cookie-pathls_cookie-securels_cookie-expires.
        ENDLOOP.
        SKIP.

        " vollständige HTTP Nachricht lesen
        " CHECK l_http_status = 200.
        l_raw_message l_client->response->get_raw_message).

        " xstring -> string
*        o_conv_r = cl_abap_conv_in_ce=>create( input = l_raw_message  encoding = 'UTF-8' ).
*        o_conv_r->read( IMPORTING data = l_str_msg ).
        cl_abap_conv_in_ce=>createinput l_raw_message  encoding 'UTF-8' )->readIMPORTING data l_str_msg ).
        WRITE'RAW MESSAGE'l_str_msg.
        SKIP.

        " HTTP Body als Character-Daten
        l_str_msg l_client->response->get_cdata).
        WRITE 'CDATA' COLOR COL_HEADING.
        WRITE / l_str_msg.
        SKIP.

        " HTTP Connection schließen
        l_client->close).

      CATCH cx_root INTO DATA(l_x).
        WRITE / l_x->get_textCOLOR COL_NEGATIVE.
    ENDTRY.

  ENDMETHOD.
ENDCLASS.

SAP ABAP: Determine Timezone for Plant

    DATA:       lt_tzone TYPE STANDARD TABLE OF tznzone WITH DEFAULT KEY,       l_tzone  TYPE tznzone.     " get time zone for plant   ...