james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [7/7] james-project git commit: JAMES-1718 rely on runtime exceptions
Date Fri, 15 Apr 2016 08:53:29 GMT
JAMES-1718 rely on runtime exceptions


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/e71a7cd5
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/e71a7cd5
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/e71a7cd5

Branch: refs/heads/master
Commit: e71a7cd5a96ab507c0a0d87900100f1d18c70f51
Parents: 7e39d44
Author: Benoit Tellier <btellier@linagora.com>
Authored: Wed Apr 13 09:45:20 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Fri Apr 15 15:50:30 2016 +0700

----------------------------------------------------------------------
 server/data/data-jmap-cassandra/pom.xml         |  5 -----
 .../access/CassandraAccessTokenDAO.java         | 10 +++++++--
 .../access/CassandraAccessTokenRepository.java  | 13 ++++-------
 .../CassandraAccessTokenRepositoryTest.java     |  2 +-
 server/data/data-jmap/pom.xml                   |  4 ----
 .../jmap/api/access/AccessTokenRepository.java  |  4 +---
 .../access/MemoryAccessTokenRepository.java     |  7 +++---
 .../AbstractAccessTokenRepositoryTest.java      | 23 ++++++++++++--------
 .../jmap/crypto/AccessTokenManagerImpl.java     | 15 ++++++++-----
 .../jmap/crypto/AccessTokenManagerImplTest.java |  2 +-
 10 files changed, 43 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/data/data-jmap-cassandra/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-jmap-cassandra/pom.xml b/server/data/data-jmap-cassandra/pom.xml
index 10cae1a..f07ab0d 100644
--- a/server/data/data-jmap-cassandra/pom.xml
+++ b/server/data/data-jmap-cassandra/pom.xml
@@ -229,11 +229,6 @@
                     <groupId>org.slf4j</groupId>
                     <artifactId>slf4j-api</artifactId>
                 </dependency>
-                <dependency>
-                    <groupId>org.slf4j</groupId>
-                    <artifactId>slf4j-simple</artifactId>
-                    <scope>test</scope>
-                </dependency>
             </dependencies>
         </profile>
         <profile>

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenDAO.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenDAO.java
b/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenDAO.java
index b5c884c..5bdd9be 100644
--- a/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenDAO.java
+++ b/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenDAO.java
@@ -25,9 +25,14 @@ import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
 import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;
 import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
 import static com.datastax.driver.core.querybuilder.QueryBuilder.ttl;
+import static org.apache.james.jmap.api.access.AccessTokenRepository.TOKEN_EXPIRATION_IN_MS;
 
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+
+import javax.inject.Inject;
+import javax.inject.Named;
 
 import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
 import org.apache.james.jmap.api.access.AccessToken;
