-
Notifications
You must be signed in to change notification settings - Fork 153
Enhanced Session Management in Apache Roller #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
170937d
Roller session improvements.
snoopdave dfd7e1b
Add a test and fixes for problems revealed.
snoopdave 8d09507
Restore listener methods.
snoopdave 26fc09f
Session manager only manages logged-in user sessions.
snoopdave c31fb71
Use default methods rather than adapter, also add test for session ma…
snoopdave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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.CacheHandlerAdapter; | ||
| 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 extends CacheHandlerAdapter { | ||
| @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 hidden or 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 hidden or 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
55 changes: 55 additions & 0 deletions
55
app/src/main/java/org/apache/roller/weblogger/util/cache/CacheHandlerAdapter.java
This file contains hidden or 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,55 @@ | ||
| /* | ||
| * 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.util.cache; | ||
|
|
||
| import org.apache.roller.weblogger.pojos.*; | ||
|
|
||
| public class CacheHandlerAdapter implements CacheHandler { | ||
|
|
||
| @Override | ||
| public void invalidate(WeblogEntry entry) { | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(Weblog website) { | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(WeblogBookmark bookmark) { | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(WeblogBookmarkFolder folder) { | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(WeblogEntryComment comment) { | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(User user) { | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(WeblogCategory category) { | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(WeblogTemplate template) { | ||
| } | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
app/src/test/java/org/apache/roller/weblogger/ui/core/RollerLoginSessionManagerTest.java
This file contains hidden or 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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.