You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2021/11/01 04:33:24 UTC

[james-project] 04/05: JAMES-3539 Introduce PushSubscriptionHelpers to avoid duplicated code

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 2983ac57799349d102ac03440bd322dbcf6392e1
Author: Quan Tran <hq...@linagora.com>
AuthorDate: Fri Oct 29 09:16:40 2021 +0700

    JAMES-3539 Introduce PushSubscriptionHelpers to avoid duplicated code
---
 .../pushsubscription/PushSubscriptionHelpers.java  | 60 ++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/pushsubscription/PushSubscriptionHelpers.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/pushsubscription/PushSubscriptionHelpers.java
new file mode 100644
index 0000000..e9fa422
--- /dev/null
+++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/pushsubscription/PushSubscriptionHelpers.java
@@ -0,0 +1,60 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.api.pushsubscription;
+
+import static org.apache.james.jmap.api.model.PushSubscription.EXPIRES_TIME_MAX_DAY;
+
+import java.time.Clock;
+import java.time.ZonedDateTime;
+import java.util.Optional;
+
+import org.apache.james.jmap.api.model.PushSubscription;
+import org.apache.james.jmap.api.model.PushSubscriptionExpiredTime;
+import org.apache.james.jmap.api.model.PushSubscriptionKeys;
+
+import scala.Option;
+import scala.jdk.javaapi.OptionConverters;
+
+public class PushSubscriptionHelpers {
+    public static boolean isInThePast(PushSubscriptionExpiredTime expire, Clock clock) {
+        return expire.isBefore(ZonedDateTime.now(clock));
+    }
+
+    public static boolean isInThePast(Option<PushSubscriptionExpiredTime> expire, Clock clock) {
+        return expire.map(value -> isInThePast(value, clock)).getOrElse(() -> false);
+    }
+
+    public static PushSubscriptionExpiredTime evaluateExpiresTime(Optional<ZonedDateTime> inputTime, Clock clock) {
+        ZonedDateTime now = ZonedDateTime.now(clock);
+        ZonedDateTime maxExpiresTime = now.plusDays(EXPIRES_TIME_MAX_DAY());
+        return PushSubscriptionExpiredTime.apply(inputTime.filter(input -> input.isBefore(maxExpiresTime))
+            .orElse(maxExpiresTime));
+    }
+
+    public static boolean isNotOutdatedSubscription(PushSubscription subscription, Clock clock) {
+        return subscription.expires().isAfter(ZonedDateTime.now(clock));
+    }
+
+    public static boolean isInvalidPushSubscriptionKey(Option<PushSubscriptionKeys> keysOption) {
+        return OptionConverters.toJava(keysOption)
+            .map(key -> key.p256dh().isEmpty() || key.auth().isEmpty())
+            .orElse(false);
+    }
+}
\ No newline at end of file

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org