Easy and Powerul OhData Client Library

OhData is providing a powerful and easy to use oData client library leveraging Olingo as a foundation.

License

License

Categories

Categories

Data CLI User Interface
GroupId

GroupId

org.doktorodata.ohdata
ArtifactId

ArtifactId

ohdata-client
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Easy and Powerul OhData Client Library
OhData is providing a powerful and easy to use oData client library leveraging Olingo as a foundation.
Project Organization

Project Organization

Michael Dietz
Source Code Management

Source Code Management

https://github.com/doktorodata/OhData-Client.git

Download ohdata-client

How to add to project

<!-- https://jarcasting.com/artifacts/org.doktorodata.ohdata/ohdata-client/ -->
<dependency>
    <groupId>org.doktorodata.ohdata</groupId>
    <artifactId>ohdata-client</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.doktorodata.ohdata/ohdata-client/
implementation 'org.doktorodata.ohdata:ohdata-client:1.0.0'
// https://jarcasting.com/artifacts/org.doktorodata.ohdata/ohdata-client/
implementation ("org.doktorodata.ohdata:ohdata-client:1.0.0")
'org.doktorodata.ohdata:ohdata-client:jar:1.0.0'
<dependency org="org.doktorodata.ohdata" name="ohdata-client" rev="1.0.0">
  <artifact name="ohdata-client" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.doktorodata.ohdata', module='ohdata-client', version='1.0.0')
)
libraryDependencies += "org.doktorodata.ohdata" % "ohdata-client" % "1.0.0"
[org.doktorodata.ohdata/ohdata-client "1.0.0"]

Dependencies

compile (6)

Group / Artifact Type Version
org.apache.olingo : olingo-odata2-core jar 2.0.7
junit : junit jar 4.8.2
org.apache.cxf : cxf-rt-frontend-jaxrs jar 2.7.6
org.slf4j : slf4j-log4j12 jar 1.7.1
org.json : json jar 20160810
com.sun.codemodel : codemodel jar 2.6

provided (2)

Group / Artifact Type Version
javax.servlet : servlet-api jar 2.5
org.slf4j : slf4j-api jar 1.7.1

Project Modules

There are no modules declared in this project.

OhData-Client - Consume OData in an easy and powerful way

Introduction

What is OData and what is the idea of this library

OData (http://www.odata.org/) is an "open protocol to allow the creation and consumption of queryable and interoperable RESTful APIs in a simple and standard way." It has the official slogan "the best way to REST". It is quite powerful and has various benefits, but also has it's downsides and complexities. I personally like OData and made good experience in the .NET world, but the in the Java world it is not so easy to use. "OhData Client" uses the most familiar OData library Olingo and builds on top a powerful library that makes it possible to consume OData V2 services in a few seconds. Come in and see with your own eyes.

Getting Started

Maven

Add maven dependency to your POM.

<dependency>
	<groupId>org.doktorodata.ohdata</groupId>
	<artifactId>OhData-Client</artifactId>
	<version>1.0.0</version>
</dependency>

Add a destination file

Create a destination file that contains the URL of your OData services. The base URL is also sufficient and you can add the full path later. Additionally the destination contains the option to provide the authentication method.

Remark This approach (storing the authentication in this text file) is currently the starting point, if you need something more sufficient please don't hesitate to build your own ConnectionFactory (e.g. using a secure store) by extending the abstract class org.doktorodata.ohdata.connectivity.ConnectionFactory,

Here are several examples for destination files:

Without Authentication

URL=http://services.odata.org/V2/Northwind/Northwind.svc
Authentication=None

With Basic Auth

URL=http://services.odata.org/V2/Northwind/Northwind.svc
Authentication=Basic
User=DRODATA
Password=Great1234

With oAuth Token

URL=http://services.odata.org/V2/Northwind/Northwind.svc
Authentication=oAuthToken
Password=kh3khuwz7z23h23h2iuh232732h3k2

With HTTP Proxy

URL=http://services.odata.org/V2/Northwind/Northwind.svc
Authentication=None
proxyhost=proxy.local
proxyport=9000

Generate the entity stubs

The usage of the OhData library starts with the generation of stubs. Therefore the metadata of the OData services is read and a the stub generator creates POJO classes for all entities in the OData services. These classes are later used in your productive code.

Therefore I usualy create a simple JAVA class that calls the EntityStubGenerator in the main method. This generator is called with the following attributes:

  • the base package to which the stubs shall be generated
  • the destination of the OData services
  • the base path to which the stubs shall be generated
public class RegenerateTestArctifacts {

	private static final String basePackage = "org.doktorodata.client.test.gen";
	private static final String basePath = "./src/test/java";
	private static final String destination = "src/test/resources/Northwind.destination";
	
	public static void main(String[] args) throws Exception {		
		EntityStubGenerator pocoGen = new EntityStubGenerator(basePackage, destination, basePath);
		pocoGen.generateEntityStubs();
		
	}
}

Start coding

After the successful generation of the stubs these classes can be used to call the OData services comfortably. The initialization part creates the OhCallerFactory with the name of the destination. The OhEntityAccess uses Generics and is initialized with the concrete entity class you want to access in the OData service. Afterwards the OhQuery allows to create complex queries and they are executed calling the query method of the OhEntityAccess. In this example you can see how it works.

String destination = "src/test/resources/Northwind.destination";
String productName = "Aniseed Syrup";

//Initialize
OhCallerFactory ocf = OhCallerFactory.createWithDestination(destination);
OhEntityAccess<Products> oea = new OhEntityAccess<>(ocf, Products.class);
	
//Query by Product Name
OhQuery query = new OhQuery();
query.filter("ProductName", "eq", product.getProductName());
oea.query(query).getFeed();

More Questions and Answers

Example OData Services

There are several OData service examples that can be access for testing purposes.

OData Versions V2 vs V4

This is one of the disadvantages. There are various versions of OData. The most used ones are V2 and V4. This OhData Client library currently only supports V2, but as soon as we have finished this we will continue with V4.

Can I contribute

Yes, feel free to send me an email and I'm happy for every support I can get.

Versions

Version
1.0.0
0.9.5
0.8.0