[ABAP] Infos zu ICF-Service lesen / Prüfen, ob SSO2-Service aktiv

* Transaktion: SICF
* Tabelle: ICFSERVICE

* Service-Name, hier z.B. MYSSOCNTL (SSO2-Service)
PARAMETERS: p_srv TYPE icfservice-icf_name DEFAULT 'MYSSOCNTL'.

START-OF-SELECTION.

  TRY.
* Suche im Baum der ICF-Services im Internet-Kommunikations-Framework
      SELECT SINGLE icfparguid
        INTO @DATA(lv_icfparguid)
        FROM icfservice
        WHERE icf_name = @p_srv.

      IF sy-subrc = 0.
        DATA: lv_hostnr	TYPE icfhostnum.       " Nummer eines virtuellen Hosts
        DATA: it_serv_info TYPE icfservtbl.    " Tabelle von ICF-Services und deren zugehörige Handler
        DATA: lv_url TYPE string.              " URL zu Service
        DATA: lv_active	TYPE c.                " Service aktiv oder inaktiv
        DATA: lv_icfdocu TYPE icfdocu.         " Documentation der ICFSERVICE Einträge
        DATA: lv_icf_custstr TYPE icf_custstr. " ICF: String für customizingdaten der Anwendung
        DATA: lv_serv_sign TYPE c.             " Kz, um welche Art v. Service (Host, int Alias, Service)
        DATA: lv_host_info TYPE icfvirhost.    " Internet Communication Framework: Virtuelle Hosts

* Liefert Informationen zu ICF-Service
        cl_icf_tree=>if_icf_tree~get_info_from_serv( EXPORTING
                                                       icf_name        = p_srv
                                                       icfparguid      = lv_icfparguid
                                                       authority_check = abap_false
                                                     IMPORTING
                                                       hostnr          = lv_hostnr
                                                       serv_info       = it_serv_info
                                                       url             = lv_url
                                                       active          = lv_active
                                                       icfdocu         = lv_icfdocu
                                                       icf_custstr     = lv_icf_custstr
                                                       serv_sign       = lv_serv_sign
                                                       host_info       = lv_host_info
                                                   ).

        cl_demo_output=>write_data( lv_hostnr ).

        IF lines( it_serv_info ) > 0.
          DATA(lv_srv_info) = it_serv_info[ 1 ].
          cl_demo_output=>write_data( lv_srv_info-service ).
          cl_demo_output=>write_data( lv_srv_info-handlertbl ).
          cl_demo_output=>write_data( lv_srv_info-pathfields ).
        ENDIF.

        cl_demo_output=>write_data( lv_url ).
        cl_demo_output=>write_data( lv_active ).
        cl_demo_output=>write_data( lv_icfdocu ).
        cl_demo_output=>write_data( lv_icf_custstr ).
        cl_demo_output=>write_data( lv_serv_sign ).
        cl_demo_output=>write_data( lv_host_info ).

        DATA: lv_urlsuffix TYPE icfredurl.    " Rest einer Url nach ICF-Service
        DATA: lv_icfnodguid TYPE icfnodguid.  " Nodguid des letzen service im Pfad
        DATA: lv_icf_name TYPE icfname.       " Name des letzen Service im ICF-Baum
        DATA: lv_int_alias TYPE icfalias.     " Kein Service sondern interner Alias
        DATA: lv_ext_alias TYPE icfalias.     " Kein Service sondern externer Alias
        DATA: lv_icfactive TYPE icfactive.    " Service ist aktiv
        DATA: lv_icfaltnme TYPE icfaltnme.    " Alternative Namensgebung

* Ermittelt aus einer URL dem letzten Service im ICF-Baum
        cl_icf_tree=>if_icf_tree~service_from_url( EXPORTING
                                                    url             = lv_url
                                                    hostnumber      = lv_hostnr
                                                    authority_check = abap_false
                                                   IMPORTING
                                                    urlsuffix  = lv_urlsuffix
                                                    icfnodguid = lv_icfnodguid
                                                    icf_name   = lv_icf_name
                                                    int_alias  = lv_int_alias
                                                    ext_alias  = lv_ext_alias
                                                    icfactive  = lv_icfactive
                                                    icfaltnme  = lv_icfaltnme
                                                  ).

        cl_demo_output=>write_data( lv_urlsuffix ).
        cl_demo_output=>write_data( lv_icfnodguid ).
        cl_demo_output=>write_data( lv_icf_name ).
        cl_demo_output=>write_data( lv_int_alias ).
        cl_demo_output=>write_data( lv_ext_alias ).
        cl_demo_output=>write_data( lv_icfactive ).
        cl_demo_output=>write_data( lv_icfaltnme ).

