james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1719316 - in /james/project/trunk/server/data/data-jmap/src: main/java/org/apache/james/jmap/memory/ main/java/org/apache/james/jmap/memory/access/ test/ test/java/ test/java/org/ test/java/org/apache/ test/java/org/apache/james/ test/java...
Date Fri, 11 Dec 2015 10:07:19 GMT
Author: matthieu
Date: Fri Dec 11 10:07:18 2015
New Revision: 1719316

URL: http://svn.apache.org/viewvc?rev=1719316&view=rev
Log:
JAMES-1644 Provide an InMemory implementation for AccessTokenRepository

Added:
    james/project/trunk/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/
    james/project/trunk/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/
    james/project/trunk/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
    james/project/trunk/server/data/data-jmap/src/test/
    james/project/trunk/server/data/data-jmap/src/test/java/
    james/project/trunk/server/data/data-jmap/src/test/java/org/
    james/project/trunk/server/data/data-jmap/src/test/java/org/apache/
    james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/
    james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/jmap/
    james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/
    james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/
    james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java

Added: james/project/trunk/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java?rev=1719316&view=auto
==============================================================================
--- james/project/trunk/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
(added)
+++ james/project/trunk/server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepository.java
Fri Dec 11 10:07:18 2015
@@ -0,0 +1,67 @@
+/****************************************************************
+ * 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.memory.access;
+
+import com.google.common.base.Preconditions;
+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 javax.inject.Inject;
+import javax.inject.Singleton;
+
+@Singleton
+public class MemoryAccessTokenRepository implements AccessTokenRepository {
+
+    private final PassiveExpiringMap<AccessToken, Boolean> tokensExpirationDates;
+
+    @Inject
+    public MemoryAccessTokenRepository(long durationInMilliseconds) {
+        tokensExpirationDates = new PassiveExpiringMap<>(durationInMilliseconds);
+    }
+
+    @Override
+    public void addToken(AccessToken accessToken) throws AccessTokenAlreadyStored{
+        Preconditions.checkNotNull(accessToken);
+        synchronized (tokensExpirationDates) {
+            if (tokensExpirationDates.putIfAbsent(accessToken, true) != null) {
+                throw new AccessTokenAlreadyStored(accessToken);
+            }
+        }
+    }
+
+    @Override
+    public void removeToken(AccessToken accessToken) {
+        Preconditions.checkNotNull(accessToken);
+        synchronized (tokensExpirationDates) {
+            tokensExpirationDates.remove(accessToken);
+        }
+    }
+
+    @Override
+    public boolean verifyToken(AccessToken accessToken) {
+        Preconditions.checkNotNull(accessToken);
+        synchronized (tokensExpirationDates) {
+            return tokensExpirationDates.containsKey(accessToken);
+        }
+    }
+
+}

Added: james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java?rev=1719316&view=auto
==============================================================================
--- james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java
(added)
+++ james/project/trunk/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/access/MemoryAccessTokenRepositoryTest.java
Fri Dec 11 10:07:18 2015
@@ -0,0 +1,92 @@
+/****************************************************************
+ * 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.memory.access;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
+
+import org.apache.james.jmap.api.access.AccessToken;
+import org.apache.james.jmap.api.access.exceptions.AccessTokenAlreadyStored;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MemoryAccessTokenRepositoryTest {
+
+    private static final AccessToken TOKEN = AccessToken.fromString("TOKEN");
+    private static final int TTL_IN_MS = 100;
+
+    private MemoryAccessTokenRepository accessTokenRepository;
+
+    @Before
+    public void setUp() {
+        accessTokenRepository = new MemoryAccessTokenRepository(TTL_IN_MS);
+    }
+
+    @Test
+    public void validTokenMustWork() throws Exception {
+        accessTokenRepository.addToken(TOKEN);
+        assertThat(accessTokenRepository.verifyToken(TOKEN)).isTrue();
+    }
+
+    @Test
+    public void nonStoredTokensMustBeInvalid() throws Exception {
+        assertThat(accessTokenRepository.verifyToken(TOKEN)).isFalse();
+    }
+
+    @Test
+    public void removedTokensMustBeInvalid() throws Exception {
+        accessTokenRepository.addToken(TOKEN);
+        accessTokenRepository.removeToken(TOKEN);
+        assertThat(accessTokenRepository.verifyToken(TOKEN)).isFalse();
+    }
+
+    @Test(expected = AccessTokenAlreadyStored.class)
+    public void addTokenMustThrowWhenTokenIsAlreadyStored() throws Exception {
+        try {
+            accessTokenRepository.addToken(TOKEN);
+        } catch(Exception e) {
+            fail("Exception caught", e);
+        }
+        accessTokenRepository.addToken(TOKEN);
+    }
+
+    @Test
+    public void outDatedTokenMustBeInvalid() throws Exception {
+        accessTokenRepository.addToken(TOKEN);
+        Thread.sleep(200);
+        assertThat(accessTokenRepository.verifyToken(TOKEN)).isFalse();
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void addTokenMustThrowWhenTokenIsNull() throws Exception {
+        accessTokenRepository.addToken(null);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void removeTokenTokenMustThrowWhenTokenIsNull() throws Exception {
+        accessTokenRepository.removeToken(null);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void verifyTokenTokenMustThrowWhenTokenIsNull() throws Exception {
+        accessTokenRepository.verifyToken(null);
+    }
+
+}



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