james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [5/7] james-project git commit: JAMES-1718 AccessTokenRepository should not throw upon already stored AccessToken
Date Fri, 15 Apr 2016 08:53:27 GMT
JAMES-1718 AccessTokenRepository should not throw upon already stored AccessToken


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

Branch: refs/heads/master
Commit: 231beb503f826742698e73ff81038246da178385
Parents: 0cc164a
Author: Benoit Tellier <btellier@linagora.com>
Authored: Mon Apr 11 18:39:23 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Fri Apr 15 15:50:26 2016 +0700

----------------------------------------------------------------------
 .../access/CassandraAccessTokenDAO.java         | 11 ++++----
 .../access/CassandraAccessTokenRepository.java  |  7 ++---
 .../james/jmap/api/access/AccessToken.java      |  2 +-
 .../jmap/api/access/AccessTokenRepository.java  |  3 +-
 .../exceptions/AccessTokenAlreadyStored.java    | 29 --------------------
 .../access/MemoryAccessTokenRepository.java     |  7 ++---
 .../AbstractAccessTokenRepositoryTest.java      |  7 -----
 7 files changed, 11 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/231beb50/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 43e4811..b5c884c 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
@@ -56,7 +56,6 @@ public class CassandraAccessTokenDAO {
             .where(eq(CassandraAccessTokenTable.TOKEN, bindMarker(CassandraAccessTokenTable.TOKEN))));
 
         this.insertStatement = session.prepare(insertInto(CassandraAccessTokenTable.TABLE_NAME)
-            .ifNotExists()
             .value(CassandraAccessTokenTable.TOKEN, bindMarker(CassandraAccessTokenTable.TOKEN))
             .value(CassandraAccessTokenTable.USERNAME, bindMarker(CassandraAccessTokenTable.USERNAME))
             .using(ttl(bindMarker(TTL))));
@@ -66,21 +65,21 @@ public class CassandraAccessTokenDAO {
             .where(eq(CassandraAccessTokenTable.TOKEN, bindMarker(CassandraAccessTokenTable.TOKEN))));
     }
 
