initial commit
This commit is contained in:
33
src/main/java/zw/qantra/tm/utils/ContextUtils.java
Normal file
33
src/main/java/zw/qantra/tm/utils/ContextUtils.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package zw.qantra.tm.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.util.StdDateFormat;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ContextUtils {
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public String getPrettyObject(Object object) {
|
||||
try {
|
||||
return this.mapper
|
||||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||
.setDateFormat(new StdDateFormat().withColonInTimeZone(true))
|
||||
.registerModule(new JavaTimeModule())
|
||||
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
|
||||
.writerWithDefaultPrettyPrinter()
|
||||
.writeValueAsString(object);
|
||||
} catch (JsonProcessingException e) {
|
||||
System.out.print(e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
75
src/main/java/zw/qantra/tm/utils/Utils.java
Normal file
75
src/main/java/zw/qantra/tm/utils/Utils.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package zw.qantra.tm.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.util.StdDateFormat;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Utils {
|
||||
public static String hashPassword(String password) {
|
||||
try {
|
||||
// Create a MessageDigest instance for SHA-256
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
// Perform the hashing
|
||||
byte[] hashedBytes = digest.digest(password.getBytes());
|
||||
|
||||
// Convert the hashed bytes to a hexadecimal string
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : hashedBytes) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) {
|
||||
hexString.append('0');
|
||||
}
|
||||
hexString.append(hex);
|
||||
}
|
||||
|
||||
return hexString.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("Error hashing password", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getUid(){
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
public static <T> T getJson(String json, Class<T> clazz){
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
|
||||
mapper.registerModule(new JavaTimeModule());
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
|
||||
Object obj = null;
|
||||
try {
|
||||
obj = mapper.readValue(json, clazz);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return (T) obj;
|
||||
}
|
||||
|
||||
public static String setJson(Object obj){
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
|
||||
mapper.registerModule(new JavaTimeModule());
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
|
||||
String json = "";
|
||||
try {
|
||||
json = mapper.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user