package net.curisit.securis.services; import java.io.IOException; import java.net.URI; import java.text.MessageFormat; import javax.inject.Named; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import net.curisit.integrity.beans.ServerConfigVersions; import net.curisit.integrity.beans.ServiceResponse; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; // The Java class will be hosted at the URI path "/myresource" @Path("/license") public class LicenseServices { // private LicenseHelper licenseHelper = InjectorFactory.getInjector().getInstance(LicenseHelper.class); private static final Logger log = LoggerFactory.getLogger(LicenseServices.class); private static final int DEFAULT_LICENSE_EXPIRATION = 365; private static final String LICENSE_STRING = "CurisIntegrity Config Server v{0}.{1}"; @com.google.inject.Inject @Named("base-uri") private URI uri; public LicenseServices() { } /** * * @return the server version in format majorVersion.minorVersion */ @GET @Produces( { MediaType.TEXT_HTML }) public Response index() { try { String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/index.html")); return Response.ok().entity(index).build(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Response.ok().entity(MessageFormat.format(LICENSE_STRING, 0, 1)).build(); } @GET @Path("/dummy") @Produces( { MediaType.TEXT_PLAIN }) public Response dummy(@Context HttpServletRequest request) { log.info("Request: " + request.getPathInfo()); return Response.ok().entity((uri == null)).build(); } /** * @return the version of the three entities that can be synchronized (Users, DataSet and Settings) */ @GET @Path("/current/{license}") @Produces( { MediaType.APPLICATION_JSON }) public ServiceResponse status(@PathParam("license") String license, @DefaultValue("-1") @QueryParam("minorVersion") int minorVersion, @DefaultValue("-1") @QueryParam("majorVersion") int majorVersion) { log.info("Called 'current' service with license: {}", license); ServiceResponse response = new ServiceResponse(); return response; } // // private ServiceResponse buildErrorResponse(ServiceResponse response, String msgErrorCode) { // response.setSuccess(false); // response.setErrorMessage(localManager.getString(msgErrorCode)); // response.setErrorMessageCode(msgErrorCode); // return response; // } // // private Date calculateCaducation() { // Integer licenseExpiration = systemParams.getParamAsInt(SystemParams.Keys.CONFIG_SERVER_LICENSE_EXPIRATION); // if (licenseExpiration == null) // licenseExpiration = DEFAULT_LICENSE_EXPIRATION; // return Utils.addDays(new Date(), licenseExpiration); // } // // private boolean validateLicense(String license) { // BasicApplication ba = basicApplicationDao.findByLicense(license); // return (ba != null); // } // // private boolean validateVersion(int minorVersion, int majorVersion) { // return (versionManager.getMajorVersion() == majorVersion); // } // // private BasicApplication findBasicApp(String license) { // BasicApplication ba = basicApplicationDao.findByLicense(license); // return ba; // } // // private License generateLicense() { // // TODO complete all field of the license // License license = new License(); // license.setCustomerCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CUSTOMER_CODE)); // license.setCSCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CS_CODE)); // license.setCRCLogo("00000000"); // license.setExpirationDate(calculateCaducation()); // license.setInstallCode(codeGenerator.generateInstalationNumber()); // return license; // } }