[ABAP] Konstanten für gängige MIME-Typen (IF_REST_MEDIA_TYPE, CL_FXS_MIME_TYPES)

Variante 1 (IF_REST_MEDIA_TYPE)

Konstante                       MIME-Typ
--------------------------------------------------------------
gc_all                          '*/*'
gc_appl_all                     'application/*'
gc_appl_xml                     'application/xml'
gc_appl_xhtml_xml               'application/xhtml+xml'
gc_appl_atom_xml                'application/atom+xml'
gc_appl_atom_xml_feed           'application/atom+xml;type=feed'
gc_appl_atom_xml_entry          'application/atom+xml;type=entry'
gc_appl_atomcat_xml             'application/atomcat+xml'
gc_appl_atomsvc_xml             'application/atomsvc+xml'
gc_appl_opensearch_descr_xml    'application/opensearchdescription+xml'
gc_appl_all_xml                 'application/*+xml'
gc_appl_java                    'application/java'
gc_appl_java_archive            'application/java-archive'
gc_appl_json                    'application/json'
gc_appl_json_odata_verbose      'application/json;odata=verbose'
gc_appl_mac_binhex40            'application/mac-binhex40'
gc_appl_mathml_xml              'application/mathml+xml'
gc_appl_msword                  'application/msword'
gc_appl_octet_stream            'application/octet-stream'
gc_appl_onenote                 'application/onenote'
gc_appl_pdf                     'application/pdf'
gc_appl_postscript              'application/postscript'
gc_appl_rdf_xml                 'application/rdf+xml'
gc_appl_relax_ng_compact_synt   'application/relax-ng-compact-syntax'
gc_appl_rss_xml                 'application/rss+xml'
gc_appl_rtf                     'application/rtf'
gc_appl_sparql_results_json     'application/sparql-results+json'
gc_appl_sparql_results_xml      'application/sparql-results+xml'
gc_appl_trix                    'application/trix'
gc_appl_vnd_google_earthkmlxml  'application/vndgoogle-earthkml+xml'
gc_appl_vnd_google_earth_kmz    'application/vndgoogle-earthkmz'
gc_appl_vnd_mozilla_xul_xml     'application/vndmozillaxul+xml'
gc_appl_vnd_ms_cab_compressed   'application/vndms-cab-compressed'
gc_appl_vnd_ms_excel            'application/vndms-excel'
gc_appl_vnd_ms_powerpoint       'application/vndms-powerpoint'
gc_appl_vnd_ms_project          'application/vndms-project'
gc_appl_vnd_sun_wadl_xml        'application/vndsunwadl+xml'
gc_appl_voicexml_xml            'application/voicexml+xml'
gc_appl_zip                     'application/zip'
gc_audio_all                    'audio/*'
gc_audio_basic                  'audio/basic'
gc_audio_midi                   'audio/midi'
gc_audio_mpeg                   'audio/mpeg'
gc_image_all                    'image/*'
gc_image_bmp                    'image/bmp'
gc_image_gif                    'image/gif'
gc_image_jpeg                   'image/jpeg'
gc_image_png                    'image/png'
gc_image_svg_xml                'image/svg+xml'
gc_image_tiff                   'image/tiff'
gc_message_all                  'message/*'
gc_model_all                    'model/*'
gc_model_vrml                   'model/vrml'
gc_multipart_all                'multipart/*'
gc_multipart_form_data          'multipart/form-data'
gc_text_all                     'text/*'
gc_text_calendar                'text/calendar'
gc_text_css                     'text/css'
gc_text_csv                     'text/csv'
gc_text_html                    'text/html'
gc_text_javascript              'text/javascript'
gc_text_n_triples               'text/n-triples'
gc_text_n3                      'text/n3'
gc_text_plain                   'text/plain'
gc_text_tab_separated_values    'text/tab-separated-values'
gc_text_uri_list                'text/uri-list'
gc_video_all                    'video/*'
gc_video_mp4                    'video/mp4'
gc_video_mpeg                   'video/mpeg'
gc_video_quicktime              'video/quicktime'
gc_appl_www_form_url_encoded    'application/x-www-form-urlencoded'
gc_multipart_mixed              'multipart/mixed'
gc_appl_http                    'application/http'