@@ -47,9 +52,10 @@ public class CassandraAccessTokenDAO {
     private final PreparedStatement selectStatement;
     private final int durationInSeconds;
 
-    public CassandraAccessTokenDAO(Session session, long durationInMilliseconds) {
+    @Inject
+    public CassandraAccessTokenDAO(Session session, @Named(TOKEN_EXPIRATION_IN_MS) long durationInMilliseconds)
{
         this.cassandraAsyncExecutor = new CassandraAsyncExecutor(session);
-        this.durationInSeconds = Ints.checkedCast(durationInMilliseconds / 1000);
+        this.durationInSeconds = Ints.checkedCast(TimeUnit.MILLISECONDS.toSeconds(durationInMilliseconds));
 
         this.removeStatement = session.prepare(delete()
             .from(CassandraAccessTokenTable.TABLE_NAME)

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepository.java
b/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepository.java
index 4ddbc1c..1dac5e7 100644
--- a/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepository.java
+++ b/server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepository.java
@@ -22,23 +22,20 @@ package org.apache.james.jmap.cassandra.access;
 import java.util.concurrent.CompletableFuture;
 
 import javax.inject.Inject;
-import javax.inject.Named;
 
 import org.apache.james.jmap.api.access.AccessToken;
 import org.apache.james.jmap.api.access.AccessTokenRepository;
 import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
 
-import com.datastax.driver.core.Session;
 import com.google.common.base.Preconditions;
-import com.jasongoodwin.monads.Try;
 
 public class CassandraAccessTokenRepository implements AccessTokenRepository {
 
     private final CassandraAccessTokenDAO cassandraAccessTokenDAO;
 
     @Inject
-    public CassandraAccessTokenRepository(Session session, @Named(TOKEN_EXPIRATION_IN_MS)
long durationInMilliseconds) {
-        this.cassandraAccessTokenDAO = new CassandraAccessTokenDAO(session, durationInMilliseconds);
+    public CassandraAccessTokenRepository(CassandraAccessTokenDAO cassandraAccessTokenDAO)
{
+        this.cassandraAccessTokenDAO = cassandraAccessTokenDAO;
     }
 
     @Override
@@ -58,13 +55,11 @@ public class CassandraAccessTokenRepository implements AccessTokenRepository
{
     }
 
     @Override
-    public CompletableFuture<Try<String>> getUsernameFromToken(AccessToken accessToken)
throws InvalidAccessToken {
+    public CompletableFuture<String> getUsernameFromToken(AccessToken accessToken)
throws InvalidAccessToken {
         Preconditions.checkNotNull(accessToken);
 
         return cassandraAccessTokenDAO.getUsernameFromToken(accessToken)
             .thenApply(
-                optional -> Try.ofFailable(
-                    () -> optional.orElseThrow(
-                        () -> new InvalidAccessToken(accessToken))));
+                optional -> optional.<InvalidAccessToken>orElseThrow(() -> new
InvalidAccessToken(accessToken)));
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
index 355e45d..1217698 100644
--- a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
+++ b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/access/CassandraAccessTokenRepositoryTest.java
@@ -31,7 +31,7 @@ public class CassandraAccessTokenRepositoryTest extends AbstractAccessTokenRepos
     @Override
     protected AccessTokenRepository createAccessTokenRepository() {
         cassandra = CassandraCluster.create(new CassandraAccessModule());
-        return new CassandraAccessTokenRepository(cassandra.getConf(), TTL_IN_MS);
+        return new CassandraAccessTokenRepository(new CassandraAccessTokenDAO(cassandra.getConf(),
TTL_IN_MS));
     }
 
     @After

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/data/data-jmap/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/pom.xml b/server/data/data-jmap/pom.xml
index 4166b7e..48b55ff 100644
--- a/server/data/data-jmap/pom.xml
+++ b/server/data/data-jmap/pom.xml
@@ -182,10 +182,6 @@
                     <artifactId>guava</artifactId>
                 </dependency>
                 <dependency>
-                    <groupId>com.jason-goodwin</groupId>
-                    <artifactId>better-monads</artifactId>
-                </dependency>
-                <dependency>
                     <groupId>org.apache.commons</groupId>
                     <artifactId>commons-collections4</artifactId>
                 </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessTokenRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessTokenRepository.java
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessTokenRepository.java
index a2a7e2f..525fea9 100644
--- a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessTokenRepository.java
+++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessTokenRepository.java
@@ -23,8 +23,6 @@ import java.util.concurrent.CompletableFuture;
 
 import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
 
-import com.jasongoodwin.monads.Try;
-
 public interface AccessTokenRepository {
 
     String TOKEN_EXPIRATION_IN_MS = "tokenExpirationInMs";
@@ -33,6 +31,6 @@ public interface AccessTokenRepository {
 
     CompletableFuture<Void> removeToken(AccessToken accessToken);
 
-    CompletableFuture<Try<String>> getUsernameFromToken(AccessToken accessToken)
throws InvalidAccessToken;
+    CompletableFuture<String> getUsernameFromToken(AccessToken accessToken) throws
InvalidAccessToken;
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
index 0a67751..b236599 100644
--- a/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
+++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
@@ -21,6 +21,7 @@ package org.apache.james.jmap.memory.access;
 
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
 
 import javax.inject.Inject;
 import javax.inject.Named;
@@ -32,7 +33,6 @@ import org.apache.james.jmap.api.access.AccessTokenRepository;
 import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
 
 import com.google.common.base.Preconditions;
-import com.jasongoodwin.monads.Try;
 
 @Singleton
 public class MemoryAccessTokenRepository implements AccessTokenRepository {
@@ -65,11 +65,12 @@ public class MemoryAccessTokenRepository implements AccessTokenRepository
{
     }
 
     @Override
-    public CompletableFuture<Try<String>> getUsernameFromToken(AccessToken accessToken)
throws InvalidAccessToken {
+    public CompletableFuture<String> getUsernameFromToken(AccessToken accessToken)
throws InvalidAccessToken {
         Preconditions.checkNotNull(accessToken);
         synchronized (tokensExpirationDates) {
             return CompletableFuture.completedFuture(
-                Try.ofFailable(() -> Optional.ofNullable(tokensExpirationDates.get(accessToken)).orElseThrow(()
-> new InvalidAccessToken(accessToken))));
+                Optional.ofNullable(tokensExpirationDates.get(accessToken))
+                    .<CompletionException>orElseThrow(() -> new CompletionException(new
InvalidAccessToken(accessToken))));
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
index 95953ec..34de4f5 100644
--- a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
+++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/access/AbstractAccessTokenRepositoryTest.java
@@ -22,6 +22,8 @@ package org.apache.james.jmap.api.access;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
+import java.util.concurrent.CompletionException;
+
 import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
 import org.junit.Before;
 import org.junit.Test;
@@ -30,7 +32,7 @@ public abstract class AbstractAccessTokenRepositoryTest {
 
     private static final AccessToken TOKEN = AccessToken.generate();
     private static final String USERNAME = "username";
-    public static final long TTL_IN_MS = 1000;
+    protected static final long TTL_IN_MS = 1000;
 
     private AccessTokenRepository accessTokenRepository;
 
@@ -43,27 +45,30 @@ public abstract class AbstractAccessTokenRepositoryTest {
 
     @Test
     public void validTokenMustBeRetrieved() throws Throwable {
-        accessTokenRepository.addToken(USERNAME, TOKEN);
-        assertThat(accessTokenRepository.getUsernameFromToken(TOKEN).join().get()).isEqualTo(USERNAME);
+        accessTokenRepository.addToken(USERNAME, TOKEN).join();
+        assertThat(accessTokenRepository.getUsernameFromToken(TOKEN).join()).isEqualTo(USERNAME);
     }
 
     @Test
     public void absentTokensMustBeInvalid() throws Exception {
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join().get()).isInstanceOf(InvalidAccessToken.class);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
     }
 
     @Test
     public void removedTokensMustBeInvalid() throws Exception {
-        accessTokenRepository.addToken(USERNAME, TOKEN);
-        accessTokenRepository.removeToken(TOKEN);
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join().get()).isInstanceOf(InvalidAccessToken.class);
+        accessTokenRepository.addToken(USERNAME, TOKEN).join();
+        accessTokenRepository.removeToken(TOKEN).join();
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
     }
 
     @Test
     public void outDatedTokenMustBeInvalid() throws Exception {
-        accessTokenRepository.addToken(USERNAME, TOKEN);
+        accessTokenRepository.addToken(USERNAME, TOKEN).join();
         Thread.sleep(2 * TTL_IN_MS);
-        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join().get()).isInstanceOf(InvalidAccessToken.class);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).isInstanceOf(CompletionException.class);
+        assertThatThrownBy(() -> accessTokenRepository.getUsernameFromToken(TOKEN).join()).hasCauseInstanceOf(InvalidAccessToken.class);
     }
 
     @Test(expected = NullPointerException.class)

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/AccessTokenManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/AccessTokenManagerImpl.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/AccessTokenManagerImpl.java
index 0131071..e5a5cbc 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/AccessTokenManagerImpl.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/AccessTokenManagerImpl.java
@@ -19,6 +19,9 @@
 
 package org.apache.james.jmap.crypto;
 
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.ExecutionException;
+
 import javax.inject.Inject;
 import javax.inject.Singleton;
 
@@ -51,11 +54,13 @@ public class AccessTokenManagerImpl implements AccessTokenManager {
     @Override
     public String getUsernameFromToken(AccessToken token) throws InvalidAccessToken {
         try {
-            return accessTokenRepository.getUsernameFromToken(token).join().get();
-        } catch (InvalidAccessToken invalidAccessToken) {
-            throw invalidAccessToken;
-        } catch (Throwable throwable) {
-            throw Throwables.propagate(throwable);
+            return accessTokenRepository.getUsernameFromToken(token).join();
+        } catch (CompletionException completionException) {
+            if (completionException.getCause() instanceof InvalidAccessToken) {
+                throw (InvalidAccessToken) completionException.getCause();
+            } else {
+                throw Throwables.propagate(completionException);
+            }
         }
     }
     

http://git-wip-us.apache.org/repos/asf/james-project/blob/e71a7cd5/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/AccessTokenManagerImplTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/AccessTokenManagerImplTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/AccessTokenManagerImplTest.java
index fb3716a..acb539b 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/AccessTokenManagerImplTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/AccessTokenManagerImplTest.java
@@ -52,7 +52,7 @@ public class AccessTokenManagerImplTest {
     @Test
     public void grantShouldStoreATokenOnUsername() throws Exception {
         AccessToken token = accessTokenManager.grantAccessToken("username");
-        assertThat(accessTokenRepository.getUsernameFromToken(token).join().get()).isEqualTo("username");
+        assertThat(accessTokenRepository.getUsernameFromToken(token).join()).isEqualTo("username");
     }
     
     @Test(expected=NullPointerException.class)


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


Mime
View raw message