Technical Articles
SLN Spotlight_SAP Labs India: ODATA Protocol implementation for IOT Applications on SAP HCP
Blog Series:ODATA Protocol implementation for IOT Applications on SAP HCP to be precise – A JAVA Web Application using Apache Olingo API
This is a series of blog on Olingo OData implementation and divided into parts based on the topics, which I came across, while implementing the Olingo 2.0 APIs, First part consists of setting up of project and implementation of the Olingo OData APIs.
Part 1: Setup of OData APIs through Olingo 2.0 API.
Part 2: Entity class overview in JPA based WEB applications
Part 3: Adding Custom annotation and labels to metadata.
Part 4: JPA mapping or redefining the columns.
Part 5: Debugging and Error Handling implementation in Olingo 2.0 OData Services.
Part 6: Options to provide OData/REST services through JPAContext.
Part 7: Olingo OData 2.0 limitations
1. Introduction
This is series of documents on Apache Olingo OData2 services for HANA cloud platform (i.e. XSA and Cloud foundry) and JPA based web application. More details on creating JPA based application will be covered below. Apache Olingo is a Java library that implements the Open Data Protocol (OData). Apache Olingo serves client and server aspects of OData. It currently supports OData 2.0 for JPA Entities. Blog also provides different ways to customize pre/post processes for different type Requests on OData Services to various use cases, custom annotation, JPA mapping, entity mapping, debugging.
2. Prerequisite Activities
2.1 Create and build JPA Project
Web Project is already developed with JPA Entity to consume HANA Cloud Platform Database (HANA or MaxDB). Project should have persistence.xml, resource.xml and class files for JPA entities as shown in below picture.
2.2 Get dependent Library:
Add Following maven dependencies in project pom.xml to include OData Olingo and Apache CXF Jars
3. Configuration and Implementation
3.1 Configure persistence.xml in JPA model
Open persistence.xml file from <ProjectName>/Java Resources/src/META-IN and Add the following yellow-marked <property> line to the persistence.xml file
property name=“eclipselink.target-database” value=“org.eclipse.persistence.platform.database.HANAPlatform”
3.1.1 Configure resource.xml in JPA model
In resource.xml describes in cloud foundry, when application is pushed it will read the resource file about the HDI container to connect to Database, Here we have defined the provider, which can work with TOMEE-1.7 version or with tomcat8 (but we need to include the jar cxf-rt-frontend-jaxrs 2.7.8 version in pom.xml). Here service=sampleTest is the persistent unit name which has to define in web.xml, and JtaManaged is false, so that the JTA transaction will be maintained by Entity Manager factory.
3.2 Create Required Java Classes
3.2.1 Create Factory class for Managing Connection with Database using Server Context (eg. JpaEntityManagerFactory.java)
3.2.2 Create new Java class by extending ODataJPAServiceFactory to provide means for initializing Entity Data Model (EDM) Provider and OData JPA Processors.
3.3 Configure web descriptor file (web.xml)
Configure the web application as shown below by adding the following servlet configuration to web.xml. The Service factory (e.g. SampleCustomOdataJpaServiceFactory) which was implemented is configured in the web.xml of the OData Application as one of the init parameters.
4. Testing OData Service
Publish and start web application (Contain JPA Entity) on Cloud foundry, XSA or local web server and Open the application URL and append recourse path provided in web.xml for accessing OData Service,
Test Other OData operations using following tutorial http://www.odata.org/getting-started/basic-tutorial/
As well as you can check the ODATA APIs using Advance rest client or Postman (Both are Chrome extension). You can test all the method like GET, PUT, POST, DELETE etc. using Advance rest Client.
5. Custom OData Processing
Most of the time in web project we need to provide custom pre and post processing with default OData processing like
- Adding additional filters or select filter to restrict usages of OData GET Request
- Manipulating results of GET Request
- Triggering some functionality in case of new Entity creation
- Filled non-provided data for Entity.
- Redefining metadata like renaming of any columns or any renaming any entity class name.
5.1 Create Custom JPA Processor for OData Request
Extend default implement class org.apache.olingo.odata2.jpa.processor.core.ODataJPAProcessorDefault for OData Processing handling with your own Custom Processor Class like e.g. Class CustomoDataJPAProcessor in following screen shot and override method for different operations on case by case basis
5.1.1. Override readEntitySet Method for extending GET request to get entitySet as return – The instance variable jpaProcessor can be used to process the OData request. The jpaProcessor returns the JPA entities after processing the OData request.
5.1.2. Override createEntity method for extending POST request to create new record for entity – create private method to manipulate entity record and add any additional processing, You can see other default method in default JPA Processor class ODataJPAProcessorDefault,
5.1.3 Use CustomODataServiceFactory for extending your service factory class instead of using default ODataJPAServiceFactory
Change extension for class created in step 3.2.
References:
What is the Open Data Protocol (OData)? http://www.odata.org/
What is JPA (JAVA Persistence API)? http://wiki.eclipse.org/EclipseLink/FAQ/JPA
Why does SAP prefer OData as connectivity?
These two SCN blogs motivates the reasons why SAP prefers to use OData as connectivity:
Hope this blog covers the topic of setting up and implementation of OData services using Apache olingo API. In next part of my blog I will be explaining about the Entity class overview in JPA based web application.
I have checked in my code to github.
Few of my other blog post:-
- Database migration in SAP Cloud platform using Liquibase
- CDS extension feature in HANA
- logging in SCP with Java and Tomee using slf4j, logback, Jolokia
- Binary content upload & download from HANA DB via Apache Olingo OData in SCP
Suggestions and questions are welcomed !!!
Thank you for this blog series!
Have you or are you willing to share the project .war file or source code via github?
If not, is the project this blog was initiated with available on Github?
The JAVA applications I've attempted to use thus far, do not contain both persistence.xml and resources.xml as mentioned as a pre-requisite.
Either would be extremely helpful! Thank you in advance – Austin
Hi Austin,
Thanks for your positive feedback !!
In the above blog, I have mentioned the code for persistence.xml refer section 3.1 Configure persistence.xml in JPA model, for resource.xml refer section 3.1.1 Configure resource.xml in JPA model.
This is what you need to add code snippet in persistence.xml and resource.xml files as mentioned in project structure.
Will inform, once the code is uploaded in github.
Thanks,
Rajnish
Rajnish,
I want to clone the project that you initiated the tutorial with (section 2.1) so that I can follow along and deploy this tutorial on my HCP trial account. Is the project you used as a Pre-requisite in section 2.1 available? If not, do you have know of another github based JPA Project that is compatible with this tutorial?
Thank you in advance!
Hi Austin,
i have already checked in the code to github,
please find the github url github.
Thanks,
Rajnish
Hello Rajnish,
do you plan to update this blog post with an odata version 4 service?
Or is there a better approach than olingo when the requirement is to support actions and functions and customized insert/ update processing within an odata version 4 endpoint, maybe node.js?
Thanks,
Oliver
Hi guys,
Just wondering if anyone can enlighten me on this issue I'm having below when deploying app on CF? I believe my app don't actually using this AuditLog at all
2021-08-30T23:23:27.09+0800 [APP/PROC/WEB/0] OUT { "written_at":"2021-08-30T15:23:27.083Z","written_ts":1630337007085000000,"tenant_id":"-","component_type":"application","component_id":"c75d251f-1163-4d26-ae20-c2841f816513","space_name":"dev","component_name":"myapp","component_instance":"0","organization_id":"c8ff02d1-2a59-45db-b0ab-9a3403b999da","correlation_id":"-","organization_name":"7b8311d5trial","space_id":"819d0cea-2da1-4d63-960c-a1e072b635ee","container_id":"10.32.3.7","tenant_subdomain":"-","type":"log","logger":"/System/AuditLog","thread":"main","level":"ERROR","categories":[],"msg":"Cannot send audit log message to the backend: audit log service environment configuration is missing or incomplete." }
What is the significant of this log:
Cannot send audit log message to the backend: audit log service environment configuration is missing or incomplete.
Thanks.