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.
187 lines
6.2 KiB
187 lines
6.2 KiB
package de.srsoftware.widerhall; |
|
|
|
import de.srsoftware.tools.translations.Translation; |
|
import de.srsoftware.widerhall.data.MailingList; |
|
import de.srsoftware.widerhall.data.User; |
|
|
|
import javax.mail.MessagingException; |
|
import javax.mail.Multipart; |
|
import javax.mail.Part; |
|
import javax.servlet.http.HttpServletRequest; |
|
import java.io.IOException; |
|
import java.net.URLEncoder; |
|
import java.nio.charset.StandardCharsets; |
|
import java.security.MessageDigest; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.sql.ResultSet; |
|
import java.sql.SQLException; |
|
import java.util.Map; |
|
import java.util.Random; |
|
import java.util.stream.Collectors; |
|
|
|
import static de.srsoftware.widerhall.Constants.*; |
|
|
|
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 char boundedChar(int i) { |
|
i = (i<0 ? -i : i) % 62; |
|
i += '0'; |
|
if (i>57) i+=7; |
|
if (i>90) i+=6; |
|
return (char) i; |
|
} |
|
|
|
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"); |
|
} catch (NoSuchAlgorithmException e) { |
|
e.printStackTrace(); |
|
return null; |
|
} |
|
} |
|
|
|
/** |
|
* 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) { |
|
StringBuffer buf = new StringBuffer(bytes.length*2); |
|
for (var byt : bytes) buf.append(hex(byt)); |
|
return buf.toString(); |
|
} |
|
|
|
public static String hex(int b){ |
|
int lower = b & 0x0F; |
|
int upper = (b & 0xF0) >> 4; |
|
return (char)(upper < 10 ? '0'+upper : 'A'+upper-10)+""+(char)(lower < 10 ? '0'+lower : 'A'+lower-10); |
|
} |
|
|
|
public static boolean isEmail(String email) { |
|
return email.matches(EMAIL_PATTERN); |
|
} |
|
|
|
public static String randomString(int length) { |
|
Random rand = new Random(); |
|
StringBuilder sb = new StringBuilder(); |
|
for (int i=0; i<length; i++) { |
|
int k = rand.nextInt(); |
|
char c = boundedChar(k); |
|
System.out.println("adding '"+c+"'…"); |
|
sb.append(c); |
|
} |
|
return sb.toString(); |
|
} |
|
|
|
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){ |
|
for (int i=0; i<pass.length();i++){ |
|
if (!Character.isLetterOrDigit(pass.charAt(i))) return false; // password contains special character |
|
} |
|
return true; |
|
} |
|
if (pass.length() < 10){ |
|
var digit = false; |
|
var letter = false; |
|
for (int i=0; i<pass.length();i++){ |
|
char c = pass.charAt(i); |
|
if (!Character.isLetterOrDigit(c)) return false; // password contains special character |
|
digit |= Character.isDigit(c); |
|
letter |= Character.isLetter(c); |
|
if (letter && digit) return false; // password contains letters and digits |
|
} |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
public static String t(String tx, Object ... fills){ |
|
return Translation.get(Application.class,tx,fills); |
|
} |
|
|
|
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 int unset(int value, int...flags) { |
|
for (int flag : flags){ |
|
if ((value & flag) > 0) value ^= flag; |
|
} |
|
return value; |
|
} |
|
}
|
|
|