Processing Entities
Procedure
The processing of the entities can be structured in three basic parts:
-
Deserialize the single request object and get access to the request data
-
Route the request to the handler class of the REST application serving the request
-
Set the single response into the multipart response object
Access to the list of the requests contained in a multipart request is provided by method GET_ENTITIES of interface IF_REST_MP_ENTITY. To make access to each request easy, an object of type CL_REST_HTTP_REQ_PROV is created with the single request as input. This object enables the REST application to access the settings of the single request. The single request is deserialized and access to the request object is provided by method GET_REQUEST.
The REST application serving for the request is created. The router class object implementing IF_REST_HANDLER is read from the REST application read by method GET_ROOT_HANDLER (see section The REST Application). An empty response object is created and passed onto the HANDLE method of the router together with the request object from the request provider class.
If the resource URI and HTTP method specified in the request is valid, then the request gets routed to the handler class method of the correct REST application.
After returning from the application processing, a new HTTP response is created in the multipart response using the IF_REST_MP_ENTITY~CREATE_ENTITY method. The previously passed in response object is now filled with the response from the application. An object of type CL_REST_HTTP_RESP_PROV is created and is written via the WRITE_TO method to the newly created single response object of the multipart response.
method if_rest_resource~post.
field-symbols:
<lo_entity> type ref to if_rest_entity.
data:
lo_resp_prov type ref to cl_rest_http_resp_prov,
lo_rest_entity_resp type ref to if_rest_entity,
lo_router type ref to if_rest_handler,
lo_appl type ref to if_rest_application,
lo_request type ref to if_rest_request,
lo_req_prov type ref to cl_rest_http_req_prov,
lt_entities type if_rest_mp_entity=>rest_entity_t.
...
lt_entities = lo_mp_entity_req->get_entities( ).
loop at lt_entities assigning <lo_entity>.
create object lo_req_prov
exporting
io_entity = <lo_entity>.
lo_request = lo_req_prov->get_request( ).
create object lo_appl type cl_rest_sample_cars_appl.
lo_router = lo_appl->get_root_handler( ).
lo_router->handle(
io_request = lo_request
io_response = lo_response ).
lo_rest_entity_resp = lo_mp_entity->create_entity( ).
create object lo_resp_prov.
lo_resp_prov->set_response( lo_response ).
lo_resp_prov->write_to( lo_rest_entity_resp ).
endloop.
mo_response->set_status( cl_rest_status_code=>gc_success_accepted ).
mo_response->set_reason( if_http_status=>reason_202 ).
endmethod.
Within the loop, each of the REST entities is treated like a REST request as if it would get passed in by a single request. The single request REST application handler classes get reused. At the end, the overall response is set in the MO_RESPONSE object of the multipart request. The response code is 202 ("Accepted").