Skip to Content

Recently in my current project, I came across a requirement where function import of an external service had to be called in $Batch mode. Here the Apache Olingo library provides out-of-the box support for preparing batch request.

Please refer to the link below for more details:

https://olingo.apache.org/doc/odata2/tutorials/batchClientApi.html

 

  1. In order to call this function import, of an external service or a microservice, first of all get the productive URI of the external service or microservice. In case of Cloud Foundry, you will get the productive URL of the external service from the VCAP services in your environment variables.

String serviceURL = “https://<Service URL of external service/microservice>”;

 

String batchUri = "";
// Build URI
batchUri = serviceURL.concat("$batch").trim();

 

 

  1. Now you have to prepare the batch request payload which involves steps below:

First prepare the request content i.e. in our case the function import URL and the required parameters.

 

Step-1: Create Request Content; basically you have to pass the required function import parameters

String requestContent = "";
requestContent = requestContent.concat(“BookId='”).concat(“Test_Book”)
.concat("'") .concat(“BookName='”).concat(“SAP Cloud Foundry”).concat("'")
.concat(“Quantity='”).concat(“1”).concat("'");

 

Step-2: Create Change Set Header

Map<String, String> changeSetHeadersRequest = new HashMap<String, String>();

changeSetHeadersRequest.put("sap-contextid-accept", "header");
changeSetHeadersRequest.put("Accept", "application/json");
changeSetHeadersRequest.put("Accept-Language", "en-US");
changeSetHeadersRequest.put("DataServiceVersion", "2.0");
changeSetHeadersRequest.put("MaxDataServiceVersion", "2.0");
changeSetHeadersRequest.put("sap-cancel-on-close", "true");
changeSetHeadersRequest.put("content-type", "application/json");

 

Step-3: Assign this change set header along with request content to a Batch Change Set and then to BatchPart

BatchChangeSetPart changeRequest = BatchChangeSetPart.method("POST")
.uri("CreateOrderForBook?" + requestContent).headers(changeSetHeadersRequest).build();

BatchChangeSet changeSet = BatchChangeSet.newBuilder().build();
changeSet.add(changeRequest);

List<BatchPart> batchParts = new ArrayList<BatchPart>();
batchParts.add(changeSet);

Step-4: Now create an input stream from the batch part

InputStream payload= EntityProvider.writeBatchRequest(batchParts, "batch_123");

 

Step-5: Convert the InputSteam payload from step above to a String requestContent

String requestContent = IOUtils.toString(payload, StandardCharsets.UTF_8.toString());

Step-6: Now an HTTP request has to be created which will contain headers and the requestContent(Created above).In case your app-to-app/app-to-service communication is secured via Jwt Token then it needs to be passed in HTTP header as below:

HttpHeaders headers = new HttpHeaders();

String jToken = "Bearer ";
jToken = jToken.concat(jwtToken);
headers.set("Authorization", jToken);

 

then pass the Content Type as “multipart/mixed” in the HTTP Header
String contentType = “multipart/mixed;boundary=batch_123”;

headers.setContentType(MediaType.valueOf(contentType));

Pass the request content from Step-5 and the HttpHeader to create HTTPEntity

HttpEntity<String> requestEntity = new HttpEntity<String>(requestContent, headers);

 

3. After preparing the HTTP Request entity, call the external service/app with Rest Template and call the appropriate HTTP method as per your requirement.

ResponseEntity<String> response=restTemplate.exchange(batchUri, HttpMethod.POST, requestEntity, String.class);

 

In my second blog, I will show how to interpret batch response payload.

 

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply