| .. | .. |
|---|
| 40 | 40 | import com.google.inject.persist.Transactional; |
|---|
| 41 | 41 | |
|---|
| 42 | 42 | /** |
|---|
| 43 | | - * License resource, this service will provide methods to create, modify and delete licenses |
|---|
| 43 | + * License resource, this service will provide methods to create, modify and |
|---|
| 44 | + * delete licenses |
|---|
| 44 | 45 | * |
|---|
| 45 | 46 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 46 | 47 | */ |
|---|
| 47 | 48 | @Path("/license") |
|---|
| 48 | 49 | public class LicenseResource { |
|---|
| 49 | 50 | |
|---|
| 50 | | - private static final Logger LOG = LogManager.getLogger(LicenseResource.class); |
|---|
| 51 | + private static final Logger LOG = LogManager.getLogger(LicenseResource.class); |
|---|
| 51 | 52 | |
|---|
| 52 | | - @Inject |
|---|
| 53 | | - TokenHelper tokenHelper; |
|---|
| 53 | + @Inject |
|---|
| 54 | + TokenHelper tokenHelper; |
|---|
| 54 | 55 | |
|---|
| 55 | | - @Inject |
|---|
| 56 | | - Provider<EntityManager> emProvider; |
|---|
| 56 | + @Inject |
|---|
| 57 | + Provider<EntityManager> emProvider; |
|---|
| 57 | 58 | |
|---|
| 58 | | - public LicenseResource() { |
|---|
| 59 | | - } |
|---|
| 59 | + public LicenseResource() {} |
|---|
| 60 | 60 | |
|---|
| 61 | | - /** |
|---|
| 62 | | - * |
|---|
| 63 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 64 | | - */ |
|---|
| 65 | | - @GET |
|---|
| 66 | | - @Path("/") |
|---|
| 67 | | - @Securable |
|---|
| 68 | | - @Produces( |
|---|
| 69 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 70 | | - public Response index(@QueryParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 71 | | - LOG.info("Getting licenses list "); |
|---|
| 61 | + /** |
|---|
| 62 | + * |
|---|
| 63 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 64 | + */ |
|---|
| 65 | + @GET |
|---|
| 66 | + @Path("/") |
|---|
| 67 | + @Securable |
|---|
| 68 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 69 | + public Response index(@QueryParam("packId") Integer packId, @Context BasicSecurityContext bsc) { |
|---|
| 70 | + LOG.info("Getting licenses list "); |
|---|
| 72 | 71 | |
|---|
| 73 | | - EntityManager em = emProvider.get(); |
|---|
| 72 | + EntityManager em = emProvider.get(); |
|---|
| 74 | 73 | |
|---|
| 75 | | - if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 76 | | - Pack pack = em.find(Pack.class, packId); |
|---|
| 77 | | - if (pack == null) |
|---|
| 78 | | - return Response.ok().build(); |
|---|
| 79 | | - if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) { |
|---|
| 80 | | - LOG.error("Pack with id {} not accesible by user {}", pack, bsc.getUserPrincipal()); |
|---|
| 81 | | - return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack licenses").build(); |
|---|
| 82 | | - } |
|---|
| 83 | | - } |
|---|
| 84 | | - TypedQuery<License> q = em.createNamedQuery("list-licenses-by-pack", License.class); |
|---|
| 85 | | - q.setParameter("packId", packId); |
|---|
| 86 | | - List<License> list = q.getResultList(); |
|---|
| 74 | + if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 75 | + Pack pack = em.find(Pack.class, packId); |
|---|
| 76 | + if (pack == null) { |
|---|
| 77 | + return Response.ok().build(); |
|---|
| 78 | + } |
|---|
| 79 | + if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) { |
|---|
| 80 | + LOG.error("Pack with id {} not accesible by user {}", pack, bsc.getUserPrincipal()); |
|---|
| 81 | + return Response.status(Status.UNAUTHORIZED) |
|---|
| 82 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack licenses").build(); |
|---|
| 83 | + } |
|---|
| 84 | + } |
|---|
| 85 | + TypedQuery<License> q = em.createNamedQuery("list-licenses-by-pack", License.class); |
|---|
| 86 | + q.setParameter("packId", packId); |
|---|
| 87 | + List<License> list = q.getResultList(); |
|---|
| 87 | 88 | |
|---|
| 88 | | - return Response.ok(list).build(); |
|---|
| 89 | | - } |
|---|
| 89 | + return Response.ok(list).build(); |
|---|
| 90 | + } |
|---|
| 90 | 91 | |
|---|
| 91 | | - /** |
|---|
| 92 | | - * |
|---|
| 93 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 94 | | - * @throws SeCurisServiceException |
|---|
| 95 | | - */ |
|---|
| 96 | | - @GET |
|---|
| 97 | | - @Path("/{licId}") |
|---|
| 98 | | - @Securable |
|---|
| 99 | | - @Produces( |
|---|
| 100 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 101 | | - public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 102 | | - LOG.info("Getting organization data for id: {}: ", licId); |
|---|
| 92 | + /** |
|---|
| 93 | + * |
|---|
| 94 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 95 | + * @throws SeCurisServiceException |
|---|
| 96 | + */ |
|---|
| 97 | + @GET |
|---|
| 98 | + @Path("/{licId}") |
|---|
| 99 | + @Securable |
|---|
| 100 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 101 | + public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 102 | + LOG.info("Getting organization data for id: {}: ", licId); |
|---|
| 103 | 103 | |
|---|
| 104 | | - EntityManager em = emProvider.get(); |
|---|
| 105 | | - License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 106 | | - return Response.ok(lic).build(); |
|---|
| 107 | | - } |
|---|
| 104 | + EntityManager em = emProvider.get(); |
|---|
| 105 | + License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 106 | + return Response.ok(lic).build(); |
|---|
| 107 | + } |
|---|
| 108 | 108 | |
|---|
| 109 | | - /** |
|---|
| 110 | | - * |
|---|
| 111 | | - * @return The license file, only of license is active |
|---|
| 112 | | - * @throws SeCurisServiceException |
|---|
| 113 | | - */ |
|---|
| 114 | | - @GET |
|---|
| 115 | | - @Path("/{licId}/download") |
|---|
| 116 | | - @Securable |
|---|
| 117 | | - @Produces( |
|---|
| 118 | | - { MediaType.APPLICATION_OCTET_STREAM }) |
|---|
| 119 | | - public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 109 | + /** |
|---|
| 110 | + * |
|---|
| 111 | + * @return The license file, only of license is active |
|---|
| 112 | + * @throws SeCurisServiceException |
|---|
| 113 | + */ |
|---|
| 114 | + @GET |
|---|
| 115 | + @Path("/{licId}/download") |
|---|
| 116 | + @Securable |
|---|
| 117 | + @Produces({ MediaType.APPLICATION_OCTET_STREAM }) |
|---|
| 118 | + public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 120 | 119 | |
|---|
| 121 | | - EntityManager em = emProvider.get(); |
|---|
| 122 | | - License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 120 | + EntityManager em = emProvider.get(); |
|---|
| 121 | + License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 123 | 122 | |
|---|
| 124 | | - if (lic.getLicenseData() == null) { |
|---|
| 125 | | - LOG.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal()); |
|---|
| 126 | | - throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License has not contain data to generate license file"); |
|---|
| 127 | | - } |
|---|
| 128 | | - if (License.Status.isActionValid(License.Action.DOWNLOAD, lic.getStatus())) { |
|---|
| 129 | | - LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal()); |
|---|
| 130 | | - throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License is not active, so It can not be downloaded"); |
|---|
| 131 | | - } |
|---|
| 132 | | - return Response.ok(lic.getLicenseData()).build(); |
|---|
| 133 | | - } |
|---|
| 123 | + if (lic.getLicenseData() == null) { |
|---|
| 124 | + LOG.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal()); |
|---|
| 125 | + throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License has not contain data to generate license file"); |
|---|
| 126 | + } |
|---|
| 127 | + if (License.Status.isActionValid(License.Action.DOWNLOAD, lic.getStatus())) { |
|---|
| 128 | + LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal()); |
|---|
| 129 | + throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License is not active, so It can not be downloaded"); |
|---|
| 130 | + } |
|---|
| 131 | + return Response.ok(lic.getLicenseData()).build(); |
|---|
| 132 | + } |
|---|
| 134 | 133 | |
|---|
| 135 | | - @PUT |
|---|
| 136 | | - @POST |
|---|
| 137 | | - @Path("/{licId}/activate") |
|---|
| 138 | | - @Securable |
|---|
| 139 | | - @Transactional |
|---|
| 140 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 141 | | - @Produces( |
|---|
| 142 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 143 | | - public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 134 | + @PUT |
|---|
| 135 | + @POST |
|---|
| 136 | + @Path("/{licId}/activate") |
|---|
| 137 | + @Securable |
|---|
| 138 | + @Transactional |
|---|
| 139 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 140 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 141 | + public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 144 | 142 | |
|---|
| 145 | | - EntityManager em = emProvider.get(); |
|---|
| 146 | | - License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 143 | + EntityManager em = emProvider.get(); |
|---|
| 144 | + License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 147 | 145 | |
|---|
| 148 | | - if (License.Status.isActionValid(License.Action.ACTIVATION, lic.getStatus())) { |
|---|
| 149 | | - LOG.error("License with id {} can not be activated from current license status", licId); |
|---|
| 150 | | - throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId + " can not be activated from the current license status"); |
|---|
| 151 | | - } |
|---|
| 146 | + if (License.Status.isActionValid(License.Action.ACTIVATION, lic.getStatus())) { |
|---|
| 147 | + LOG.error("License with id {} can not be activated from current license status", licId); |
|---|
| 148 | + throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId |
|---|
| 149 | + + " can not be activated from the current license status"); |
|---|
| 150 | + } |
|---|
| 152 | 151 | |
|---|
| 153 | | - lic.setStatus(License.Status.ACTIVE); |
|---|
| 154 | | - lic.setModificationTimestamp(new Date()); |
|---|
| 155 | | - em.persist(lic); |
|---|
| 156 | | - User user = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 157 | | - em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE)); |
|---|
| 158 | | - return Response.ok(lic).build(); |
|---|
| 159 | | - } |
|---|
| 152 | + lic.setStatus(License.Status.ACTIVE); |
|---|
| 153 | + lic.setModificationTimestamp(new Date()); |
|---|
| 154 | + em.persist(lic); |
|---|
| 155 | + User user = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 156 | + em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE)); |
|---|
| 157 | + return Response.ok(lic).build(); |
|---|
| 158 | + } |
|---|
| 160 | 159 | |
|---|
| 161 | | - @PUT |
|---|
| 162 | | - @POST |
|---|
| 163 | | - @Path("/{licId}/send") |
|---|
| 164 | | - @Securable |
|---|
| 165 | | - @Transactional |
|---|
| 166 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 167 | | - @Produces( |
|---|
| 168 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 169 | | - public Response send(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 160 | + @PUT |
|---|
| 161 | + @POST |
|---|
| 162 | + @Path("/{licId}/send") |
|---|
| 163 | + @Securable |
|---|
| 164 | + @Transactional |
|---|
| 165 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 166 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 167 | + public Response send(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 170 | 168 | |
|---|
| 171 | | - EntityManager em = emProvider.get(); |
|---|
| 172 | | - License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 169 | + EntityManager em = emProvider.get(); |
|---|
| 170 | + License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 173 | 171 | |
|---|
| 174 | | - User user = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 175 | | - // TODO: Send mail with lic file |
|---|
| 176 | | - lic.setModificationTimestamp(new Date()); |
|---|
| 177 | | - em.persist(lic); |
|---|
| 178 | | - em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail())); |
|---|
| 179 | | - return Response.ok(lic).build(); |
|---|
| 180 | | - } |
|---|
| 172 | + User user = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 173 | + // TODO: Send mail with lic file |
|---|
| 174 | + lic.setModificationTimestamp(new Date()); |
|---|
| 175 | + em.persist(lic); |
|---|
| 176 | + em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail())); |
|---|
| 177 | + return Response.ok(lic).build(); |
|---|
| 178 | + } |
|---|
| 181 | 179 | |
|---|
| 182 | | - @PUT |
|---|
| 183 | | - @POST |
|---|
| 184 | | - @Path("/{licId}/cancel") |
|---|
| 185 | | - @Securable |
|---|
| 186 | | - @Transactional |
|---|
| 187 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 188 | | - @Produces( |
|---|
| 189 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 190 | | - public Response cancel(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 180 | + @PUT |
|---|
| 181 | + @POST |
|---|
| 182 | + @Path("/{licId}/cancel") |
|---|
| 183 | + @Securable |
|---|
| 184 | + @Transactional |
|---|
| 185 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 186 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 187 | + public Response cancel(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 191 | 188 | |
|---|
| 192 | | - EntityManager em = emProvider.get(); |
|---|
| 193 | | - License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 189 | + EntityManager em = emProvider.get(); |
|---|
| 190 | + License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 194 | 191 | |
|---|
| 195 | | - if (License.Status.isActionValid(License.Action.CANCEL, lic.getStatus())) { |
|---|
| 196 | | - LOG.error("License with id {} can not be canceled from current license status", licId); |
|---|
| 197 | | - throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId + " can not be canceled from the current license status"); |
|---|
| 198 | | - } |
|---|
| 192 | + if (License.Status.isActionValid(License.Action.CANCEL, lic.getStatus())) { |
|---|
| 193 | + LOG.error("License with id {} can not be canceled from current license status", licId); |
|---|
| 194 | + throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId |
|---|
| 195 | + + " can not be canceled from the current license status"); |
|---|
| 196 | + } |
|---|
| 199 | 197 | |
|---|
| 200 | | - lic.setStatus(License.Status.CANCELED); |
|---|
| 201 | | - lic.setModificationTimestamp(new Date()); |
|---|
| 202 | | - em.persist(lic); |
|---|
| 198 | + lic.setStatus(License.Status.CANCELED); |
|---|
| 199 | + lic.setModificationTimestamp(new Date()); |
|---|
| 200 | + em.persist(lic); |
|---|
| 203 | 201 | |
|---|
| 204 | | - User user = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 205 | | - em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CANCEL)); |
|---|
| 206 | | - return Response.ok(lic).build(); |
|---|
| 207 | | - } |
|---|
| 202 | + User user = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 203 | + em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CANCEL)); |
|---|
| 204 | + return Response.ok(lic).build(); |
|---|
| 205 | + } |
|---|
| 208 | 206 | |
|---|
| 209 | | - @POST |
|---|
| 210 | | - @Path("/") |
|---|
| 211 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 212 | | - @Securable |
|---|
| 213 | | - @Produces( |
|---|
| 214 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 215 | | - @Transactional |
|---|
| 216 | | - public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 217 | | - LOG.info("Creating new license from create()"); |
|---|
| 218 | | - EntityManager em = emProvider.get(); |
|---|
| 219 | | - Pack pack = null; |
|---|
| 220 | | - if (lic.getPackId() != null) { |
|---|
| 221 | | - pack = em.find(Pack.class, lic.getPackId()); |
|---|
| 222 | | - if (pack == null) { |
|---|
| 223 | | - LOG.error("License pack with id {} not found in DB", lic.getPackId()); |
|---|
| 224 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's pack not found with ID: " + lic.getPackId()).build(); |
|---|
| 225 | | - } else { |
|---|
| 226 | | - if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 227 | | - if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) { |
|---|
| 228 | | - LOG.error("License for pack with id {} can not be created by user {}", pack.getId(), bsc.getUserPrincipal()); |
|---|
| 229 | | - return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized action on pack license").build(); |
|---|
| 230 | | - } |
|---|
| 231 | | - } |
|---|
| 232 | | - } |
|---|
| 233 | | - } |
|---|
| 207 | + @POST |
|---|
| 208 | + @Path("/") |
|---|
| 209 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 210 | + @Securable |
|---|
| 211 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 212 | + @Transactional |
|---|
| 213 | + public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 214 | + LOG.info("Creating new license from create()"); |
|---|
| 215 | + EntityManager em = emProvider.get(); |
|---|
| 216 | + Pack pack = null; |
|---|
| 217 | + if (lic.getPackId() != null) { |
|---|
| 218 | + pack = em.find(Pack.class, lic.getPackId()); |
|---|
| 219 | + if (pack == null) { |
|---|
| 220 | + LOG.error("License pack with id {} not found in DB", lic.getPackId()); |
|---|
| 221 | + return Response.status(Status.NOT_FOUND) |
|---|
| 222 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's pack not found with ID: " + lic.getPackId()).build(); |
|---|
| 223 | + } else { |
|---|
| 224 | + if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 225 | + if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) { |
|---|
| 226 | + LOG.error("License for pack with id {} can not be created by user {}", pack.getId(), bsc.getUserPrincipal()); |
|---|
| 227 | + return Response.status(Status.UNAUTHORIZED) |
|---|
| 228 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized action on pack license").build(); |
|---|
| 229 | + } |
|---|
| 230 | + } |
|---|
| 231 | + } |
|---|
| 232 | + } |
|---|
| 234 | 233 | |
|---|
| 235 | | - User createdBy = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 234 | + User createdBy = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 236 | 235 | |
|---|
| 237 | | - // ODO: Manage status if request data is set |
|---|
| 238 | | - lic.setCreatedBy(createdBy); |
|---|
| 239 | | - lic.setStatus(License.Status.CREATED); |
|---|
| 240 | | - lic.setCreationTimestamp(new Date()); |
|---|
| 241 | | - lic.setModificationTimestamp(lic.getCreationTimestamp()); |
|---|
| 242 | | - em.persist(lic); |
|---|
| 243 | | - em.persist(createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE)); |
|---|
| 236 | + // ODO: Manage status if request data is set |
|---|
| 237 | + lic.setCreatedBy(createdBy); |
|---|
| 238 | + lic.setStatus(License.Status.CREATED); |
|---|
| 239 | + lic.setCreationTimestamp(new Date()); |
|---|
| 240 | + lic.setModificationTimestamp(lic.getCreationTimestamp()); |
|---|
| 241 | + em.persist(lic); |
|---|
| 242 | + em.persist(createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE)); |
|---|
| 244 | 243 | |
|---|
| 245 | | - return Response.ok(lic).build(); |
|---|
| 246 | | - } |
|---|
| 244 | + return Response.ok(lic).build(); |
|---|
| 245 | + } |
|---|
| 247 | 246 | |
|---|
| 248 | | - @POST |
|---|
| 249 | | - @Path("/") |
|---|
| 250 | | - @Consumes(MediaType.MULTIPART_FORM_DATA) |
|---|
| 251 | | - @Securable |
|---|
| 252 | | - @Produces( |
|---|
| 253 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 254 | | - @Transactional |
|---|
| 255 | | - public Response createWithFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException { |
|---|
| 256 | | - License lic = new License(); |
|---|
| 257 | | - lic.setCode(mpfdi.getFormDataPart("code", String.class, null)); |
|---|
| 258 | | - lic.setRequestData(mpfdi.getFormDataPart("request_data", String.class, null)); |
|---|
| 259 | | - lic.setPackId(mpfdi.getFormDataPart("pack_id", Integer.class, null)); |
|---|
| 260 | | - lic.setFullName(mpfdi.getFormDataPart("full_name", String.class, null)); |
|---|
| 261 | | - lic.setEmail(mpfdi.getFormDataPart("email", String.class, null)); |
|---|
| 262 | | - lic.setComments(mpfdi.getFormDataPart("comments", String.class, null)); |
|---|
| 247 | + @POST |
|---|
| 248 | + @Path("/") |
|---|
| 249 | + @Consumes(MediaType.MULTIPART_FORM_DATA) |
|---|
| 250 | + @Securable |
|---|
| 251 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 252 | + @Transactional |
|---|
| 253 | + public Response createWithFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException { |
|---|
| 254 | + License lic = new License(); |
|---|
| 255 | + lic.setCode(mpfdi.getFormDataPart("code", String.class, null)); |
|---|
| 256 | + lic.setRequestData(mpfdi.getFormDataPart("request_data", String.class, null)); |
|---|
| 257 | + lic.setPackId(mpfdi.getFormDataPart("pack_id", Integer.class, null)); |
|---|
| 258 | + lic.setFullName(mpfdi.getFormDataPart("full_name", String.class, null)); |
|---|
| 259 | + lic.setEmail(mpfdi.getFormDataPart("email", String.class, null)); |
|---|
| 260 | + lic.setComments(mpfdi.getFormDataPart("comments", String.class, null)); |
|---|
| 263 | 261 | |
|---|
| 264 | | - return create(lic, bsc); |
|---|
| 265 | | - } |
|---|
| 262 | + return create(lic, bsc); |
|---|
| 263 | + } |
|---|
| 266 | 264 | |
|---|
| 267 | | - @PUT |
|---|
| 268 | | - @POST |
|---|
| 269 | | - @Path("/{licId}") |
|---|
| 270 | | - @Securable |
|---|
| 271 | | - @Transactional |
|---|
| 272 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 273 | | - @Produces( |
|---|
| 274 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 275 | | - public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 276 | | - LOG.info("Modifying organization with id: {}", licId); |
|---|
| 265 | + @PUT |
|---|
| 266 | + @POST |
|---|
| 267 | + @Path("/{licId}") |
|---|
| 268 | + @Securable |
|---|
| 269 | + @Transactional |
|---|
| 270 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 271 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 272 | + public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 273 | + LOG.info("Modifying organization with id: {}", licId); |
|---|
| 277 | 274 | |
|---|
| 278 | | - EntityManager em = emProvider.get(); |
|---|
| 275 | + EntityManager em = emProvider.get(); |
|---|
| 279 | 276 | |
|---|
| 280 | | - License currentLicense = getCurrentLicense(licId, bsc, em); |
|---|
| 277 | + License currentLicense = getCurrentLicense(licId, bsc, em); |
|---|
| 281 | 278 | |
|---|
| 282 | | - currentLicense.setCode(lic.getCode()); |
|---|
| 283 | | - currentLicense.setFullName(lic.getFullName()); |
|---|
| 284 | | - currentLicense.setEmail(lic.getEmail()); |
|---|
| 285 | | - currentLicense.setRequestData(lic.getRequestData()); |
|---|
| 286 | | - currentLicense.setModificationTimestamp(new Date()); |
|---|
| 287 | | - em.persist(currentLicense); |
|---|
| 279 | + currentLicense.setCode(lic.getCode()); |
|---|
| 280 | + currentLicense.setFullName(lic.getFullName()); |
|---|
| 281 | + currentLicense.setEmail(lic.getEmail()); |
|---|
| 282 | + currentLicense.setRequestData(lic.getRequestData()); |
|---|
| 283 | + currentLicense.setModificationTimestamp(new Date()); |
|---|
| 284 | + em.persist(currentLicense); |
|---|
| 288 | 285 | |
|---|
| 289 | | - return Response.ok(currentLicense).build(); |
|---|
| 290 | | - } |
|---|
| 286 | + return Response.ok(currentLicense).build(); |
|---|
| 287 | + } |
|---|
| 291 | 288 | |
|---|
| 292 | | - @DELETE |
|---|
| 293 | | - @Path("/{licId}") |
|---|
| 294 | | - @Transactional |
|---|
| 295 | | - @Securable |
|---|
| 296 | | - @Produces( |
|---|
| 297 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 298 | | - public Response delete(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 299 | | - LOG.info("Deleting license with id: {}", licId); |
|---|
| 300 | | - EntityManager em = emProvider.get(); |
|---|
| 301 | | - License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 289 | + @DELETE |
|---|
| 290 | + @Path("/{licId}") |
|---|
| 291 | + @Transactional |
|---|
| 292 | + @Securable |
|---|
| 293 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 294 | + public Response delete(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 295 | + LOG.info("Deleting license with id: {}", licId); |
|---|
| 296 | + EntityManager em = emProvider.get(); |
|---|
| 297 | + License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 302 | 298 | |
|---|
| 303 | | - if (lic.getStatus() != License.Status.CANCELED || lic.getStatus() != License.Status.CREATED) { |
|---|
| 304 | | - LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus()); |
|---|
| 305 | | - return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License can not be deleted in current status").build(); |
|---|
| 306 | | - } |
|---|
| 299 | + if (lic.getStatus() != License.Status.CANCELED || lic.getStatus() != License.Status.CREATED) { |
|---|
| 300 | + LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus()); |
|---|
| 301 | + return Response.status(Status.FORBIDDEN) |
|---|
| 302 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License can not be deleted in current status").build(); |
|---|
| 303 | + } |
|---|
| 307 | 304 | |
|---|
| 308 | | - em.remove(lic); |
|---|
| 309 | | - return Response.ok(Utils.createMap("success", true, "id", licId)).build(); |
|---|
| 310 | | - } |
|---|
| 305 | + em.remove(lic); |
|---|
| 306 | + return Response.ok(Utils.createMap("success", true, "id", licId)).build(); |
|---|
| 307 | + } |
|---|
| 311 | 308 | |
|---|
| 312 | | - private License getCurrentLicense(Integer licId, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException { |
|---|
| 313 | | - if (licId == null || licId.equals("")) { |
|---|
| 314 | | - LOG.error("License ID is mandatory"); |
|---|
| 315 | | - throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Missing license ID"); |
|---|
| 316 | | - } |
|---|
| 309 | + private License getCurrentLicense(Integer licId, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException { |
|---|
| 310 | + if (licId == null || "".equals(licId)) { |
|---|
| 311 | + LOG.error("License ID is mandatory"); |
|---|
| 312 | + throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Missing license ID"); |
|---|
| 313 | + } |
|---|
| 317 | 314 | |
|---|
| 318 | | - License lic = em.find(License.class, licId); |
|---|
| 319 | | - if (lic == null) { |
|---|
| 320 | | - LOG.error("License with id {} not found in DB", licId); |
|---|
| 321 | | - throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "License not found for ID: " + licId); |
|---|
| 322 | | - } |
|---|
| 323 | | - if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 324 | | - if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) { |
|---|
| 325 | | - LOG.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal()); |
|---|
| 326 | | - throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data"); |
|---|
| 327 | | - } |
|---|
| 328 | | - } |
|---|
| 329 | | - return lic; |
|---|
| 330 | | - } |
|---|
| 315 | + License lic = em.find(License.class, licId); |
|---|
| 316 | + if (lic == null) { |
|---|
| 317 | + LOG.error("License with id {} not found in DB", licId); |
|---|
| 318 | + throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "License not found for ID: " + licId); |
|---|
| 319 | + } |
|---|
| 320 | + if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 321 | + if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) { |
|---|
| 322 | + LOG.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal()); |
|---|
| 323 | + throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data"); |
|---|
| 324 | + } |
|---|
| 325 | + } |
|---|
| 326 | + return lic; |
|---|
| 327 | + } |
|---|
| 331 | 328 | |
|---|
| 332 | | - private User getUser(String username, EntityManager em) throws SeCurisServiceException { |
|---|
| 333 | | - User user = null; |
|---|
| 334 | | - if (username != null) { |
|---|
| 335 | | - user = em.find(User.class, username); |
|---|
| 336 | | - if (user == null) { |
|---|
| 337 | | - throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "User not found with username: " + username); |
|---|
| 338 | | - } |
|---|
| 339 | | - } |
|---|
| 340 | | - return user; |
|---|
| 341 | | - } |
|---|
| 329 | + private User getUser(String username, EntityManager em) throws SeCurisServiceException { |
|---|
| 330 | + User user = null; |
|---|
| 331 | + if (username != null) { |
|---|
| 332 | + user = em.find(User.class, username); |
|---|
| 333 | + if (user == null) { |
|---|
| 334 | + throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "User not found with username: " + username); |
|---|
| 335 | + } |
|---|
| 336 | + } |
|---|
| 337 | + return user; |
|---|
| 338 | + } |
|---|
| 342 | 339 | |
|---|
| 343 | | - private LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) { |
|---|
| 344 | | - LicenseHistory lh = new LicenseHistory(); |
|---|
| 345 | | - lh.setLicense(lic); |
|---|
| 346 | | - lh.setUser(user); |
|---|
| 347 | | - lh.setTimestamp(new Date()); |
|---|
| 348 | | - lh.setAction(action); |
|---|
| 349 | | - lh.setComments(comments); |
|---|
| 350 | | - return lh; |
|---|
| 351 | | - } |
|---|
| 340 | + private LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) { |
|---|
| 341 | + LicenseHistory lh = new LicenseHistory(); |
|---|
| 342 | + lh.setLicense(lic); |
|---|
| 343 | + lh.setUser(user); |
|---|
| 344 | + lh.setTimestamp(new Date()); |
|---|
| 345 | + lh.setAction(action); |
|---|
| 346 | + lh.setComments(comments); |
|---|
| 347 | + return lh; |
|---|
| 348 | + } |
|---|
| 352 | 349 | |
|---|
| 353 | | - private LicenseHistory createLicenseHistoryAction(License lic, User user, String action) { |
|---|
| 354 | | - return createLicenseHistoryAction(lic, user, action, null); |
|---|
| 355 | | - } |
|---|
| 350 | + private LicenseHistory createLicenseHistoryAction(License lic, User user, String action) { |
|---|
| 351 | + return createLicenseHistoryAction(lic, user, action, null); |
|---|
| 352 | + } |
|---|
| 356 | 353 | } |
|---|