setup for proper cusine/category searching

main
tim holloway 2 months ago
parent e7f373f663
commit 7f4fc58a36
  1. 2
      pom.xml
  2. 10
      src/main/java/com/mousetech/gourmetj/AdminMainBean.java
  3. 30
      src/main/java/com/mousetech/gourmetj/CookieBean.java
  4. 59
      src/main/java/com/mousetech/gourmetj/JSFUtils.java
  5. 1
      src/main/java/com/mousetech/gourmetj/RecipeDetailBean.java
  6. 1
      src/main/resources/META-INF/resources/main.xhtml

@ -7,7 +7,7 @@
<groupId>com.mousetech.gourmet</groupId>
<artifactId>gourmetj</artifactId>
<version>0.2.7</version>
<version>0.2.8</version>
<packaging>jar</packaging>
<name>GourmetJ</name>

@ -68,7 +68,8 @@ public class AdminMainBean implements Serializable {
}
/**
* @param cookieBean the cookieBean to set
* @param cookieBean the cookieBean to set.
* @deprecated Not invoked by @Inject
*/
public void setCookieBean(CookieBean cookieBean) {
this.cookieBean = cookieBean;
@ -210,13 +211,6 @@ public class AdminMainBean implements Serializable {
}
searchText = searchText.trim();
// Persist current settings
try {
cookieBean.saveCookies();
} catch (UnsupportedEncodingException e) {
// Something is really wrong if we can't create UTF-8!
log.error("Unable to save cookies!", e);
}
RecipeSearchType st = searchtypeEnum();
switch (st) {
case rst_BY_NAME:

@ -17,7 +17,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
/**
@ -28,7 +28,7 @@ import jakarta.inject.Named;
* @since Jan 31, 2024
*/
@Named
@RequestScoped
@ViewScoped
public class CookieBean {
private static final String KEY_DISPLAY_ROWS = "displayRows";
@ -44,10 +44,15 @@ public class CookieBean {
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
@ -63,11 +68,6 @@ public class CookieBean {
*/
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);
@ -86,6 +86,13 @@ public class CookieBean {
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();
}
}
// ************************
@ -97,7 +104,7 @@ public class CookieBean {
}
public void setSearchText(String value) {
cookieMap.put(KEY_SEARCH_FOR, value);
setCookieValue(KEY_SEARCH_FOR, value);
}
// **
@ -110,7 +117,8 @@ public class CookieBean {
}
public void setSearchType(Integer value) {
cookieMap.put(KEY_SEARCH_TYPE, String.valueOf(value));
String st = String.valueOf(value);
setCookieValue(KEY_SEARCH_TYPE, st);
}
// **
@ -123,11 +131,13 @@ public class CookieBean {
}
public void setDisplayListSize(Integer value) {
cookieMap.put(KEY_DISPLAY_ROWS, String.valueOf(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.");

@ -3,6 +3,7 @@
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
@ -12,6 +13,7 @@ import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.Flash;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
@ -126,29 +128,52 @@ public class JSFUtils {
/**
* Get cookie values.
*/
public static Map<String, String> getCookies(){
Map<String, Object> m0 = getExternalContext().getRequestCookieMap();
Map<String, String>m1 = new HashMap<String, String>();
m1 = m0.entrySet()
.stream()
.collect(Collectors.toMap(
e -> e.getKey(),
e -> ((Cookie)e.getValue()).getValue()));
public static Map<String, String> getCookies() {
Map<String, Object> m0 =
getExternalContext().getRequestCookieMap();
Map<String, String> m1 = new HashMap<String, String>();
m1 = m0.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(),
e -> ((Cookie) e.getValue()).getValue()));
return m1;
}
public static String getCookie(String cookieName) {
Map<String, Object> map =
getExternalContext().getRequestCookieMap();
if (map == null) {
return null; // no cookies at all
}
Cookie cookie = (Cookie) map.get(cookieName);
if (cookie == null) {
return null;
}
return cookie.getValue();
}
/**
* Set a cookie value in Response.
* @param name Cookie name
* @param value Cookie value
* @param properties Cookie property Map (timeout, <i>etc.</i>)
* @throws UnsupportedEncodingException
*
* @param name Cookie name
* @param value Cookie value
* @param properties Cookie property Map (timeout,
* <i>etc.</i>)
* @throws UnsupportedEncodingException
*/
public static void outputCookie(String name,
String value, Map<String, Object> properties) throws UnsupportedEncodingException {
getExternalContext().addResponseCookie(name,
URLEncoder.encode(value, "UTF-8"),
properties);
public static void outputCookie(String name, String value,
Map<String, Object> properties)
throws UnsupportedEncodingException {
// getExternalContext().addResponseCookie(name,
// URLEncoder.encode(value, "UTF-8"),
// properties);
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(31536000);
cookie.setPath("/");
jakarta.servlet.http.HttpServletResponse resp =
(HttpServletResponse) getExternalContext()
.getResponse();
resp.addCookie(cookie);
}
/**

@ -720,7 +720,6 @@ public class RecipeDetailBean implements Serializable {
}
if (recipeService.save(this.getRecipe())) {
////////////userSession.setRecipe(null);
return "recipeDetails?faces-redirect=true";
} else {
JSFUtils.addErrorMessage("Save recipe failed");

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html >
<ui:composition template="/WEB-INF/layout/layout.xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"

Loading…
Cancel
Save