| .. | .. |
|---|
| 28 | 28 | import net.curisit.securis.beans.SignedLicenseBean; |
|---|
| 29 | 29 | import net.curisit.securis.beans.StatusBean; |
|---|
| 30 | 30 | import net.curisit.securis.db.License; |
|---|
| 31 | | -import net.curisit.securis.db.LicenseType; |
|---|
| 32 | | -import net.curisit.securis.db.Pack; |
|---|
| 33 | 31 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 34 | 32 | import net.curisit.securis.security.Securable; |
|---|
| 35 | 33 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| .. | .. |
|---|
| 96 | 94 | return Response.ok(status).build(); |
|---|
| 97 | 95 | } |
|---|
| 98 | 96 | |
|---|
| 99 | | - |
|---|
| 100 | 97 | /** |
|---|
| 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 | + * |
|---|
| 102 | 101 | * @param mpfdi |
|---|
| 103 | 102 | * @param bsc |
|---|
| 104 | 103 | * @return |
|---|
| .. | .. |
|---|
| 108 | 107 | @POST |
|---|
| 109 | 108 | @Path("/request") |
|---|
| 110 | 109 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 111 | | - //TODO: Enable this: @Securable |
|---|
| 110 | + // TODO: Enable this: @Securable |
|---|
| 112 | 111 | @Produces({ |
|---|
| 113 | 112 | MediaType.APPLICATION_JSON |
|---|
| 114 | 113 | }) |
|---|
| 115 | 114 | @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 { |
|---|
| 117 | 117 | LOG.info("Request to get license: {}", request); |
|---|
| 118 | 118 | |
|---|
| 119 | 119 | Map<String, Object> metadata = getLicenseMetadata(request); |
|---|
| 120 | 120 | License licDB = getLicenseData(request); |
|---|
| 121 | | - |
|---|
| 121 | + |
|---|
| 122 | 122 | Date expirationDate = licDB.getExpirationDate(); |
|---|
| 123 | 123 | String licenseTypeCode = licDB.getPack().getLicenseType().getCode(); |
|---|
| 124 | 124 | String licenseCode = licDB.getCode(); |
|---|
| .. | .. |
|---|
| 126 | 126 | SignedLicenseBean signedLic = new SignedLicenseBean(lic); |
|---|
| 127 | 127 | return Response.ok(signedLic).build(); |
|---|
| 128 | 128 | } |
|---|
| 129 | | - |
|---|
| 129 | + |
|---|
| 130 | 130 | /** |
|---|
| 131 | 131 | * Returns a License file in JSON format from an uploaded Request file |
|---|
| 132 | + * |
|---|
| 132 | 133 | * @param mpfdi |
|---|
| 133 | 134 | * @param bsc |
|---|
| 134 | 135 | * @return |
|---|
| .. | .. |
|---|
| 145 | 146 | }) |
|---|
| 146 | 147 | @Transactional |
|---|
| 147 | 148 | @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 { |
|---|
| 149 | 151 | 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)); |
|---|
| 151 | 155 | req.setArch(mpfdi.getFormDataPart("arch", String.class, null)); |
|---|
| 152 | 156 | req.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null)); |
|---|
| 153 | | - req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null)); |
|---|
| 154 | 157 | req.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null)); |
|---|
| 155 | 158 | req.setOsName(mpfdi.getFormDataPart("osName", String.class, null)); |
|---|
| 156 | 159 | |
|---|
| .. | .. |
|---|
| 170 | 173 | @POST |
|---|
| 171 | 174 | @Path("/renew") |
|---|
| 172 | 175 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 173 | | - //TODO: Enable this: @Securable |
|---|
| 176 | + // TODO: Enable this: @Securable |
|---|
| 174 | 177 | @Produces({ |
|---|
| 175 | 178 | MediaType.APPLICATION_JSON |
|---|
| 176 | 179 | }) |
|---|
| 177 | 180 | @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 { |
|---|
| 179 | 183 | LOG.info("Renew license: {}", previousLic); |
|---|
| 180 | 184 | |
|---|
| 181 | 185 | if (previousLic.getExpirationDate().after(DateUtils.addMonths(new Date(), 1))) { |
|---|
| .. | .. |
|---|
| 184 | 188 | |
|---|
| 185 | 189 | Map<String, Object> metadata = getLicenseMetadata(previousLic); |
|---|
| 186 | 190 | License licDB = getLicenseData(previousLic); |
|---|
| 187 | | - |
|---|
| 191 | + |
|---|
| 188 | 192 | Date expirationDate = licDB.getExpirationDate(); |
|---|
| 189 | 193 | String licenseTypeCode = licDB.getPack().getLicenseType().getCode(); |
|---|
| 190 | 194 | String licenseCode = licDB.getCode(); |
|---|
| 191 | 195 | LicenseBean lic = licenseGenerator.generateLicense(previousLic, metadata, expirationDate, licenseTypeCode, licenseCode); |
|---|
| 192 | 196 | return Response.ok(lic).build(); |
|---|
| 193 | 197 | } |
|---|
| 194 | | - |
|---|
| 198 | + |
|---|
| 195 | 199 | /** |
|---|
| 196 | 200 | * Returns a new License file in JSON format based in a previous license |
|---|
| 201 | + * |
|---|
| 197 | 202 | * @param mpfdi |
|---|
| 198 | 203 | * @param bsc |
|---|
| 199 | 204 | * @return |
|---|
| .. | .. |
|---|
| 210 | 215 | }) |
|---|
| 211 | 216 | @Transactional |
|---|
| 212 | 217 | @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 { |
|---|
| 214 | 220 | 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)); |
|---|
| 217 | 223 | lic.setArch(mpfdi.getFormDataPart("arch", String.class, null)); |
|---|
| 218 | 224 | 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)); |
|---|
| 219 | 227 | lic.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null)); |
|---|
| 220 | 228 | lic.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null)); |
|---|
| 221 | 229 | lic.setOsName(mpfdi.getFormDataPart("osName", String.class, null)); |
|---|
| .. | .. |
|---|
| 227 | 235 | |
|---|
| 228 | 236 | return createFromRequest(lic, bsc); |
|---|
| 229 | 237 | } |
|---|
| 230 | | - |
|---|
| 231 | | - |
|---|
| 232 | | - |
|---|
| 238 | + |
|---|
| 233 | 239 | 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 |
|---|
| 235 | 242 | 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); |
|---|
| 243 | 253 | return lic; |
|---|
| 244 | 254 | } |
|---|
| 245 | | - |
|---|
| 255 | + |
|---|
| 246 | 256 | /** |
|---|
| 247 | 257 | * Extract the corresponding metadata for the Request license given |
|---|
| 258 | + * |
|---|
| 248 | 259 | * @param req |
|---|
| 249 | 260 | * @return |
|---|
| 250 | 261 | * @throws SeCurisException |
|---|
| 251 | 262 | */ |
|---|
| 252 | 263 | @SuppressWarnings("unchecked") |
|---|
| 253 | 264 | 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"); |
|---|
| 256 | 268 | Map<String, Object> metadata = null; |
|---|
| 257 | 269 | try { |
|---|
| 258 | 270 | 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 | + } |
|---|
| 262 | 275 | metadata = new TreeMap<>(metadata); |
|---|
| 263 | 276 | } catch (IOException e) { |
|---|
| 264 | 277 | LOG.error("Error reading dummy metadata file", e); |
|---|
| 265 | 278 | throw new SeCurisException("Error reading dummy metadata file"); |
|---|
| 266 | 279 | } |
|---|
| 267 | | - |
|---|
| 280 | + |
|---|
| 268 | 281 | return metadata; |
|---|
| 269 | 282 | } |
|---|
| 270 | 283 | |
|---|
| 271 | | - |
|---|
| 272 | 284 | } |
|---|