-
Notifications
You must be signed in to change notification settings - Fork 2
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
[NEW] store checked-in cookie and show checked-in alert #278
Open
maxones25
wants to merge
11
commits into
dev
Choose a base branch
from
feature/new_store_check_in_cookie
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
612d3ee
add class CheckInInterceptor
maxones25 a6988d6
add changes
maxones25 718e458
Change Text Alignment to center
maxones25 f360759
Move Check Cookie Code from Pre to Post Method
maxones25 47690ca
added newlines, fixed package info and types in ContactTracingService
Gabril-E 09bdde2
Merge branch 'feature/new_store_check_in_cookie' of https://github.co…
Gabril-E d176761
eof newlines
Gabril-E 428f125
organized imports formated some code
Gabril-E 1d728de
Merge branch 'dev' into feature/new_store_check_in_cookie
Gabril-E 6672336
Update layout.html
oliverhummel 725dfc7
Update layout.html
oliverhummel 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
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
89 changes: 89 additions & 0 deletions
89
src/main/java/de/hs_mannheim/informatik/ct/controller/interceptor/CheckInInterceptor.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,89 @@ | ||
/* | ||
* Corona Tracking Tool der Hochschule Mannheim | ||
* Copyright (C) 2021 Hochschule Mannheim | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package de.hs_mannheim.informatik.ct.controller.interceptor; | ||
|
||
import de.hs_mannheim.informatik.ct.controller.Utilities; | ||
import de.hs_mannheim.informatik.ct.model.RoomVisit; | ||
import de.hs_mannheim.informatik.ct.persistence.services.RoomVisitService; | ||
import de.hs_mannheim.informatik.ct.persistence.services.VisitorService; | ||
import de.hs_mannheim.informatik.ct.util.CookieManager; | ||
import lombok.val; | ||
import lombok.var; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CheckInInterceptor implements HandlerInterceptor { | ||
|
||
@Autowired | ||
private VisitorService visitorService; | ||
|
||
@Autowired | ||
private RoomVisitService roomVisitService; | ||
|
||
@Autowired | ||
private Utilities util; | ||
|
||
private static final String CHECKED_IN_COOKIE_NAME = "checkedInEmail"; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, | ||
@Nullable ModelAndView modelAndView) throws Exception { | ||
val cookieManager = new CookieManager(request, response); | ||
var isCheckedIn = false; | ||
val checkedInEmail = cookieManager.getCookieValue(CookieManager.Cookies.CHECKED_IN_EMAIL); | ||
if(checkedInEmail != null){ | ||
val checkedInRoom = getCheckedInRoomName(checkedInEmail); | ||
if(checkedInRoom != null){ | ||
isCheckedIn = true; | ||
request.setAttribute("checkedInRoom", checkedInRoom); | ||
}else{ | ||
cookieManager.removeCookie(CookieManager.Cookies.CHECKED_IN_EMAIL); | ||
} | ||
} | ||
request.setAttribute("checkedInEmail", checkedInEmail); | ||
request.setAttribute("isCheckedIn", isCheckedIn); | ||
} | ||
|
||
private String getCheckedInRoomName(String email){ | ||
List<RoomVisit> roomVisits = findCurrentRoomVisitsByEmail(email); | ||
return roomVisits.size() > 0 ? roomVisits.get(0).getRoom().getName() : null; | ||
} | ||
|
||
private List<RoomVisit> findCurrentRoomVisitsByEmail(String email){ | ||
List<RoomVisit> roomVisits = new ArrayList<>(); | ||
val visitor = visitorService.findVisitorByEmail(email); | ||
if(visitor.isPresent()) { | ||
for(val roomVisit : roomVisitService.getCheckedInRoomVisits(visitor.get())){ | ||
roomVisits.add(roomVisit); | ||
} | ||
} | ||
return roomVisits; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/de/hs_mannheim/informatik/ct/controller/resolver/CookieManagerResolver.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,50 @@ | ||
/* | ||
* Corona Tracking Tool der Hochschule Mannheim | ||
* Copyright (C) 2021 Hochschule Mannheim | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package de.hs_mannheim.informatik.ct.controller.resolver; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
import de.hs_mannheim.informatik.ct.util.CookieManager; | ||
|
||
public class CookieManagerResolver implements HandlerMethodArgumentResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter methodParameter) { | ||
return methodParameter.getParameter().getType() == CookieManager.class; | ||
} | ||
|
||
@Override | ||
public Object resolveArgument( | ||
MethodParameter methodParameter, | ||
ModelAndViewContainer modelAndViewContainer, | ||
NativeWebRequest nativeWebRequest, | ||
WebDataBinderFactory webDataBinderFactory) throws Exception { | ||
|
||
HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); | ||
HttpServletResponse response = (HttpServletResponse) nativeWebRequest.getNativeResponse(); | ||
|
||
return new CookieManager(request, response); | ||
} | ||
} |
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
151 changes: 151 additions & 0 deletions
151
src/main/java/de/hs_mannheim/informatik/ct/util/CookieManager.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,151 @@ | ||
/* | ||
* Corona Tracking Tool der Hochschule Mannheim | ||
* Copyright (C) 2021 Hochschule Mannheim | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package de.hs_mannheim.informatik.ct.util; | ||
|
||
import javax.servlet.http.Cookie; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.time.LocalTime; | ||
|
||
import org.springframework.web.util.WebUtils; | ||
|
||
import lombok.val; | ||
|
||
public class CookieManager { | ||
private HttpServletRequest request; | ||
private HttpServletResponse response; | ||
|
||
public CookieManager(HttpServletRequest request){ | ||
this.request = request; | ||
} | ||
|
||
public CookieManager(HttpServletRequest request, HttpServletResponse response){ | ||
this(request); | ||
this.response = response; | ||
} | ||
|
||
public enum Cookies{ | ||
CHECKED_IN_EMAIL("checkedInEmail"); | ||
|
||
String name; | ||
|
||
Cookies(String name) { | ||
this.name = name; | ||
} | ||
|
||
String getName(){ | ||
return this.name; | ||
} | ||
} | ||
|
||
/** | ||
* create a new cookie with a cookie factory | ||
* | ||
* @param cookieType cookie type | ||
* @param value value of the cookie | ||
*/ | ||
private Cookie createCookie(Cookies cookieType, String value) { | ||
Cookie cookie = null; | ||
switch (cookieType){ | ||
case CHECKED_IN_EMAIL: | ||
cookie = new CookieBuilder(cookieType.getName(), value) | ||
.maxAge(getSecondsTill(LocalTime.parse(ScheduledMaintenanceTasks.FORCED_END_TIME))) | ||
.build(); | ||
break; | ||
} | ||
return cookie; | ||
} | ||
|
||
/** | ||
* add a cookie to the http response | ||
* | ||
* @param cookieType cookie type | ||
* @param value value of the cookie | ||
*/ | ||
public void addCookie(Cookies cookieType, String value) { | ||
val cookie = createCookie(cookieType, value); | ||
response.addCookie(cookie); | ||
} | ||
|
||
/** | ||
* remove a cookie from the http response | ||
* | ||
* @param cookieType cookie type | ||
*/ | ||
public void removeCookie(Cookies cookieType) { | ||
Cookie cookie = new Cookie(cookieType.getName(), ""); | ||
cookie.setMaxAge(0); | ||
cookie.setPath("/"); | ||
response.addCookie(cookie); | ||
} | ||
|
||
/** | ||
* get a cookie from http request | ||
* | ||
* @param cookieType cookie type | ||
*/ | ||
public String getCookieValue(Cookies cookieType){ | ||
val cookie = WebUtils.getCookie(request, cookieType.getName()); | ||
return cookie!=null ? cookie.getValue() : null; | ||
} | ||
|
||
/** | ||
* remove a cookie from the http response | ||
* | ||
* @param maxAgeEndTime time when the cookie should be invalid | ||
* @return max age of the cookie | ||
*/ | ||
private int getSecondsTill(LocalTime maxAgeEndTime){ | ||
int now = LocalTime.now().toSecondOfDay(); | ||
int endTime = maxAgeEndTime.toSecondOfDay(); | ||
int nextDayEndTime = ((24 * 60 * 60) + endTime); | ||
return (now > endTime) ? nextDayEndTime - now : endTime - now; | ||
} | ||
|
||
private static class CookieBuilder{ | ||
private String name, value, path; | ||
private int maxAge; | ||
|
||
public CookieBuilder(String name, String value){ | ||
this.name = name; | ||
this.value = value; | ||
this.path = "/"; | ||
this.maxAge = 0; | ||
} | ||
|
||
public CookieBuilder maxAge(int maxAge){ | ||
this.maxAge = maxAge; | ||
return this; | ||
} | ||
|
||
public CookieBuilder path(String path){ | ||
this.path = path; | ||
return this; | ||
} | ||
|
||
public Cookie build(){ | ||
val cookie = new Cookie(name, value); | ||
cookie.setPath(path); | ||
if(maxAge>0){ | ||
cookie.setMaxAge(maxAge); | ||
} | ||
return cookie; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't we manage cookies in a much simpler fashion before this? Can you briefly explain, why this is neccessary, pls?