| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis.services; |
|---|
| 2 | 2 | |
|---|
| 3 | +import java.io.File; |
|---|
| 3 | 4 | import java.io.IOException; |
|---|
| 4 | 5 | import java.util.Date; |
|---|
| 5 | 6 | import java.util.List; |
|---|
| 7 | +import java.util.Map; |
|---|
| 8 | +import java.util.TreeMap; |
|---|
| 6 | 9 | |
|---|
| 7 | 10 | import javax.inject.Inject; |
|---|
| 8 | 11 | import javax.inject.Provider; |
|---|
| .. | .. |
|---|
| 22 | 25 | import javax.ws.rs.core.Response; |
|---|
| 23 | 26 | import javax.ws.rs.core.Response.Status; |
|---|
| 24 | 27 | |
|---|
| 28 | +import net.curisit.integrity.commons.JsonUtils; |
|---|
| 25 | 29 | import net.curisit.integrity.commons.Utils; |
|---|
| 26 | 30 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 31 | +import net.curisit.securis.LicenseGenerator; |
|---|
| 32 | +import net.curisit.securis.ReqGenerator; |
|---|
| 33 | +import net.curisit.securis.SeCurisException; |
|---|
| 34 | +import net.curisit.securis.beans.LicenseBean; |
|---|
| 35 | +import net.curisit.securis.beans.RequestBean; |
|---|
| 27 | 36 | import net.curisit.securis.db.License; |
|---|
| 28 | 37 | import net.curisit.securis.db.LicenseHistory; |
|---|
| 38 | +import net.curisit.securis.db.LicenseType; |
|---|
| 29 | 39 | import net.curisit.securis.db.Pack; |
|---|
| 30 | 40 | import net.curisit.securis.db.User; |
|---|
| 31 | 41 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| .. | .. |
|---|
| 33 | 43 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 34 | 44 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 35 | 45 | |
|---|
| 46 | +import org.apache.commons.io.IOUtils; |
|---|
| 36 | 47 | import org.apache.logging.log4j.LogManager; |
|---|
| 37 | 48 | import org.apache.logging.log4j.Logger; |
|---|
| 49 | +import org.bouncycastle.jce.provider.asymmetric.ec.ECUtil; |
|---|
| 38 | 50 | import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; |
|---|
| 39 | 51 | |
|---|
| 40 | 52 | import com.google.inject.persist.Transactional; |
|---|
| .. | .. |
|---|
| 56 | 68 | @Inject |
|---|
| 57 | 69 | Provider<EntityManager> emProvider; |
|---|
| 58 | 70 | |
|---|
| 59 | | - public LicenseResource() { |
|---|
| 60 | | - } |
|---|
| 71 | + @Inject |
|---|
| 72 | + LicenseGenerator licenseGenerator; |
|---|
| 61 | 73 | |
|---|
| 62 | 74 | /** |
|---|
| 63 | 75 | * |
|---|
| .. | .. |
|---|
| 138 | 150 | return Response.ok(lic.getLicenseData()).build(); |
|---|
| 139 | 151 | } |
|---|
| 140 | 152 | |
|---|
| 153 | + /** |
|---|
| 154 | + * Activate the given license |
|---|
| 155 | + * @param licId |
|---|
| 156 | + * @param bsc |
|---|
| 157 | + * @return |
|---|
| 158 | + * @throws SeCurisServiceException |
|---|
| 159 | + */ |
|---|
| 141 | 160 | @PUT |
|---|
| 142 | 161 | @POST |
|---|
| 143 | 162 | @Path("/{licId}/activate") |
|---|
| .. | .. |
|---|
| 166 | 185 | return Response.ok(lic).build(); |
|---|
| 167 | 186 | } |
|---|
| 168 | 187 | |
|---|
| 188 | + /** |
|---|
| 189 | + * Send license file by email to the organization |
|---|
| 190 | + * @param licId |
|---|
| 191 | + * @param bsc |
|---|
| 192 | + * @return |
|---|
| 193 | + * @throws SeCurisServiceException |
|---|
| 194 | + */ |
|---|
| 169 | 195 | @PUT |
|---|
| 170 | 196 | @POST |
|---|
| 171 | 197 | @Path("/{licId}/send") |
|---|
| .. | .. |
|---|
| 188 | 214 | return Response.ok(lic).build(); |
|---|
| 189 | 215 | } |
|---|
| 190 | 216 | |
|---|
| 217 | + /** |
|---|
| 218 | + * Cancel given license |
|---|
| 219 | + * @param licId |
|---|
| 220 | + * @param bsc |
|---|
| 221 | + * @return |
|---|
| 222 | + * @throws SeCurisServiceException |
|---|
| 223 | + */ |
|---|
| 191 | 224 | @PUT |
|---|
| 192 | 225 | @POST |
|---|
| 193 | 226 | @Path("/{licId}/cancel") |
|---|
| .. | .. |
|---|
| 246 | 279 | |
|---|
| 247 | 280 | User createdBy = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 248 | 281 | |
|---|
| 249 | | - // ODO: Manage status if request data is set |
|---|
| 282 | + // TODO: Manage status if request data is set |
|---|
| 250 | 283 | lic.setCreatedBy(createdBy); |
|---|
| 251 | 284 | lic.setStatus(License.Status.CREATED); |
|---|
| 252 | 285 | lic.setCreationTimestamp(new Date()); |
|---|
| .. | .. |
|---|
| 256 | 289 | |
|---|
| 257 | 290 | return Response.ok(lic).build(); |
|---|
| 258 | 291 | } |
|---|
| 259 | | - |
|---|
| 292 | + |
|---|
| 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 = new TreeMap<>((Map<String, Object>)JsonUtils.json2map(metadataJson).get(req.getAppCode())); |
|---|
| 307 | + } catch (IOException e) { |
|---|
| 308 | + LOG.error("Error reading dummy metadata file", e); |
|---|
| 309 | + throw new SeCurisException("Error reading dummy metadata file"); |
|---|
| 310 | + } |
|---|
| 311 | + |
|---|
| 312 | + return metadata; |
|---|
| 313 | + } |
|---|
| 314 | + private License getLicenseData(RequestBean req) throws SeCurisException { |
|---|
| 315 | + // TODO: The dummy expiration date is temporal, this info should be read from DB |
|---|
| 316 | + License lic = new License(); |
|---|
| 317 | + lic.setExpirationDate(new Date(new Date().getTime() + (1000L * 3600 * 24 * 365 * 10))); |
|---|
| 318 | + lic.setCode(req.getAppCode() + "-LIC-INTERNAL"); |
|---|
| 319 | + LicenseType lt = new LicenseType(); |
|---|
| 320 | + lt.setCode("TYPE-" + req.getAppCode()); |
|---|
| 321 | + Pack pack = new Pack(); |
|---|
| 322 | + pack.setLicenseType(lt); |
|---|
| 323 | + lic.setPack(pack); |
|---|
| 324 | + return lic; |
|---|
| 325 | + } |
|---|
| 326 | + |
|---|
| 327 | + /** |
|---|
| 328 | + * Request a new license file based in a RequestBean object sent as parameter |
|---|
| 329 | + * @param mpfdi |
|---|
| 330 | + * @param bsc |
|---|
| 331 | + * @return |
|---|
| 332 | + * @throws IOException |
|---|
| 333 | + * @throws SeCurisServiceException |
|---|
| 334 | + */ |
|---|
| 260 | 335 | @POST |
|---|
| 261 | | - @Path("/") |
|---|
| 336 | + @Path("/request") |
|---|
| 337 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 338 | + //TODO: Enable this: @Securable |
|---|
| 339 | + @Produces({ |
|---|
| 340 | + MediaType.APPLICATION_JSON |
|---|
| 341 | + }) |
|---|
| 342 | + @Transactional |
|---|
| 343 | + public Response createFromRequest(RequestBean request, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 344 | + LOG.info("Request to get license: {}", request); |
|---|
| 345 | + |
|---|
| 346 | + Map<String, Object> metadata = getLicenseMetadata(request); |
|---|
| 347 | + License licDB = getLicenseData(request); |
|---|
| 348 | + |
|---|
| 349 | + Date expirationDate = licDB.getExpirationDate(); |
|---|
| 350 | + String licenseTypeCode = licDB.getPack().getLicenseType().getCode(); |
|---|
| 351 | + String licenseCode = licDB.getCode(); |
|---|
| 352 | + LicenseBean lic = licenseGenerator.generateLicense(request, metadata, expirationDate, licenseTypeCode, licenseCode); |
|---|
| 353 | + return Response.ok(lic).build(); |
|---|
| 354 | + } |
|---|
| 355 | + |
|---|
| 356 | + @POST |
|---|
| 357 | + @Path("/request") |
|---|
| 262 | 358 | @Consumes(MediaType.MULTIPART_FORM_DATA) |
|---|
| 263 | 359 | @Securable |
|---|
| 264 | 360 | @Produces({ |
|---|
| 265 | 361 | MediaType.APPLICATION_JSON |
|---|
| 266 | 362 | }) |
|---|
| 267 | 363 | @Transactional |
|---|
| 268 | | - public Response createWithFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException { |
|---|
| 269 | | - License lic = new License(); |
|---|
| 270 | | - lic.setCode(mpfdi.getFormDataPart("code", String.class, null)); |
|---|
| 271 | | - lic.setRequestData(mpfdi.getFormDataPart("request_data", String.class, null)); |
|---|
| 272 | | - lic.setPackId(mpfdi.getFormDataPart("pack_id", Integer.class, null)); |
|---|
| 273 | | - lic.setFullName(mpfdi.getFormDataPart("full_name", String.class, null)); |
|---|
| 274 | | - lic.setEmail(mpfdi.getFormDataPart("email", String.class, null)); |
|---|
| 275 | | - lic.setComments(mpfdi.getFormDataPart("comments", String.class, null)); |
|---|
| 364 | + @SuppressWarnings("unchecked") |
|---|
| 365 | + public Response createFromRequestFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 366 | + RequestBean req = new RequestBean(); |
|---|
| 367 | + req.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null)); |
|---|
| 368 | + req.setArch(mpfdi.getFormDataPart("arch", String.class, null)); |
|---|
| 369 | + req.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null)); |
|---|
| 370 | + req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null)); |
|---|
| 371 | + req.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null)); |
|---|
| 372 | + req.setOsName(mpfdi.getFormDataPart("osName", String.class, null)); |
|---|
| 276 | 373 | |
|---|
| 277 | | - return create(lic, bsc); |
|---|
| 374 | + return createFromRequest(req, bsc); |
|---|
| 278 | 375 | } |
|---|
| 279 | 376 | |
|---|
| 377 | + |
|---|
| 378 | + |
|---|
| 379 | + |
|---|
| 380 | + |
|---|
| 280 | 381 | @PUT |
|---|
| 281 | 382 | @POST |
|---|
| 282 | 383 | @Path("/{licId}") |
|---|