/** * Local date time of local date time. * * @param time the time * @return the local date time */ public static LocalDateTime localDateTimeOf(final Date time) { return localDateTimeOf(time.getTime()); }
/** * Gets creation time. Attempts to parse the value * as a {@link ZonedDateTime}. Otherwise, assumes a * {@link LocalDateTime} and converts it based on system's * default zone. * * @return the creation time */ @JsonIgnore public ZonedDateTime getCreationZonedDateTime() { val dt = DateTimeUtils.zonedDateTimeOf(this.creationTime); if (dt != null) { return dt; } val lt = DateTimeUtils.localDateTimeOf(this.creationTime); return DateTimeUtils.zonedDateTimeOf(lt.atZone(ZoneId.systemDefault())); }
/** * Does ending time allow service access boolean. * * @return true/false */ protected boolean doesEndingTimeAllowServiceAccess() { if (this.endingDateTime != null) { val et = DateTimeUtils.zonedDateTimeOf(this.endingDateTime); if (et != null) { if (ZonedDateTime.now().isAfter(et)) { LOGGER.warn("Service access not allowed because it ended at [{}]. Now is [{}]", this.endingDateTime, ZonedDateTime.now()); return false; } } else { val etLocal = DateTimeUtils.localDateTimeOf(this.endingDateTime); if (etLocal != null) { if (LocalDateTime.now().isAfter(etLocal)) { LOGGER.warn("Service access not allowed because it ended at [{}]. Now is [{}]", this.endingDateTime, LocalDateTime.now()); return false; } } } } return true; }
/** * Does starting time allow service access boolean. * * @return true/false */ protected boolean doesStartingTimeAllowServiceAccess() { if (this.startingDateTime != null) { val st = DateTimeUtils.zonedDateTimeOf(this.startingDateTime); if (st != null) { if (ZonedDateTime.now().isBefore(st)) { LOGGER.warn("Service access not allowed because it starts at [{}]. Zoned now is [{}]", this.startingDateTime, ZonedDateTime.now()); return false; } } else { val stLocal = DateTimeUtils.localDateTimeOf(this.startingDateTime); if (stLocal != null) { if (LocalDateTime.now().isBefore(stLocal)) { LOGGER.warn("Service access not allowed because it starts at [{}]. Local now is [{}]", this.startingDateTime, ZonedDateTime.now()); return false; } } } } return true; } }
/** * Returns a predicate that determined whether a service has expired. * * @return true if the service is still valid. false if service has expired. */ public static Predicate<RegisteredService> getRegisteredServiceExpirationPolicyPredicate() { return service -> { try { if (service == null) { return false; } val policy = service.getExpirationPolicy(); if (policy == null || StringUtils.isBlank(policy.getExpirationDate())) { return true; } val now = getCurrentSystemTime(); val expirationDate = DateTimeUtils.localDateTimeOf(policy.getExpirationDate()); LOGGER.debug("Service expiration date is [{}] while now is [{}]", expirationDate, now); return !now.isAfter(expirationDate); } catch (final Exception e) { LOGGER.warn(e.getMessage(), e); } return false; }; }