* ist Service aktiv
        DATA(lv_is_active) = cl_icf_tree=>is_service_active( nodeguid	= lv_icfnodguid
*                                                             URL =
*                                                             HOSTNAME =
                                                           ).

        cl_demo_output=>write_data( lv_is_active ).

* HTML-Code vom Demo-Output holen
        DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
        cl_abap_browser=>show_html( EXPORTING
                                      title        = 'ICF-Service-Info'
                                      html_string  = lv_html
                                      container    = cl_gui_container=>default_screen ).

*         cl_gui_container=>default_screen erzwingen
        WRITE: space.
      ENDIF.
    CATCH cx_root INTO DATA(e_txt).
  ENDTRY.

[ABAP] HTML im Browser-Fenster anzeigen

* Variante 1 (cl_abap_browser)
* Dummy Screen
SELECTION-SCREEN BEGIN OF SCREEN 100.
SELECTION-SCREEN END OF SCREEN 100.

* Daten holen
SELECT * FROM mara INTO TABLE @DATA(it_mara).

* Browserfenster mit HTML-Code im cl_gui_container=>default_screen anzeigen
cl_abap_browser=>show_html( html_string = cl_demo_output=>get( it_mara )
                            container   = cl_gui_container=>default_screen ).

* Toolbar ausblenden
cl_abap_list_layout=>suppress_toolbar( ).

* leeres Dynpro anzeigen und Ausgabe von cl_gui_container=>default_screen erzwingen
CALL SELECTION-SCREEN 100.

[ABAP] Konvertierung HTML-String nach HTML-itab

* Konvertierungsklasse HTML-String nach HTML-itab
CLASS lcl_html_str_to_itab DEFINITION FINAL.
  PUBLIC SECTION.

* Zeilenlänge für HTML-itab
    CONSTANTS: co_html_length TYPE i VALUE 1024.
* HTML-itab
    TYPES: ty_html_line TYPE c LENGTH co_html_length.
    TYPES: ty_it_html TYPE STANDARD TABLE OF ty_html_line WITH DEFAULT KEY.

    CLASS-METHODS:
      convert
        IMPORTING
                  i_html_str        TYPE string
        RETURNING VALUE(rv_it_html) TYPE ty_it_html.

ENDCLASS.

CLASS lcl_html_str_to_itab IMPLEMENTATION.
**********************************************************************
* Konvertiert HTML-String nach HTML-itab
**********************************************************************
* i_html_str -> HTML-String
* rv_it_html <- HTML-itab
**********************************************************************
  METHOD convert.

    DATA(lv_right) = i_html_str.

    WHILE strlen( lv_right ) >= co_html_length.
* linker Teil
      DATA(lv_left) = substring( val = lv_right off = 0 len = co_html_length ).
      APPEND lv_left TO rv_it_html.

* Rest
      lv_right = substring( val = lv_right off = co_html_length len = strlen( lv_right ) - co_html_length ).
    ENDWHILE.

* Rest anfügen
    IF strlen( lv_right ) > 0.
      APPEND lv_right TO rv_it_html.
    ENDIF.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
* Daten holen
  SELECT * FROM tnapr INTO TABLE @DATA(it_tnapr).

* HTML-Daten aus itab generieren
  DATA(lv_html) = cl_demo_output=>get( it_tnapr ).

* HTML-String nach HTML itab konvertieren
  DATA(it_html) = lcl_html_str_to_itab=>convert( lv_html  ).

* HTML-Viewer
  DATA(o_hv) = NEW cl_gui_html_viewer( parent = cl_gui_container=>default_screen ).
* URL holen
  DATA: lv_url type swk_url.

  o_hv->load_data( IMPORTING
                     assigned_url = lv_url
                   CHANGING
                     data_table   = it_html ).

