obfuscate

in src/zrest/zcl_rest_utility_class.clas.abap [0:0]


  METHOD obfuscate.
************************************************************************************
    FIELD-SYMBOLS : <fs>           TYPE any,
                    <fchar_string> TYPE any.
    DATA : converted        TYPE REF TO cl_abap_conv_in_ce,
           regex            TYPE REF TO cl_abap_regex,
           matcher          TYPE REF TO cl_abap_matcher,
           obfuscate_word   TYPE REF TO data,
           lchar_string     TYPE REF TO data,
           regex_result_tab TYPE        match_result_tab,
           regex_result     TYPE        match_result,
           st_obfuscate     LIKE LINE OF it_zobfuscate,
           lv_string        TYPE string,
           length_of_word   TYPE i.
************************************************************************************
*   Convert to string                                                              *
************************************************************************************
* Check if the obfuscation is needed and input is not initial
    CHECK : it_zobfuscate IS NOT INITIAL ,
            input IS NOT INITIAL.
*   if yes , convert the xstring to string for the processing
    TRY.
        converted = cl_abap_conv_in_ce=>create(
            input = input ).
      CATCH cx_parameter_invalid_range .
      CATCH cx_sy_codepage_converter_init .
    ENDTRY.
    TRY.
        converted->read(
          IMPORTING
            data = lv_string ).
      CATCH cx_sy_conversion_codepage .
      CATCH cx_sy_codepage_converter_init .
      CATCH cx_parameter_invalid_type .
      CATCH cx_parameter_invalid_range .
    ENDTRY.
************************************************************************************
*   Loop at the fields which needed to be obfuscated                               *
************************************************************************************
    LOOP AT it_zobfuscate INTO st_obfuscate.
      CREATE OBJECT : regex EXPORTING pattern = st_obfuscate-fieldtag ignore_case = abap_true,
                      matcher EXPORTING regex = regex text = lv_string.
      CREATE DATA obfuscate_word TYPE c LENGTH st_obfuscate-length.
      length_of_word = strlen( lv_string ).
      CREATE DATA lchar_string TYPE c LENGTH length_of_word.
      regex_result_tab = matcher->find_all( ).
      length_of_word =  strlen( st_obfuscate-fieldtag ).
      ASSIGN obfuscate_word->* TO <fs>.
      ASSIGN lchar_string->* TO <fchar_string>.
      <fchar_string> = lv_string.

      DO st_obfuscate-length TIMES.
        sy-index = sy-index - 1.
        <fs>+sy-index(1) = '*'.
      ENDDO.
*     Repace the words in all string
      LOOP AT regex_result_tab INTO regex_result.
        length_of_word =  strlen( st_obfuscate-fieldtag ).
        length_of_word = length_of_word + regex_result-offset.
        <fchar_string>+length_of_word(st_obfuscate-length) = <fs> .
      ENDLOOP.
      CLEAR : st_obfuscate,length_of_word,regex_result_tab,regex_result.
      lv_string = <fchar_string>.
      UNASSIGN : <fs> , <fchar_string>.
    ENDLOOP.
************************************************************************************
*   Make it string                                                                 *
************************************************************************************
    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text   = lv_string
      IMPORTING
        buffer = result
      EXCEPTIONS
        failed = 1
        OTHERS = 2.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.