Variante 2 (CL_FXS_MIME_TYPES)

Konstante                           MIME-Typ
--------------------------------------------------------------
CL_FXS_MIME_TYPES=>CO_BINARY        'application/octet-stream'
CL_FXS_MIME_TYPES=>CO_PDF           'application/pdf'
CL_FXS_MIME_TYPES=>CO_IMAGE_BITMAP  'image/x-ms-bmp'
CL_FXS_MIME_TYPES=>CO_IMAGE_PNG     'image/png'
CL_FXS_MIME_TYPES=>CO_IMAGE_GIF     'image/gif'
CL_FXS_MIME_TYPES=>CO_IMAGE_TIFF    'image/tiff'
CL_FXS_MIME_TYPES=>CO_IMAGE_JPEG    'image/jpeg'

[ABAP] Einfacher REST-Service (ohne OData)

Piyush Philip auf sap.com hat eine simple Möglichkeit beschrieben, wie man in ABAP einen REST-Service mit den entsprechenden CRUD-Methoden implementieren kann. Dazu ist im wesentlichen nur eine eigene Handler-Klasse zu implementieren, die von IF_HTTP_EXTENSION erbt. Diese Handler-Klasse wird von einem Service des ICF gerufen.

  1. SE80 -> Neue Klasse anlegen
  2. In der Klasse im Reiter Interfaces: vom Interface IF_HTTP_EXTENSION erben
  3. die Interface-Methode IF_HTTP_EXTENSION~HANDLE_REQUEST implementieren (implementiert nur REST, kein OData-Sevice!)
    METHOD if_http_extension~handle_request.
    
    * Typen
        TYPES: BEGIN OF ty_json_request,
                 param1 TYPE char64,
                 param2 TYPE char64,
               END OF ty_json_request.
    
        TYPES: BEGIN OF ty_json_response,
                 success   TYPE string,
                 msg       TYPE string,
                 operation TYPE string,
               END OF ty_json_response.
    
    * Variablen
        DATA: rv_response TYPE ty_json_response.
        DATA: it_path_info TYPE stringtab.
        DATA: it_query_strings TYPE stringtab.
    
    * Header Fields auslesen
        DATA: it_header_fields TYPE tihttpnvp.
        server->request->get_header_fields( CHANGING fields = it_header_fields ).
    
    * Request Methode (POST, GET, PUT, DELETE ...)
        DATA(lv_request_method) = server->request->get_header_field( name = '~request_method' ).
    
    * URL Pathinfo
        DATA(lv_path_info) = server->request->get_header_field( name = '~path_info' ).
    * erstes '/' entfernen
        SHIFT lv_path_info LEFT BY 1 PLACES.
        SPLIT lv_path_info AT '/' INTO TABLE it_path_info.
    
    * URL Parameter (Query Strings) für Auswertung
        DATA(lv_query_string) = server->request->get_header_field( name = '~query_string' ).
        SPLIT lv_query_string AT '&' INTO TABLE it_query_strings.
    
    * Request Methoden behandeln
        CASE lv_request_method.
    * C (Create)
          WHEN 'POST'.
    * Beispiel: per Request übermittelte JSON-Daten auslesen und behandeln
    * Content-Typ auslesen
            DATA(lv_request_content_type) = server->request->get_content_type( ).
    
    * Wenn Content-Typ JSON
            IF lv_request_content_type CP if_rest_media_type=>gc_appl_json.
    * JSON-Daten auslesen und nach ABAP-Typ konvertieren -> Parameternamen müssen übereinstimmen, sonst HTTP 500!
              DATA(lv_request_cdata) = server->request->get_cdata( ).
    
              DATA: lv_request_abap TYPE ty_json_request.
    
              TRY.
                  /ui2/cl_json=>deserialize( EXPORTING json = lv_request_cdata
                                                       pretty_name = /ui2/cl_json=>pretty_mode-camel_case
                                             CHANGING  data = lv_request_abap ).
    
                CATCH cx_root INTO DATA(e_txt).
                  MESSAGE e_txt->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
              ENDTRY.
    
              rv_response = VALUE #( success   = 'true'
                                     msg       = lv_request_abap-param1 && ' | ' && lv_request_abap-param2
                                     operation = lv_request_method ).
            ELSE.
              rv_response = VALUE #( success   = 'false'
                                     msg       = 'Request content type must be application/json'
                                     operation = lv_request_method ).
            ENDIF.
    * R (Read)
          WHEN 'GET'.
            DATA(lv_param) = server->request->get_form_field( 'param' ).
    
            rv_response = VALUE #( success   = 'true'
                                   msg       = lv_param
                                   operation = lv_request_method ).
    * U (Update)
          WHEN 'PUT'.
            rv_response = VALUE #( success   = 'true'
                                   msg       = ''
                                   operation = lv_request_method ).
    * D (Delete)
          WHEN 'DELETE'.
            rv_response = VALUE #( success   = 'true'
                                   msg       = ''
                                   operation = lv_request_method ).
    
    * unerlaubte Methoden abfangen
          WHEN OTHERS.
    * Fehlercode 405 zurückgeben
            server->response->set_status( code   = cl_rest_status_code=>gc_client_error_meth_not_allwd
                                          reason = 'method not allowed' ).
    
            server->response->set_content_type( if_rest_media_type=>gc_appl_json ).
    
            server->response->set_header_field( name  = if_http_header_fields=>allow
                                                value = 'POST,GET,PUT,DELETE' ).
    
            rv_response = VALUE #( success   = 'false'
                                   msg       = 'error'
                                   operation = lv_request_method ).
        ENDCASE.
    
    * wenn erfolgreich, dann Wert zurückgeben
        IF rv_response-success = 'true'.
    * HTTP-Status 200
          server->response->set_status( code   = cl_rest_status_code=>gc_success_ok
                                        reason = 'Ok.' ).
    
    * application/json
          server->response->set_content_type( if_rest_media_type=>gc_appl_json ).
    
    * Service-Response als JSON zurückgeben
          server->response->set_cdata( data = /ui2/cl_json=>serialize( data        = rv_response
                                                                       pretty_name = /ui2/cl_json=>pretty_mode-camel_case ) ).
    
        ENDIF.
    
    ENDMETHOD.
    
  4. SICF -> neue ICF-Node (Sub-Element) unterhalb default_host -> sap -> bc als neuen unabhängigen Service anlegen
  5. in der Handlerliste des Service die erstellte Klasse als „Handler“ angeben
  6. auf die neue ICF-Node rechtsklicken -> Service aktivieren
  7. auf die neue ICF-Node rechtsklicken -> Service testen
  8. URL aus Browserfenster kopieren (http://domain.de/sap/bc/servicename?sap-client=900)
  9. im Browser mir einem Rest-Plugin (FireFox-Prugin: Rested) testen

Links

[ABAP] Mit Hilfe der Klasse cl_fxs_image_processor Infos zu einem Bild auslesen, Größe verändern (Resize) und Bildformat konvertieren (Convert)

* Im Beispiel wird das Eingabebild (PNG) nach JPG konvertiert und auf 800x600px in der Größe verändert

* Dateiname der Bilddatei zum hochladen
PARAMETERS: p_fname TYPE file_table-filename OBLIGATORY.
PARAMETERS: p_width TYPE i DEFAULT 800.
PARAMETERS: p_height TYPE i DEFAULT 600.

* wenn die F4-Hilfe für den Dateinamen aufgerufen wird
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

  DATA: lv_rc TYPE i.
  DATA: it_files TYPE filetable.
  DATA: lv_action TYPE i.

* File-Tabelle leeren, da hier noch alte Einträge von vorherigen Aufrufen drin stehen können
  CLEAR it_files.

* FileOpen-Dialog aufrufen
  TRY.
      cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                    file_filter = |png (*.png)\|*.png\|{ cl_gui_frontend_services=>filetype_all }|
                                                  CHANGING
                                                    file_table  = it_files
                                                    rc          = lv_rc
                                                    user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
          p_fname = it_files[ 1 ]-filename.
        ENDIF.
      ENDIF.

    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.

START-OF-SELECTION.

  TRY.
      DATA: lv_filesize TYPE w3param-cont_len.
      DATA: lv_filetype TYPE w3param-cont_type.
      DATA: it_bin_data TYPE w3mimetabtype.

      WRITE: / p_fname.

* Bild auf Appl. Server hochladen (binary)
      cl_gui_frontend_services=>gui_upload( EXPORTING
                                              filename   = |{ p_fname }|
                                              filetype   = 'BIN'
                                            IMPORTING
                                              filelength = lv_filesize
                                            CHANGING
                                              data_tab   = it_bin_data ).

* solix -> xstring
      DATA(lv_img_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

* FXS-Objekt holen
      DATA(o_ip) = NEW cl_fxs_image_processor( ).

* Bild zum Konverter hinzufügen
      DATA(lv_hndl) = o_ip->add_image( iv_data = lv_img_data ).

      DATA: lv_mimetype	TYPE string.
      DATA: lv_xres	TYPE i.
      DATA: lv_yres	TYPE i.
      DATA: lv_xdpi	TYPE i.
      DATA: lv_ydpi	TYPE i.
      DATA: lv_bitdepth	TYPE i.

* Infos holen
      o_ip->get_info( EXPORTING
                        iv_handle   = lv_hndl
                      IMPORTING
                        ev_mimetype = lv_mimetype
                        ev_xres     = lv_xres
                        ev_yres     = lv_yres
                        ev_xdpi     = lv_xdpi
                        ev_ydpi     = lv_ydpi
                        ev_bitdepth = lv_bitdepth ).

* Infos ausgeben
      WRITE: / 'MIME-Type:', lv_mimetype.
      WRITE: / '        X:', lv_xres.
      WRITE: / '        Y:', lv_yres.
      WRITE: / '    X-DPI:', lv_xdpi.
      WRITE: / '    Y-DPI:', lv_ydpi.
      WRITE: / '      Bit:', lv_bitdepth.

* Größe ändern (Resize)
      o_ip->resize( EXPORTING
                      iv_handle = lv_hndl
                      iv_xres   = p_width
                      iv_yres   = p_height ).

* Konvertierung (Convert) nach JPG
* Zielformate können nur sein (andernfalls gibt es eine Exception vom Typ cx_sy_range_out_of_bounds):
*   cl_fxs_mime_types=>co_image_bitmap
*   cl_fxs_mime_types=>co_image_png
*   cl_fxs_mime_types=>co_image_jpeg
      o_ip->convert( EXPORTING
                      iv_handle = lv_hndl
                      iv_format = cl_fxs_mime_types=>co_image_jpeg ).

* Infos holen
      o_ip->get_info( EXPORTING
                        iv_handle   = lv_hndl
                      IMPORTING
                        ev_mimetype = lv_mimetype
                        ev_xres     = lv_xres
                        ev_yres     = lv_yres
                        ev_xdpi     = lv_xdpi
                        ev_ydpi     = lv_ydpi
                        ev_bitdepth = lv_bitdepth ).

* Infos ausgeben
      WRITE: / 'MIME-Type:', lv_mimetype.
      WRITE: / '        X:', lv_xres.
      WRITE: / '        Y:', lv_yres.
      WRITE: / '    X-DPI:', lv_xdpi.
      WRITE: / '    Y-DPI:', lv_ydpi.
      WRITE: / '      Bit:', lv_bitdepth.

* Binärdaten (xstring) des Bildes holen
      DATA(lv_img_conv_data) = o_ip->get_image( EXPORTING iv_handle = lv_hndl ).

* xstring -> solix
      DATA(it_img_conv_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_img_conv_data ).

      WRITE: / |{ p_fname }.jpg|.

* Image lokal speichern
      cl_gui_frontend_services=>gui_download( EXPORTING
                                                filename     = |{ p_fname }.jpg|
                                                filetype     = 'BIN'
                                                bin_filesize = xstrlen( lv_img_conv_data )
                                              CHANGING
                                                data_tab     = it_img_conv_data ).

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

[ABAP] MIME-Repository: Objekt-ID, Pfad, Attribute, Berechtigungen

DATA: lv_io    TYPE skwf_io.
DATA: lv_error TYPE skwf_error.

* Repository-Objekt (MIME) über Pfad suchen
CALL FUNCTION 'SKWF_NMSPC_IO_FIND_BY_ADDRESS'
  EXPORTING
    url   = '/SAP/PUBLIC/xyz.jpg'
    appl  = wbmr_c_skwf_appl_name " 'MIME'
  IMPORTING
    io    = lv_io
    error = lv_error.

IF lv_error-type = 'E'.
ELSE.
  WRITE: / lv_io-objid, lv_io-objtype, lv_io-object, lv_io-class.

* Objekttyp
  CASE lv_io-objtype.
    WHEN skwfc_obtype_folder.
      WRITE: / 'Folder'.
    WHEN skwfc_obtype_any.
      WRITE: / 'Any'.
    WHEN skwfc_obtype_loio.
      WRITE: / 'Logical object'.
    WHEN skwfc_obtype_phio.
      WRITE: / 'Physical object'.
    WHEN skwfc_obtype_rela.
      WRITE: / 'Relation'.
  ENDCASE.

  TRY.
* Zugriff prüfen
      cl_mime_services=>mime_check_authority( i_operation = 'SHOW' " Aktion ('INSERT','MODIFY','SHOW','DELETE')
                                              i_object_io = lv_io ).

      DATA: lv_url TYPE skwf_url.
      DATA: lv_relative_url TYPE skwf_descr.

      DATA: lv_error2 TYPE skwf_error.

* Pfad zum Repository-Objekt (MIME) suchen
      CALL FUNCTION 'SKWF_NMSPC_IO_ADDRESS_GET'
        EXPORTING
          io           = lv_io
        IMPORTING
          url          = lv_url
          relative_url = lv_relative_url
          error        = lv_error2.

      IF lv_error2-type = 'E'.
      ELSE.
        WRITE: / lv_url.
        WRITE: / lv_relative_url.
      ENDIF.

    CATCH cx_root INTO DATA(e).
  ENDTRY.
ENDIF.

[ABAP] MIME-Repository: logische Objekt-ID, physiche Object-ID, Berechtigungen

DATA: lv_loio	TYPE skwf_io.
DATA: lv_is_folder TYPE boole_d.

DATA(o_mime_rep) = cl_mime_repository_api=>get_api( ).

TRY.
* logische IO zu MIME-Objekt holen
    o_mime_rep->get_io_for_url( EXPORTING
                                  i_url       = '/SAP/PUBLIC/xyz.jpg'
                                IMPORTING
                                  e_is_folder = lv_is_folder
                                  e_loio      = lv_loio ).

    WRITE: / lv_is_folder.
    WRITE: / lv_loio.

    DATA: lv_error TYPE skwf_error.
    DATA: it_phios TYPE STANDARD TABLE OF skwf_io WITH DEFAULT KEY.

* physical IOs zu log. IO holen
    CALL FUNCTION 'SKWF_LOIO_ALL_PHIOS_GET'
      EXPORTING
        loio  = lv_loio
      IMPORTING
        error = lv_error
      TABLES
        phios = it_phios.

    LOOP AT it_phios ASSIGNING FIELD-SYMBOL(<p>).

      DATA: lv_err TYPE skwf_error.

* Berechtigungen zu physical IO prüfen
      CALL FUNCTION 'SKWF_AUTH_OBJECT_CHECK'
        EXPORTING
          activity = skwfa_c_act_read
          object   = <p>
        IMPORTING
          error    = lv_err.

      IF lv_err-type = 'E'.
      ELSE.
        WRITE: / <p>.
      ENDIF.

    ENDLOOP.

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