Selasa, 23 September 2014

How to create executable package/procedure on URL

Posting ini sebenarnya pernah saya bahas, namun sekarang saya akan membahasnya lebih spesifik pada download file dengan web server apex listener.

Adapun hal yang harus diperhatikan pada bagian
  1. Package/procedure
    • Contoh
    • create or replace PACKAGE BODY web_utils IS
        -- retrieves image from blob in product info
        PROCEDURE display_bu_logo (p_id number, p_product_name varchar2) 
        IS
          l_mime        varchar2 (255);
          l_length      number;
          l_file_name   varchar2 (2000);
          lob_loc       blob;
        begin
          if p_id is not null then
            select mimetype
                 , filename
                 , nvl(product_image, empty_blob()) blob_content
                 , dbms_lob.getlength(product_image)
              into l_mime
                 , l_file_name
                 , lob_loc 
                 , l_length
              from demo_product_info
             where product_id = p_id;    
          elsif p_product_name is not null then
            select mimetype
                 , filename
                 , nvl(product_image, empty_blob()) blob_content
                 , dbms_lob.getlength(product_image)
              into l_mime
                 , l_file_name
                 , lob_loc 
                 , l_length
              from demo_product_info
             where lower(product_name) = lower(p_product_name);     
          end if;
      
      
         owa_util.mime_header (nvl (l_mime, 'application/octet'), false);
      
         -- set the size so the browser knows how much to download
         htp.p ('Content-length: ' || l_length);
         -- the filename will be used by the browser if the users does a save as
         htp.p (   'Content-Disposition:  attachment; filename="'
                || replace (replace (substr (l_file_name,
                                             instr (l_file_name, '/') + 1
                                            ),
                                     chr (10),
                                     null
                                    ),
                            chr (13),
                            null
                           )
                || '"'
               );
      
         -- close the headers
         owa_util.http_header_close;
         -- download the BLOB
         wpg_docload.download_file (lob_loc);
      
        END display_bu_logo;
      END web_utils;
      
    • Tambahkan nvl(lob_column, empty_blob())
    • Gunakan htp.p
    • Untuk kasus image jika ingin tampil inline, lakukan comment pada bagian
    • --   htp.p (   'Content-Disposition:  attachment; filename="'
      --          || replace (replace (substr (l_file_name,
      --                                       instr (l_file_name, '/') + 1
      --                                      ),
      --                               chr (10),
      --                               null
      --                              ),
      --                      chr (13),
      --                      null
      --                     )
      --          || '"'
      --         );
      
  2. Security
    • Anda harus tahu environment server apa yang dipakai, apakah EPG, OHS atau APEX Listener
    • Untuk APEX Listener/OHS anda cukup grant APEX_PUBLIC_USER
    • Untuk OHS grant APEX_PUBLIC_USER dan daftarkan procedure yang akan dipanggil di APEX_XXXXXX.wwv_flow_epg_include_mod_local
    • Untuk EPG anda grant ke ANONYMOUS dan aktifkan procedure di APEX_XXXXXX.wwv_flow_epg_include_mod_local
  3. Cara memanggil dengan signature parameter.
    • mulai dengan ? pisahkan dengan &
    • Posisi tidak penting namun harus disebutkan semua kecuali punya default value
    • Untuk kasus APEX listener parameter signature akan selalu dirubah dengan menjadi lowercase, hati-hati jika memakai parameter non-case-sensitive seperti BI Publisher web service
    • contoh
    • http://localhost:8888/ords/hr.web_utils.display_bu_logo?p_product_name=Blouse&p_id=
      
    • contoh array parameter
    • http://localhost:8888/ords/!hr.web_utils.display_bu_logo?p_product_name=Blouse&p_id=
      
Catatan :
  • APEX Listener / ORDS 2.0.8.163.10.40