Web implementation of the Gourmet Recipe Manager
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.
 
 
 
 

152 lines
3.5 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.faces.view.ViewScoped;
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
@ViewScoped
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;
final Map<String, Object> properties = new HashMap<>();
/**
* Constructor.
*/
public CookieBean() {
properties.put("maxAge", 31536000);
properties.put("path", "/");
properties.put("SameSite", "Strict");
}
@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 {
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);
try {
JSFUtils.outputCookie(name, value, properties);
} catch (UnsupportedEncodingException e) {
// Should never happen. But...
log.error("Unable to encode cookie", e);
e.printStackTrace();
}
}
// ************************
// App-specific properties
// ************************
public String getSearchText() {
return cookieMap.get(KEY_SEARCH_FOR);
}
public void setSearchText(String value) {
setCookieValue(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) {
String st = String.valueOf(value);
setCookieValue(KEY_SEARCH_TYPE, st);
}
// **
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) {
setCookieValue(KEY_DISPLAY_ROWS, String.valueOf(value));
}
/**
* IdleMonitor backing methods (session/View timeout)
* Todo: move to a more general location. Currently
* only used by view editor, not Main!
*/
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();
}
}