* Daten anzeigen
  o_hv->show_url( url      = lv_url
                  in_place = abap_true ). " im SAP-Fenster (abap_true) oder im Externen Browser (abap_false) anzeigen

* cl_gui_container=>default_screen erzwingen
  WRITE: space.

[ABAP] Datenausgabe mit cl_demo_output

Demoprogramme

* DEMO_USAGE_OUTPUT_GET
* DEMO_USAGE_OUTPUT_STATIC
* DEMO_USAGE_OUTPUT_INSTANCE
* DEMO_USAGE_OUTPUT_STREAM

Text mit Abschnitten

* Abschnitt 1
cl_demo_output=>begin_section( '1. Überschrift' ).
cl_demo_output=>begin_section( '1.1 Überschrift' ).
* Text
cl_demo_output=>write_text( 'Text 1.1' ).
cl_demo_output=>write( 'Text 1.1 non proportional' ).
cl_demo_output=>end_section( ).
cl_demo_output=>end_section( ).
* Abschnitt 2
cl_demo_output=>begin_section( '2. Überschrift' ).
cl_demo_output=>begin_section( '2.1 Überschrift' ).
* Text
cl_demo_output=>write_text( 'Text 2.1' ).
cl_demo_output=>write( 'Text 2.1 non proportional' ).
cl_demo_output=>end_section( ).
cl_demo_output=>end_section( ).
* alles anzeigen
cl_demo_output=>display( ).

Überschrift

cl_demo_output=>next_section( 'Überschrift' ).

Variablen, Strukturen, Tabellen

TYPES: BEGIN OF ty_struct,
         f1 TYPE string,
         f2 TYPE i,
       END OF ty_struct.

TYPES: ty_it_tab TYPE STANDARD TABLE OF ty_struct WITH DEFAULT KEY.

DATA(lv_struct) = VALUE ty_struct( f1 = 'Field1' f2 = 1 ).
DATA(it_tab) = VALUE ty_it_tab( ( f1 = 'T1' f2 = 1 )
                                ( f1 = 'T2' f2 = 2 )
                                ( f1 = 'T3' f2 = 3 ) ).

cl_demo_output=>write_data( value = -100         name = 'Zahl' ).
cl_demo_output=>write_data( value = 'ein String' name = 'Text' ).
cl_demo_output=>write_data( value = lv_struct    name = 'Struct' ).
cl_demo_output=>write_data( value = it_tab       name = 'Tab' ).
* alles anzeigen
cl_demo_output=>display( ).

Trennlinie

cl_demo_output=>write_text( 'Oben' ).
cl_demo_output=>line( ).
cl_demo_output=>write_text( 'Unten' ).
* alles anzeigen
cl_demo_output=>display( ).

HTML

* HTML hinzufügen
cl_demo_output=>write_html( '<b>Text bold</b>' ).
* HTML anzeigen
cl_demo_output=>display_html( '<i>Text italic</i>' ).

XML

DATA: lv_xml TYPE string.
...
cl_demo_output=>display_xml( lv_xml ).

JSON

DATA: lv_json TYPE xstring.
...
cl_demo_output=>display_json( lv_json ).

Plaintext ausgeben

cl_demo_output=>set_mode( cl_demo_output=>text_mode ).
cl_demo_output=>write_data( value = it_tab name = 'Tab' ).
cl_demo_output=>display( ).

oder

SELECT * FROM tnapr INTO TABLE @DATA(it_tnapr).
cl_demo_output=>new( 'TEXT' )->display( it_tnapr ).

HTML-Code generieren lassen

SELECT * FROM mara INTO TABLE @DATA(it_mara).
IF sy-subrc = 0.
  cl_demo_output=>write_data( it_mara ).
* HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).
* Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING
                                title        = 'Daten aus der Tabelle MARA'
                                html_string  = lv_html
                                container    = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
  WRITE: space.
ENDIF.

Links

[ABAP] HTTP-Client Anfrage (cl_http_client)

Testprogramm: RSHTTP20

Variante 1 (URL)

TRY.
* URL
    DATA(lv_url) = |https://www.google.de/|.

    DATA: o_client TYPE REF TO if_http_client.

