/osgi-jax-rs-connector

An OSGi - JAX-RS 2.0 Connector, software repository available on the link below

Primary LanguageJava

OSGi - JAX-RS Connector 3.2.0 Build Status


JAX-RS (JSR 311) is the community-driven standard for building RESTful web services with Java. The reference implementation for JAX-RS is Jersey and ships as OSGi bundles. This project connects Jersey and OSGi at the service level. This means that OSGi services can be published as RESTful web services by simply registering them as OSGi services and also consumed as OSGi services ;).

To see how to get started with JAX-RS 2.0 and Jersey please read the Jersey getting started guide.

Features

The OSGi-JAX-RS Connector provides two bundles. A publisher and a consumer. Both can be used completely separately or together, it's up to you. Additional the connector provides custom @Provider and Feature implementations that can be used optionally.

Publisher

The publisher is located in the bundle com.eclipsesource.jaxrs.publisher. All it does is tracking services. When it spots a service that is annotated with the JAX-RS annotations @Path or @Provider the work begins. The publisher hooks these services into Jersey and the OSGi HTTPService. Basically this means it publishes them as RESTful web services. It's just that simple.

By default the publisher registers the services using the context path /services. This means an OSGi service that is annotated with @Path( "/foo" ) will be available using the path /services/foo. This context path is configurable using the OSGi configuration admin. You can configure the service using the service.pid com.eclipsesource.jaxrs.connector and the property root to define a custom path.

As said earlier, the publisher uses the OSGi HTTPService to publish the services. As a result all configuration topics regarding ports, protocol and so on are up to the HTTPService implementation of your choice.

If it's your wish to publish services on different ports, just register them and add the service property http.port with the port of your choice. Of course it's necessary that an HTTPService is up and running on such a port.

Consumer

The consumer is located in the bundle com.eclipsesource.jaxrs.consumer. The idea of the consumer is to reuse your @Path and @Provider interfaces for calling a service. From a technical point of view it takes your interfaces together with a base url and creates Java Proxies. These proxies will make an HTTP call when a method will be invoked on it. The proxy knows which http method, parameters and so on it should use because you have it defined with the JAX-RS annotations. The consumer uses the JAX-RS 2.0 client API to send requests. So, there will be no additional dependencies.

A nice side effect of the consumer is, that it does not need OSGi. It's just a jar that can be used to create the mentioned proxies. See the ConsumerFactory for more information.

When using it together with OSGi it provides a helper to create your proxies and automatically register them as OSGi services. But it's up to you if you want to take care regarding the publishing by your own. See the ConsumerPublisher for more information.

A detailed explanation of the concepts of the consumer together with some examples can be found on our blog: Consuming REST services in Java the cool way, Consuming REST services in OSGi the cool way.

Providers

The custom @Provider and Feature implementations are located in their own features. The following features are currently included.

  • com.eclipsesource.jaxrs.provider.moxy - Allows the de/serialization using EclipseLink MOXy.
  • com.eclipsesource.jaxrs.provider.gson - Allows the de/serialization using Gson.
  • com.eclipsesource.jaxrs.provider.security - Provides an OSGi friendly integration of Jersey's/JAX-RS security features. Read the wiki for more information.
  • com.eclipsesource.jaxrs.provider.sse - Provides an integration of Jersey's SSE feature.

Installation

To ease the installation we provide a p2 repository and we publish the connector to maven central.

p2

Install from this software repository into your target: http://hstaudacher.github.io/osgi-jax-rs-connector

Please note: If dependencies can't be satisfied please disable "include required software" within the target editor.

Maven

Publisher:

<dependency>
    <groupId>com.eclipsesource</groupId>
    <artifactId>osgi-jaxrs-connector</artifactId>
</dependency>

Consumer:

<dependency>
    <groupId>com.eclipsesource</groupId>
    <artifactId>jaxrs-consumer</artifactId>
</dependency>

Please note: Due to a renaming in version 3.0 the publisher is still called connector within maven central.

Usage

Basically all you need to to is add the publisher and/or the consumer to your OSGi container and start them. For people new to OSGi the steps to get started with the connector and Eclipse are described below.

  1. Add the com.eclipsesource.jaxrs.connector.feature (OSGi JAX-RS Connector) to your target using the url mentioned in the Installation/p2 section.
  2. Add the com.eclipsesource.jaxrs.publisher and its dependencies bundles to your OSGi instance.
  3. Convert some OSGi services to resources like in this tutorial
  4. Point your client to the specified url. Don't forget that the default root path is /services. So registering a service with the path /example would lead to /services/example.

The steps how the consumer will be handled are dscribed in this post.

Examples

Examples for the publisher and consumer can be found within the examples folder.

Publisher Examples

Some examples exist for the publisher. They are located in the bundles com.eclipsesource.jaxrs.connector.example and com.eclipsesource.jaxrs.connector.example.ds. As the names say one uses Declarative services and the other don't. In both exampels a simple POJO is annotated with @Path and will be registered as an OSGIi service. Both bundles contain an Eclipse Launch Configuration to start them (please don't forget to add the publisher bundle to the launch config). After starting the launch configs the service will be available at http://localhost:9090/services/osgi-jax-rs.

As a bonus the com.eclipsesource.jaxrs.connector.example.ds contains an example how to use the @Provider together with the publisher.

Besides these basic example two example exist that are shwoing how the security integration and Jersey's SSE integration works.

Consumer Examples

The example for the consumer is splitted into two bundles called com.eclipsesource.jaxrs.consumer.example and com.eclipsesource.jaxrs.consumer.example.caller. The example bundle contains the data model and the resource interface while the caller just calls the service. The example fetches data from github and is pretty simple.

Requirements

  • OSGi Core Specification 4.2 and an OSGi HttpService implementation (e.g. Equinox, Felix).
  • JRE 1.6 (same as Jersey)

Alternatives

Jersey version

Jersey 2.5.0 + dependencies are included in the p2 software repository.

Changelog

Checkout the github releases.

License

The code is published under the terms of the Eclipse Public License, version 1.0.

Included binaries from Jersey, version 2.5.0, which are published under two licenses, the CDDL 1.1 and GPL 2 with CPE