|
|
|
@ -28,19 +28,33 @@ public class Util {
@@ -28,19 +28,33 @@ public class Util {
|
|
|
|
|
private static final MessageDigest SHA256 = getSha256(); |
|
|
|
|
private static final String EMAIL_PATTERN = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"; |
|
|
|
|
|
|
|
|
|
public static String urlEncode(Map<String, Object> data) { |
|
|
|
|
String params = data.entrySet() |
|
|
|
|
.stream() |
|
|
|
|
.map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue())) |
|
|
|
|
.collect(Collectors.joining("&")); |
|
|
|
|
return params; |
|
|
|
|
public static String dropEmail(String tx) { |
|
|
|
|
return tx.replaceAll( "[.\\-\\w]+@[.\\-\\w]+", "[email_removed]"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static String encode(Object value) { |
|
|
|
|
return URLEncoder.encode(value.toString(), StandardCharsets.UTF_8); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean getCheckbox(HttpServletRequest req, String key) { |
|
|
|
|
return "on".equals(req.getParameter(key)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static MailingList getMailingList(HttpServletRequest req) { |
|
|
|
|
var listEmail = req.getParameter(LIST); |
|
|
|
|
if (listEmail == null || listEmail.isBlank()) return null; |
|
|
|
|
return MailingList.load(listEmail); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static <T> T getNullable(ResultSet rs, String colName) throws SQLException { |
|
|
|
|
final T val = (T) rs.getObject(colName); |
|
|
|
|
return rs.wasNull() ? null : val; |
|
|
|
|
} |
|
|
|
|
public static String getPath(HttpServletRequest req) { |
|
|
|
|
var path = req.getPathInfo(); |
|
|
|
|
return path == null ? INDEX : path.substring(1); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
public static MessageDigest getSha256() { |
|
|
|
|
try { |
|
|
|
|
return MessageDigest.getInstance("SHA-256"); |
|
|
|
@ -50,9 +64,43 @@ public class Util {
@@ -50,9 +64,43 @@ public class Util {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String sha256(String s) { |
|
|
|
|
byte[] bytes = SHA256.digest(s.getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
return hex(bytes); |
|
|
|
|
/** |
|
|
|
|
* Return the primary text content of the message. |
|
|
|
|
*/ |
|
|
|
|
public static String getText(Part p) throws MessagingException, IOException { |
|
|
|
|
// https://javaee.github.io/javamail/FAQ
|
|
|
|
|
if (p.isMimeType("text/*")) return (String)p.getContent(); |
|
|
|
|
|
|
|
|
|
if (p.isMimeType("multipart/alternative")) { |
|
|
|
|
// prefer html text over plain text
|
|
|
|
|
Multipart mp = (Multipart)p.getContent(); |
|
|
|
|
String text = null; |
|
|
|
|
for (int i = 0; i < mp.getCount(); i++) { |
|
|
|
|
Part bp = mp.getBodyPart(i); |
|
|
|
|
if (bp.isMimeType("text/plain")) { |
|
|
|
|
if (text == null) text = getText(bp); |
|
|
|
|
continue; |
|
|
|
|
} else if (bp.isMimeType("text/html")) { |
|
|
|
|
String s = getText(bp); |
|
|
|
|
if (s != null) return s; |
|
|
|
|
} else { |
|
|
|
|
return getText(bp); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return text; |
|
|
|
|
} else if (p.isMimeType("multipart/*")) { |
|
|
|
|
Multipart mp = (Multipart)p.getContent(); |
|
|
|
|
for (int i = 0; i < mp.getCount(); i++) { |
|
|
|
|
String s = getText(mp.getBodyPart(i)); |
|
|
|
|
if (s != null) return s; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static User getUser(HttpServletRequest req) { |
|
|
|
|
var o = req.getSession().getAttribute(USER); |
|
|
|
|
return o instanceof User ? (User) o : null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static String hex(byte[] bytes) { |
|
|
|
@ -67,14 +115,15 @@ public class Util {
@@ -67,14 +115,15 @@ public class Util {
|
|
|
|
|
return (char)(upper < 10 ? '0'+upper : 'A'+upper-10)+""+(char)(lower < 10 ? '0'+lower : 'A'+lower-10); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String t(String tx, Object ... fills){ |
|
|
|
|
return Translation.get(Application.class,tx,fills); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean isEmail(String email) { |
|
|
|
|
return email.matches(EMAIL_PATTERN); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String sha256(String s) { |
|
|
|
|
byte[] bytes = SHA256.digest(s.getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
return hex(bytes); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean simplePassword(String pass) { |
|
|
|
|
if (pass.length() < 6) return true; |
|
|
|
|
if (pass.length() < 8){ |
|
|
|
@ -98,75 +147,23 @@ public class Util {
@@ -98,75 +147,23 @@ public class Util {
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static int unset(int value, int...flags) { |
|
|
|
|
for (int flag : flags){ |
|
|
|
|
if ((value & flag) > 0) value ^= flag; |
|
|
|
|
} |
|
|
|
|
return value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static User getUser(HttpServletRequest req) { |
|
|
|
|
var o = req.getSession().getAttribute(USER); |
|
|
|
|
return o instanceof User ? (User) o : null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getPath(HttpServletRequest req) { |
|
|
|
|
var path = req.getPathInfo(); |
|
|
|
|
return path == null ? INDEX : path.substring(1); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static MailingList getMailingList(HttpServletRequest req) { |
|
|
|
|
var listEmail = req.getParameter(LIST); |
|
|
|
|
if (listEmail == null || listEmail.isBlank()) return null; |
|
|
|
|
return MailingList.load(listEmail); |
|
|
|
|
public static String t(String tx, Object ... fills){ |
|
|
|
|
return Translation.get(Application.class,tx,fills); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean getCheckbox(HttpServletRequest req, String key) { |
|
|
|
|
return "on".equals(req.getParameter(key)); |
|
|
|
|
public static String urlEncode(Map<String, Object> data) { |
|
|
|
|
String params = data.entrySet() |
|
|
|
|
.stream() |
|
|
|
|
.map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue())) |
|
|
|
|
.collect(Collectors.joining("&")); |
|
|
|
|
return params; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Return the primary text content of the message. |
|
|
|
|
*/ |
|
|
|
|
public static String getText(Part p) throws MessagingException, IOException { |
|
|
|
|
// https://javaee.github.io/javamail/FAQ
|
|
|
|
|
if (p.isMimeType("text/*")) return (String)p.getContent(); |
|
|
|
|
|
|
|
|
|
if (p.isMimeType("multipart/alternative")) { |
|
|
|
|
// prefer html text over plain text
|
|
|
|
|
Multipart mp = (Multipart)p.getContent(); |
|
|
|
|
String text = null; |
|
|
|
|
for (int i = 0; i < mp.getCount(); i++) { |
|
|
|
|
Part bp = mp.getBodyPart(i); |
|
|
|
|
if (bp.isMimeType("text/plain")) { |
|
|
|
|
if (text == null) text = getText(bp); |
|
|
|
|
continue; |
|
|
|
|
} else if (bp.isMimeType("text/html")) { |
|
|
|
|
String s = getText(bp); |
|
|
|
|
if (s != null) return s; |
|
|
|
|
} else { |
|
|
|
|
return getText(bp); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return text; |
|
|
|
|
} else if (p.isMimeType("multipart/*")) { |
|
|
|
|
Multipart mp = (Multipart)p.getContent(); |
|
|
|
|
for (int i = 0; i < mp.getCount(); i++) { |
|
|
|
|
String s = getText(mp.getBodyPart(i)); |
|
|
|
|
if (s != null) return s; |
|
|
|
|
} |
|
|
|
|
public static int unset(int value, int...flags) { |
|
|
|
|
for (int flag : flags){ |
|
|
|
|
if ((value & flag) > 0) value ^= flag; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String dropEmail(String tx) { |
|
|
|
|
return tx.replaceAll( "[.\\-\\w]+@[.\\-\\w]+", "[email_removed]"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static <T> T getNullable(ResultSet rs, String colName) throws SQLException { |
|
|
|
|
final T val = (T) rs.getObject(colName); |
|
|
|
|
return rs.wasNull() ? null : val; |
|
|
|
|
return value; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|