* Client-Objekt erzeugen
    cl_http_client=>create_by_url( EXPORTING
                                     url     = lv_url
                                   IMPORTING
                                     client  = o_client ).

    IF o_client IS BOUND.
* Anmeldedaten übermitteln
      o_client->authenticate( username = ''
                              password = '' ).

* Logon-Popup ein- bzw. ausschalten
      o_client->propertytype_logon_popup = o_client->co_enabled.

* HTTP-Prtotokoll-Version
      o_client->request->set_version( version = if_http_request=>co_protocol_version_1_1 ).
* HTTP-Method
      o_client->request->set_method( if_http_request=>co_request_method_get ).

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

* Cookies akzeptieren
      o_client->propertytype_accept_cookie = if_http_client=>co_enabled.
* Kompression akzeptieren
      o_client->propertytype_accept_compress = if_http_client=>co_enabled.

* HTTP GET senden, evtl. Timeout angeben
      o_client->send( timeout = if_http_client=>co_timeout_default ).

* Response lesen
      o_client->receive( ).

      DATA: lv_http_status TYPE i.
      DATA: lv_status_text TYPE string.

* HTTP Return Code holen
      o_client->response->get_status( IMPORTING
                                        code   = lv_http_status
                                        reason = lv_status_text ).

      WRITE: / 'HTTP_STATUS_CODE:', lv_http_status.
      WRITE: / 'STATUS_TEXT:', lv_status_text.

* Header-Daten der Übertragung
      WRITE: / 'HEADER_FIELDS:'.
      DATA(it_header_fields) = VALUE tihttpnvp( ).
      o_client->response->get_header_fields( CHANGING fields = it_header_fields ).

      LOOP AT it_header_fields ASSIGNING FIELD-SYMBOL(<f>).
        WRITE: / '[', <f>-name, ']', <f>-value.
      ENDLOOP.

* Cookies holen
      WRITE: / 'COOKIES:'.
      DATA(it_cookies) = VALUE tihttpcki( ).
      o_client->response->get_cookies( CHANGING cookies = it_cookies ).

      LOOP AT it_cookies ASSIGNING FIELD-SYMBOL(<c>).
        WRITE: / '[', <c>-name, ']', <c>-value, <c>-xdomain, <c>-path, <c>-secure, <c>-expires.
      ENDLOOP.

* vollständige HTTP Nachricht lesen
      DATA(lv_raw_message) = o_client->response->get_raw_message( ).
      DATA: lv_str_msg TYPE string.

* xstring -> string
      DATA(o_conv_r) = cl_abap_conv_in_ce=>create( input    = lv_raw_message
                                                   encoding = 'UTF-8' ).

      o_conv_r->read( IMPORTING data = lv_str_msg ).

      WRITE: / 'RAW MESSAGE:', lv_str_msg.

* Wenn Status 200 (Ok)
      IF lv_http_status = 200.
* HTTP Body als Character-Daten
        DATA(lv_result) = o_client->response->get_cdata( ).

        WRITE: / 'CDATA:'.
        WRITE: / lv_result.
      ENDIF.

* HTTP Connection schließen
      o_client->close( ).
    ENDIF.
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (HTTP-Alias)

* mit SM59 einrichten
DATA: lv_http_dest TYPE rfcdest VALUE 'HTTP_ALIAS'.

DATA: o_client TYPE REF TO if_http_client.

DATA: lv_http_rc TYPE i.
DATA: lv_reason TYPE string.

TRY.
    cl_http_client=>create_by_destination( EXPORTING
                                             destination = lv_http_dest
                                           IMPORTING
                                             client      = o_client ).

    o_client->request->set_method( if_http_request=>co_request_method_get ).

    o_client->send( ).

    o_client->receive( ).

    o_client->response->get_status( IMPORTING
                                      code   = lv_http_rc
                                      reason = lv_reason ).

    IF lv_http_rc = 200.
      DATA(lv_xml) = o_client->response->get_cdata( ).

      cl_abap_browser=>show_xml( xml_string = lv_xml
                                 title      = 'XML-Daten'
                                 size       = cl_abap_browser=>large ).
    ELSE.
      WRITE: / 'Fehler: ', lv_http_rc.
    ENDIF.

    o_client->close( ).

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] Interne Tabelle in XML wandeln und anzeigen

