rsanchez
2014-09-25 b77838d1005c45740968816c70088dff2ad655d3
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -28,14 +28,10 @@
2828 import net.curisit.integrity.commons.JsonUtils;
2929 import net.curisit.integrity.commons.Utils;
3030 import net.curisit.securis.DefaultExceptionHandler;
31
-import net.curisit.securis.LicenseGenerator;
32
-import net.curisit.securis.ReqGenerator;
3331 import net.curisit.securis.SeCurisException;
34
-import net.curisit.securis.beans.LicenseBean;
3532 import net.curisit.securis.beans.RequestBean;
3633 import net.curisit.securis.db.License;
3734 import net.curisit.securis.db.LicenseHistory;
38
-import net.curisit.securis.db.LicenseType;
3935 import net.curisit.securis.db.Pack;
4036 import net.curisit.securis.db.User;
4137 import net.curisit.securis.security.BasicSecurityContext;
....@@ -46,8 +42,6 @@
4642 import org.apache.commons.io.IOUtils;
4743 import org.apache.logging.log4j.LogManager;
4844 import org.apache.logging.log4j.Logger;
49
-import org.bouncycastle.jce.provider.asymmetric.ec.ECUtil;
50
-import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
5145
5246 import com.google.inject.persist.Transactional;
5347
....@@ -67,9 +61,6 @@
6761
6862 @Inject
6963 Provider<EntityManager> emProvider;
70
-
71
- @Inject
72
- LicenseGenerator licenseGenerator;
7364
7465 /**
7566 *
....@@ -290,167 +281,7 @@
290281 return Response.ok(lic).build();
291282 }
292283
293
- /**
294
- * Extract the corresponding metadata for the Request license given
295
- * @param req
296
- * @return
297
- * @throws SeCurisException
298
- */
299
- @SuppressWarnings("unchecked")
300
- private Map<String, Object> getLicenseMetadata(RequestBean req) throws SeCurisException {
301
- // TODO: The dummy metadata file is temporal, this info should be read from DB
302
- File dummyMetadata = new File(System.getProperty("user.home") + File.separator + ".SeCuris" + File.separator + "dummy_metadata.json");
303
- Map<String, Object> metadata = null;
304
- try {
305
- String metadataJson = IOUtils.toString(dummyMetadata.toURI());
306
- metadata = (Map<String, Object>)JsonUtils.json2map(metadataJson).get(req.getAppCode());
307
- if (metadata == null)
308
- throw new SeCurisException("App code in request is unknown: " + req.getAppCode());
309
- metadata = new TreeMap<>(metadata);
310
- } catch (IOException e) {
311
- LOG.error("Error reading dummy metadata file", e);
312
- throw new SeCurisException("Error reading dummy metadata file");
313
- }
314
-
315
- return metadata;
316
- }
317
-
318
- private License getLicenseData(RequestBean req) throws SeCurisException {
319
- // TODO: The dummy expiration date is temporal, this info should be read from DB
320
- License lic = new License();
321
- lic.setExpirationDate(new Date(new Date().getTime() + (1000L * 3600 * 24 * 365 * 10)));
322
- lic.setCode(req.getAppCode() + "-LIC-INTERNAL");
323
- LicenseType lt = new LicenseType();
324
- lt.setCode("TYPE-" + req.getAppCode());
325
- Pack pack = new Pack();
326
- pack.setLicenseType(lt);
327
- lic.setPack(pack);
328
- return lic;
329
- }
330
-
331
- /**
332
- * Request a new license file based in a RequestBean object sent as parameter
333
- * @param mpfdi
334
- * @param bsc
335
- * @return
336
- * @throws IOException
337
- * @throws SeCurisServiceException
338
- */
339
- @POST
340
- @Path("/request")
341
- @Consumes(MediaType.APPLICATION_JSON)
342
- //TODO: Enable this: @Securable
343
- @Produces({
344
- MediaType.APPLICATION_JSON
345
- })
346
- @Transactional
347
- public Response createFromRequest(RequestBean request, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
348
- LOG.info("Request to get license: {}", request);
349284
350
- Map<String, Object> metadata = getLicenseMetadata(request);
351
- License licDB = getLicenseData(request);
352
-
353
- Date expirationDate = licDB.getExpirationDate();
354
- String licenseTypeCode = licDB.getPack().getLicenseType().getCode();
355
- String licenseCode = licDB.getCode();
356
- LicenseBean lic = licenseGenerator.generateLicense(request, metadata, expirationDate, licenseTypeCode, licenseCode);
357
- return Response.ok(lic).build();
358
- }
359
-
360
- /**
361
- * Returns a License file in JSON format from an uploaded Request file
362
- * @param mpfdi
363
- * @param bsc
364
- * @return
365
- * @throws IOException
366
- * @throws SeCurisServiceException
367
- * @throws SeCurisException
368
- */
369
- @POST
370
- @Path("/request")
371
- @Consumes(MediaType.MULTIPART_FORM_DATA)
372
- @Securable
373
- @Produces({
374
- MediaType.APPLICATION_JSON
375
- })
376
- @Transactional
377
- @SuppressWarnings("unchecked")
378
- public Response createFromRequestFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
379
- RequestBean req = new RequestBean();
380
- req.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null));
381
- req.setArch(mpfdi.getFormDataPart("arch", String.class, null));
382
- req.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null));
383
- req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
384
- req.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null));
385
- req.setOsName(mpfdi.getFormDataPart("osName", String.class, null));
386
-
387
- return createFromRequest(req, bsc);
388
- }
389
-
390
- /**
391
- * Create a new License file based in a previous one
392
- *
393
- * @param request
394
- * @param bsc
395
- * @return
396
- * @throws IOException
397
- * @throws SeCurisServiceException
398
- * @throws SeCurisException
399
- */
400
- @POST
401
- @Path("/renew")
402
- @Consumes(MediaType.APPLICATION_JSON)
403
- //TODO: Enable this: @Securable
404
- @Produces({
405
- MediaType.APPLICATION_JSON
406
- })
407
- @Transactional
408
- public Response renewFromPreviousLicense(LicenseBean previousLic, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
409
- LOG.info("Request to get license: {}", previousLic);
410
-
411
- Map<String, Object> metadata = getLicenseMetadata(previousLic);
412
- License licDB = getLicenseData(previousLic);
413
-
414
- Date expirationDate = licDB.getExpirationDate();
415
- String licenseTypeCode = licDB.getPack().getLicenseType().getCode();
416
- String licenseCode = licDB.getCode();
417
- LicenseBean lic = licenseGenerator.generateLicense(previousLic, metadata, expirationDate, licenseTypeCode, licenseCode);
418
- return Response.ok(lic).build();
419
- }
420
-
421
- /**
422
- * Returns a new License file in JSON format based in a previous license
423
- * @param mpfdi
424
- * @param bsc
425
- * @return
426
- * @throws IOException
427
- * @throws SeCurisServiceException
428
- * @throws SeCurisException
429
- */
430
- @POST
431
- @Path("/renew")
432
- @Consumes(MediaType.MULTIPART_FORM_DATA)
433
- @Securable
434
- @Produces({
435
- MediaType.APPLICATION_JSON
436
- })
437
- @Transactional
438
- @SuppressWarnings("unchecked")
439
- public Response renewFromLicenseFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
440
- LicenseBean lic = new LicenseBean();
441
- // TODO: Add more license parameters
442
- lic.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null));
443
- lic.setArch(mpfdi.getFormDataPart("arch", String.class, null));
444
- lic.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null));
445
- lic.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
446
- lic.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null));
447
- lic.setOsName(mpfdi.getFormDataPart("osName", String.class, null));
448
-
449
- return createFromRequest(lic, bsc);
450
- }
451
-
452
-
453
-
454285
455286 @PUT
456287 @POST