All files / src/ui/core zcl_abapgit_gui_asset_manager.clas.abap

95.48% Statements 169/177
70% Branches 14/20
100% Functions 1/1
95.48% Lines 169/177

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 1781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 1x   20x 1x 1x   1x 1x 1x 1x 1x 1x 1x 34x 34x 34x 34x 34x 34x 34x 34x 2x 34x 30x 30x 34x 34x 34x 34x 34x 34x 1x  
CLASS zcl_abapgit_gui_asset_manager DEFINITION PUBLIC FINAL CREATE PUBLIC .
 
  PUBLIC SECTION.
 
    INTERFACES zif_abapgit_gui_asset_manager.
 
    CLASS-METHODS create
      RETURNING
        VALUE(ri_asset_manager) TYPE REF TO zif_abapgit_gui_asset_manager.
 
  PROTECTED SECTION.
  PRIVATE SECTION.
 
    TYPES:
      BEGIN OF ty_asset_entry.
        INCLUDE TYPE zif_abapgit_gui_asset_manager~ty_web_asset.
    TYPES: mime_name TYPE wwwdatatab-objid,
      END OF ty_asset_entry.
    TYPES:
      ty_asset_register TYPE STANDARD TABLE OF ty_asset_entry WITH KEY url.
 
    DATA mt_asset_register TYPE ty_asset_register.
 
    METHODS get_mime_asset
      IMPORTING
        iv_mime_name    TYPE c
      RETURNING
        VALUE(rv_xdata) TYPE xstring
      RAISING
        zcx_abapgit_exception.
 
    METHODS load_asset
      IMPORTING
        is_asset_entry  TYPE ty_asset_entry
      RETURNING
        VALUE(rs_asset) TYPE zif_abapgit_gui_asset_manager~ty_web_asset
      RAISING
        zcx_abapgit_exception.
 
ENDCLASS.
 
 
 
CLASS zcl_abapgit_gui_asset_manager IMPLEMENTATION.
 
 
  METHOD create.
    CREATE OBJECT ri_asset_manager TYPE zcl_abapgit_gui_asset_manager.
  ENDMETHOD.
 
 
  METHOD get_mime_asset.
 
    DATA: ls_key    TYPE wwwdatatab,
          lv_size_c TYPE wwwparams-value,
          lv_size   TYPE i,
          lt_w3mime TYPE STANDARD TABLE OF w3mime,
          ls_w3mime LIKE LINE OF lt_w3mime.
 
    ls_key-relid = 'MI'.
    ls_key-objid = iv_mime_name.
 
    " Get exact file size
    CALL FUNCTION 'WWWPARAMS_READ'
      EXPORTING
        relid            = ls_key-relid
        objid            = ls_key-objid
        name             = 'filesize'
      IMPORTING
        value            = lv_size_c
      EXCEPTIONS
        entry_not_exists = 1.
 
    IF sy-subrc IS NOT INITIAL.
      RETURN.
    ENDIF.
 
    lv_size = lv_size_c.
 
    " Get binary data
    CALL FUNCTION 'WWWDATA_IMPORT'
      EXPORTING
        key               = ls_key
      TABLES
        mime              = lt_w3mime
      EXCEPTIONS
        wrong_object_type = 1
        import_error      = 2.
 
    IF sy-subrc IS NOT INITIAL.
      RETURN.
    ENDIF.
 
    LOOP AT lt_w3mime INTO ls_w3mime.
      CONCATENATE rv_xdata ls_w3mime-line INTO rv_xdata IN BYTE MODE.
    ENDLOOP.
    rv_xdata = rv_xdata(lv_size).
 
  ENDMETHOD.
 
 
  METHOD load_asset.
 
    MOVE-CORRESPONDING is_asset_entry TO rs_asset.
    IF rs_asset-content IS INITIAL AND is_asset_entry-mime_name IS NOT INITIAL.
      " inline content has the priority
      rs_asset-content = get_mime_asset( is_asset_entry-mime_name ).
    ENDIF.
    IF rs_asset-content IS INITIAL.
      zcx_abapgit_exception=>raise( |failed to load GUI asset: { is_asset_entry-url }| ).
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_gui_asset_manager~get_all_assets.
 
    FIELD-SYMBOLS <ls_a> LIKE LINE OF mt_asset_register.
 
    LOOP AT mt_asset_register ASSIGNING <ls_a>.
      APPEND load_asset( <ls_a> ) TO rt_assets.
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_gui_asset_manager~get_asset.
 
    FIELD-SYMBOLS <ls_a> LIKE LINE OF mt_asset_register.
 
    READ TABLE mt_asset_register WITH KEY url = iv_url ASSIGNING <ls_a>.
    IF <ls_a> IS NOT ASSIGNED.
      zcx_abapgit_exception=>raise( |Cannot find GUI asset: { iv_url }| ).
    ENDIF.
    rs_asset = load_asset( <ls_a> ).
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_gui_asset_manager~get_text_asset.
 
    DATA ls_asset TYPE zif_abapgit_gui_asset_manager~ty_web_asset.
    ls_asset = zif_abapgit_gui_asset_manager~get_asset( iv_url ).
 
    IF ls_asset-type <> 'text'.
      zcx_abapgit_exception=>raise( |Not a text asset: { iv_url }| ).
    ENDIF.
 
    IF iv_assert_subtype IS NOT INITIAL AND ls_asset-subtype <> iv_assert_subtype.
      zcx_abapgit_exception=>raise( |Wrong subtype ({ iv_assert_subtype }): { iv_url }| ).
    ENDIF.
 
    rv_asset = zcl_abapgit_convert=>xstring_to_string_utf8( ls_asset-content ).
 
  ENDMETHOD.
 
 
  METHOD zif_abapgit_gui_asset_manager~register_asset.
 
    DATA ls_asset LIKE LINE OF mt_asset_register.
 
    SPLIT iv_type AT '/' INTO ls_asset-type ls_asset-subtype.
    ls_asset-url          = iv_url.
    ls_asset-mime_name    = iv_mime_name.
    ls_asset-is_cacheable = iv_cacheable.
    IF iv_base64 IS NOT INITIAL.
      ls_asset-content = zcl_abapgit_convert=>base64_to_xstring( iv_base64 ).
    ELSEIF iv_inline IS NOT INITIAL.
      ls_asset-content = zcl_abapgit_convert=>string_to_xstring( iv_inline ).
    ENDIF.
 
    DELETE mt_asset_register WHERE url = iv_url.
    " TODO: Maybe forbid overwriting cacheable assets as they were probably already cached ... arguable
    APPEND ls_asset TO mt_asset_register.
 
  ENDMETHOD.
ENDCLASS.