Compare commits

...

2 Commits

Author SHA1 Message Date
7bbd9df624 Focus pocus 2024-02-18 16:17:32 -05:00
7f4fc58a36 setup for proper cusine/category searching 2024-02-18 15:38:18 -05:00
6 changed files with 69 additions and 37 deletions

View File

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

View File

@ -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) { public void setCookieBean(CookieBean cookieBean) {
this.cookieBean = cookieBean; this.cookieBean = cookieBean;
@ -210,13 +211,6 @@ public class AdminMainBean implements Serializable {
} }
searchText = searchText.trim(); 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(); RecipeSearchType st = searchtypeEnum();
switch (st) { switch (st) {
case rst_BY_NAME: case rst_BY_NAME:

View File

@ -17,7 +17,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped; import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named; import jakarta.inject.Named;
/** /**
@ -28,7 +28,7 @@ import jakarta.inject.Named;
* @since Jan 31, 2024 * @since Jan 31, 2024
*/ */
@Named @Named
@RequestScoped @ViewScoped
public class CookieBean { public class CookieBean {
private static final String KEY_DISPLAY_ROWS = "displayRows"; private static final String KEY_DISPLAY_ROWS = "displayRows";
@ -44,10 +44,15 @@ public class CookieBean {
private Map<String, String> cookieMap; private Map<String, String> cookieMap;
final Map<String, Object> properties = new HashMap<>();
/** /**
* Constructor. * Constructor.
*/ */
public CookieBean() { public CookieBean() {
properties.put("maxAge", 31536000);
properties.put("path", "/");
properties.put("SameSite", "Strict");
} }
@PostConstruct @PostConstruct
@ -63,11 +68,6 @@ public class CookieBean {
*/ */
public void saveCookies() public void saveCookies()
throws UnsupportedEncodingException { 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()) { for (Entry<String, String> e : cookieMap.entrySet()) {
JSFUtils.outputCookie(e.getKey(), e.getValue(), JSFUtils.outputCookie(e.getKey(), e.getValue(),
properties); properties);
@ -86,6 +86,13 @@ public class CookieBean {
public void setCookieValue(String name, String value) { public void setCookieValue(String name, String value) {
cookieMap.put(name, 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) { 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) { 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) { 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) * IdleMonitor backing methods (session/View timeout)
* Todo: move to a more general location. Currently
* only used by view editor, not Main!
*/ */
public void sessionIdleListener() { public void sessionIdleListener() {
log.info("Session Idle Listener fired."); log.info("Session Idle Listener fired.");

View File

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

View File

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

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html >
<ui:composition template="/WEB-INF/layout/layout.xhtml" <ui:composition template="/WEB-INF/layout/layout.xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:f="http://xmlns.jcp.org/jsf/core"
@ -10,10 +11,12 @@
<h:messages /> <h:messages />
<h:form id="form1"> <h:form id="form1">
<div> <div>
<p:focus />
<p:autoComplete id="searchFor" size="45" <p:autoComplete id="searchFor" size="45"
placeholder="Recipe title/cuisine/category, etc.)" placeholder="Recipe title/cuisine/category, etc.)"
value="#{adminMainBean.searchText}" value="#{adminMainBean.searchText}"
completeMethod="#{adminMainBean.searchSuggestionList}" completeMethod="#{adminMainBean.searchSuggestionList}"
onfocus="jQuery('#form1\\:searchFor_input').select();"
> >
<f:ajax event="change" execute="@this" <f:ajax event="change" execute="@this"
render="form2:table1" render="form2:table1"
@ -23,6 +26,7 @@
<p:outputLabel for="@next" value=" In " /> <p:outputLabel for="@next" value=" In " />
<p:selectOneMenu id="ctlSearchType" <p:selectOneMenu id="ctlSearchType"
value="#{cookieBean.searchType}" value="#{cookieBean.searchType}"
onchange="jQuery('#form1\\:searchFor_input').trigger('focus');"
> >
<f:selectItems <f:selectItems
value="#{appBean.searchTypeList}" value="#{appBean.searchTypeList}"