Variante 1 (Klassen)

DATA it_itab TYPE STANDARD TABLE OF mara.

SELECT * FROM mara INTO TABLE it_itab UP TO 10 ROWS.

DATA(o_writer_itab_xml) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_xml10 ).
CALL TRANSFORMATION id SOURCE values = it_itab RESULT XML o_writer_itab_xml.
DATA(itab_xml) = cl_abap_codepage=>convert_from( o_writer_itab_xml->get_output( ) ).

cl_abap_browser=>show_xml( xml_string = itab_xml
                           title      = 'Überschrift'
                           size       = cl_abap_browser=>large ).

Variante 2 (FuBa)

DATA lv_xml TYPE xstring.
DATA lo_dom TYPE REF TO if_ixml_document.
DATA it_itab TYPE STANDARD TABLE OF mara.

SELECT * FROM mara INTO TABLE it_itab UP TO 10 ROWS.

CALL TRANSFORMATION id SOURCE data_node = it_itab RESULT XML lv_xml.

CALL FUNCTION 'SDIXML_XML_TO_DOM'
  EXPORTING
    xml           = lv_xml
  IMPORTING
    document      = lo_dom
  EXCEPTIONS
    invalid_input = 1
    OTHERS        = 2.

IF sy-subrc = 0.
  CALL FUNCTION 'SDIXML_DOM_TO_SCREEN'
    EXPORTING
      document    = lo_dom
      title       = 'XML-Doc'
      encoding    = 'UTF-8'
    EXCEPTIONS
      no_document = 1
      OTHERS      = 2.

  IF sy-subrc = 0.

  ENDIF.
ENDIF.

[ABAP] URL im SAP-Browser als PopUp-Fenster anzeigen

Beispiel 1

cl_abap_browser=>show_url( url          = 'www.google.de'
                           title        = 'ABAP-Browser'
                           size         = cl_abap_browser=>large
                           modal        = abap_true
                           printing     = abap_false
                           buttons      = abap_true
                           format       = cl_abap_browser=>landscape
                           position     = cl_abap_browser=>topleft
                           context_menu = abap_false ).

Beispiel 2

* HTML-Code generieren lassen
SELECT * FROM tnapr INTO TABLE @DATA(it_tnapr).
DATA(lv_html) = cl_demo_output=>get( it_tnapr ).

* HTML-Code anzeigen
cl_abap_browser=>show_html( html_string  = lv_html
                            size         = cl_abap_browser=>xlarge
                            format       = cl_abap_browser=>landscape
                            context_menu = abap_true
                            buttons       = cl_abap_browser=>navigate_html ).

[ABAP] XML-Dokument aufbauen / erstellen und rendern

TRY.
* Interface
    DATA(o_ixml) = cl_ixml=>create( ).

* Document
    DATA(o_doc) = o_ixml->create_document( ).

* Root definieren
    DATA(o_root) = o_doc->create_simple_element_ns( prefix = 'asx'
                                                    name = 'list'
                                                    parent = o_doc ).

* zwei Root-Element-Attribute hinzufügen
    o_root->set_attribute_ns( name =  'asx'
                              prefix = 'xmlns'
                              value = 'http://www.google.com/xml' ).

    o_root->set_attribute_ns( name =  'version'
                              value = '1.0' ).

* Node1 unterhalb Root definieren
    DATA(o_node1) = o_doc->create_simple_element_ns( prefix = 'asx'
                                                     name = 'names'
                                                     parent = o_root ).

* Node2 zur Node1 hinzufügen
    DATA(o_node2) = o_doc->create_simple_element_ns( name = 'name'
                                                     value = 'ABC'
                                                     parent = o_node1  ).

* XML-String generieren
    DATA: xml TYPE string.

    DATA(o_stream) = o_ixml->create_stream_factory( )->create_ostream_cstring( xml ).
    o_ixml->create_renderer( document = o_doc
                             ostream = o_stream )->render( ).

* Anzeige im Browser
    cl_abap_browser=>show_xml( xml_string = xml
                               title      = 'Test XML'
                               size       = cl_abap_browser=>large ).

  CATCH cx_root INTO DATA(e_text).
    WRITE: / e_text->get_text( ).
ENDTRY.