| .. | .. |
|---|
| 5 | 5 | import java.util.List; |
|---|
| 6 | 6 | |
|---|
| 7 | 7 | import javax.annotation.security.RolesAllowed; |
|---|
| 8 | +import javax.crypto.SealedObject; |
|---|
| 8 | 9 | import javax.inject.Inject; |
|---|
| 9 | 10 | import javax.inject.Provider; |
|---|
| 10 | 11 | import javax.persistence.EntityManager; |
|---|
| .. | .. |
|---|
| 24 | 25 | |
|---|
| 25 | 26 | import net.curisit.integrity.commons.Utils; |
|---|
| 26 | 27 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 28 | +import net.curisit.securis.SeCurisException; |
|---|
| 27 | 29 | import net.curisit.securis.db.LicenseType; |
|---|
| 28 | 30 | import net.curisit.securis.db.Organization; |
|---|
| 29 | 31 | import net.curisit.securis.db.Pack; |
|---|
| .. | .. |
|---|
| 45 | 47 | @Path("/pack") |
|---|
| 46 | 48 | public class PackResource { |
|---|
| 47 | 49 | |
|---|
| 48 | | - private static final Logger log = LogManager.getLogger(PackResource.class); |
|---|
| 50 | + private static final Logger LOG = LogManager.getLogger(PackResource.class); |
|---|
| 49 | 51 | |
|---|
| 50 | 52 | @Inject |
|---|
| 51 | 53 | TokenHelper tokenHelper; |
|---|
| .. | .. |
|---|
| 66 | 68 | @Produces( |
|---|
| 67 | 69 | { MediaType.APPLICATION_JSON }) |
|---|
| 68 | 70 | public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 69 | | - log.info("Getting packs list "); |
|---|
| 71 | + LOG.info("Getting packs list "); |
|---|
| 70 | 72 | |
|---|
| 71 | 73 | EntityManager em = emProvider.get(); |
|---|
| 72 | 74 | // TypedQuery<Pack> q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| 73 | 75 | |
|---|
| 74 | 76 | TypedQuery<Pack> q; |
|---|
| 75 | 77 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 76 | | - log.info("Getting all packs for user: " + bsc.getUserPrincipal()); |
|---|
| 78 | + LOG.info("Getting all packs for user: " + bsc.getUserPrincipal()); |
|---|
| 77 | 79 | q = em.createNamedQuery("list-packs", Pack.class); |
|---|
| 78 | 80 | } else { |
|---|
| 79 | 81 | q = em.createNamedQuery("list-packs-by-orgs", Pack.class); |
|---|
| .. | .. |
|---|
| 88 | 90 | } |
|---|
| 89 | 91 | |
|---|
| 90 | 92 | private Response generateErrorUnathorizedAccess(Pack pack, Principal user) { |
|---|
| 91 | | - log.error("Pack with id {} not accesible by user {}", pack, user); |
|---|
| 93 | + LOG.error("Pack with id {} not accesible by user {}", pack, user); |
|---|
| 92 | 94 | return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build(); |
|---|
| 93 | 95 | } |
|---|
| 94 | 96 | |
|---|
| .. | .. |
|---|
| 102 | 104 | @Produces( |
|---|
| 103 | 105 | { MediaType.APPLICATION_JSON }) |
|---|
| 104 | 106 | public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 105 | | - log.info("Getting pack data for id: {}: ", packId); |
|---|
| 107 | + LOG.info("Getting pack data for id: {}: ", packId); |
|---|
| 106 | 108 | if (packId == null || packId.equals("")) { |
|---|
| 107 | | - log.error("Pack ID is mandatory"); |
|---|
| 109 | + LOG.error("Pack ID is mandatory"); |
|---|
| 108 | 110 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 109 | 111 | } |
|---|
| 110 | 112 | |
|---|
| 111 | 113 | EntityManager em = emProvider.get(); |
|---|
| 112 | 114 | Pack pack = em.find(Pack.class, packId); |
|---|
| 113 | 115 | if (pack == null) { |
|---|
| 114 | | - log.error("Pack with id {} not found in DB", packId); |
|---|
| 116 | + LOG.error("Pack with id {} not found in DB", packId); |
|---|
| 115 | 117 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 116 | 118 | } |
|---|
| 117 | 119 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) { |
|---|
| .. | .. |
|---|
| 131 | 133 | { MediaType.APPLICATION_JSON }) |
|---|
| 132 | 134 | @Transactional |
|---|
| 133 | 135 | public Response create(Pack pack, @Context BasicSecurityContext bsc) { |
|---|
| 134 | | - log.info("Creating new pack"); |
|---|
| 136 | + LOG.info("Creating new pack"); |
|---|
| 135 | 137 | EntityManager em = emProvider.get(); |
|---|
| 136 | 138 | |
|---|
| 137 | | - Organization org = null; |
|---|
| 138 | | - if (pack.getOrgId() != null) { |
|---|
| 139 | | - org = em.find(Organization.class, pack.getOrgId()); |
|---|
| 140 | | - if (org == null) { |
|---|
| 141 | | - log.error("Organization pack with id {} not found in DB", pack.getOrgId()); |
|---|
| 142 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build(); |
|---|
| 143 | | - } |
|---|
| 139 | + try { |
|---|
| 140 | + setPackOrganization(pack, pack.getOrgId(), em); |
|---|
| 141 | + } catch (SeCurisException e) { |
|---|
| 142 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 144 | 143 | } |
|---|
| 145 | | - LicenseType lt = null; |
|---|
| 146 | | - if (pack.getLicTypeId() != null) { |
|---|
| 147 | | - lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 148 | | - if (lt == null) { |
|---|
| 149 | | - log.error("Pack license type with id {} not found in DB", pack.getLicTypeId()); |
|---|
| 150 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 151 | | - } |
|---|
| 144 | + |
|---|
| 145 | + try { |
|---|
| 146 | + setPackLicenseType(pack, pack.getLicTypeId(), em); |
|---|
| 147 | + } catch (SeCurisException e) { |
|---|
| 148 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 152 | 149 | } |
|---|
| 153 | 150 | |
|---|
| 154 | 151 | User user = em.find(User.class, bsc.getUserPrincipal().getName()); |
|---|
| 155 | 152 | |
|---|
| 156 | 153 | pack.setCreatedBy(user); |
|---|
| 157 | | - pack.setLicenseType(lt); |
|---|
| 158 | | - pack.setOrganization(org); |
|---|
| 159 | 154 | pack.setCreationTimestamp(new Date()); |
|---|
| 160 | 155 | em.persist(pack); |
|---|
| 161 | 156 | |
|---|
| 162 | 157 | return Response.ok(pack).build(); |
|---|
| 158 | + } |
|---|
| 159 | + |
|---|
| 160 | + private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException { |
|---|
| 161 | + LicenseType lt = null; |
|---|
| 162 | + if (licTypeId != null) { |
|---|
| 163 | + lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 164 | + if (lt == null) { |
|---|
| 165 | + LOG.error("Pack license type with id {} not found in DB", licTypeId); |
|---|
| 166 | + throw new SeCurisException("Pack license type not found with ID: " + licTypeId); |
|---|
| 167 | +// return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 168 | + } |
|---|
| 169 | + } |
|---|
| 170 | + pack.setLicenseType(lt); |
|---|
| 163 | 171 | } |
|---|
| 164 | 172 | |
|---|
| 165 | 173 | @PUT |
|---|
| .. | .. |
|---|
| 172 | 180 | @Produces( |
|---|
| 173 | 181 | { MediaType.APPLICATION_JSON }) |
|---|
| 174 | 182 | public Response modify(Pack pack, @PathParam("packId") Integer packId) { |
|---|
| 175 | | - log.info("Modifying pack with id: {}", packId); |
|---|
| 183 | + LOG.info("Modifying pack with id: {}", packId); |
|---|
| 176 | 184 | EntityManager em = emProvider.get(); |
|---|
| 177 | 185 | Pack currentPack = em.find(Pack.class, packId); |
|---|
| 178 | 186 | |
|---|
| 179 | | - Organization org = null; |
|---|
| 180 | | - if (pack.getOrgId() != null) { |
|---|
| 181 | | - org = em.find(Organization.class, pack.getOrgId()); |
|---|
| 182 | | - if (org == null) { |
|---|
| 183 | | - log.error("Organization pack with id {} not found in DB", pack.getOrgId()); |
|---|
| 184 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build(); |
|---|
| 185 | | - } |
|---|
| 187 | + try { |
|---|
| 188 | + setPackOrganization(currentPack, pack.getOrgId(), em); |
|---|
| 189 | + } catch (SeCurisException e) { |
|---|
| 190 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 186 | 191 | } |
|---|
| 187 | | - LicenseType lt = null; |
|---|
| 188 | | - if (pack.getLicTypeId() != null) { |
|---|
| 189 | | - lt = em.find(LicenseType.class, pack.getLicTypeId()); |
|---|
| 190 | | - if (lt == null) { |
|---|
| 191 | | - log.error("Pack license type with id {} not found in DB", pack.getLicTypeId()); |
|---|
| 192 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build(); |
|---|
| 193 | | - } |
|---|
| 192 | + |
|---|
| 193 | + try { |
|---|
| 194 | + setPackLicenseType(currentPack, pack.getLicTypeId(), em); |
|---|
| 195 | + } catch (SeCurisException e) { |
|---|
| 196 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 194 | 197 | } |
|---|
| 198 | + |
|---|
| 195 | 199 | currentPack.setLicensePreactivation(pack.isLicensePreactivation()); |
|---|
| 196 | | - currentPack.setLicenseType(lt); |
|---|
| 197 | | - currentPack.setOrganization(org); |
|---|
| 198 | 200 | currentPack.setCode(pack.getCode()); |
|---|
| 199 | 201 | currentPack.setComments(pack.getComments()); |
|---|
| 200 | 202 | currentPack.setNumLicenses(pack.getNumLicenses()); |
|---|
| .. | .. |
|---|
| 202 | 204 | em.persist(currentPack); |
|---|
| 203 | 205 | |
|---|
| 204 | 206 | return Response.ok(pack).build(); |
|---|
| 207 | + } |
|---|
| 208 | + |
|---|
| 209 | + private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException { |
|---|
| 210 | + Organization org = null; |
|---|
| 211 | + if (orgId != null) { |
|---|
| 212 | + org = em.find(Organization.class, orgId); |
|---|
| 213 | + if (org == null) { |
|---|
| 214 | + LOG.error("Organization pack with id {} not found in DB", orgId); |
|---|
| 215 | + throw new SeCurisException("Pack organization not found with ID: " + orgId); |
|---|
| 216 | + } |
|---|
| 217 | + } |
|---|
| 218 | + currentPack.setOrganization(org); |
|---|
| 205 | 219 | } |
|---|
| 206 | 220 | |
|---|
| 207 | 221 | @DELETE |
|---|
| .. | .. |
|---|
| 212 | 226 | @Produces( |
|---|
| 213 | 227 | { MediaType.APPLICATION_JSON }) |
|---|
| 214 | 228 | public Response delete(@PathParam("packId") String packId) { |
|---|
| 215 | | - log.info("Deleting pack with id: {}", packId); |
|---|
| 229 | + LOG.info("Deleting pack with id: {}", packId); |
|---|
| 216 | 230 | EntityManager em = emProvider.get(); |
|---|
| 217 | 231 | Pack org = em.find(Pack.class, Integer.parseInt(packId)); |
|---|
| 218 | 232 | if (org == null) { |
|---|
| 219 | | - log.error("Pack with id {} can not be deleted, It was not found in DB", packId); |
|---|
| 233 | + LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId); |
|---|
| 220 | 234 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId).build(); |
|---|
| 221 | 235 | } |
|---|
| 222 | 236 | |
|---|