-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhanced Session Management in Apache Roller (#148)
* Roller session improvements. * Add a test and fixes for problems revealed. * Restore listener methods. * Session manager only manages logged-in user sessions. * Use default methods rather than adapter, also add test for session manager (a wip).
- Loading branch information
Showing
6 changed files
with
396 additions
and
42 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
app/src/main/java/org/apache/roller/weblogger/ui/core/RollerLoginSessionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.apache.roller.weblogger.ui.core; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.roller.weblogger.pojos.User; | ||
import org.apache.roller.weblogger.util.cache.Cache; | ||
import org.apache.roller.weblogger.util.cache.CacheHandler; | ||
import org.apache.roller.weblogger.util.cache.CacheManager; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class RollerLoginSessionManager { | ||
private static final Log log = LogFactory.getLog(RollerLoginSessionManager.class); | ||
private static final String CACHE_ID = "roller.session.cache"; | ||
private final Cache sessionCache; | ||
|
||
public static RollerLoginSessionManager getInstance() { | ||
return RollerLoginSessionManager.SingletonHolder.INSTANCE; | ||
} | ||
|
||
private static class SingletonHolder { | ||
private static final RollerLoginSessionManager INSTANCE = new RollerLoginSessionManager(); | ||
} | ||
|
||
class SessionCacheHandler implements CacheHandler { | ||
@Override | ||
public void invalidate(User user) { | ||
if (user != null && user.getUserName() != null) { | ||
sessionCache.remove(user.getUserName()); | ||
} | ||
} | ||
} | ||
|
||
/** Testing purpose only */ | ||
RollerLoginSessionManager(Cache cache) { | ||
this.sessionCache = cache; | ||
CacheManager.registerHandler(new SessionCacheHandler()); | ||
} | ||
|
||
private RollerLoginSessionManager() { | ||
Map<String, String> cacheProps = new HashMap<>(); | ||
cacheProps.put("id", CACHE_ID); | ||
cacheProps.put("size", "1000"); // Cache up to 1000 sessions | ||
cacheProps.put("timeout", "3600"); // Session timeout in seconds (1 hour) | ||
this.sessionCache = CacheManager.constructCache(null, cacheProps); | ||
CacheManager.registerHandler(new SessionCacheHandler()); | ||
} | ||
|
||
public void register(String userName, RollerSession session) { | ||
if (userName != null && session != null) { | ||
this.sessionCache.put(userName, session); | ||
log.debug("Registered session for user: " + userName); | ||
} | ||
} | ||
|
||
public RollerSession get(String userName) { | ||
if (userName != null) { | ||
return (RollerSession) this.sessionCache.get(userName); | ||
} | ||
return null; | ||
} | ||
|
||
public void invalidate(String userName) { | ||
if (userName != null) { | ||
this.sessionCache.remove(userName); | ||
log.debug("Invalidated session for user: " + userName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
app/src/test/java/org/apache/roller/weblogger/ui/core/RollerLoginSessionManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. 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. For additional information regarding | ||
* copyright in this work, please see the NOTICE file in the top level | ||
* directory of this distribution. | ||
*/ | ||
|
||
package org.apache.roller.weblogger.ui.core; | ||
|
||
import org.apache.roller.weblogger.pojos.User; | ||
import org.apache.roller.weblogger.util.cache.Cache; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
class RollerLoginSessionManagerTest { | ||
private RollerLoginSessionManager sessionManager; | ||
private Cache mockCache; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mockCache = mock(Cache.class); | ||
sessionManager = new RollerLoginSessionManager(mockCache); | ||
} | ||
|
||
@Test | ||
void testRegisterSession() { | ||
RollerSession mockSession = mock(RollerSession.class); | ||
String userName = "testUser"; | ||
|
||
sessionManager.register(userName, mockSession); | ||
|
||
verify(mockCache).put(userName, mockSession); | ||
} | ||
|
||
@Test | ||
void testGetSession() { | ||
RollerSession mockSession = mock(RollerSession.class); | ||
String userName = "testUser"; | ||
when(mockCache.get(userName)).thenReturn(mockSession); | ||
|
||
RollerSession result = sessionManager.get(userName); | ||
|
||
assertEquals(mockSession, result); | ||
verify(mockCache).get(userName); | ||
} | ||
|
||
@Test | ||
void testInvalidateSession() { | ||
String userName = "testUser"; | ||
|
||
sessionManager.invalidate(userName); | ||
|
||
verify(mockCache).remove(userName); | ||
} | ||
|
||
@Test | ||
void testCacheHandlerInvalidation() { | ||
User mockUser = mock(User.class); | ||
String userName = "testUser"; | ||
when(mockUser.getUserName()).thenReturn(userName); | ||
|
||
sessionManager.new SessionCacheHandler().invalidate(mockUser); | ||
|
||
verify(mockCache).remove(userName); | ||
} | ||
|
||
@Test | ||
void testNullInputHandling() { | ||
RollerSession mockSession = mock(RollerSession.class); | ||
|
||
sessionManager.register(null, mockSession); | ||
sessionManager.invalidate(null); | ||
sessionManager.get(null); | ||
|
||
verify(mockCache, never()).put(any(), any()); | ||
verify(mockCache, never()).remove(any()); | ||
verify(mockCache, never()).get(any()); | ||
} | ||
|
||
@Test | ||
void testSessionTimeout() { | ||
String userName = "testUser"; | ||
when(mockCache.get(userName)).thenReturn(null); | ||
|
||
RollerSession result = sessionManager.get(userName); | ||
|
||
assertNull(result); | ||
verify(mockCache).get(userName); | ||
} | ||
} |
Oops, something went wrong.