parent
8bcd8cb9a1
commit
9cf0f89e76
10 changed files with 271 additions and 87 deletions
@ -0,0 +1,127 @@ |
||||
/** |
||||
* 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; |
||||
|
||||
/** |
||||
* Caching object for cookie data persistence. |
||||
* |
||||
* @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; |
||||
|
||||
/** |
||||
* 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)); |
||||
} |
||||
} |
Loading…
Reference in new issue