You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by da...@apache.org on 2023/07/31 11:20:54 UTC

[causeway] 01/03: CAUSEWAY-3543: removes dependency on failsafe, use java.time instead of joda time

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

danhaywood pushed a commit to branch CAUSEWAY-3543
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit 86e765e430876b6a4e8f4025377b348f790c233c
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Mon Jul 31 10:59:27 2023 +0100

    CAUSEWAY-3543: removes dependency on failsafe, use java.time instead of joda time
---
 bom/pom.xml                                        |  7 ---
 viewers/restfulobjects/client/pom.xml              |  5 ---
 .../client/src/main/java/module-info.java          |  1 -
 .../client/auth/oauth2/azure/TokenCache.java       | 52 ++++------------------
 4 files changed, 9 insertions(+), 56 deletions(-)

diff --git a/bom/pom.xml b/bom/pom.xml
index 61cbeee2cf..ddca9245b3 100644
--- a/bom/pom.xml
+++ b/bom/pom.xml
@@ -123,8 +123,6 @@ It is therefore a copy of org.apache:apache, with customisations clearly identif
 
         <htmlparser.version>2.1</htmlparser.version>
 
-        <failsafe.version>2.4.4</failsafe.version>
-
         <javafaker.version>1.0.2</javafaker.version>
         <javassist.version>3.29.2-GA</javassist.version>
 
@@ -1446,11 +1444,6 @@ It is therefore a copy of org.apache:apache, with customisations clearly identif
 				<artifactId>woodstox-core</artifactId>
 				<version>6.5.1</version>
 			</dependency>
-            <dependency>
-                <groupId>net.jodah</groupId>
-                <artifactId>failsafe</artifactId>
-                <version>${failsafe.version}</version>
-            </dependency>
             <dependency>
 				<groupId>org.codehaus.woodstox</groupId>
 				<artifactId>stax2-api</artifactId>
diff --git a/viewers/restfulobjects/client/pom.xml b/viewers/restfulobjects/client/pom.xml
index 23617db97d..f26067c527 100644
--- a/viewers/restfulobjects/client/pom.xml
+++ b/viewers/restfulobjects/client/pom.xml
@@ -46,11 +46,6 @@
 			<artifactId>spring-context</artifactId>
 		</dependency>
 
-        <dependency>
-            <groupId>net.jodah</groupId>
-            <artifactId>failsafe</artifactId>
-        </dependency>
-
         <!-- TEST DEPENDENCIES -->
 
         <dependency>
diff --git a/viewers/restfulobjects/client/src/main/java/module-info.java b/viewers/restfulobjects/client/src/main/java/module-info.java
index ba25a29276..4f62cbb1ba 100644
--- a/viewers/restfulobjects/client/src/main/java/module-info.java
+++ b/viewers/restfulobjects/client/src/main/java/module-info.java
@@ -36,6 +36,5 @@ module org.apache.causeway.viewer.restfulobjects.client {
     requires org.apache.logging.log4j;
     requires spring.core;
     requires org.apache.causeway.schema;
-    requires net.jodah.failsafe;
     requires org.slf4j;
 }
\ No newline at end of file
diff --git a/viewers/restfulobjects/client/src/main/java/org/apache/causeway/viewer/restfulobjects/client/auth/oauth2/azure/TokenCache.java b/viewers/restfulobjects/client/src/main/java/org/apache/causeway/viewer/restfulobjects/client/auth/oauth2/azure/TokenCache.java
index 22a05b7219..da63a1746a 100644
--- a/viewers/restfulobjects/client/src/main/java/org/apache/causeway/viewer/restfulobjects/client/auth/oauth2/azure/TokenCache.java
+++ b/viewers/restfulobjects/client/src/main/java/org/apache/causeway/viewer/restfulobjects/client/auth/oauth2/azure/TokenCache.java
@@ -1,51 +1,21 @@
 package org.apache.causeway.viewer.restfulobjects.client.auth.oauth2.azure;
 
-import java.time.temporal.ChronoUnit;
+import lombok.val;
+
+import java.time.ZonedDateTime;
 
 import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.Form;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriBuilder;
 
 import org.apache.causeway.commons.functional.Railway;
-
-import org.apache.causeway.commons.internal.exceptions._Exceptions;
 import org.apache.causeway.viewer.restfulobjects.client.auth.oauth2.Oauth2Creds;
 
-import org.joda.time.DateTime;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import lombok.val;
-
-import net.jodah.failsafe.Failsafe;
-import net.jodah.failsafe.RetryPolicy;
-
 public class TokenCache {
 
-    protected static Logger LOG = LoggerFactory.getLogger(TokenCache.class);
-
-    private final RetryPolicy<Response> retryPolicyForAnyException = retryPolicyForAnyException();
-
-    private static <T> RetryPolicy<T> retryPolicyForAnyException() {
-        return retryPolicyFor(Exception.class);
-    }
-
-    private static <T> RetryPolicy<T> retryPolicyFor(Class<? extends Exception>... exceptionClasses) {
-        return new RetryPolicy<T>()
-                .handle(exceptionClasses)
-                .withBackoff(500, 16000, ChronoUnit.MILLIS)
-                .withMaxRetries(3)
-                .onRetry(ctx -> {
-                    LOG.warn("Failed attempt {}, retrying...", (ctx.getAttemptCount() - 1));
-                })
-                .onFailure(ctx -> LOG.warn("Failed retry policy after {} attempts. Exception:\n {}", (ctx.getAttemptCount() - 1), _Exceptions.asStacktrace(
-                        ctx.getFailure())));
-    }
-
     private final Oauth2Creds creds;
     private final ClientBuilder clientBuilder;
 
@@ -55,7 +25,7 @@ public class TokenCache {
     }
 
     private String jwtToken;
-    private DateTime jwtTokenExpiresAt;
+    private ZonedDateTime jwtTokenExpiresAt;
 
     public Railway<Exception, String> getToken() {
 
@@ -75,7 +45,7 @@ public class TokenCache {
                 .param("grant_type", "client_credentials");
 
         val invocation = invocationBuilder.buildPost(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
-        val response = Failsafe.with(retryPolicyForAnyException).get(() -> invocation.invoke());
+        val response = invocation.invoke();
 
         val entity = response.readEntity(String.class);
 
@@ -104,22 +74,18 @@ public class TokenCache {
         return Railway.success(jwtToken);
     }
 
-    public boolean isTokenExpired() {
-        return !isTokenValid();
-    }
-
     private boolean isTokenValid() {
         if (jwtTokenExpiresAt == null) {
             return false;
         }
 
-        // token must remain valid for at least 5 minutes from now.
-        val inFiveMinutesTime = now().plusMinutes(5);
+        // token must remain valid for at least 2 additional minutes from now.
+        val inFiveMinutesTime = now().plusMinutes(2);
         return jwtTokenExpiresAt.isBefore(inFiveMinutesTime);
     }
 
-    private static DateTime now() {
-        return DateTime.now();
+    private static ZonedDateTime now() {
+        return ZonedDateTime.now();
     }
 
 }