From 3a4f598d47254e07c62776324e775f39d595ff5f Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Mon, 27 Jan 2014 07:05:20 +0000
Subject: [PATCH] #395 feature - Added license download and activate actions

---
 securis/src/main/java/net/curisit/securis/services/LicenseResource.java |   99 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 96 insertions(+), 3 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
index f9cb7c3..3e3898d 100644
--- a/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseResource.java
@@ -27,6 +27,7 @@
 import net.curisit.integrity.exception.CurisException;
 import net.curisit.securis.DefaultExceptionHandler;
 import net.curisit.securis.db.License;
+import net.curisit.securis.db.LicenseHistory;
 import net.curisit.securis.db.Pack;
 import net.curisit.securis.db.User;
 import net.curisit.securis.security.BasicSecurityContext;
@@ -119,6 +120,92 @@
 		return Response.ok(lic).build();
 	}
 
+	/**
+	 * 
+	 * @return The license file, only of license is active
+	 */
+	@GET
+	@Path("/{licId}/download")
+	@Securable
+	@Produces(
+		{ MediaType.APPLICATION_OCTET_STREAM })
+	public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) {
+		log.info("Getting license data for id: {}: ", licId);
+		if (licId == null || licId.equals("")) {
+			log.error("License ID is mandatory");
+			return Response.status(Status.NOT_FOUND).build();
+		}
+
+		EntityManager em = emProvider.get();
+		License lic = em.find(License.class, licId);
+		if (lic == null) {
+			log.error("License with id {} not found in DB", licId);
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build();
+		}
+		if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
+			if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
+				log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
+				return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
+			}
+		}
+		if (lic.getLicenseData() == null) {
+			log.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal());
+			return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License has not contain data to generate license file").build();
+		}
+		if (lic.getStatus() != License.Status.ACTIVE) {
+			log.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal());
+			return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License is not active, so It can not be downloaded").build();
+		}
+		return Response.ok(lic.getLicenseData()).build();
+	}
+
+	@PUT
+	@POST
+	@Path("/{licId}/activate")
+	@Securable
+	@Transactional
+	@Consumes(MediaType.APPLICATION_JSON)
+	@Produces(
+		{ MediaType.APPLICATION_JSON })
+	public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) {
+		log.info("Getting license data for id: {}: ", licId);
+		if (licId == null || licId.equals("")) {
+			log.error("License ID is mandatory");
+			return Response.status(Status.NOT_FOUND).build();
+		}
+
+		EntityManager em = emProvider.get();
+		License lic = em.find(License.class, licId);
+		if (lic == null) {
+			log.error("License with id {} not found in DB", licId);
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build();
+		}
+		if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
+			if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
+				log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
+				return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
+			}
+		}
+
+		User user = null;
+		try {
+			user = getUser(bsc.getUserPrincipal().getName(), em);
+		} catch (CurisException ex) {
+			return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Current user not found in DB: " + bsc.getUserPrincipal()).build();
+		}
+
+		lic.setStatus(License.Status.ACTIVE);
+		lic.setModificationTimestamp(new Date());
+		em.persist(lic);
+		LicenseHistory lh = new LicenseHistory();
+		lh.setLicense(lic);
+		lh.setUser(user);
+		lh.setTimestamp(new Date());
+		lh.setAction(LicenseHistory.Actions.ACTIVATE);
+		em.persist(lh);
+		return Response.ok(lic).build();
+	}
+
 	@POST
 	@Path("/")
 	@Consumes(MediaType.APPLICATION_JSON)
@@ -145,9 +232,9 @@
 			}
 		}
 
+		User createdBy = null;
 		try {
-			User createdBy = getUser(bsc.getUserPrincipal().getName(), em);
-			lic.setCreatedBy(createdBy);
+			createdBy = getUser(bsc.getUserPrincipal().getName(), em);
 		} catch (CurisException ex) {
 			String createdByUsername = lic.getCreatedById();
 			log.error("License created by user with id {} not found in DB", createdByUsername);
@@ -155,11 +242,17 @@
 		}
 
 		// ODO: Manage status if request data is set
-		lic.setCanceledBy(null);
+		lic.setCreatedBy(createdBy);
 		lic.setStatus(License.Status.CREATED);
 		lic.setCreationTimestamp(new Date());
 		lic.setModificationTimestamp(lic.getCreationTimestamp());
 		em.persist(lic);
+		LicenseHistory lh = new LicenseHistory();
+		lh.setLicense(lic);
+		lh.setUser(createdBy);
+		lh.setTimestamp(new Date());
+		lh.setAction(LicenseHistory.Actions.CREATE);
+		em.persist(lh);
 
 		return Response.ok(lic).build();
 	}

--
Gitblit v1.3.2