Example: Conditional Handling
Use
The description of REST services with conditional handling is explained in details in section Conditional Handling. This section describes only the implementation of the conditional handling used in this example.
Process
To enable the processing class for serving conditional handling based on HTTP ETags, the instance attribute MV_CONDITIONAL_HANDLING is set to value ABAP_TRUE (which is actually the default value in the handler's base class CL_REST_RESOURCE). To implement an own conditional handling, the method GET_CONDIGIONAL_INFO must be redefined in the handler class, otherwise the GET method is called implicitly for the ETag reading. However this results in the retrieval of data to be returned and may result in a reduction in performance.
The signature of the conditional information method provides the values required by the REST library to handle conditional requests.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Protected Method CL_REST_SAMPLE_CAR->GET_CONDITIONAL_INFO
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_METHOD TYPE STRING
* | [<---] EV_EXISTING TYPE ABAP_BOOL
* | [<---] EV_STATUS TYPE I
* | [<---] ES_ETAG TYPE REST_ETAG_S
* | [<---] EV_MODIFICATION_DATE TYPE TIMESTAMP
* | [<---] EV_GET_RESPONSE TYPE ABAP_BOOL
* +--------------------------------------------------------------------------------------</SIGNATURE>
The parameters of the method are:
-
IV_METHOD: The HTTP method of the currently processed request.
-
EV_EXISTING: A return of ABAP_TRUE indicates that the requested resource was found. On a POST call, this parameter is always set to ABAP_FALSE.
-
EV_STATUS: The HTTP response code based on the specified HTTP method and the resource identifying information provided by a client request.
-
ES_ETAG: Contains the ETag value for the requested resource in case of a successful reading.
-
EV_MODIFICATION_DATA: Contains the modification date of the requested resource.
-
EV_GET_RESPONSE: Indicates using an ABAP_TRUE that a response was already created during ETag handling, that is, the REST library expects to have already the response set in the response member of the class. The intention behind this handling is that conditional handling and data retrieval are strongly tied together and thus a subsequent data read or change call can be saved by the library. If there is no such strict conceptual link for the ETag handling and resource data, and if the data access is more complex, then the returned value for this parameter must be set to ABAP_FALSE to indicate to the REST library that no resulting data was prepared during the conditional handling in this method.
For more information on this method, see Implementing Conditional Handling.
The conditional handling implementation used in this example is as follows:
method get_conditional_info.
data:
lt_etag type rest_etag_t,
lv_etag_in type string.
* -1- set default return
ev_existing = abap_false.
ev_status = cl_rest_status_code=>gc_server_error_internal.
clear: es_etag, ev_modification_date.
ev_get_response = abap_false.
* -2- conditional handling based on HTTP methods
case iv_method.
when if_rest_message=>gc_method_get or
if_rest_message=>gc_method_put or
if_rest_message=>gc_method_delete.
get_etag_for_request( importing es_etag = es_etag ev_modification_date = ev_modification_date ev_status = ev_status ).
if ev_status <> cl_rest_status_code=>gc_success_ok.
return.
endif.
ev_existing = abap_true. " requested resource found
ev_get_response = abap_false. " no response generated in this call
when others.
ev_status = cl_rest_status_code=>gc_client_error_meth_not_allwd.
endcase.
endmethod.
For the supported HTTP methods (GET, PUT, DELETE), the following method GET_ETAG_FOR_REQUEST returns the current ETag for the requested resource. The actual ETag calculation is based on the modification date of the requested Car resource.
method get_etag_for_request.
data:
lv_etag_value type string,
lv_updated type tzntstmpl,
lv_id type i.
* -1- get ID for requested resource from request;
* there is no need for checking for digits in the 'ID':
* the template matching already filtered out any non-integer values
lv_id = mo_request->get_uri_attribute( iv_name = 'ID' ).
* -2- get 'updated' information for requested resource
select single update_timestamp from r_car
into lv_updated
where id = lv_id.
* -3- set return
if sy-subrc <> 0.
clear es_etag.
ev_status = cl_rest_status_code=>gc_client_error_not_found.
else.
es_etag-tag = get_etag( lv_updated ).
es_etag-weak = abap_true.
ev_status = cl_rest_status_code=>gc_success_ok.
ev_modification_date = lv_updated.
endif.
endmethod.
The method GET_ETAG does a simple formatting of the ETag value and is of minor interest here. All ETags in our examples are flagged as weak (see Conditional Handling).