working on string-templated based static pages
This commit is contained in:
65
src/main/java/de/srsoftware/widerhall/Util.java
Normal file
65
src/main/java/de/srsoftware/widerhall/Util.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package de.srsoftware.widerhall;
|
||||
|
||||
import de.srsoftware.examples.translations.App;
|
||||
import de.srsoftware.tools.translations.Translation;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private static String encode(Object value) {
|
||||
return URLEncoder.encode(value.toString(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
|
||||
public static MessageDigest getSha256() {
|
||||
try {
|
||||
return MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String sha256(String s) {
|
||||
byte[] bytes = SHA256.digest(s.getBytes(StandardCharsets.UTF_8));
|
||||
return hex(bytes);
|
||||
}
|
||||
|
||||
private static String hex(byte[] bytes) {
|
||||
StringBuffer buf = new StringBuffer(bytes.length*2);
|
||||
for (var byt : bytes) buf.append(hex(byt));
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private static String hex(byte b){
|
||||
return (b<16 ? "0" : "") + Integer.toHexString(b);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user