-    public CompletableFuture<Boolean> addToken(String username, AccessToken accessToken)
{
-        return cassandraAsyncExecutor.executeReturnApplied(insertStatement.bind()
-            .setUUID(CassandraAccessTokenTable.TOKEN, accessToken.getToken())
+    public CompletableFuture<Void> addToken(String username, AccessToken accessToken)
{
+        return cassandraAsyncExecutor.executeVoid(insertStatement.bind()
+            .setUUID(CassandraAccessTokenTable.TOKEN, accessToken.asUUID())
             .setString(CassandraAccessTokenTable.USERNAME, username)
             .setInt(TTL, durationInSeconds));
     }
 
     public CompletableFuture<Void> removeToken(AccessToken accessToken) {
         return cassandraAsyncExecutor.executeVoid(removeStatement.bind()
-            .setUUID(CassandraAccessTokenTable.TOKEN, accessToken.getToken()));
+            .setUUID(CassandraAccessTokenTable.TOKEN, accessToken.asUUID()));
     }
 
     public CompletableFuture<Optional<String>> getUsernameFromToken(AccessToken
accessToken) {
         return cassandraAsyncExecutor.executeSingleRow(selectStatement.bind()
-            .setUUID(CassandraAccessTokenTable.TOKEN, accessToken.getToken()))
+            .setUUID(CassandraAccessTokenTable.TOKEN, accessToken.asUUID()))
             .thenApply(optional -> optional.map(row -> row.getString(CassandraAccessTokenTable.USERNAME)));
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/231beb50/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 2afd24d..0dd51a7 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
@@ -24,7 +24,6 @@ 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.AccessTokenAlreadyStored;
 import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
 
 import com.datastax.driver.core.Session;
@@ -40,14 +39,12 @@ public class CassandraAccessTokenRepository implements AccessTokenRepository
{
     }
 
     @Override
-    public void addToken(String username, AccessToken accessToken) throws AccessTokenAlreadyStored
{
+    public void addToken(String username, AccessToken accessToken) {
         Preconditions.checkNotNull(username);
         Preconditions.checkArgument(! username.isEmpty(), "Username should not be empty");
         Preconditions.checkNotNull(accessToken);
 
-        if (!cassandraAccessTokenDAO.addToken(username, accessToken).join()) {
-            throw new AccessTokenAlreadyStored(accessToken);
-        }
+        cassandraAccessTokenDAO.addToken(username, accessToken).join();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/231beb50/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessToken.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessToken.java
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessToken.java
index ea86453..d206c68 100644
--- a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessToken.java
+++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/AccessToken.java
@@ -48,7 +48,7 @@ public class AccessToken {
         return token.toString();
     }
 
-    public UUID getToken() {
+    public UUID asUUID() {
         return token;
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/231beb50/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 cd00ff3..6fb3ae0 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
@@ -19,14 +19,13 @@
 
 package org.apache.james.jmap.api.access;
 
-import org.apache.james.jmap.api.access.exceptions.AccessTokenAlreadyStored;
 import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
 
 public interface AccessTokenRepository {
 
     String TOKEN_EXPIRATION_IN_MS = "tokenExpirationInMs";
     
-    void addToken(String username, AccessToken accessToken) throws AccessTokenAlreadyStored;
+    void addToken(String username, AccessToken accessToken);
 
     void removeToken(AccessToken accessToken);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/231beb50/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/exceptions/AccessTokenAlreadyStored.java
----------------------------------------------------------------------
diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/exceptions/AccessTokenAlreadyStored.java
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/exceptions/AccessTokenAlreadyStored.java
deleted file mode 100644
index 94e83c9..0000000
--- a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/access/exceptions/AccessTokenAlreadyStored.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/****************************************************************
- * 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.access.exceptions;
-
-import org.apache.james.jmap.api.access.AccessToken;
-
-public class AccessTokenAlreadyStored extends RuntimeException {
-
-    public AccessTokenAlreadyStored(AccessToken token) {
-        super(token.serialize() + " is already stored");
-    }
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/231beb50/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 a333aae..2c16fc2 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
@@ -28,7 +28,6 @@ import javax.inject.Singleton;
 import org.apache.commons.collections4.map.PassiveExpiringMap;
 import org.apache.james.jmap.api.access.AccessToken;
 import org.apache.james.jmap.api.access.AccessTokenRepository;
-import org.apache.james.jmap.api.access.exceptions.AccessTokenAlreadyStored;
 import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
 
 import com.google.common.base.Preconditions;
@@ -44,14 +43,12 @@ public class MemoryAccessTokenRepository implements AccessTokenRepository
{
     }
 
     @Override
-    public void addToken(String username, AccessToken accessToken) throws AccessTokenAlreadyStored{
+    public void addToken(String username, AccessToken accessToken) {
         Preconditions.checkNotNull(username);
         Preconditions.checkArgument(! username.isEmpty(), "Username should not be empty");
         Preconditions.checkNotNull(accessToken);
         synchronized (tokensExpirationDates) {
-            if (tokensExpirationDates.putIfAbsent(accessToken, username) != null) {
-                throw new AccessTokenAlreadyStored(accessToken);
-            }
+            tokensExpirationDates.put(accessToken, username);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/231beb50/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 648a92e..04482be 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,7 +22,6 @@ package org.apache.james.jmap.api.access;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
-import org.apache.james.jmap.api.access.exceptions.AccessTokenAlreadyStored;
 import org.apache.james.jmap.api.access.exceptions.InvalidAccessToken;
 import org.junit.Before;
 import org.junit.Test;
@@ -61,12 +60,6 @@ public abstract class AbstractAccessTokenRepositoryTest {
     }
 
     @Test
-    public void addTokenMustThrowWhenTokenIsAlreadyStored() throws Exception {
-        accessTokenRepository.addToken(USERNAME, TOKEN);
-        assertThatThrownBy(() -> accessTokenRepository.addToken(USERNAME, TOKEN)).isInstanceOf(AccessTokenAlreadyStored.class);
-    }
-
-    @Test
     public void outDatedTokenMustBeInvalid() throws Exception {
         accessTokenRepository.addToken(USERNAME, TOKEN);
         Thread.sleep(2 * TTL_IN_MS);


---------------------------------------------------------------------
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