Template handler deleting an image
This document covers sample implementations for template POST handler deleting uploaded images using the plug-in image browser. All examples deletes uploaded images from the plug-in sample application table UC_FROALA_SAMPLE_BLOBS.
- Simple
- Default parameters
- Image checksum
- deletes an image using an image id included in image URL
- can be combined with handler browsing images Simple, Default parameters and Custom parameters.
- deletes an image using an image id included in image URL
- can be combined with handler browsing images Simple, Default parameters and Custom parameters.
- secures delete handler with access token passed using default parameters
- 
deletes an image using image id encoded as an image URL checksum 
- 
must be combined with handler uploading an image Image checksum 
- 
must be combined with handler displaying an image Image checksum 
- 
must be combined with handler browsing images Image checksum 
- 
secures delete handler with access token passed using default parameters 
Requirements
Handler deleting an uploaded image must meet the following requirements:
- must use POST method
- must be compilable at the run time
- must return 200 HTTP status on success
Handler definition
- Simple
- Default parameters
- Image checksum
| Attribute | Value | 
|---|---|
| RESTful Service Module | A custom RESTservice | 
| Module Base Path | /customrest/ | 
| URI Template | delete-simple | 
| Full URL | https://example-domain.com/ords/workspacename/customrest/delete-simple | 
| Handler Method | POST | 
| Source Type | PL/SQL | 
| Mime Types Allowed | Not defined | 
| Attribute | Value | 
|---|---|
| RESTful Service Module | A custom RESTservice | 
| Module Base Path | /customrest/ | 
| URI Template | delete-default | 
| Full URL | https://example-domain.com/ords/workspacename/customrest/delete-default | 
| Handler Method | POST | 
| Source Type | PL/SQL | 
| Mime Types Allowed | Not defined | 
| Attribute | Value | 
|---|---|
| RESTful Service Module | A custom RESTservice | 
| Module Base Path | /customrest/ | 
| URI Template | delete-checksum | 
| Full URL | https://example-domain.com/ords/workspacename/customrest/upload-checksum | 
| Handler Method | POST | 
| Source Type | PL/SQL | 
| Mime Types Allowed | Not defined | 
Handler parameters
The upload handler doesn't require registering any resource parameters as long as REST specific bind variable :status_code  is used to set request HTTP status. The request query parameters are accessibile in the PL/SQL code as bind variables with same name.
Handler Source
The handler deleting uploaded image must be defined as PL/SQL.
- Simple
- Default parameters
- Image checksum
declare
  v_image_id varchar2(4000) := :imageId;
begin   
  begin
    if v_image_id is null then
      raise_application_error(-20001, 'image id is missing');
    end if;
    
    delete from UC_FROALA_SAMPLE_BLOBS where id = v_image_id;
      
    if SQL%ROWCOUNT = 0 then
      raise no_data_found;
    end if;
    :status_code := 200;
  exception
    when others then
      apex_json.open_object;    
      apex_json.write('error', 'Image not found');
      apex_json.write('id', v_image_id, true);
      apex_json.close_object;
      raise;
  end;
exception  
  when no_data_found then
    :status_code := 404;
  when others then
    :status_code := 500;
end;
declare
  v_access_token      uc_rte_access_token;
  v_image_id          varchar2(4000) := :imageId;
  /* default query parameters computed on page load */
  //highlight-next-line
  v_param_accessToken varchar2(32767) := :accessToken;
begin   
  begin
    begin
      //highlight-next-line
      v_access_token := uc_rte_access_token.create_from_token( v_param_accessToken );
      if v_access_token.is_valid = 0 then
        raise_application_error(-20001, 'Expired access token');  
      end if;
      exception 
        when others then
          raise_application_error(-20001, 'Invalid access token');  
    end;
    if v_image_id is null then
      raise_application_error(-20001, 'image id is missing');
    end if;
    
    delete from UC_FROALA_SAMPLE_BLOBS where id = v_image_id;
      
    if SQL%ROWCOUNT = 0 then
      raise no_data_found;
    end if;
    :status_code := 200;
  exception
    when others then
      apex_json.open_object;    
      apex_json.write('error', 'Image not found');
      apex_json.write('id', v_image_id, true);
      apex_json.close_object;
      raise;
  end;
exception  
  when no_data_found then
    :status_code := 404;
  when others then
    if SQLCODE = -20001 then
      :status_code := 403;
    else
      :status_code := 500;
    end if;
end;
declare
  v_access_token       uc_rte_access_token;  
  v_image_id           varchar2(4000);
  v_checksum_decoded   varchar2(4000);  
  /* default query parameters computed on page load */
  v_param_accessToken  varchar2(32767) := :accessToken;
  //highlight-next-line
  v_img_param_checksum varchar2(32767) := :checksum;
begin   
  begin
    begin
      v_access_token := uc_rte_access_token.create_from_token( v_param_accessToken );
      if v_access_token.is_valid = 0 then
        raise_application_error(-20001, 'Expired access token');  
      end if;
      exception 
        when others then
          raise_application_error(-20001, 'Invalid access token');  
    end;
    if v_img_param_checksum is null then
      raise_application_error(-20001, 'Checksum not included');
    end if;
    begin
      //highlight-next-line
      v_checksum_decoded := UC_RTE_ACCESS_TOKEN.decode( v_img_param_checksum ); 
    exception
      when others then
        raise_application_error(-20001, 'Failed decoding checksum');
    end;
    //highlight-next-line
    v_image_id := apex_string.split(v_checksum_decoded, ',')(2);
    
    delete from UC_FROALA_SAMPLE_BLOBS where id = v_image_id;
      
    if SQL%ROWCOUNT = 0 then
      raise no_data_found;
    end if;
    :status_code := 200;
  exception
    when others then
      apex_json.open_object;    
      apex_json.write('error', 'Image not found');
      apex_json.write('id', v_image_id, true);
      apex_json.close_object;
      raise;
  end
exception  
  when no_data_found then
    :status_code := 404;
  when others then
    if SQLCODE = -20001 then
      :status_code := 403;
    else
      :status_code := 500;
    end if;
end;
Handler Output
When an image deletion is successful, the handler should only set HTTP status code 200.
Error handling
The plug-in display deleting error as APEX inline page notification error with message Image delete failed and message assigned to JSON error property, for example:
Image delete failed: Image not found.