You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
3.2 KiB
142 lines
3.2 KiB
/**
|
|
* Copyright (C) 2024, Tim Holloway
|
|
*
|
|
* Manages app data persisted client-side in cookies.
|
|
*
|
|
* Date written: Jan 31, 2024
|
|
* Author: Tim Holloway <timh@mousetech.com>
|
|
*/
|
|
package com.mousetech.gourmetj;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
import jakarta.enterprise.context.RequestScoped;
|
|
import jakarta.inject.Named;
|
|
|
|
/**
|
|
* Request caching object for cookie data persistence.
|
|
* Does double-duty serving View Session timeouts.
|
|
*
|
|
* @author timh
|
|
* @since Jan 31, 2024
|
|
*/
|
|
@Named
|
|
@RequestScoped
|
|
public class CookieBean {
|
|
|
|
private static final String KEY_DISPLAY_ROWS = "displayRows";
|
|
|
|
private static final String KEY_SEARCH_TYPE = "searchType";
|
|
|
|
private static final String KEY_SEARCH_FOR = "searchFor";
|
|
|
|
/* Logger */
|
|
|
|
private static final Logger log =
|
|
LoggerFactory.getLogger(CookieBean.class);
|
|
|
|
private Map<String, String> cookieMap;
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public CookieBean() {
|
|
}
|
|
|
|
@PostConstruct
|
|
public void init() {
|
|
this.cookieMap = JSFUtils.getCookies();
|
|
}
|
|
|
|
/**
|
|
* Persist us to client cookie storage
|
|
*
|
|
* @throws UnsupportedEncodingException (which should never
|
|
* happen)
|
|
*/
|
|
public void saveCookies()
|
|
throws UnsupportedEncodingException {
|
|
final Map<String, Object> properties = new HashMap<>();
|
|
properties.put("maxAge", 31536000);
|
|
properties.put("path", "/");
|
|
properties.put("SameSite", "Strict");
|
|
|
|
for (Entry<String, String> e : cookieMap.entrySet()) {
|
|
JSFUtils.outputCookie(e.getKey(), e.getValue(),
|
|
properties);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Cookie value by name
|
|
*
|
|
* @param name Name of the cookie
|
|
* @return Value stored in the cookie
|
|
*/
|
|
public String getCookieValue(String name) {
|
|
return cookieMap.get(name);
|
|
}
|
|
|
|
public void setCookieValue(String name, String value) {
|
|
cookieMap.put(name, value);
|
|
}
|
|
|
|
// ************************
|
|
// App-specific properties
|
|
// ************************
|
|
|
|
public String getSearchText() {
|
|
return cookieMap.get(KEY_SEARCH_FOR);
|
|
}
|
|
|
|
public void setSearchText(String value) {
|
|
cookieMap.put(KEY_SEARCH_FOR, value);
|
|
}
|
|
|
|
// **
|
|
public Integer getSearchType() {
|
|
if (!cookieMap.containsKey(KEY_SEARCH_TYPE)) {
|
|
cookieMap.put(KEY_SEARCH_TYPE, "0");
|
|
}
|
|
String st = cookieMap.get(KEY_SEARCH_TYPE);
|
|
return Integer.valueOf(String.valueOf(st));
|
|
}
|
|
|
|
public void setSearchType(Integer value) {
|
|
cookieMap.put(KEY_SEARCH_TYPE, String.valueOf(value));
|
|
}
|
|
|
|
// **
|
|
public Integer getDisplayListSize() {
|
|
if (!cookieMap.containsKey(KEY_DISPLAY_ROWS)) {
|
|
cookieMap.put(KEY_DISPLAY_ROWS, "30");
|
|
}
|
|
String st = cookieMap.get(KEY_DISPLAY_ROWS);
|
|
return Integer.valueOf(String.valueOf(st));
|
|
}
|
|
|
|
public void setDisplayListSize(Integer value) {
|
|
cookieMap.put(KEY_DISPLAY_ROWS, String.valueOf(value));
|
|
}
|
|
|
|
/**
|
|
* IdleMonitor backing methods (session/View timeout)
|
|
*/
|
|
public void sessionIdleListener() {
|
|
log.info("Session Idle Listener fired.");
|
|
JSFUtils.addWarningMessage("Timeout approaching. Save your work!");
|
|
}
|
|
|
|
public void sessionTimeout() {
|
|
log.info("Session Timeout Listener fired.");
|
|
JSFUtils.logout();
|
|
}
|
|
|
|
}
|
|
|