net.java.truelicense.ws.rs.LicenseConsumerService Maven / Gradle / Ivy
/*
* Copyright (C) 2005-2015 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truelicense.ws.rs;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import static javax.ws.rs.core.MediaType.*;
import javax.ws.rs.ext.*;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import net.java.truelicense.core.*;
import net.java.truelicense.core.io.*;
import net.java.truelicense.core.util.Objects;
import net.java.truelicense.obfuscate.Obfuscate;
/**
* A RESTful web service for license management in consumer applications.
*
* @since TrueLicense 2.3
* @author Christian Schlichtherle
*/
@Path("license")
@Produces({ APPLICATION_JSON, APPLICATION_XML, TEXT_XML })
@Immutable
public final class LicenseConsumerService {
private static final int BAD_REQUEST_STATUS_CODE = 400;
private static final int PAYMENT_REQUIRED_STATUS_CODE = 402;
private static final int NOT_FOUND_STATUS_CODE = 404;
@Obfuscate private static final String SUBJECT = "subject";
private static final QName subject = new QName(SUBJECT);
private final LicenseConsumerManager manager;
/**
* Constructs a license consumer service.
* This constructor immediately resolves the license consumer manager by
* looking up a {@code ContextResolver} in the
* given providers.
* You can provide a context resolver for this class like this:
* {@code
* package ...;
*
* import javax.jax.rs.ext.*;
* import net.java.truelicense.core.LicenseConsumerManager;
*
* @Provider
* public class LicenseConsumerManagerResolver
* implements ContextResolver {
* @Override public LicenseConsumerManager getContext(Class> type) {
* return LicensingSchema.manager();
* }
* }
* }
*
* where {@code LicensingSchema} is your custom licensing schema,
* e.g. as generated by the TrueLicense Maven Archetype.
*
* @param providers the providers.
*/
public LicenseConsumerService(@Context Providers providers) {
this(manager(providers));
}
private static LicenseConsumerManager manager(final Providers providers) {
final ContextResolver
resolver = providers.getContextResolver(LicenseConsumerManager.class, WILDCARD_TYPE);
if (null == resolver)
throw new IllegalArgumentException("No @Provider annotated ContextResolver available.");
return resolver.getContext(LicenseConsumerManager.class);
}
/**
* Constructs a license consumer service.
* This is the preferable constructor with Dependency Injection frameworks,
* e.g. CDI, Spring or Guice.
*
* @param manager the license consumer manager.
*/
@Inject
public LicenseConsumerService(final LicenseConsumerManager manager) {
this.manager = Objects.requireNonNull(manager);
}
@GET
@Path("subject")
@Produces(APPLICATION_JSON)
public String subjectAsJson() { return '"' + subject() + '"'; }
@GET
@Path("subject")
@Produces({ APPLICATION_XML, TEXT_XML })
public JAXBElement subjectAsXml() {
return new JAXBElement(subject, String.class, subject());
}
@GET
@Path("subject")
@Produces(TEXT_PLAIN)
public String subject() { return manager.subject(); }
@POST
public void install(final byte[] key)
throws LicenseConsumerServiceException {
final MemoryStore store = new MemoryStore();
store.data(key);
try {
manager.install(store);
} catch (LicenseManagementException ex) {
throw new LicenseConsumerServiceException(BAD_REQUEST_STATUS_CODE, ex);
}
}
@GET
public License view(
final @QueryParam("verify") @DefaultValue("false") boolean verify)
throws LicenseConsumerServiceException {
final License license;
try {
license = manager.view();
} catch (LicenseManagementException ex) {
throw new LicenseConsumerServiceException(NOT_FOUND_STATUS_CODE, ex);
}
if (verify) {
try {
manager.verify();
} catch (LicenseManagementException ex) {
throw new LicenseConsumerServiceException(PAYMENT_REQUIRED_STATUS_CODE, ex);
}
}
return license;
}
@DELETE
public void uninstall() throws LicenseConsumerServiceException {
try {
manager.uninstall();
} catch (LicenseManagementException ex) {
throw new LicenseConsumerServiceException(NOT_FOUND_STATUS_CODE, ex);
}
}
}