rsanchez
2014-10-22 ddec2c5c7b7842536d6d705c2de20f96e16c8aa8
securis/src/main/java/net/curisit/securis/services/ApiResource.java
....@@ -28,8 +28,6 @@
2828 import net.curisit.securis.beans.SignedLicenseBean;
2929 import net.curisit.securis.beans.StatusBean;
3030 import net.curisit.securis.db.License;
31
-import net.curisit.securis.db.LicenseType;
32
-import net.curisit.securis.db.Pack;
3331 import net.curisit.securis.security.BasicSecurityContext;
3432 import net.curisit.securis.security.Securable;
3533 import net.curisit.securis.services.exception.SeCurisServiceException;
....@@ -96,9 +94,10 @@
9694 return Response.ok(status).build();
9795 }
9896
99
-
10097 /**
101
- * Request a new license file based in a RequestBean object sent as parameter
98
+ * Request a new license file based in a RequestBean object sent as
99
+ * parameter
100
+ *
102101 * @param mpfdi
103102 * @param bsc
104103 * @return
....@@ -108,17 +107,18 @@
108107 @POST
109108 @Path("/request")
110109 @Consumes(MediaType.APPLICATION_JSON)
111
- //TODO: Enable this: @Securable
110
+ // TODO: Enable this: @Securable
112111 @Produces({
113112 MediaType.APPLICATION_JSON
114113 })
115114 @Transactional
116
- public Response createFromRequest(RequestBean request, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
115
+ public Response createFromRequest(RequestBean request, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException,
116
+ SeCurisException {
117117 LOG.info("Request to get license: {}", request);
118118
119119 Map<String, Object> metadata = getLicenseMetadata(request);
120120 License licDB = getLicenseData(request);
121
-
121
+
122122 Date expirationDate = licDB.getExpirationDate();
123123 String licenseTypeCode = licDB.getPack().getLicenseType().getCode();
124124 String licenseCode = licDB.getCode();
....@@ -126,9 +126,10 @@
126126 SignedLicenseBean signedLic = new SignedLicenseBean(lic);
127127 return Response.ok(signedLic).build();
128128 }
129
-
129
+
130130 /**
131131 * Returns a License file in JSON format from an uploaded Request file
132
+ *
132133 * @param mpfdi
133134 * @param bsc
134135 * @return
....@@ -145,12 +146,14 @@
145146 })
146147 @Transactional
147148 @SuppressWarnings("unchecked")
148
- public Response createFromRequestFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
149
+ public Response createFromRequestFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException,
150
+ SeCurisServiceException, SeCurisException {
149151 RequestBean req = new RequestBean();
150
- req.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null));
152
+ req.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null));
153
+ req.setLicenseTypeCode(mpfdi.getFormDataPart("licenseTypeCode", String.class, null));
154
+ req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
151155 req.setArch(mpfdi.getFormDataPart("arch", String.class, null));
152156 req.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null));
153
- req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
154157 req.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null));
155158 req.setOsName(mpfdi.getFormDataPart("osName", String.class, null));
156159
....@@ -170,12 +173,13 @@
170173 @POST
171174 @Path("/renew")
172175 @Consumes(MediaType.APPLICATION_JSON)
173
- //TODO: Enable this: @Securable
176
+ // TODO: Enable this: @Securable
174177 @Produces({
175178 MediaType.APPLICATION_JSON
176179 })
177180 @Transactional
178
- public Response renewFromPreviousLicense(LicenseBean previousLic, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
181
+ public Response renewFromPreviousLicense(LicenseBean previousLic, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException,
182
+ SeCurisException {
179183 LOG.info("Renew license: {}", previousLic);
180184
181185 if (previousLic.getExpirationDate().after(DateUtils.addMonths(new Date(), 1))) {
....@@ -184,16 +188,17 @@
184188
185189 Map<String, Object> metadata = getLicenseMetadata(previousLic);
186190 License licDB = getLicenseData(previousLic);
187
-
191
+
188192 Date expirationDate = licDB.getExpirationDate();
189193 String licenseTypeCode = licDB.getPack().getLicenseType().getCode();
190194 String licenseCode = licDB.getCode();
191195 LicenseBean lic = licenseGenerator.generateLicense(previousLic, metadata, expirationDate, licenseTypeCode, licenseCode);
192196 return Response.ok(lic).build();
193197 }
194
-
198
+
195199 /**
196200 * Returns a new License file in JSON format based in a previous license
201
+ *
197202 * @param mpfdi
198203 * @param bsc
199204 * @return
....@@ -210,12 +215,15 @@
210215 })
211216 @Transactional
212217 @SuppressWarnings("unchecked")
213
- public Response renewFromLicenseFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException {
218
+ public Response renewFromLicenseFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException,
219
+ SeCurisServiceException, SeCurisException {
214220 LicenseBean lic = new LicenseBean();
215
- // TODO: Add more license parameters
216
- lic.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null));
221
+
222
+ lic.setAppName(mpfdi.getFormDataPart("appName", String.class, null));
217223 lic.setArch(mpfdi.getFormDataPart("arch", String.class, null));
218224 lic.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null));
225
+ lic.setPackCode(mpfdi.getFormDataPart("packCode", String.class, null));
226
+ lic.setLicenseTypeCode(mpfdi.getFormDataPart("licenseCode", String.class, null));
219227 lic.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null));
220228 lic.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null));
221229 lic.setOsName(mpfdi.getFormDataPart("osName", String.class, null));
....@@ -227,46 +235,50 @@
227235
228236 return createFromRequest(lic, bsc);
229237 }
230
-
231
-
232
-
238
+
233239 private License getLicenseData(RequestBean req) throws SeCurisException {
234
- // TODO: The dummy expiration date is temporal, this info should be read from DB
240
+ // TODO: The dummy expiration date is temporal, this info should be read
241
+ // from DB
235242 License lic = new License();
236
- lic.setExpirationDate(new Date(new Date().getTime() + (1000L * 3600 * 24 * 30 )));
237
- lic.setCode(req.getAppCode() + "-LIC-INTERNAL");
238
- LicenseType lt = new LicenseType();
239
- lt.setCode("TYPE-" + req.getAppCode());
240
- Pack pack = new Pack();
241
- pack.setLicenseType(lt);
242
- lic.setPack(pack);
243
+ // TODO: IMPLEMENT LICENSE GENERATION
244
+
245
+ // lic.setExpirationDate(new Date(new Date().getTime() + (1000L * 3600 *
246
+ // 24 * 30)));
247
+ // lic.setCode(req.getPackCode() + "-LIC-INTERNAL");
248
+ // LicenseType lt = new LicenseType();
249
+ // lt.setCode("TYPE-" + req.getAppCode());
250
+ // Pack pack = new Pack();
251
+ // pack.setLicenseType(lt);
252
+ // lic.setPack(pack);
243253 return lic;
244254 }
245
-
255
+
246256 /**
247257 * Extract the corresponding metadata for the Request license given
258
+ *
248259 * @param req
249260 * @return
250261 * @throws SeCurisException
251262 */
252263 @SuppressWarnings("unchecked")
253264 private Map<String, Object> getLicenseMetadata(RequestBean req) throws SeCurisException {
254
- // TODO: The dummy metadata file is temporal, this info should be read from DB
255
- File dummyMetadata = new File(System.getProperty("user.home") + File.separator + ".SeCuris" + File.separator + "dummy_metadata.json");
265
+ // TODO: The dummy metadata file is temporal, this info should be read
266
+ // from DB
267
+ File dummyMetadata = new File(System.getProperty("user.home") + File.separator + ".SeCuris" + File.separator + "dummy_metadata.json");
256268 Map<String, Object> metadata = null;
257269 try {
258270 String metadataJson = IOUtils.toString(dummyMetadata.toURI());
259
- metadata = (Map<String, Object>)JsonUtils.json2map(metadataJson).get(req.getAppCode());
260
- if (metadata == null)
261
- throw new SeCurisException("App code in request is unknown: " + req.getAppCode());
271
+ metadata = (Map<String, Object>) JsonUtils.json2map(metadataJson).get(req.getLicenseTypeCode());
272
+ if (metadata == null) {
273
+ throw new SeCurisException("App code in request is unknown: " + req.getLicenseTypeCode());
274
+ }
262275 metadata = new TreeMap<>(metadata);
263276 } catch (IOException e) {
264277 LOG.error("Error reading dummy metadata file", e);
265278 throw new SeCurisException("Error reading dummy metadata file");
266279 }
267
-
280
+
268281 return metadata;
269282 }
270283
271
-
272284 }