rsanchez
2016-03-03 2c9782e63437da9ef4c885fd5f793e7f7e79a6cb
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -25,6 +25,15 @@
2525 import javax.ws.rs.core.Response;
2626 import javax.ws.rs.core.Response.Status;
2727
28
+import org.apache.commons.io.IOUtils;
29
+import org.apache.commons.lang3.ObjectUtils;
30
+import org.apache.logging.log4j.LogManager;
31
+import org.apache.logging.log4j.Logger;
32
+
33
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
34
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
35
+import com.fasterxml.jackson.annotation.JsonProperty;
36
+
2837 import net.curisit.integrity.commons.Utils;
2938 import net.curisit.securis.DefaultExceptionHandler;
3039 import net.curisit.securis.LicenseGenerator;
....@@ -52,15 +61,6 @@
5261 import net.curisit.securis.utils.JsonUtils;
5362 import net.curisit.securis.utils.LicUtils;
5463
55
-import org.apache.commons.io.IOUtils;
56
-import org.apache.commons.lang3.ObjectUtils;
57
-import org.apache.logging.log4j.LogManager;
58
-import org.apache.logging.log4j.Logger;
59
-
60
-import com.fasterxml.jackson.annotation.JsonAutoDetect;
61
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
62
-import com.fasterxml.jackson.annotation.JsonProperty;
63
-
6464 /**
6565 * License resource, this service will provide methods to create, modify and
6666 * delete licenses
....@@ -70,570 +70,547 @@
7070 @Path("/license")
7171 public class LicenseResource {
7272
73
- private static final Logger LOG = LogManager.getLogger(LicenseResource.class);
73
+ private static final Logger LOG = LogManager.getLogger(LicenseResource.class);
7474
75
- @Inject
76
- private EmailManager emailManager;
75
+ @Inject
76
+ private EmailManager emailManager;
7777
78
- @Inject
79
- private UserHelper userHelper;
78
+ @Inject
79
+ private UserHelper userHelper;
8080
81
- @Inject
82
- private LicenseHelper licenseHelper;
81
+ @Inject
82
+ private LicenseHelper licenseHelper;
8383
84
- @Context
85
- EntityManager em;
84
+ @Context
85
+ EntityManager em;
8686
87
- @Inject
88
- private LicenseGenerator licenseGenerator;
87
+ @Inject
88
+ private LicenseGenerator licenseGenerator;
8989
90
- /**
91
- *
92
- * @return the server version in format majorVersion.minorVersion
93
- */
94
- @GET
95
- @Path("/")
96
- @Securable
97
- @Produces({
98
- MediaType.APPLICATION_JSON
99
- })
100
- public Response index(@QueryParam("packId") Integer packId, @Context BasicSecurityContext bsc) {
101
- LOG.info("Getting licenses list ");
90
+ /**
91
+ *
92
+ * @return the server version in format majorVersion.minorVersion
93
+ */
94
+ @GET
95
+ @Path("/")
96
+ @Securable
97
+ @Produces({ MediaType.APPLICATION_JSON })
98
+ public Response index(@QueryParam("packId") Integer packId, @Context BasicSecurityContext bsc) {
99
+ LOG.info("Getting licenses list ");
102100
103
- // EntityManager em = emProvider.get();
104
- em.clear();
101
+ // EntityManager em = emProvider.get();
102
+ em.clear();
105103
106
- if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
107
- Pack pack = em.find(Pack.class, packId);
108
- if (pack == null) {
109
- return Response.ok().build();
110
- }
111
- if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) {
112
- LOG.error("Pack with id {} not accesible by user {}", pack, bsc.getUserPrincipal());
113
- return Response.status(Status.UNAUTHORIZED)
114
- .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack licenses").build();
115
- }
116
- }
117
- TypedQuery<License> q = em.createNamedQuery("list-licenses-by-pack", License.class);
118
- q.setParameter("packId", packId);
119
- List<License> list = q.getResultList();
104
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
105
+ Pack pack = em.find(Pack.class, packId);
106
+ if (pack == null) {
107
+ return Response.ok().build();
108
+ }
109
+ if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) {
110
+ LOG.error("Pack with id {} not accesible by user {}", pack, bsc.getUserPrincipal());
111
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack licenses").build();
112
+ }
113
+ }
114
+ TypedQuery<License> q = em.createNamedQuery("list-licenses-by-pack", License.class);
115
+ q.setParameter("packId", packId);
116
+ List<License> list = q.getResultList();
120117
121
- return Response.ok(list).build();
122
- }
118
+ return Response.ok(list).build();
119
+ }
123120
124
- /**
125
- *
126
- * @return the server version in format majorVersion.minorVersion
127
- * @throws SeCurisServiceException
128
- */
129
- @GET
130
- @Path("/{licId}")
131
- @Securable
132
- @Produces({
133
- MediaType.APPLICATION_JSON
134
- })
135
- public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
136
- LOG.info("Getting organization data for id: {}: ", licId);
121
+ /**
122
+ *
123
+ * @return the server version in format majorVersion.minorVersion
124
+ * @throws SeCurisServiceException
125
+ */
126
+ @GET
127
+ @Path("/{licId}")
128
+ @Securable
129
+ @Produces({ MediaType.APPLICATION_JSON })
130
+ public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
131
+ LOG.info("Getting organization data for id: {}: ", licId);
137132
138
- // EntityManager em = emProvider.get();
139
- em.clear();
140
- License lic = getCurrentLicense(licId, bsc, em);
141
- return Response.ok(lic).build();
142
- }
133
+ // EntityManager em = emProvider.get();
134
+ em.clear();
135
+ License lic = getCurrentLicense(licId, bsc, em);
136
+ return Response.ok(lic).build();
137
+ }
143138
144
- /**
145
- *
146
- * @return The license file, only of license is active
147
- * @throws SeCurisServiceException
148
- */
149
- @GET
150
- @Path("/{licId}/download")
151
- @Securable
152
- @Produces({
153
- MediaType.APPLICATION_OCTET_STREAM
154
- })
155
- @EnsureTransaction
156
- public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
139
+ /**
140
+ *
141
+ * @return The license file, only of license is active
142
+ * @throws SeCurisServiceException
143
+ */
144
+ @GET
145
+ @Path("/{licId}/download")
146
+ @Securable
147
+ @Produces({ MediaType.APPLICATION_OCTET_STREAM })
148
+ @EnsureTransaction
149
+ public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
157150
158
- // EntityManager em = emProvider.get();
159
- License lic = getCurrentLicense(licId, bsc, em);
151
+ // EntityManager em = emProvider.get();
152
+ License lic = getCurrentLicense(licId, bsc, em);
160153
161
- if (lic.getLicenseData() == null) {
162
- LOG.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal());
163
- throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License has not contain data to generate license file");
164
- }
165
- if (!License.Status.isActionValid(License.Action.DOWNLOAD, lic.getStatus())) {
166
- LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal());
167
- throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License is not active, so It can not be downloaded");
168
- }
169
- em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.DOWNLOAD));
170
- return Response
171
- .ok(lic.getLicenseData())
172
- .header("Content-Disposition",
173
- String.format("attachment; filename=\"%s\"", lic.getPack().getLicenseType().getApplication().getLicenseFilename())).build();
174
- }
154
+ if (lic.getLicenseData() == null) {
155
+ LOG.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal());
156
+ throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License has not contain data to generate license file");
157
+ }
158
+ if (!License.Status.isActionValid(License.Action.DOWNLOAD, lic.getStatus())) {
159
+ LOG.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal());
160
+ throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License is not active, so It can not be downloaded");
161
+ }
162
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.DOWNLOAD));
163
+ return Response.ok(lic.getLicenseData())
164
+ .header("Content-Disposition", String.format("attachment; filename=\"%s\"", lic.getPack().getLicenseType().getApplication().getLicenseFilename())).build();
165
+ }
175166
176
- /**
177
- * Activate the given license
178
- *
179
- * @param licId
180
- * @param bsc
181
- * @return
182
- * @throws SeCurisServiceException
183
- */
184
- @PUT
185
- @POST
186
- @Path("/{licId}/activate")
187
- @Securable
188
- @EnsureTransaction
189
- @Consumes(MediaType.APPLICATION_JSON)
190
- @Produces({
191
- MediaType.APPLICATION_JSON
192
- })
193
- public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
167
+ /**
168
+ * Activate the given license
169
+ *
170
+ * @param licId
171
+ * @param bsc
172
+ * @return
173
+ * @throws SeCurisServiceException
174
+ */
175
+ @PUT
176
+ @POST
177
+ @Path("/{licId}/activate")
178
+ @Securable
179
+ @EnsureTransaction
180
+ @Consumes(MediaType.APPLICATION_JSON)
181
+ @Produces({ MediaType.APPLICATION_JSON })
182
+ public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
194183
195
- // EntityManager em = emProvider.get();
196
- License lic = getCurrentLicense(licId, bsc, em);
184
+ // EntityManager em = emProvider.get();
185
+ License lic = getCurrentLicense(licId, bsc, em);
197186
198
- if (!License.Status.isActionValid(License.Action.ACTIVATION, lic.getStatus())) {
199
- LOG.error("License with id {} can not be activated from current license status", licId);
200
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "License with id " + licId
201
- + " can not be activated from the current license status");
202
- }
187
+ if (!License.Status.isActionValid(License.Action.ACTIVATION, lic.getStatus())) {
188
+ LOG.error("License with id {} can not be activated from current license status", licId);
189
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "License with id " + licId + " can not be activated from the current license status");
190
+ }
203191
204
- if (lic.getPack().getNumAvailables() == 0) {
205
- throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The pack has not available licenses");
206
- }
192
+ if (lic.getPack().getNumAvailables() == 0) {
193
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "The pack has not available licenses");
194
+ }
207195
208
- validateRequestData(lic.getPack(), lic.getRequestData());
196
+ validateRequestData(lic.getPack(), lic.getRequestData(), lic.getActivationCode());
209197
210
- License existingLicense = License.findActiveLicenseByRequestData(lic.getRequestData(), em);
211
- if (existingLicense != null && existingLicense.getStatus() == LicenseStatus.ACTIVE) {
212
- throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "An active license already exists for the given request data");
213
- }
198
+ License existingLicense = License.findActiveLicenseByRequestData(lic.getRequestData(), em);
199
+ if (existingLicense != null && existingLicense.getStatus() == LicenseStatus.ACTIVE) {
200
+ throw new SeCurisServiceException(ErrorCodes.NO_AVAILABLE_LICENSES, "An active license already exists for the given request data");
201
+ }
214202
215
- lic.setStatus(LicenseStatus.ACTIVE);
216
- lic.setModificationTimestamp(new Date());
203
+ lic.setStatus(LicenseStatus.ACTIVE);
204
+ lic.setModificationTimestamp(new Date());
217205
218
- em.persist(lic);
219
- User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
220
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE));
221
- return Response.ok(lic).build();
222
- }
206
+ em.persist(lic);
207
+ User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
208
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE));
209
+ return Response.ok(lic).build();
210
+ }
223211
224
- /**
225
- * Send license file by email to the organization
226
- *
227
- * @param licId
228
- * @param bsc
229
- * @return
230
- * @throws SeCurisServiceException
231
- */
232
- @PUT
233
- @POST
234
- @Path("/{licId}/send")
235
- @Securable
236
- @EnsureTransaction
237
- @Consumes(MediaType.APPLICATION_JSON)
238
- @Produces({
239
- MediaType.APPLICATION_JSON
240
- })
241
- public Response send(@PathParam("licId") Integer licId, @DefaultValue("false") @FormParam("add_cc") Boolean addCC,
242
- @Context BasicSecurityContext bsc) throws SeCurisServiceException, SeCurisException {
212
+ /**
213
+ * Send license file by email to the organization
214
+ *
215
+ * @param licId
216
+ * @param bsc
217
+ * @return
218
+ * @throws SeCurisServiceException
219
+ */
220
+ @PUT
221
+ @POST
222
+ @Path("/{licId}/send")
223
+ @Securable
224
+ @EnsureTransaction
225
+ @Consumes(MediaType.APPLICATION_JSON)
226
+ @Produces({ MediaType.APPLICATION_JSON })
227
+ public Response send(@PathParam("licId") Integer licId, @DefaultValue("false") @FormParam("add_cc") Boolean addCC, @Context BasicSecurityContext bsc)
228
+ throws SeCurisServiceException, SeCurisException {
243229
244
- // EntityManager em = emProvider.get();
245
- License lic = getCurrentLicense(licId, bsc, em);
246
- Application app = lic.getPack().getLicenseType().getApplication();
247
- File licFile = null;
248
- if (lic.getLicenseData() == null) {
249
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "There is no license file available");
250
- }
230
+ // EntityManager em = emProvider.get();
231
+ License lic = getCurrentLicense(licId, bsc, em);
232
+ Application app = lic.getPack().getLicenseType().getApplication();
233
+ File licFile = null;
234
+ if (lic.getLicenseData() == null) {
235
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "There is no license file available");
236
+ }
251237
252
- if (lic.getFullName() == null) {
253
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Please add an user name in license data to send it the license file");
254
- }
238
+ if (lic.getFullName() == null) {
239
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Please add an user name in license data to send it the license file");
240
+ }
255241
256
- User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
257
- try {
258
- String subject = MessageFormat.format(Config.get(Config.KEYS.EMAIL_LIC_DEFAULT_SUBJECT), lic.getPack().getAppName());
259
- String email_tpl = IOUtils.toString(this.getClass().getResourceAsStream("/lic_email.template.en"));
260
- String body = MessageFormat.format(email_tpl, lic.getFullName(), app.getName());
261
- licFile = licenseHelper.createTemporaryLicenseFile(lic, app.getLicenseFilename());
242
+ User user = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
243
+ try {
244
+ String subject = MessageFormat.format(Config.get(Config.KEYS.EMAIL_LIC_DEFAULT_SUBJECT), lic.getPack().getAppName());
245
+ String email_tpl = IOUtils.toString(this.getClass().getResourceAsStream("/lic_email.template.en"));
246
+ String body = MessageFormat.format(email_tpl, lic.getFullName(), app.getName());
247
+ licFile = licenseHelper.createTemporaryLicenseFile(lic, app.getLicenseFilename());
262248
263
- emailManager.sendEmail(subject, body, lic.getEmail(), addCC ? user.getEmail() : null, licFile);
264
- } catch (IOException e) {
265
- LOG.error("Error creating temporary license file", e);
266
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available");
267
- } finally {
268
- if (licFile != null) {
269
- licFile.delete();
270
- licFile.getParentFile().delete();
271
- }
272
- }
249
+ emailManager.sendEmail(subject, body, lic.getEmail(), addCC ? user.getEmail() : null, licFile);
250
+ } catch (IOException e) {
251
+ LOG.error("Error creating temporary license file", e);
252
+ throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "There is no license file available");
253
+ } finally {
254
+ if (licFile != null) {
255
+ licFile.delete();
256
+ licFile.getParentFile().delete();
257
+ }
258
+ }
273259
274
- // lic.setModificationTimestamp(new Date());
275
- // em.merge(lic);
276
- em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
277
- return Response.ok(lic).build();
278
- }
260
+ // lic.setModificationTimestamp(new Date());
261
+ // em.merge(lic);
262
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
263
+ return Response.ok(lic).build();
264
+ }
279265
280
- /**
281
- * Cancel given license
282
- *
283
- * @param licId
284
- * @param bsc
285
- * @return
286
- * @throws SeCurisServiceException
287
- */
288
- @PUT
289
- @POST
290
- @Path("/{licId}/cancel")
291
- @Securable
292
- @EnsureTransaction
293
- @Consumes(MediaType.APPLICATION_JSON)
294
- @Produces({
295
- MediaType.APPLICATION_JSON
296
- })
297
- public Response cancel(@PathParam("licId") Integer licId, CancellationLicenseActionBean actionData, @Context BasicSecurityContext bsc)
298
- throws SeCurisServiceException {
266
+ /**
267
+ * Cancel given license
268
+ *
269
+ * @param licId
270
+ * @param bsc
271
+ * @return
272
+ * @throws SeCurisServiceException
273
+ */
274
+ @PUT
275
+ @POST
276
+ @Path("/{licId}/cancel")
277
+ @Securable
278
+ @EnsureTransaction
279
+ @Consumes(MediaType.APPLICATION_JSON)
280
+ @Produces({ MediaType.APPLICATION_JSON })
281
+ public Response cancel(@PathParam("licId") Integer licId, CancellationLicenseActionBean actionData, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
299282
300
- // EntityManager em = emProvider.get();
301
- License lic = getCurrentLicense(licId, bsc, em);
283
+ // EntityManager em = emProvider.get();
284
+ License lic = getCurrentLicense(licId, bsc, em);
302285
303
- if (!License.Status.isActionValid(License.Action.CANCEL, lic.getStatus())) {
304
- LOG.error("License with id {} can not be canceled from current license status", licId);
305
- throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId
306
- + " can not be canceled from the current license status");
307
- }
286
+ if (!License.Status.isActionValid(License.Action.CANCEL, lic.getStatus())) {
287
+ LOG.error("License with id {} can not be canceled from current license status", licId);
288
+ throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License with id " + licId + " can not be canceled from the current license status");
289
+ }
308290
309
- if (actionData.reason == null) {
310
- LOG.error("To cancel an active License we need a reason, lic ID: {}, user: {}", lic.getId(), bsc.getUserPrincipal().getName());
311
- throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "Active license with id " + licId
312
- + " can not be canceled without a reason");
313
- }
291
+ if (actionData.reason == null) {
292
+ LOG.error("To cancel an active License we need a reason, lic ID: {}, user: {}", lic.getId(), bsc.getUserPrincipal().getName());
293
+ throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "Active license with id " + licId + " can not be canceled without a reason");
294
+ }
314295
315
- licenseHelper.cancelLicense(lic, actionData.reason, bsc, em);
316
- return Response.ok(lic).build();
317
- }
296
+ licenseHelper.cancelLicense(lic, actionData.reason, bsc, em);
297
+ return Response.ok(lic).build();
298
+ }
318299
319
- /**
320
- * Check if there is some pack with the same code
321
- *
322
- * @param code
323
- * Pack code
324
- * @param em
325
- * DB session object
326
- * @return <code>true</code> if code is already used, <code>false</code>
327
- * otherwise
328
- */
329
- private boolean checkIfCodeExists(String code, EntityManager em) {
330
- TypedQuery<License> query = em.createNamedQuery("license-by-code", License.class);
331
- query.setParameter("code", code);
332
- int lics = query.getResultList().size();
333
- return lics > 0;
334
- }
300
+ /**
301
+ * Check if there is some pack with the same code
302
+ *
303
+ * @param code
304
+ * Pack code
305
+ * @param em
306
+ * DB session object
307
+ * @return <code>true</code> if code is already used, <code>false</code>
308
+ * otherwise
309
+ */
310
+ private boolean checkIfCodeExists(String code, EntityManager em) {
311
+ TypedQuery<License> query = em.createNamedQuery("license-by-code", License.class);
312
+ query.setParameter("code", code);
313
+ int lics = query.getResultList().size();
314
+ return lics > 0;
315
+ }
335316
336
- @POST
337
- @Path("/")
338
- @Consumes(MediaType.APPLICATION_JSON)
339
- @Securable
340
- @Produces({
341
- MediaType.APPLICATION_JSON
342
- })
343
- @EnsureTransaction
344
- public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
345
- // EntityManager em = emProvider.get();
317
+ @POST
318
+ @Path("/")
319
+ @Consumes(MediaType.APPLICATION_JSON)
320
+ @Securable
321
+ @Produces({ MediaType.APPLICATION_JSON })
322
+ @EnsureTransaction
323
+ public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
324
+ // EntityManager em = emProvider.get();
346325
347
- if (checkIfCodeExists(lic.getCode(), em)) {
348
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The license code is already used in an existing license");
349
- }
350
- if (lic.getActivationCode() == null) {
351
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The activation code is mandatory");
352
- }
353
- License existingLic = License.findLicenseByActivationCode(lic.getActivationCode(), em);
354
- if (existingLic != null) {
355
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The activation code is already used in: " + existingLic.getCode());
356
- }
357
- if (!LicUtils.checkValidLicenseCodeCrc(lic.getCode())) {
358
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The license code is not valid");
359
- }
360
- if (!Utils.isValidEmail(lic.getEmail())) {
361
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The user email should be a valid email");
362
- }
326
+ if (checkIfCodeExists(lic.getCode(), em)) {
327
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The license code is already used in an existing license");
328
+ }
329
+ if (lic.getActivationCode() == null) {
330
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The activation code is mandatory");
331
+ }
332
+ License existingLic = License.findLicenseByActivationCode(lic.getActivationCode(), em);
333
+ if (existingLic != null) {
334
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The activation code is already used in: " + existingLic.getCode());
335
+ }
336
+ if (!LicUtils.checkValidLicenseCodeCrc(lic.getCode())) {
337
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The license code is not valid");
338
+ }
339
+ if (!Utils.isValidEmail(lic.getEmail())) {
340
+ throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "The user email should be a valid email");
341
+ }
342
+
343
+ Pack pack = null;
344
+ if (lic.getPackId() != null) {
345
+ pack = em.find(Pack.class, lic.getPackId());
346
+ }
363347
364
- Pack pack = null;
365
- if (lic.getPackId() != null) {
366
- pack = em.find(Pack.class, lic.getPackId());
367
- }
348
+ if (pack == null) {
349
+ LOG.error("License pack with id {} not found in DB", lic.getPackId());
350
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "License's pack not found with ID: " + lic.getPackId());
351
+ } else {
352
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN) && !bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) {
353
+ LOG.error("License for pack with id {} can not be created by user {}", pack.getId(), bsc.getUserPrincipal());
354
+ throw new SeCurisServiceException(ErrorCodes.UNAUTHORIZED_ACCESS, "Unathorized action on pack license");
355
+ }
356
+ if (pack.getStatus() != PackStatus.ACTIVE) {
357
+ LOG.error("Current pack, {}, is not active so licenses cannot be created", pack.getId());
358
+ throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Current pack is not active so licenses cannot be created");
359
+ }
360
+ }
368361
369
- if (pack == null) {
370
- LOG.error("License pack with id {} not found in DB", lic.getPackId());
371
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "License's pack not found with ID: " + lic.getPackId());
372
- } else {
373
- if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN) && !bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) {
374
- LOG.error("License for pack with id {} can not be created by user {}", pack.getId(), bsc.getUserPrincipal());
375
- throw new SeCurisServiceException(ErrorCodes.UNAUTHORIZED_ACCESS, "Unathorized action on pack license");
376
- }
377
- if (pack.getStatus() != PackStatus.ACTIVE) {
378
- LOG.error("Current pack, {}, is not active so licenses cannot be created", pack.getId());
379
- throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "Current pack is not active so licenses cannot be created");
380
- }
381
- }
362
+ User createdBy = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
382363
383
- User createdBy = userHelper.getUser(bsc.getUserPrincipal().getName(), em);
364
+ if (lic.getRequestData() != null) {
365
+ License existingLicense = License.findActiveLicenseByRequestData(lic.getRequestData(), em);
366
+ if (existingLicense != null) {
367
+ throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There is already an active license for current request data");
368
+ }
384369
385
- if (lic.getRequestData() != null) {
386
- License existingLicense = License.findActiveLicenseByRequestData(lic.getRequestData(), em);
387
- if (existingLicense != null) {
388
- throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There is already an active license for current request data");
389
- }
370
+ if (pack.getNumAvailables() > 0) {
390371
391
- if (pack.getNumAvailables() > 0) {
372
+ SignedLicenseBean signedLicense = generateLicense(lic, em);
373
+ // If user provide a request data the license status is passed
374
+ // directly to ACTIVE
375
+ lic.setStatus(LicenseStatus.ACTIVE);
376
+ try {
377
+ lic.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class));
378
+ if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
379
+ throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
380
+ }
381
+ lic.setLicenseData(JsonUtils.toJSON(signedLicense));
382
+ } catch (SeCurisException e) {
383
+ LOG.error("Error generating license JSON", e);
384
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
385
+ }
386
+ }
387
+ } else {
388
+ lic.setStatus(LicenseStatus.CREATED);
389
+ }
390
+ lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(lic.getCode()));
391
+ lic.setCreatedBy(createdBy);
392
+ lic.setCreationTimestamp(new Date());
393
+ lic.setModificationTimestamp(lic.getCreationTimestamp());
392394
393
- SignedLicenseBean signedLicense = generateLicense(lic, em);
394
- // If user provide a request data the license status is passed
395
- // directly to ACTIVE
396
- lic.setStatus(LicenseStatus.ACTIVE);
397
- try {
398
- lic.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class));
399
- if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
400
- throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be activated");
401
- }
402
- lic.setLicenseData(JsonUtils.toJSON(signedLicense));
403
- } catch (SeCurisException e) {
404
- LOG.error("Error generating license JSON", e);
405
- throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generating license JSON");
406
- }
407
- }
408
- } else {
409
- lic.setStatus(LicenseStatus.CREATED);
410
- }
411
- lic.setCodeSuffix(LicUtils.getLicenseCodeSuffix(lic.getCode()));
412
- lic.setCreatedBy(createdBy);
413
- lic.setCreationTimestamp(new Date());
414
- lic.setModificationTimestamp(lic.getCreationTimestamp());
395
+ em.persist(lic);
396
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
397
+ if (lic.getStatus() == LicenseStatus.ACTIVE) {
398
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.ACTIVATE, "Activated on creation"));
399
+ }
415400
416
- em.persist(lic);
417
- em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
418
- if (lic.getStatus() == LicenseStatus.ACTIVE) {
419
- em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.ACTIVATE, "Activated on creation"));
420
- }
401
+ return Response.ok(lic).build();
402
+ }
421403
422
- return Response.ok(lic).build();
423
- }
404
+ private SignedLicenseBean generateLicense(License license, EntityManager em) throws SeCurisServiceException {
405
+ SignedLicenseBean sl = null;
406
+ Pack pack = em.find(Pack.class, license.getPackId());
407
+ RequestBean rb = validateRequestData(pack, license.getRequestData(), license.getActivationCode());
408
+ try {
409
+ LicenseBean lb = licenseGenerator.generateLicense(rb, licenseHelper.extractPackMetadata(pack.getMetadata()), licenseHelper.getExpirationDateFromPack(pack, false),
410
+ license.getCode(), pack.getAppName());
411
+ sl = new SignedLicenseBean(lb);
412
+ } catch (SeCurisException e) {
413
+ throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Error generating license: " + e.toString());
414
+ }
415
+ return sl;
416
+ }
424417
425
- private SignedLicenseBean generateLicense(License license, EntityManager em) throws SeCurisServiceException {
426
- SignedLicenseBean sl = null;
427
- Pack pack = em.find(Pack.class, license.getPackId());
428
- RequestBean rb = validateRequestData(pack, license.getRequestData());
429
- try {
430
- LicenseBean lb = licenseGenerator.generateLicense(rb, licenseHelper.extractPackMetadata(pack.getMetadata()),
431
- licenseHelper.getExpirationDateFromPack(pack, false), license.getCode(), pack.getAppName());
432
- sl = new SignedLicenseBean(lb);
433
- } catch (SeCurisException e) {
434
- throw new SeCurisServiceException(ErrorCodes.INVALID_LICENSE_REQUEST_DATA, "Error generating license: " + e.toString());
435
- }
436
- return sl;
437
- }
418
+ /**
419
+ * We check if the given request data is valid for the current Pack and has
420
+ * a valid format
421
+ *
422
+ * @param pack
423
+ * @param requestData
424
+ * @throws SeCurisServiceException
425
+ */
426
+ private RequestBean validateRequestData(Pack pack, String requestData, String activationCode) throws SeCurisServiceException {
427
+ if (requestData == null) {
428
+ throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA, "Request data is empty");
429
+ }
430
+ RequestBean rb = null;
431
+ try {
432
+ rb = JsonUtils.json2object(requestData, RequestBean.class);
433
+ } catch (SeCurisException e) {
434
+ throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data has not a valid format");
435
+ }
438436
439
- /**
440
- * We check if the given request data is valid for the current Pack and has
441
- * a valid format
442
- *
443
- * @param pack
444
- * @param requestData
445
- * @throws SeCurisServiceException
446
- */
447
- private RequestBean validateRequestData(Pack pack, String requestData) throws SeCurisServiceException {
448
- if (requestData == null) {
449
- throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA, "Request data is empty");
450
- }
451
- RequestBean rb = null;
452
- try {
453
- rb = JsonUtils.json2object(requestData, RequestBean.class);
454
- } catch (SeCurisException e) {
455
- throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data has not a valid format");
456
- }
437
+ if (rb.getActivationCode() != null && activationCode != null) {
438
+ if (!rb.getActivationCode().equals(activationCode)) {
439
+ throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Activation code mismatch");
440
+ }
441
+ } else {
457442
458
- if (!rb.getCustomerCode().equals(pack.getOrganization().getCode())) {
459
- throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data not valid, wrong Organization code");
460
- }
461
- if (!rb.getLicenseTypeCode().equals(pack.getLicenseTypeCode())) {
462
- throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data not valid, wrong License type code");
463
- }
464
- // TODO: [rsanchez] Verify that if pack code is null we ignore it
465
- if (rb.getPackCode() != null && !rb.getPackCode().equals(pack.getCode())) {
466
- throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data not valid, wrong Pack code");
467
- }
468
- return rb;
469
- }
443
+ if (!rb.getCustomerCode().equals(pack.getOrganization().getCode())) {
444
+ throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data not valid, wrong Organization code");
445
+ }
446
+ if (!rb.getLicenseTypeCode().equals(pack.getLicenseTypeCode())) {
447
+ throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data not valid, wrong License type code");
448
+ }
449
+ if (rb.getPackCode() != null && !rb.getPackCode().equals(pack.getCode())) {
450
+ throw new SeCurisServiceException(ErrorCodes.INVALID_REQUEST_DATA_FORMAT, "Request data not valid, wrong Pack code");
451
+ }
452
+ }
453
+ return rb;
454
+ }
470455
471
- @PUT
472
- @POST
473
- @Path("/{licId}")
474
- @Securable
475
- @EnsureTransaction
476
- @Consumes(MediaType.APPLICATION_JSON)
477
- @Produces({
478
- MediaType.APPLICATION_JSON
479
- })
480
- public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
481
- LOG.info("Modifying license with id: {}", licId);
456
+ @PUT
457
+ @POST
458
+ @Path("/{licId}")
459
+ @Securable
460
+ @EnsureTransaction
461
+ @Consumes(MediaType.APPLICATION_JSON)
462
+ @Produces({ MediaType.APPLICATION_JSON })
463
+ public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
464
+ LOG.info("Modifying license with id: {}", licId);
482465
483
- // EntityManager em = emProvider.get();
466
+ // EntityManager em = emProvider.get();
484467
485
- License currentLicense = getCurrentLicense(licId, bsc, em);
486
- currentLicense.setComments(lic.getComments());
487
- currentLicense.setFullName(lic.getFullName());
488
- currentLicense.setEmail(lic.getEmail());
489
- if (currentLicense.getActivationCode() == null) {
490
- currentLicense.setActivationCode(lic.getActivationCode());
491
- }
468
+ License currentLicense = getCurrentLicense(licId, bsc, em);
469
+ currentLicense.setComments(lic.getComments());
470
+ currentLicense.setFullName(lic.getFullName());
471
+ currentLicense.setEmail(lic.getEmail());
472
+ if (currentLicense.getActivationCode() == null) {
473
+ currentLicense.setActivationCode(lic.getActivationCode());
474
+ }
492475
493
- if (currentLicense.getStatus() == LicenseStatus.CREATED && !ObjectUtils.equals(currentLicense.getReqDataHash(), lic.getReqDataHash())) {
494
- if (lic.getRequestData() != null) {
495
- SignedLicenseBean signedLicense = generateLicense(lic, em);
496
- try {
497
- // Next 2 lines are necessary to normalize the String that
498
- // contains
499
- // the request.
500
- currentLicense.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class));
501
- LOG.info("JSON generated for request: \n{}", currentLicense.getRequestData());
502
- if (BlockedRequest.isRequestBlocked(currentLicense.getRequestData(), em)) {
503
- throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be used again");
504
- }
505
- currentLicense.setLicenseData(JsonUtils.toJSON(signedLicense));
506
- } catch (SeCurisException e) {
507
- LOG.error("Error generaing license JSON", e);
508
- throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generaing license JSON");
509
- }
510
- } else {
511
- // This set method could pass a null value
512
- currentLicense.setRequestData(null);
513
- }
514
- }
476
+ if (currentLicense.getStatus() == LicenseStatus.CREATED && !ObjectUtils.equals(currentLicense.getReqDataHash(), lic.getReqDataHash())) {
477
+ if (lic.getRequestData() != null) {
478
+ SignedLicenseBean signedLicense = generateLicense(lic, em);
479
+ try {
480
+ // Next 2 lines are necessary to normalize the String that
481
+ // contains
482
+ // the request.
483
+ currentLicense.setRequestData(JsonUtils.toJSON(signedLicense, RequestBean.class));
484
+ LOG.info("JSON generated for request: \n{}", currentLicense.getRequestData());
485
+ if (BlockedRequest.isRequestBlocked(currentLicense.getRequestData(), em)) {
486
+ throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is blocked and cannot be used again");
487
+ }
488
+ currentLicense.setLicenseData(JsonUtils.toJSON(signedLicense));
489
+ } catch (SeCurisException e) {
490
+ LOG.error("Error generaing license JSON", e);
491
+ throw new SeCurisServiceException(ErrorCodes.INVALID_FORMAT, "Error generaing license JSON");
492
+ }
493
+ } else {
494
+ // This set method could pass a null value
495
+ currentLicense.setRequestData(null);
496
+ }
497
+ }
515498
516
- currentLicense.setModificationTimestamp(new Date());
517
- em.persist(currentLicense);
518
- em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.MODIFY));
499
+ currentLicense.setModificationTimestamp(new Date());
500
+ em.persist(currentLicense);
501
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.MODIFY));
519502
520
- return Response.ok(currentLicense).build();
521
- }
503
+ return Response.ok(currentLicense).build();
504
+ }
522505
523
- @DELETE
524
- @Path("/{licId}")
525
- @EnsureTransaction
526
- @Securable
527
- @Produces({
528
- MediaType.APPLICATION_JSON
529
- })
530
- public Response delete(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
531
- LOG.info("Deleting license with id: {}", licId);
532
- // EntityManager em = emProvider.get();
533
- License lic = getCurrentLicense(licId, bsc, em);
506
+ @DELETE
507
+ @Path("/{licId}")
508
+ @EnsureTransaction
509
+ @Securable
510
+ @Produces({ MediaType.APPLICATION_JSON })
511
+ public Response delete(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
512
+ LOG.info("Deleting license with id: {}", licId);
513
+ // EntityManager em = emProvider.get();
514
+ License lic = getCurrentLicense(licId, bsc, em);
534515
535
- if (!License.Status.isActionValid(License.Action.DELETE, lic.getStatus())) {
536
- LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus());
537
- throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can not be deleted in current status: " + lic.getStatus().name());
538
- }
539
- if (lic.getStatus() == LicenseStatus.BLOCKED) {
540
- // If license is removed and it's blocked then the blocked request
541
- // should be removed, that is,
542
- // the license deletion will unblock the request data
543
- BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
544
- if (blockedReq != null) {
545
- // This if is to avoid some race condition or if the request has
546
- // been already removed manually
547
- em.remove(blockedReq);
548
- }
549
- }
516
+ if (!License.Status.isActionValid(License.Action.DELETE, lic.getStatus())) {
517
+ LOG.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus());
518
+ throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can not be deleted in current status: " + lic.getStatus().name());
519
+ }
520
+ if (lic.getStatus() == LicenseStatus.BLOCKED) {
521
+ // If license is removed and it's blocked then the blocked request
522
+ // should be removed, that is,
523
+ // the license deletion will unblock the request data
524
+ BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
525
+ if (blockedReq != null) {
526
+ // This if is to avoid some race condition or if the request has
527
+ // been already removed manually
528
+ em.remove(blockedReq);
529
+ }
530
+ }
550531
551
- em.remove(lic);
552
- return Response.ok(Utils.createMap("success", true, "id", licId)).build();
553
- }
532
+ em.remove(lic);
533
+ return Response.ok(Utils.createMap("success", true, "id", licId)).build();
534
+ }
554535
555
- @POST
556
- @Path("/{licId}/block")
557
- @EnsureTransaction
558
- @Securable
559
- @Produces({
560
- MediaType.APPLICATION_JSON
561
- })
562
- public Response block(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
563
- LOG.info("Blocking license with id: {}", licId);
564
- // EntityManager em = emProvider.get();
565
- License lic = getCurrentLicense(licId, bsc, em);
536
+ @POST
537
+ @Path("/{licId}/block")
538
+ @EnsureTransaction
539
+ @Securable
540
+ @Produces({ MediaType.APPLICATION_JSON })
541
+ public Response block(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
542
+ LOG.info("Blocking license with id: {}", licId);
543
+ // EntityManager em = emProvider.get();
544
+ License lic = getCurrentLicense(licId, bsc, em);
566545
567
- if (!License.Status.isActionValid(License.Action.BLOCK, lic.getStatus())) {
568
- LOG.error("License can only be blocked in CANCELLED status, current: {}", lic.getStatus().name());
569
- throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can only be blocked in CANCELLED status");
570
- }
571
- if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
572
- throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is already blocked");
573
- }
574
- BlockedRequest blockedReq = new BlockedRequest();
575
- blockedReq.setCreationTimestamp(new Date());
576
- blockedReq.setBlockedBy(userHelper.getUser(bsc, em));
577
- blockedReq.setRequestData(lic.getRequestData());
546
+ if (!License.Status.isActionValid(License.Action.BLOCK, lic.getStatus())) {
547
+ LOG.error("License can only be blocked in CANCELLED status, current: {}", lic.getStatus().name());
548
+ throw new SeCurisServiceException(ErrorCodes.WRONG_STATUS, "License can only be blocked in CANCELLED status");
549
+ }
550
+ if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
551
+ throw new SeCurisServiceException(ErrorCodes.BLOCKED_REQUEST_DATA, "Given request data is already blocked");
552
+ }
553
+ BlockedRequest blockedReq = new BlockedRequest();
554
+ blockedReq.setCreationTimestamp(new Date());
555
+ blockedReq.setBlockedBy(userHelper.getUser(bsc, em));
556
+ blockedReq.setRequestData(lic.getRequestData());
578557
579
- em.persist(blockedReq);
580
- lic.setStatus(LicenseStatus.BLOCKED);
581
- lic.setModificationTimestamp(new Date());
582
- em.merge(lic);
558
+ em.persist(blockedReq);
559
+ lic.setStatus(LicenseStatus.BLOCKED);
560
+ lic.setModificationTimestamp(new Date());
561
+ em.merge(lic);
583562
584
- em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.BLOCK));
585
- return Response.ok(Utils.createMap("success", true, "id", licId)).build();
586
- }
563
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.BLOCK));
564
+ return Response.ok(Utils.createMap("success", true, "id", licId)).build();
565
+ }
587566
588
- @POST
589
- @Path("/{licId}/unblock")
590
- @EnsureTransaction
591
- @Securable
592
- @Produces({
593
- MediaType.APPLICATION_JSON
594
- })
595
- public Response unblock(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
596
- LOG.info("Unblocking license with id: {}", licId);
597
- // EntityManager em = emProvider.get();
598
- License lic = getCurrentLicense(licId, bsc, em);
567
+ @POST
568
+ @Path("/{licId}/unblock")
569
+ @EnsureTransaction
570
+ @Securable
571
+ @Produces({ MediaType.APPLICATION_JSON })
572
+ public Response unblock(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
573
+ LOG.info("Unblocking license with id: {}", licId);
574
+ // EntityManager em = emProvider.get();
575
+ License lic = getCurrentLicense(licId, bsc, em);
599576
600
- if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
601
- BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
602
- em.remove(blockedReq);
577
+ if (BlockedRequest.isRequestBlocked(lic.getRequestData(), em)) {
578
+ BlockedRequest blockedReq = em.find(BlockedRequest.class, lic.getReqDataHash());
579
+ em.remove(blockedReq);
603580
604
- lic.setStatus(LicenseStatus.CANCELLED);
605
- lic.setModificationTimestamp(new Date());
606
- em.merge(lic);
607
- em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.UNBLOCK));
608
- } else {
609
- LOG.info("Request data for license {} is NOT blocked", licId);
610
- }
581
+ lic.setStatus(LicenseStatus.CANCELLED);
582
+ lic.setModificationTimestamp(new Date());
583
+ em.merge(lic);
584
+ em.persist(licenseHelper.createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.UNBLOCK));
585
+ } else {
586
+ LOG.info("Request data for license {} is NOT blocked", licId);
587
+ }
611588
612
- return Response.ok(Utils.createMap("success", true, "id", licId)).build();
613
- }
589
+ return Response.ok(Utils.createMap("success", true, "id", licId)).build();
590
+ }
614591
615
- private License getCurrentLicense(Integer licId, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
616
- if (licId == null || "".equals(licId)) {
617
- LOG.error("License ID is mandatory");
618
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Missing license ID");
619
- }
592
+ private License getCurrentLicense(Integer licId, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
593
+ if (licId == null || "".equals(licId)) {
594
+ LOG.error("License ID is mandatory");
595
+ throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Missing license ID");
596
+ }
620597
621
- License lic = em.find(License.class, licId);
622
- if (lic == null) {
623
- LOG.error("License with id {} not found in DB", licId);
624
- throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "License not found for ID: " + licId);
625
- }
626
- if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN) && !bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
627
- LOG.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
628
- throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data");
629
- }
630
- return lic;
631
- }
598
+ License lic = em.find(License.class, licId);
599
+ if (lic == null) {
600
+ LOG.error("License with id {} not found in DB", licId);
601
+ throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "License not found for ID: " + licId);
602
+ }
603
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN) && !bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
604
+ LOG.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
605
+ throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data");
606
+ }
607
+ return lic;
608
+ }
632609
633
- @JsonAutoDetect
634
- @JsonIgnoreProperties(ignoreUnknown = true)
635
- static class CancellationLicenseActionBean {
636
- @JsonProperty
637
- private String reason;
638
- }
610
+ @JsonAutoDetect
611
+ @JsonIgnoreProperties(ignoreUnknown = true)
612
+ static class CancellationLicenseActionBean {
613
+ @JsonProperty
614
+ private String reason;
615
+ }
639616 }