Integration

How to use Apache Olingo4 with Apache Camel

This short tutorial shows how to use Apache Olingo4 in combination with Apache Camel. First, some background information:  We are working regularly with Apache Camel in our customer projects. In one of those projects we were faced by data in OData V4 (Open Data Protocol) format, that we had to interact with. OData is a web-based protocol, which has been defined initially by Microsoft but since version 4.0 OData is OASIS standard. That’s the point where Olingo came into place. Apache Olingo4 is a Java library that implements the Open Data Protocol 4.0.

In this post I will describe how to interact with data in OData format using Apache Camel and Olingo4. Therefore, I’ll explain the following steps:

  1. How to use Olingo4 with maven (dependency for pom.xml)
  2. Which data and data version I used for my example
  3. How I implemented the standard data operations: read, create, update and delete

How to use Olingo4 with maven

I used olingo4 version 2.23.2.fuse-750029-redhat-0001 and inserted the following dependencies in the pom.xml :

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-olingo4</artifactId>
   <version>2.23.2.fuse-750029-redhat-0001</version>
</dependency>
<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-olingo4-api</artifactId>
   <version>2.23.2.fuse-750029-redhat-0001</version>
</dependency>

 Which data and data version I used for my example

I use the data from https://services.odata.org/ in V4; take care that your OData has the correct version. The default version is V3, but for my example I used V4.

On this website you can find some data with full access (read-write) and some with the read-only access.

How I implemented the standard data operations

Read

For the first example I use some read-only data from https://services.odata.org/V4/Northwind/Northwind.svc/Regions. I create a Camel Route that can be started with rest. To start my route I use curl like: curl http://localhost:8080/camel/region/findRegion

Code Sample:

@Override
public void configure() {


  rest("/region")
     .get("findRegion")
     .to("direct:findRegion");
  from("direct:findRegion")
     .to("olingo4://read/Regions?serviceUri=https://services.odata.org/V4/Northwind/Northwind.svc");
  }

Create

In this example I add a new category with an ID and a Name to the data, and send a message back to the client. I set the body in the same format as in https://services.odata.org/V4/OData/OData.svc/Categories with an ID and a category name.

Code Sample:

@Override
public void configure(){
  rest("/categories")
    .get("createCategories")
    .to("direct:createCategories");
  from("direct:createCategories")
     .setBody().simple("{\n" +
           "\"ID\": 9,\n" +
           "\"Name\": \"NeueCategorie\"\n" +
           "}")
     .to("olingo4://create/Categories?serviceUri=https://services.odata.org/V4/(S(ixpsc2f4ra0xecu3a1bdizao))/OData/OData.svc")
     .transform().simple("createSuccess");
   }

Delete

In this example I delete the category with the ID=1 and send a message back to the client.

Code Sample:

 @Override
   public void configure(){
     rest("/categories")
        .get("deleteCategories")
        .to("direct:deleteCategories");
     from("direct:deleteCategories")
        .setHeader("CamelOlingo4.keyPredicate", simple("1"))
        .to("olingo4://delete/Categories?serviceUri=https://services.odata.org/V4/(S(ixpsc2f4ra0xecu3a1bdizao))/OData/OData.svc/")
        .transform().simple("deleteSuccess");

  }

Update

In this example I update a category (ID = 9) and send a message back to the client. I set the body in the same format as in https://services.odata.org/V4/OData/OData.svc/Categories with an ID and a category name.

Code Sample:

@Override
public void configure(){
   rest("/categories")
     .get("updateCategories")
     .to("direct:updateCategories");
   from("direct:updateCategories")
     .setBody().simple("{\n" +
         "\"ID\": 9,\n" +
         "\"Name\": \"Updated category\"\n" +
         "}")
      .to("olingo4://update/Categories(9)?serviceUri=https://services.odata.org/V4/(S(etj3qhdtakb5bkccbknwr2ht))/OData/OData.svc/")
      .transform().simple("updateSuccess");

  }

Conclusion

If you are already familiar with Apache Camel and want to work with OData the approach described in this blog post can be a suitable solution. I had some starting difficulties because I couldn’t find many examples online which is why I decided to provide you some concrete examples in the post. If you have any questions or further considerations feel free to contact me.

References

https://camel.apache.org/components/3.7.x/olingo4-component.html

https://services.odata.org/