Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added persisted session storage support [#538]
* Added a data model to be persisted. * Added a REST API for retrieving updates. This is a foundation only and does not actually decorate the session object. That will be done with new feature code that will leverage this feature set.
- Loading branch information
Showing
11 changed files
with
420 additions
and
0 deletions.
There are no files selected for viewing
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
8 changes: 8 additions & 0 deletions
8
comixed-model/src/main/java/org/comixedproject/model/session/SessionUpdate.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,8 @@ | ||
package org.comixedproject.model.session; | ||
|
||
/** | ||
* <code>SessionUpdate</code> holds the content of a session update returned to the browser. | ||
* | ||
* @author Darryl L. Pierce | ||
*/ | ||
public class SessionUpdate {} |
26 changes: 26 additions & 0 deletions
26
comixed-model/src/main/java/org/comixedproject/model/session/UserSession.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,26 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2020, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixedproject.model.session; | ||
|
||
/** | ||
* <code>UserSession</code> represents the server-side details for a user's session. | ||
* | ||
* @author Darryl L. Pierce | ||
*/ | ||
public class UserSession {} |
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
81 changes: 81 additions & 0 deletions
81
comixed-rest-api/src/main/java/org/comixedproject/controller/user/SessionController.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,81 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2020, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixedproject.controller.user; | ||
|
||
import com.fasterxml.jackson.annotation.JsonView; | ||
import javax.servlet.http.HttpSession; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.comixedproject.model.net.ApiResponse; | ||
import org.comixedproject.model.net.session.SessionUpdateRequest; | ||
import org.comixedproject.model.net.session.SessionUpdateResponse; | ||
import org.comixedproject.model.session.SessionUpdate; | ||
import org.comixedproject.model.session.UserSession; | ||
import org.comixedproject.service.user.SessionService; | ||
import org.comixedproject.views.View; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Log4j2 | ||
public class SessionController { | ||
public static final String SESSION_ENTRY_KEY = "session-update"; | ||
|
||
@Autowired private SessionService sessionService; | ||
|
||
/** | ||
* Retrieves a session update. Waits a given period of time before returning an empty update. | ||
* | ||
* @param request the request body | ||
* @return the session update | ||
*/ | ||
@PostMapping( | ||
value = "/api/session/updates", | ||
produces = MediaType.APPLICATION_JSON_VALUE, | ||
consumes = MediaType.APPLICATION_JSON_VALUE) | ||
@JsonView(View.SessionUpdateView.class) | ||
public ApiResponse<SessionUpdateResponse> getSessionUpdate( | ||
final HttpSession httpSession, @RequestBody() final SessionUpdateRequest request) { | ||
UserSession userSession = null; | ||
|
||
if (request.getReset()) { | ||
log.info("Resetting the user server-side session"); | ||
userSession = new UserSession(); | ||
} else { | ||
log.info("Retrieving the user server-side session"); | ||
userSession = (UserSession) httpSession.getAttribute(SESSION_ENTRY_KEY); | ||
if (userSession == null) { | ||
log.info("Creating a new server-side session"); | ||
userSession = new UserSession(); | ||
} | ||
} | ||
|
||
final Long timeout = request.getTimeout(); | ||
log.info("Getting session update: timeout={}", timeout); | ||
final SessionUpdate sessionUpdate = this.sessionService.getSessionUpdate(userSession, timeout); | ||
|
||
this.log.info("Saving server-side session"); | ||
httpSession.setAttribute(SESSION_ENTRY_KEY, userSession); | ||
|
||
log.info("Returning session update"); | ||
return new ApiResponse<>(new SessionUpdateResponse(sessionUpdate)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...xed-rest-api/src/main/java/org/comixedproject/model/net/session/SessionUpdateRequest.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,42 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2020, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixedproject.model.net.session; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
/** | ||
* <code>SessionUpdateRequest</code> contains the body for a session update request. | ||
* | ||
* @author Darryl L. Pierce | ||
*/ | ||
@NoArgsConstructor | ||
public class SessionUpdateRequest { | ||
@JsonProperty("reset") | ||
@Getter | ||
@Setter | ||
private Boolean reset; | ||
|
||
@JsonProperty("timeout") | ||
@Getter | ||
@Setter | ||
private Long timeout; | ||
} |
42 changes: 42 additions & 0 deletions
42
...ed-rest-api/src/main/java/org/comixedproject/model/net/session/SessionUpdateResponse.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,42 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2020, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixedproject.model.net.session; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonView; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.comixedproject.model.session.SessionUpdate; | ||
import org.comixedproject.views.View; | ||
|
||
/** | ||
* <code>SessionUpdateResponse</code> contains the set of updates to be applied to the browser-side | ||
* session. | ||
* | ||
* @author Darryl L. Pierce | ||
*/ | ||
@AllArgsConstructor | ||
public class SessionUpdateResponse { | ||
@JsonProperty("update") | ||
@Getter | ||
@Setter | ||
@JsonView(View.SessionUpdateView.class) | ||
private SessionUpdate update; | ||
} |
114 changes: 114 additions & 0 deletions
114
comixed-rest-api/src/test/java/org/comixedproject/controller/user/SessionControllerTest.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,114 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2020, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixedproject.controller.user; | ||
|
||
import static junit.framework.TestCase.*; | ||
import static org.comixedproject.controller.user.SessionController.SESSION_ENTRY_KEY; | ||
|
||
import javax.servlet.http.HttpSession; | ||
import org.comixedproject.model.net.ApiResponse; | ||
import org.comixedproject.model.net.session.SessionUpdateRequest; | ||
import org.comixedproject.model.net.session.SessionUpdateResponse; | ||
import org.comixedproject.model.session.SessionUpdate; | ||
import org.comixedproject.model.session.UserSession; | ||
import org.comixedproject.service.user.SessionService; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.*; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SessionControllerTest { | ||
private static final Long TEST_TIMEOUT = 717L; | ||
|
||
@InjectMocks private SessionController sessionController; | ||
@Mock private SessionService sessionService; | ||
@Mock private HttpSession httpSession; | ||
@Mock private UserSession userSession; | ||
@Captor private ArgumentCaptor<UserSession> userSessionArgumentCaptor; | ||
@Mock private SessionUpdateRequest sessionUpdateRequest; | ||
@Mock private SessionUpdate sessionUpdate; | ||
|
||
@Test | ||
public void testGetSessionUpdateNoExistingSession() { | ||
Mockito.when(sessionUpdateRequest.getReset()).thenReturn(Boolean.FALSE); | ||
Mockito.when(httpSession.getAttribute(Mockito.anyString())).thenReturn(null); | ||
Mockito.when(sessionUpdateRequest.getTimeout()).thenReturn(TEST_TIMEOUT); | ||
Mockito.when( | ||
sessionService.getSessionUpdate(userSessionArgumentCaptor.capture(), Mockito.anyLong())) | ||
.thenReturn(sessionUpdate); | ||
|
||
final ApiResponse<SessionUpdateResponse> response = | ||
sessionController.getSessionUpdate(httpSession, sessionUpdateRequest); | ||
|
||
assertNotNull(response); | ||
assertTrue(response.isSuccess()); | ||
assertNotNull(response.getResult()); | ||
assertSame(sessionUpdate, response.getResult().getUpdate()); | ||
assertNotNull(userSessionArgumentCaptor.getValue()); | ||
|
||
Mockito.verify(sessionService, Mockito.times(1)) | ||
.getSessionUpdate(userSessionArgumentCaptor.getValue(), TEST_TIMEOUT); | ||
Mockito.verify(httpSession, Mockito.times(1)) | ||
.setAttribute(SESSION_ENTRY_KEY, userSessionArgumentCaptor.getValue()); | ||
} | ||
|
||
@Test | ||
public void testGetSessionUpdateBrowserInitiateReset() { | ||
Mockito.when(sessionUpdateRequest.getReset()).thenReturn(Boolean.TRUE); | ||
Mockito.when(sessionUpdateRequest.getTimeout()).thenReturn(TEST_TIMEOUT); | ||
Mockito.when( | ||
sessionService.getSessionUpdate(userSessionArgumentCaptor.capture(), Mockito.anyLong())) | ||
.thenReturn(sessionUpdate); | ||
|
||
final ApiResponse<SessionUpdateResponse> response = | ||
sessionController.getSessionUpdate(httpSession, sessionUpdateRequest); | ||
|
||
assertNotNull(response); | ||
assertTrue(response.isSuccess()); | ||
assertNotNull(response.getResult()); | ||
assertSame(sessionUpdate, response.getResult().getUpdate()); | ||
assertNotNull(userSessionArgumentCaptor.getValue()); | ||
|
||
Mockito.verify(sessionService, Mockito.times(1)) | ||
.getSessionUpdate(userSessionArgumentCaptor.getValue(), TEST_TIMEOUT); | ||
Mockito.verify(httpSession, Mockito.times(1)) | ||
.setAttribute(SESSION_ENTRY_KEY, userSessionArgumentCaptor.getValue()); | ||
} | ||
|
||
@Test | ||
public void testGetSessionUpdate() { | ||
Mockito.when(sessionUpdateRequest.getReset()).thenReturn(Boolean.FALSE); | ||
Mockito.when(httpSession.getAttribute(Mockito.anyString())).thenReturn(userSession); | ||
Mockito.when(sessionUpdateRequest.getTimeout()).thenReturn(TEST_TIMEOUT); | ||
Mockito.when(sessionService.getSessionUpdate(Mockito.any(UserSession.class), Mockito.anyLong())) | ||
.thenReturn(sessionUpdate); | ||
|
||
final ApiResponse<SessionUpdateResponse> response = | ||
sessionController.getSessionUpdate(httpSession, sessionUpdateRequest); | ||
|
||
assertNotNull(response); | ||
assertTrue(response.isSuccess()); | ||
assertNotNull(response.getResult()); | ||
assertSame(sessionUpdate, response.getResult().getUpdate()); | ||
|
||
Mockito.verify(sessionService, Mockito.times(1)).getSessionUpdate(userSession, TEST_TIMEOUT); | ||
Mockito.verify(httpSession, Mockito.times(1)).setAttribute(SESSION_ENTRY_KEY, userSession); | ||
} | ||
} |
Oops, something went wrong.