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.
 
 
 
 

172 lines
4.4 KiB

package com.mousetech.gourmetj;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.Flash;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JSFUtils {
/* Logger */
private static final Logger log =
LoggerFactory.getLogger(JSFUtils.class);
private static ExternalContext getExternalContext() {
FacesContext facesContext =
FacesContext.getCurrentInstance();
ExternalContext externalContext =
facesContext.getExternalContext();
return externalContext;
}
/**
* Obtain resource as Stream. The caller must close this
* stream after use.
*
* @param pathName Path of the resource. For example,
* "/WEB-INF/classes/xys.properties".
* @return InputStream or <code>null</code>, if no such
* resource exists.
*/
public static InputStream getResourceStream(
String pathName) {
InputStream response = getExternalContext()
.getResourceAsStream(pathName);
return response;
}
// ===
/**
* Post an info-level message to the FacesContext where the
* &lt;h:messages&gt; tag can display it.
*
* @param string Message text
*/
public static void addInfoMessage(String string) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_INFO, string, string);
FacesContext.getCurrentInstance().addMessage(null,
message);
}
public static void addWarningMessage(String msgString) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_WARN, "WARNING", msgString);
FacesContext.getCurrentInstance().addMessage(null,
message);
}
/**
* Post an error-level message to the FacesContext where the
* &lt;h:messages&gt; tag can display it.
*
* @param string Message text
*/
public static void addErrorMessage(String string) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR, string, string);
FacesContext.getCurrentInstance().addMessage(null,
message);
}
public static void addErrorMessage(String string,
Exception e) {
if (e instanceof NullPointerException) {
addErrorMessage(
"Internal logic error (NullPointerException)");
} else {
addErrorMessage(string);
}
log.error(string, e);
}
public static Flash flashScope() {
return (FacesContext.getCurrentInstance()
.getExternalContext().getFlash());
}
/**
* Look in Flash scope for item.
*
* @param key Item to retrieve
* @return Item, or null if not in Flash OR Flash not active
*/
public static Object getFlash(String key) {
Flash flash = flashScope();
try {
return flash.get(key);
} catch ( NullPointerException nex ) {
// If session is expired, flash.get() will fail!
return null;
}
}
public static void putFlash(String key, Object value) {
flashScope().put(key, value);
}
//***********
//* COOKIE!!!
//***********
/**
* 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()));
return m1;
}
/**
* 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
*/
public static void outputCookie(String name,
String value, Map<String, Object> properties) throws UnsupportedEncodingException {
getExternalContext().addResponseCookie(name,
URLEncoder.encode(value, "UTF-8"),
properties);
}
/**
* Destroy current session, logging user out.
*/
public static void logout() {
log.warn("Logging out session");
jakarta.servlet.http.HttpSession session =
(HttpSession) getExternalContext().getSession(false);
if ( session != null ) {
session.invalidate();
} else {
log.warn("Session did not exist.");
}
}
public static HttpSession getSession(boolean create) {
return (HttpSession) getExternalContext().getSession(create);
}
}