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.
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.
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 about the Logging in cloud foundry using jolokia apis