Overview

The TrueLicense JAX-RS module provides a RESTful web service interface for consuming license keys.

Features

Using the TrueLicense JAX-RS you can…

  • Consume license keys using a RESTful web service interface based on JAX-RS.
  • Produce JSON or XML responses.
  • On error, respond with an appropriate HTTP status code, e.g. 402 “Payment Required”.

Usage

The service is implemented by the class LicenseConsumerService and accompanied by the exception mapper class LicenseConsumerServiceExceptionMapper. With a typical container, you should just let it scan the package net.java.truelicense.ws.rs so that it can find these classes.

The LicenseConsumerService needs a LicenseConsumerManager as its dependency. If no dependency injection framework is available, you can satisfy this dependency using:

package [...];

import javax.ws.rs.ext.*;
import net.java.truelicense.core.LicenseConsumerManager;

@Provider
public class LicenseConsumerManagerResolver
implements ContextResolver<LicenseConsumerManager> {
    @Override public LicenseConsumerManager getContext(Class<?> ignored) {
        return LicensingSchema.manager();
    }
}

where LicensingSchema is your custom licensing schema, e.g. as generated by the TrueLicense Maven Archetype.

If CDI is used for Dependency Injection, then you can let the container inject the dependency instead. If you have used the TrueLicense Maven Archetype to generate a project, then you just need to annotate the LicensingSchema class in the KeyMgr module like this:

package ${package}.keymgr;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import net.java.truelicense.core.LicenseConsumerManager;
[...]

public class LicensingSchema {

    [...]

    /** Returns the first license consumer manager in the configured chain-of-responsibility. [...] */
    @Produces @ApplicationScoped
    public static LicenseConsumerManager manager() { [...] }
}

The @Produces annotation tells the container that it should call the manager() method to obtain the license consumer manager. The returned object is a singleton which is typically not serializable, so the @ApplicationScoped annotation is used to wrap it in a serializable proxy with application scope. This is required to serialize any clients which hold a reference to a license consumer manager.

Last, but not least, you need to create a beans.xml file so that the CDI container scans the class path with the LicensingSchema class.