process_info

in src/zgoog_cl_content_repo_gcs.clas.abap [0:0]


  METHOD process_info.

    DATA: lo_docid         TYPE REF TO data,
          lo_contrep       TYPE REF TO data,
          lo_gcs           TYPE REF TO zgoog_cl_storage_cont_repo_v1,
          lo_struc         TYPE REF TO cl_abap_structdescr,
          lo_component     TYPE REF TO abap_compdescr,
          lo_abap_sdk_excp TYPE REF TO /goog/cx_sdk,
          lo_entity        TYPE REF TO if_http_entity.

    DATA: lv_contrep          TYPE string,
          lv_docid            TYPE string,
          lv_resultas         TYPE string,
          lv_pversion         TYPE string,
          lv_compid           TYPE string,
          lv_seckey           TYPE string,
          lv_smand            TYPE c LENGTH 1,
          lv_signature        TYPE c LENGTH 1,
          lv_p_bucket         TYPE string,
          lv_q_prefix         TYPE string,
          lv_ret_code         TYPE i,
          lv_err_text         TYPE string,
          lv_p_object         TYPE string,
          lv_object_get_error TYPE flag,
          lv_timec            TYPE string,
          lv_datec            TYPE string,
          lv_timem            TYPE string,
          lv_datem            TYPE string,
          lv_msg              TYPE string,
          lv_status           TYPE string,
          lv_inumcomps        TYPE i,
          lv_numcomps         TYPE string,
          lv_size             TYPE string,
          lv_mimetype         TYPE string,
          lv_crea_date        TYPE d,
          lv_crea_time        TYPE t,
          lv_chng_date        TYPE d,
          lv_chng_time        TYPE t,
          lv_time_created     TYPE string,
          lv_time_updated     TYPE string.

    DATA: ls_object_list   TYPE /goog/cl_storage_v1=>ty_016,
          ls_err_resp      TYPE /goog/err_resp,
          ls_item_append   TYPE /goog/cl_storage_v1=>ty_013,
          ls_doc_status    TYPE scms_docst,
          ls_meta          TYPE scms_acinf,
          ls_comp          TYPE scms_stinf,
          ls_output_object TYPE /goog/cl_storage_v1=>ty_013.

    DATA: lt_comps TYPE STANDARD TABLE OF scms_stinf.

    FIELD-SYMBOLS:<lv_contrep>       TYPE c,
                  <lv_docid>         TYPE c,
                  <ls_item>          TYPE /goog/cl_storage_v1=>ty_013,
                  <ls_metadata_comp> TYPE any,
                  <ls_source>        TYPE any,
                  <ls_target>        TYPE any,
                  <ls_source_val>    TYPE any.

    get_parameter_c( EXPORTING iv_name      = 'contRep '
                               iv_mandatory = 'S'
                     IMPORTING ev_value_s   = lv_contrep
                               er_value     = lo_contrep ).
    get_parameter_c( EXPORTING iv_name      = 'docId'
                               iv_mandatory = 'S'
                     IMPORTING ev_value_s   = lv_docid
                               er_value     = lo_docid ).

    ASSIGN lo_contrep->* TO <lv_contrep>.
    ASSIGN lo_docid->*   TO <lv_docid>.

    lv_resultas = get_parameter( iv_name    = 'resultAs'
                                 iv_default = 'ascii' ).
    lv_pversion = get_parameter( iv_name      = 'pVersion'
                                 iv_mandatory = 'X' ).
    lv_compid   = get_parameter( iv_name = 'compId' ).
    lv_seckey   = get_parameter( iv_name = 'secKey' ).

    IF lv_seckey IS NOT INITIAL.
      lv_smand = 'S'.
      get_parameter( iv_name      = 'accessMode'
                     iv_mandatory = lv_smand ).
      get_parameter( iv_name      = 'authId'
                     iv_mandatory = lv_smand ).
      get_parameter( iv_name      = 'expiration'
                     iv_mandatory = lv_smand ).
    ENDIF.

    check_parameter_consistence( ).
    IF gs_error IS NOT INITIAL.
      RETURN.
    ENDIF.

    lv_signature = check_signature( iv_docprot = 'r' ).
    IF gs_error IS NOT INITIAL.
      RETURN.
    ENDIF.

    load_goog_repo_config( <lv_contrep> ).
    IF gs_error IS NOT INITIAL.
      RETURN.
    ENDIF.

    DATA ls_data TYPE xstring.

    lv_p_bucket = gs_repo_config-bucket.

    TRY.
        CREATE OBJECT lo_gcs
          EXPORTING
            iv_key_name = gs_repo_config-keyname.

        lv_q_prefix = <lv_docid>.
        lv_p_bucket = gs_repo_config-bucket.

        IF lv_compid IS INITIAL.

          lo_gcs->list_objects( EXPORTING iv_q_prefix = lv_q_prefix
                                          iv_p_bucket = lv_p_bucket
                                IMPORTING es_output   = ls_object_list
                                          ev_ret_code = lv_ret_code
                                          ev_err_text = lv_err_text
                                          es_err_resp = ls_err_resp ).
          IF lo_gcs->is_success( lv_ret_code ) <> abap_true.
            set_error( iv_code = lv_ret_code
                       iv_text = lv_err_text ).
            RETURN.
          ELSEIF ls_object_list-items IS INITIAL.
            set_error( iv_code = 404
                       iv_text = 'Document not found in the repository' ) ##NO_TEXT.
            RETURN.
          ENDIF.
        ELSE.
          " Doc ID and Comp ID only
          ls_item_append-name = <lv_docid> && '/' && <lv_docid>.
          APPEND ls_item_append TO ls_object_list-items.

          ls_item_append-name = <lv_docid> && '/' && <lv_docid> && '-' && lv_compid.
          APPEND ls_item_append TO ls_object_list-items.
        ENDIF.

        LOOP AT ls_object_list-items ASSIGNING <ls_item>.
          lo_gcs->get_objects( EXPORTING iv_p_bucket = lv_p_bucket
                                         iv_p_object = <ls_item>-name
                               IMPORTING es_output   = ls_output_object
                                         ev_ret_code = lv_ret_code
                                         ev_err_text = lv_err_text ).

          IF lo_gcs->is_success( lv_ret_code ) <> abap_true.
            lv_object_get_error = abap_true.
            set_error( iv_code = lv_ret_code
                       iv_text = lv_err_text ).
            RETURN.
          ENDIF.

          IF <ls_item>-name = <lv_docid> && '/' && <lv_docid>.

            lv_timec = substring( val = ls_output_object-time_created
                                  off = 11
                                  len = 8 ).
            lv_datec = substring( val = ls_output_object-time_created
                                  off = 0
                                  len = 10 ).
            lv_timem = substring( val = ls_output_object-updated
                                  off = 11
                                  len = 8 ).
            lv_datem = substring( val = ls_output_object-updated
                                  off = 0
                                  len = 10 ).
            ls_doc_status = 0.

          ELSE.

            DATA: ls_metadata TYPE scms_acinf.

            ASSIGN ls_output_object-metadata->* TO <ls_metadata_comp>.
            lo_struc ?= cl_abap_structdescr=>describe_by_data( <ls_metadata_comp> ).
            LOOP AT lo_struc->components REFERENCE INTO lo_component.
              ASSIGN COMPONENT lo_component->name OF STRUCTURE <ls_metadata_comp> TO <ls_source>.
              IF sy-subrc IS NOT INITIAL.
                CONTINUE.
              ENDIF.

              ASSIGN COMPONENT lo_component->name OF STRUCTURE ls_metadata TO <ls_target>.
              IF sy-subrc IS NOT INITIAL.
                CONTINUE.
              ENDIF.

              ASSIGN <ls_source>->* TO <ls_source_val>.
              <ls_target> = <ls_source_val>.
            ENDLOOP.

            MOVE-CORRESPONDING ls_metadata TO ls_comp.

            lv_time_created = ls_output_object-time_created(19).
            ls_comp-crea_time = convert_str_to_time( lv_time_created ).

            lv_time_updated = ls_output_object-updated(19).
            ls_comp-chng_time = convert_str_to_time( lv_time_updated ).
            ls_comp-status    = 0.
            APPEND ls_comp TO lt_comps.
            CLEAR ls_comp.

          ENDIF.
        ENDLOOP.

        IF lv_object_get_error IS NOT INITIAL.
          RETURN.
        ENDIF.

      CATCH /goog/cx_sdk INTO lo_abap_sdk_excp.
        lv_msg = lo_abap_sdk_excp->get_text( ).
        set_error( iv_code = 400
                   iv_text = lv_msg ).
        RETURN.
    ENDTRY.

    IF lv_compid IS NOT INITIAL.
      lv_compid = to_lower( lv_compid ).
      LOOP AT lt_comps INTO ls_comp.
        ls_comp-comp_id = to_lower( ls_comp-comp_id ).
        IF ls_comp-comp_id <> lv_compid.
          DELETE lt_comps.
        ENDIF.
      ENDLOOP.
      IF lt_comps[] IS INITIAL.
        gs_error-status_code = 404.
        EXIT.
      ENDIF.
    ENDIF.

    lv_status = format_status( iv_status = ls_doc_status ).

    lo_entity = go_server->response.
    IF lv_resultas = 'html'.
      lo_entity->set_header_field( name  = 'Content-Type'
                                   value = 'iv_text/plain' ) ##NO_TEXT.

      CONCATENATE
        'X-DateC:'      gv_sp lv_datec  gv_nl ##NO_TEXT
        'X-TimeC:'      gv_sp lv_timec  gv_nl ##NO_TEXT
        'X-DateM:'      gv_sp lv_datem  gv_nl ##NO_TEXT
        'X-TimeM:'      gv_sp lv_timem  gv_nl ##NO_TEXT
        'X-CompStatus:' gv_sp lv_status gv_nl ##NO_TEXT
        INTO gv_body.
    ELSE.
      DESCRIBE TABLE lt_comps LINES lv_inumcomps.
      lv_numcomps = lv_inumcomps.

      lo_entity->set_header_field( name  = 'Content-Type' ##NO_TEXT
                                   value = 'multipart/form-data' ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-dateC'
                                   value = lv_datec ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-timeC'
                                   value = lv_timec ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-dateM'
                                   value = lv_datem ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-timeM'
                                   value = lv_timem ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-contRep'
                                   value = lv_contrep ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-numberComps'
                                   value = lv_numcomps ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-docId'
                                   value = lv_docid ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-docStatus'
                                   value = lv_status ) ##NO_TEXT.
      lo_entity->set_header_field( name  = 'X-pVersion'
                                   value = lv_pversion ) ##NO_TEXT.
    ENDIF.

    LOOP AT lt_comps INTO ls_comp.

      format_timestamp( EXPORTING iv_timestamp = ls_comp-crea_time
                        IMPORTING ev_date      = lv_datec
                                  ev_time      = lv_timec ).
      format_timestamp( EXPORTING iv_timestamp = ls_comp-chng_time
                        IMPORTING ev_date      = lv_datem
                                  ev_time      = lv_timem ).

      lv_size = ls_comp-comp_size.
      lv_mimetype = ls_comp-mimetype.
      lv_compid   = ls_comp-comp_id.
      lv_status = format_status( iv_status = ls_comp-status ).

      IF lv_resultas = 'html'.
        CONCATENATE
          gv_body gv_nl
          'Content-Type:'     gv_sp lv_mimetype gv_nl ##NO_TEXT
          'Content-Length:'   gv_sp '0'      gv_nl ##NO_TEXT
          'X-CompId:'         gv_sp lv_compid   gv_nl ##NO_TEXT
          'X-Content-Length:' gv_sp lv_size   gv_nl ##NO_TEXT
          'X-compDateC:'      gv_sp lv_datec  gv_nl ##NO_TEXT
          'X-compTimeC:'      gv_sp lv_timec  gv_nl ##NO_TEXT
          'X-compDateM:'      gv_sp lv_datem  gv_nl ##NO_TEXT
          'X-compTimeM:'      gv_sp lv_timem  gv_nl ##NO_TEXT
          'X-compStatus:'     gv_sp lv_status gv_nl ##NO_TEXT
          'X-pVersion:'       gv_sp lv_pversion gv_nl ##NO_TEXT
          INTO gv_body.

      ELSE.
        lo_entity = go_server->response->add_multipart( ).

        lo_entity->suppress_content_type( ).

        IF lv_mimetype IS NOT INITIAL.
          lo_entity->set_header_field( name  = 'Content-Type'
                                       value = lv_mimetype ) ##NO_TEXT.
        ENDIF.
        lo_entity->set_header_field( name  = 'Content-Length'
                                     value = '0' ) ##NO_TEXT.
        lo_entity->set_header_field( name  = 'X-compId'
                                     value = lv_compid ) ##NO_TEXT.
        lo_entity->set_header_field( name  = 'X-Content-Length'
                                     value = lv_size ) ##NO_TEXT.
        lo_entity->set_header_field( name  = 'X-compDateC'
                                     value = lv_datec ) ##NO_TEXT.
        lo_entity->set_header_field( name  = 'X-compTimeC'
                                     value = lv_timec ) ##NO_TEXT.
        lo_entity->set_header_field( name  = 'X-compDateM'
                                     value = lv_datem ) ##NO_TEXT.
        lo_entity->set_header_field( name  = 'X-compTimeM'
                                     value = lv_timem ) ##NO_TEXT.
        lo_entity->set_header_field( name  = 'X-compStatus'
                                     value = lv_status ) ##NO_TEXT.
        lo_entity->set_header_field( name  = 'X-pVersion'
                                     value = lv_pversion ) ##NO_TEXT.

      ENDIF.
    ENDLOOP.

    go_server->response->set_status( code   = 200
                                     reason = 'OK' ).