@@ -64,5 +64,8 @@ jobs:
|
|||||||
# Overwrite artefactVersion with tag name
|
# Overwrite artefactVersion with tag name
|
||||||
run: ./gradlew build -PartefactVersion=${{ github.event.release.tag_name }}
|
run: ./gradlew build -PartefactVersion=${{ github.event.release.tag_name }}
|
||||||
|
|
||||||
|
- name: List Output Items
|
||||||
|
run: ls -l ./**/build/libs
|
||||||
|
|
||||||
- name: Publish to Maven Central
|
- name: Publish to Maven Central
|
||||||
run: ./gradlew publish
|
run: ./gradlew publish
|
||||||
|
|||||||
@@ -187,7 +187,7 @@
|
|||||||
same "printed page" as the copyright notice for easier
|
same "printed page" as the copyright notice for easier
|
||||||
identification within third-party archives.
|
identification within third-party archives.
|
||||||
|
|
||||||
Copyright 2023-2024 OnixByte (HK)
|
Copyright 2024-2025 OnixByte
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-5
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -43,16 +43,12 @@ subprojects {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly("org.slf4j:slf4j-api:$slf4jVersion")
|
compileOnly("org.slf4j:slf4j-api:$slf4jVersion")
|
||||||
compileOnly("org.projectlombok:lombok:$lombokVersion")
|
|
||||||
implementation("ch.qos.logback:logback-classic:$logbackVersion")
|
implementation("ch.qos.logback:logback-classic:$logbackVersion")
|
||||||
annotationProcessor("org.slf4j:slf4j-api:$slf4jVersion")
|
annotationProcessor("org.slf4j:slf4j-api:$slf4jVersion")
|
||||||
annotationProcessor("org.projectlombok:lombok:$lombokVersion")
|
|
||||||
|
|
||||||
testCompileOnly("org.slf4j:slf4j-api:$slf4jVersion")
|
testCompileOnly("org.slf4j:slf4j-api:$slf4jVersion")
|
||||||
testCompileOnly("org.projectlombok:lombok:$lombokVersion")
|
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion")
|
testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion")
|
||||||
testAnnotationProcessor("org.slf4j:slf4j-api:$slf4jVersion")
|
testAnnotationProcessor("org.slf4j:slf4j-api:$slf4jVersion")
|
||||||
testAnnotationProcessor("org.projectlombok:lombok:$lombokVersion")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType<JavaCompile> {
|
tasks.withType<JavaCompile> {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+11
-19
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,12 +18,13 @@
|
|||||||
package com.onixbyte.devkit.core.exceptions;
|
package com.onixbyte.devkit.core.exceptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@code NotImplementedException} class is a custom runtime exception that represents a situation where a
|
* The {@code NotImplementedException} class is a custom runtime exception that represents a
|
||||||
* particular method or functionality is not implemented or is currently unavailable in the codebase.
|
* situation where a particular method or functionality is not implemented or is currently
|
||||||
|
* unavailable in the codebase.
|
||||||
* <p>
|
* <p>
|
||||||
* This exception is typically thrown when developers need to indicate that a specific part of the code is incomplete
|
* This exception is typically thrown when developers need to indicate that a specific part of the
|
||||||
* or requires further implementation. It serves as a placeholder to highlight unfinished sections of the application
|
* code is incomplete or requires further implementation. It serves as a placeholder to highlight
|
||||||
* during development and testing phases.
|
* unfinished sections of the application during development and testing phases.
|
||||||
* <p>
|
* <p>
|
||||||
* Usage Example:
|
* Usage Example:
|
||||||
* <pre>
|
* <pre>
|
||||||
@@ -33,16 +34,6 @@ package com.onixbyte.devkit.core.exceptions;
|
|||||||
* This feature will be implemented in a future release.""");
|
* This feature will be implemented in a future release.""");
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
* <b>Contact</b>
|
|
||||||
* <ul>
|
|
||||||
* <li>
|
|
||||||
* <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new"
|
|
||||||
* >GitHub Issues</a>
|
|
||||||
* </li>
|
|
||||||
* <li>
|
|
||||||
* <a href="https://discord.gg/">Discord Community</a>
|
|
||||||
* </li>
|
|
||||||
* </ul>
|
|
||||||
*
|
*
|
||||||
* @author Zihlu Wang
|
* @author Zihlu Wang
|
||||||
* @version 1.1.0
|
* @version 1.1.0
|
||||||
@@ -67,7 +58,8 @@ public class NotImplementedException extends RuntimeException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@code NotImplementedException} with the specified error message and a cause for this exception.
|
* Creates a new {@code NotImplementedException} with the specified error message and a cause
|
||||||
|
* for this exception.
|
||||||
*
|
*
|
||||||
* @param message the error message associated with this exception
|
* @param message the error message associated with this exception
|
||||||
* @param cause the cause of this exception
|
* @param cause the cause of this exception
|
||||||
@@ -86,8 +78,8 @@ public class NotImplementedException extends RuntimeException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@code NotImplementedException} with the specified error message, cause, suppression flag, and
|
* Creates a new {@code NotImplementedException} with the specified error message, cause,
|
||||||
* stack trace writable flag.
|
* suppression flag, and stack trace writable flag.
|
||||||
*
|
*
|
||||||
* @param message the error message associated with this
|
* @param message the error message associated with this
|
||||||
* exception
|
* exception
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package contains commonly used exceptions will be used in JDevKit.
|
|
||||||
*
|
|
||||||
* @author Zihlu Wang
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.devkit.core.exceptions;
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package is the core part of JDevKit, an open-source Java Development
|
|
||||||
* Kit that provides a set of convenient tools to streamline code development
|
|
||||||
* and enhance productivity. This package serves as the core package containing
|
|
||||||
* common exceptions that are used throughout the entire JDevKit project.
|
|
||||||
* <p>
|
|
||||||
* JDevKit is designed to be modular, and other specific feature modules within
|
|
||||||
* the library may rely on these exceptions from the core package.
|
|
||||||
* <p>
|
|
||||||
* For more information and the latest version of JDevKit, please visit our
|
|
||||||
* website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>.
|
|
||||||
* <p>
|
|
||||||
* <b>Contact</b>
|
|
||||||
* <ul>
|
|
||||||
* <li>
|
|
||||||
* <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new"
|
|
||||||
* >GitHub Issues</a>
|
|
||||||
* </li>
|
|
||||||
* <li>
|
|
||||||
* <a href="https://discord.gg/">Discord Community</a>
|
|
||||||
* </li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.devkit.core;
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package is the main part of JDevKit, an open-source Java class library
|
|
||||||
* that provides a set of convenient tools to streamline code development and
|
|
||||||
* enhance productivity. This package serves as the root package for several
|
|
||||||
* modules, containing {@code devkit-core}, {@code guid} and {@code dev-utils}
|
|
||||||
* module.
|
|
||||||
* <p>
|
|
||||||
* For more information and the latest version of JDevKit, please visit our
|
|
||||||
* website <a href="https://codecrafters.org.cn">codecrafters.org.cn</a>.
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* <b>Contact</b>
|
|
||||||
* <ul>
|
|
||||||
* <li>
|
|
||||||
* <a href="https://github.com/CodeCraftersCN/jdevkit/issues/new"
|
|
||||||
* >GitHub Issues</a>
|
|
||||||
* </li>
|
|
||||||
* <li>
|
|
||||||
* <a href="https://discord.gg/">Discord Community</a>
|
|
||||||
* </li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.devkit;
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!--
|
<!--
|
||||||
~ Copyright (C) 2023-2024 OnixByte.
|
~ Copyright (C) 2024-2025 OnixByte.
|
||||||
~
|
~
|
||||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
~ you may not use this file except in compliance with the License.
|
~ you may not use this file except in compliance with the License.
|
||||||
@@ -17,8 +17,10 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<property name="COLOURFUL_OUTPUT" value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
<property name="COLOURFUL_OUTPUT"
|
||||||
<property name="STANDARD_OUTPUT" value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
||||||
|
<property name="STANDARD_OUTPUT"
|
||||||
|
value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
||||||
|
|
||||||
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,7 +17,8 @@
|
|||||||
|
|
||||||
package com.onixbyte.devkit.utils;
|
package com.onixbyte.devkit.utils;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.crypto.BadPaddingException;
|
import javax.crypto.BadPaddingException;
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
@@ -34,15 +35,44 @@ import java.util.Objects;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link AesUtil} can help you encrypt and decrypt data with specified secret by AES algorithm.
|
* The {@link AesUtil} class provides utility methods for encrypting and decrypting data using the
|
||||||
|
* AES algorithm. This class supports both byte array and string data, and uses a specified secret
|
||||||
|
* key for encryption and decryption.
|
||||||
|
* <p>
|
||||||
|
* The utility methods in this class are useful for scenarios where data needs to be securely
|
||||||
|
* encrypted and decrypted.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>
|
||||||
|
* {@code
|
||||||
|
* // Encrypting and decrypting byte array data
|
||||||
|
* byte[] secretKey = "43f72073956d4c81".getBytes(StandardCharsets.UTF_8);
|
||||||
|
* byte[] data = "Hello World".getBytes(StandardCharsets.UTF_8);
|
||||||
|
* byte[] encryptedData = AesUtil.encrypt(data, secretKey);
|
||||||
|
* byte[] decryptedData = AesUtil.decrypt(encryptedData, secretKey);
|
||||||
|
* System.out.println(new String(decryptedData, StandardCharsets.UTF_8)); // Output: Hello World
|
||||||
|
*
|
||||||
|
* // Encrypting and decrypting string data
|
||||||
|
* String secret = "43f72073956d4c81";
|
||||||
|
* String encryptedString = AesUtil.encrypt("Hello World", secret);
|
||||||
|
* String decryptedString = AesUtil.decrypt(encryptedString, secret);
|
||||||
|
* System.out.println(decryptedString); // Output: Hello World
|
||||||
|
*
|
||||||
|
* // Generating a random secret key
|
||||||
|
* String randomSecret = AesUtil.generateRandomSecret();
|
||||||
|
* System.out.println(randomSecret); // Output: A ramdomly generated 16-character long secret
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author hubin@baomidou
|
* @author hubin@baomidou
|
||||||
* @version 1.1.0
|
* @version 1.1.0
|
||||||
* @since 1.1.0
|
* @since 1.1.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
public final class AesUtil {
|
public final class AesUtil {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(AesUtil.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypts the data using the AES algorithm with the given secret.
|
* Encrypts the data using the AES algorithm with the given secret.
|
||||||
*
|
*
|
||||||
@@ -54,7 +84,7 @@ public final class AesUtil {
|
|||||||
try {
|
try {
|
||||||
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
|
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
|
||||||
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
|
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
|
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); // set IV to secret
|
||||||
return cipher.doFinal(data);
|
return cipher.doFinal(data);
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedOperationException |
|
||||||
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
|
InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException |
|
||||||
@@ -78,7 +108,7 @@ public final class AesUtil {
|
|||||||
try {
|
try {
|
||||||
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
|
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
|
||||||
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
|
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
|
||||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret));
|
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(secret)); // set IV to secret
|
||||||
return cipher.doFinal(data);
|
return cipher.doFinal(data);
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
||||||
UnsupportedOperationException | InvalidKeyException |
|
UnsupportedOperationException | InvalidKeyException |
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,6 +17,9 @@
|
|||||||
|
|
||||||
package com.onixbyte.devkit.utils;
|
package com.onixbyte.devkit.utils;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
@@ -55,6 +58,8 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
public final class Base64Util {
|
public final class Base64Util {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(Base64Util.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure that there is only one Base64 Encoder.
|
* Ensure that there is only one Base64 Encoder.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,18 +17,38 @@
|
|||||||
|
|
||||||
package com.onixbyte.devkit.utils;
|
package com.onixbyte.devkit.utils;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.BooleanSupplier;
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A util for boolean calculations.
|
* The {@link BoolUtil} class provides utility methods for boolean calculations.
|
||||||
|
* This class offers methods to perform logical operations such as AND, OR, and NOT on boolean values.
|
||||||
|
* <p>
|
||||||
|
* The utility methods in this class are useful for scenarios where multiple boolean values need to be
|
||||||
|
* evaluated together, and for simplifying complex boolean expressions.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @author shaoxinke
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>
|
||||||
|
* {@code
|
||||||
|
* boolean result1 = BoolUtil.and(true, true, false); // false
|
||||||
|
* boolean result2 = BoolUtil.or(true, false, false); // true
|
||||||
|
* boolean result3 = BoolUtil.not(false); // true
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @author zihluwang
|
||||||
* @version 1.6.2
|
* @version 1.6.2
|
||||||
|
* @since 1.6.2
|
||||||
*/
|
*/
|
||||||
public final class BoolUtil {
|
public final class BoolUtil {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(BoolUtil.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logical and calculation.
|
* Logical and calculation.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,6 +17,9 @@
|
|||||||
|
|
||||||
package com.onixbyte.devkit.utils;
|
package com.onixbyte.devkit.utils;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.BooleanSupplier;
|
import java.util.function.BooleanSupplier;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@@ -32,8 +35,7 @@ import java.util.function.Supplier;
|
|||||||
* <p>
|
* <p>
|
||||||
* <b>Example:</b>
|
* <b>Example:</b>
|
||||||
* <pre>
|
* <pre>
|
||||||
* // If you want to simplify an if (exp1 || exp2), you can use the
|
* // If you want to simplify an if (exp1 || exp2), you can use the following code:
|
||||||
* // following code:
|
|
||||||
* String r1 = BranchUtil.or(1 == 1, 2 == 1)
|
* String r1 = BranchUtil.or(1 == 1, 2 == 1)
|
||||||
* .handle(() -> "1 is equal to 1 or 2 is equal to 1.");
|
* .handle(() -> "1 is equal to 1 or 2 is equal to 1.");
|
||||||
*
|
*
|
||||||
@@ -49,8 +51,7 @@ import java.util.function.Supplier;
|
|||||||
* }, () -> {
|
* }, () -> {
|
||||||
* // do something
|
* // do something
|
||||||
* });
|
* });
|
||||||
* // If you only need an if branch, you can remove the second Supplier
|
* // If you only need an if branch, you can remove the second Supplier instance.
|
||||||
* // instance.
|
|
||||||
*
|
*
|
||||||
* // To check if all boolean expressions are true, use the 'and' method:
|
* // To check if all boolean expressions are true, use the 'and' method:
|
||||||
* BranchUtil.and(1 == 1, 2 == 1)
|
* BranchUtil.and(1 == 1, 2 == 1)
|
||||||
@@ -75,6 +76,8 @@ import java.util.function.Supplier;
|
|||||||
*/
|
*/
|
||||||
public final class BranchUtil<T> {
|
public final class BranchUtil<T> {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(BranchUtil.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a {@code BranchUtil} instance.
|
* Create a {@code BranchUtil} instance.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,6 +17,9 @@
|
|||||||
|
|
||||||
package com.onixbyte.devkit.utils;
|
package com.onixbyte.devkit.utils;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
@@ -55,8 +58,7 @@ import java.util.Optional;
|
|||||||
* The above examples demonstrate how to use the {@code HashUtil} class to calculate hash values
|
* The above examples demonstrate how to use the {@code HashUtil} class to calculate hash values
|
||||||
* for a given string using different algorithms.
|
* for a given string using different algorithms.
|
||||||
* <p>
|
* <p>
|
||||||
* <b>Note:</b>
|
* The hash functions provided by the {@link HashUtil} are one-way hash functions, meaning the
|
||||||
* The hash functions provided by the HashUtil class are one-way hash functions, meaning the
|
|
||||||
* original data cannot be retrieved from the hash value. These hash functions are commonly used
|
* original data cannot be retrieved from the hash value. These hash functions are commonly used
|
||||||
* for data integrity checks and password storage, but they should not be used for
|
* for data integrity checks and password storage, but they should not be used for
|
||||||
* encryption purposes.
|
* encryption purposes.
|
||||||
@@ -68,6 +70,8 @@ import java.util.Optional;
|
|||||||
*/
|
*/
|
||||||
public final class HashUtil {
|
public final class HashUtil {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(HashUtil.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the MD2 hash value of the specified string using the given charset.
|
* Calculates the MD2 hash value of the specified string using the given charset.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,26 +17,76 @@
|
|||||||
|
|
||||||
package com.onixbyte.devkit.utils;
|
package com.onixbyte.devkit.utils;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code MapUtil} is a utility class that provides methods for converting objects to maps and maps
|
* The {@link MapUtil} class provides utility methods for converting between objects and maps.
|
||||||
* to objects.
|
* This class leverages the {@link ObjectMapAdapter} interface to perform the conversions.
|
||||||
* <p>
|
* <p>
|
||||||
* Note: Since version 1.4.2, this util class removed reflection API and transferred to a safer API.
|
* The utility methods in this class are useful for scenarios where objects need to be represented
|
||||||
* Please see documentation for more information.
|
* as maps for serialization, deserialization, or other purposes.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>
|
||||||
|
* {@code
|
||||||
|
* public class User {
|
||||||
|
* private String name;
|
||||||
|
* private int age;
|
||||||
|
*
|
||||||
|
* // getters and setters
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public class UserMapAdapter implements ObjectMapAdapter<User> {
|
||||||
|
* @Override
|
||||||
|
* public Map<String, Object> toMap(User user) {
|
||||||
|
* Map<String, Object> map = new HashMap<>();
|
||||||
|
* map.put("name", user.getName());
|
||||||
|
* map.put("age", user.getAge());
|
||||||
|
* return map;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* @Override
|
||||||
|
* public User fromMap(Map<String, Object> map) {
|
||||||
|
* User user = new User();
|
||||||
|
* user.setName((String) map.get("name"));
|
||||||
|
* user.setAge((Integer) map.get("age"));
|
||||||
|
* return user;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public class Example {
|
||||||
|
* public static void main(String[] args) {
|
||||||
|
* User user = new User();
|
||||||
|
* user.setName("John");
|
||||||
|
* user.setAge(30);
|
||||||
|
*
|
||||||
|
* UserMapAdapter adapter = new UserMapAdapter();
|
||||||
|
*
|
||||||
|
* // Convert object to map
|
||||||
|
* Map<String, Object> userMap = MapUtil.objectToMap(user, adapter);
|
||||||
|
* System.out.println(userMap); // Output: {name=John, age=30}
|
||||||
|
*
|
||||||
|
* // Convert map to object
|
||||||
|
* User newUser = MapUtil.mapToObject(userMap, adapter);
|
||||||
|
* System.out.println(newUser.getName()); // Output: John
|
||||||
|
* System.out.println(newUser.getAge()); // Output: 30
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
* @version 1.7.0
|
* @version 1.7.0
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
public final class MapUtil {
|
public final class MapUtil {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(MapUtil.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an object to a map by mapping the field names to their corresponding values.
|
* Converts an object to a map by mapping the field names to their corresponding values.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -20,9 +20,44 @@ package com.onixbyte.devkit.utils;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapts an Object to a Map, making conversion between Map and Object much more safe.
|
* The {@link ObjectMapAdapter} interface provides methods to convert between objects and maps.
|
||||||
|
* This interface is useful for scenarios where objects need to be represented as maps for
|
||||||
|
* serialization, deserialization, or other purposes.
|
||||||
*
|
*
|
||||||
* @param <T> field type
|
* <p>Implementations of this interface should provide the logic to convert an object of type
|
||||||
|
* {@code T} to a {@link Map} and vice versa.</p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>
|
||||||
|
* {@code
|
||||||
|
* public class User {
|
||||||
|
* private String name;
|
||||||
|
* private int age;
|
||||||
|
*
|
||||||
|
* // getters and setters
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public class UserMapAdapter implements ObjectMapAdapter<User> {
|
||||||
|
* @Override
|
||||||
|
* public Map<String, Object> toMap(User user) {
|
||||||
|
* Map<String, Object> map = new HashMap<>();
|
||||||
|
* map.put("name", user.getName());
|
||||||
|
* map.put("age", user.getAge());
|
||||||
|
* return map;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* @Override
|
||||||
|
* public User fromMap(Map<String, Object> map) {
|
||||||
|
* User user = new User();
|
||||||
|
* user.setName((String) map.get("name"));
|
||||||
|
* user.setAge((Integer) map.get("age"));
|
||||||
|
* return user;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param <T> the type of the object to be converted
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
* @version 1.7.0
|
* @version 1.7.0
|
||||||
* @since 1.4.2
|
* @since 1.4.2
|
||||||
@@ -40,7 +75,7 @@ public interface ObjectMapAdapter<T> {
|
|||||||
/**
|
/**
|
||||||
* Convert a Map to an object.
|
* Convert a Map to an object.
|
||||||
*
|
*
|
||||||
* @param map the map that will be converted to Object
|
* @param map the map that will be converted to an object
|
||||||
* @return the object that is converted from the Map
|
* @return the object that is converted from the Map
|
||||||
*/
|
*/
|
||||||
T toObject(Map<String, Object> map);
|
T toObject(Map<String, Object> map);
|
||||||
|
|||||||
@@ -0,0 +1,176 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.devkit.utils;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code RangeUtil} is a utility class providing methods for generating streams of integers that
|
||||||
|
* emulate the behaviour of Python's {@code range} function.
|
||||||
|
* <p>
|
||||||
|
* This class offers static methods to create ranges with various configurations. These methods
|
||||||
|
* leverage the {@link IntStream} to provide efficient and versatile integer sequences.
|
||||||
|
*
|
||||||
|
* @author zihluwang
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public final class RangeUtil {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(RangeUtil.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor prevent class being instantiated.
|
||||||
|
*/
|
||||||
|
private RangeUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a stream of integers starting from {@code 0} up to the specified {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
|
||||||
|
* further processing.
|
||||||
|
* <p>
|
||||||
|
* <b>Example Usage:</b>
|
||||||
|
* <pre>{@code
|
||||||
|
* RangeUtil.range(5).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 0
|
||||||
|
* // 1
|
||||||
|
* // 2
|
||||||
|
* // 3
|
||||||
|
* // 4
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param end upper-bound of the range (exclusive)
|
||||||
|
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
|
||||||
|
* {@code end} (exclusive)
|
||||||
|
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public static IntStream range(int end) {
|
||||||
|
if (end <= 0) {
|
||||||
|
throw new IllegalArgumentException("Parameter [end] should not less than 0, provided is " +
|
||||||
|
end);
|
||||||
|
}
|
||||||
|
return IntStream.range(0, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a stream of integers starting from the specified {@code start} value up to the
|
||||||
|
* specified {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
|
||||||
|
* further processing.
|
||||||
|
* <p>
|
||||||
|
* <b>Example Usage:</b>
|
||||||
|
* <pre>{@code
|
||||||
|
* RangeUtil.range(3, 8).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 3
|
||||||
|
* // 4
|
||||||
|
* // 5
|
||||||
|
* // 6
|
||||||
|
* // 7
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param start the starting value of the range (inclusive)
|
||||||
|
* @param end upper-bound of the range (exclusive)
|
||||||
|
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
|
||||||
|
* {@code end} (exclusive)
|
||||||
|
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public static IntStream range(int start, int end) {
|
||||||
|
if (end >= start) {
|
||||||
|
throw new IllegalStateException("Parameter [start] should less than parameter [end].");
|
||||||
|
}
|
||||||
|
return IntStream.range(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a stream of integers starting from the specified {@code start} value up to the
|
||||||
|
* specified {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
|
||||||
|
* further processing.
|
||||||
|
* <p>
|
||||||
|
* <b>Example Usage:</b>
|
||||||
|
* <pre>{@code
|
||||||
|
* RangeUtil.rangeClosed(3, 8).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 3
|
||||||
|
* // 4
|
||||||
|
* // 5
|
||||||
|
* // 6
|
||||||
|
* // 7
|
||||||
|
* // 8
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param start the starting value of the range (inclusive)
|
||||||
|
* @param end upper-bound of the range (inclusive)
|
||||||
|
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
|
||||||
|
* {@code end} (inclusive)
|
||||||
|
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public static IntStream rangeClosed(int start, int end) {
|
||||||
|
return IntStream.rangeClosed(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a stream of integers starting from the specified {@code start} value, increment by
|
||||||
|
* the specified {@code step}, up to the specified {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
|
||||||
|
* further processing.
|
||||||
|
* <p>
|
||||||
|
* <b>Example Usage:</b>
|
||||||
|
* <pre>{@code
|
||||||
|
* RangeUtil.range(3, 8, 2).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 3
|
||||||
|
* // 5
|
||||||
|
* // 7
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param start the starting value of the range (inclusive)
|
||||||
|
* @param end upper-bound of the range (exclusive)
|
||||||
|
* @param step the increment (or decrement) between each value
|
||||||
|
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
|
||||||
|
* {@code end} (exclusive)
|
||||||
|
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public static IntStream range(int start, int end, int step) {
|
||||||
|
if (step == 0) {
|
||||||
|
throw new IllegalArgumentException("Step value must not be zero.");
|
||||||
|
}
|
||||||
|
if ((step > 0 && start >= end) || (step < 0 && start <= end)) {
|
||||||
|
throw new IllegalArgumentException("Range parameters are inconsistent with the step value.");
|
||||||
|
}
|
||||||
|
return IntStream.iterate(start, (n) -> n < end, (n) -> n + step);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package is part of JDevKit, an open-source Java Development Kit that
|
|
||||||
* provides a set of convenient tools to streamline code development and
|
|
||||||
* enhance productivity. This package serves as the root package for the module
|
|
||||||
* dev-utils, which contains a collection of common utility classes commonly
|
|
||||||
* used in all Java Application development.
|
|
||||||
*
|
|
||||||
* @author Zihlu Wang
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.devkit.utils;
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!--
|
<!--
|
||||||
~ Copyright (C) 2023-2024 OnixByte.
|
~ Copyright (C) 2024-2025 OnixByte.
|
||||||
~
|
~
|
||||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
~ you may not use this file except in compliance with the License.
|
~ you may not use this file except in compliance with the License.
|
||||||
@@ -17,8 +17,10 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<property name="COLOURFUL_OUTPUT" value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
<property name="COLOURFUL_OUTPUT"
|
||||||
<property name="STANDARD_OUTPUT" value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
||||||
|
<property name="STANDARD_OUTPUT"
|
||||||
|
value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
||||||
|
|
||||||
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+2
-3
@@ -1,5 +1,5 @@
|
|||||||
#
|
#
|
||||||
# Copyright (C) 2024-2024 OnixByte.
|
# Copyright (C) 2024-2025 OnixByte.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
# you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
@@ -19,12 +19,11 @@ jacksonVersion=2.18.0
|
|||||||
javaJwtVersion=4.4.0
|
javaJwtVersion=4.4.0
|
||||||
junitVersion=5.11.2
|
junitVersion=5.11.2
|
||||||
logbackVersion=1.5.10
|
logbackVersion=1.5.10
|
||||||
lombokVersion=1.18.34
|
|
||||||
slf4jVersion=2.0.16
|
slf4jVersion=2.0.16
|
||||||
springVersion=6.1.13
|
springVersion=6.1.13
|
||||||
springBootVersion=3.3.4
|
springBootVersion=3.3.4
|
||||||
|
|
||||||
artefactVersion=1.7.0
|
artefactVersion=1.8.0
|
||||||
projectUrl=https://onixbyte.com/JDevKit
|
projectUrl=https://onixbyte.com/JDevKit
|
||||||
projectGithubUrl=https://github.com/OnixByte/JDevKit
|
projectGithubUrl=https://github.com/OnixByte/JDevKit
|
||||||
licenseName=The Apache License, Version 2.0
|
licenseName=The Apache License, Version 2.0
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
#
|
#
|
||||||
# Copyright (C) 2024-2024 OnixByte.
|
# Copyright (C) 2024-2025 OnixByte.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
# you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,10 +18,31 @@
|
|||||||
package com.onixbyte.guid;
|
package com.onixbyte.guid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@code GuidCreator} is a generic interface for generating globally unique
|
* The {@code GuidCreator} is a generic interface for generating globally unique identifiers (GUIDs)
|
||||||
* identifiers (GUIDs) of a specific type.
|
* of a specific type.
|
||||||
* <p>
|
* <p>
|
||||||
* The type of ID is determined by the class implementing this interface.
|
* The type of ID is determined by the class implementing this interface.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>{@code
|
||||||
|
* public class StringGuidCreator implements GuidCreator<String> {
|
||||||
|
* private final AtomicLong counter = new AtomicLong();
|
||||||
|
*
|
||||||
|
* @Override
|
||||||
|
* public String nextId() {
|
||||||
|
* return UUID.randomUUID().toString() + "-" + counter.incrementAndGet();
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public class Example {
|
||||||
|
* public static void main(String[] args) {
|
||||||
|
* GuidCreator<String> guidCreator = new StringGuidCreator();
|
||||||
|
* String guid = guidCreator.nextId();
|
||||||
|
* System.out.println("Generated GUID: " + guid);
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
*
|
*
|
||||||
* @param <IdType> this represents the type of the Global Unique Identifier
|
* @param <IdType> this represents the type of the Global Unique Identifier
|
||||||
* @author Zihlu Wang
|
* @author Zihlu Wang
|
||||||
@@ -32,9 +53,6 @@ public interface GuidCreator<IdType> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates and returns the next globally unique ID.
|
* Generates and returns the next globally unique ID.
|
||||||
* <p>
|
|
||||||
* The exact implementation of how the globally unique ID is generated and
|
|
||||||
* returned will depend on the class implementing this method.
|
|
||||||
*
|
*
|
||||||
* @return the next globally unique ID
|
* @return the next globally unique ID
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,13 +18,12 @@
|
|||||||
package com.onixbyte.guid.exceptions;
|
package com.onixbyte.guid.exceptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@code TimingException} class represents an exception that is thrown
|
* The {@code TimingException} class represents an exception that is thrown when there is an error
|
||||||
* when there is an error related to time sequence.
|
* related to time sequence.
|
||||||
* <p>
|
* <p>
|
||||||
* Instances of TimingException can be created with or without a message and a
|
* Instances of TimingException can be created with or without a message and a cause. The message
|
||||||
* cause. The message provides a description of the exception, while the cause
|
* provides a description of the exception, while the cause represents the underlying cause of the
|
||||||
* represents the underlying cause of the exception and provides additional
|
* exception and provides additional information about the error.
|
||||||
* information about the error.
|
|
||||||
*
|
*
|
||||||
* @author Zihlu Wang
|
* @author Zihlu Wang
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
@@ -32,15 +31,14 @@ package com.onixbyte.guid.exceptions;
|
|||||||
public class TimingException extends RuntimeException {
|
public class TimingException extends RuntimeException {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A custom exception that is thrown when there is an issue with timing or
|
* A custom exception that is thrown when there is an issue with timing or scheduling.
|
||||||
* scheduling.
|
|
||||||
*/
|
*/
|
||||||
public TimingException() {
|
public TimingException() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A custom exception that is thrown when there is an issue with timing or
|
* A custom exception that is thrown when there is an issue with timing or scheduling with
|
||||||
* scheduling with customised error message.
|
* customised error message.
|
||||||
*
|
*
|
||||||
* @param message customised message
|
* @param message customised message
|
||||||
*/
|
*/
|
||||||
@@ -49,8 +47,8 @@ public class TimingException extends RuntimeException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A custom exception that is thrown when there is an issue with timing or
|
* A custom exception that is thrown when there is an issue with timing or scheduling with
|
||||||
* scheduling with customised error message.
|
* customised error message.
|
||||||
*
|
*
|
||||||
* @param message customised message
|
* @param message customised message
|
||||||
* @param cause the cause of this exception
|
* @param cause the cause of this exception
|
||||||
@@ -60,8 +58,8 @@ public class TimingException extends RuntimeException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A custom exception that is thrown when there is an issue with timing or
|
* A custom exception that is thrown when there is an issue with timing or scheduling with
|
||||||
* scheduling with customised error message.
|
* customised error message.
|
||||||
*
|
*
|
||||||
* @param cause the cause of this exception
|
* @param cause the cause of this exception
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package contains the custom exception classes related to GUID
|
|
||||||
* generation. These exceptions are thrown when there are issues or errors
|
|
||||||
* during the generation or processing of global unique identifiers (GUIDs).
|
|
||||||
* <p>
|
|
||||||
* The main exception class in this package is {@link
|
|
||||||
* com.onixbyte.guid.exceptions.TimingException}, which is a runtime
|
|
||||||
* exception and serves as the base exception for all other custom exceptions
|
|
||||||
* related to GUID generation.
|
|
||||||
* <p>
|
|
||||||
* Custom exceptions in this package provide specific information about the
|
|
||||||
* type of error that occurred during GUID generation, making it easier for
|
|
||||||
* developers to handle and respond to different scenarios when dealing with
|
|
||||||
* GUIDs. They are designed to enhance the robustness and reliability of the
|
|
||||||
* GUID generation process by providing clear and meaningful error messages to
|
|
||||||
* the developers.
|
|
||||||
* <p>
|
|
||||||
* Developers using the GUID generation module should be aware of the possible
|
|
||||||
* exceptions that can be thrown and handle them appropriately to ensure smooth
|
|
||||||
* operation and error handling in their applications.
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.guid.exceptions;
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,15 +19,16 @@ package com.onixbyte.guid.impl;
|
|||||||
|
|
||||||
import com.onixbyte.guid.GuidCreator;
|
import com.onixbyte.guid.GuidCreator;
|
||||||
import com.onixbyte.guid.exceptions.TimingException;
|
import com.onixbyte.guid.exceptions.TimingException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@code SnowflakeGuidCreator} generates unique identifiers using the
|
* The {@code SnowflakeGuidCreator} generates unique identifiers using the Snowflake algorithm,
|
||||||
* Snowflake algorithm, which combines a timestamp, worker ID, and data centre
|
* which combines a timestamp, worker ID, and data centre ID to create 64-bit long integers. The bit
|
||||||
* ID to create 64-bit long integers. The bit distribution for the generated
|
* distribution for the generated IDs is as follows:
|
||||||
* IDs is as follows:
|
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>1 bit for sign</li>
|
* <li>1 bit for sign</li>
|
||||||
* <li>41 bits for timestamp (in milliseconds)</li>
|
* <li>41 bits for timestamp (in milliseconds)</li>
|
||||||
@@ -36,11 +37,10 @@ import java.time.ZoneId;
|
|||||||
* <li>12 bits for sequence number (per millisecond)</li>
|
* <li>12 bits for sequence number (per millisecond)</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* <p>
|
* <p>
|
||||||
* When initializing a {@link SnowflakeGuidCreator}, you must provide the
|
* When initializing a {@link SnowflakeGuidCreator}, you must provide the worker ID and data centre
|
||||||
* worker ID and data centre ID, ensuring they are within the valid range
|
* ID, ensuring they are within the valid range defined by the bit size. The generator maintains an
|
||||||
* defined by the bit size. The generator maintains an internal sequence number
|
* internal sequence number that increments for IDs generated within the same millisecond. If the
|
||||||
* that increments for IDs generated within the same millisecond. If the system
|
* system clock moves backward, an exception is thrown to prevent generating IDs with
|
||||||
* clock moves backward, an exception is thrown to prevent generating IDs with
|
|
||||||
* repeated timestamps.
|
* repeated timestamps.
|
||||||
*
|
*
|
||||||
* @author Zihlu Wang
|
* @author Zihlu Wang
|
||||||
@@ -49,9 +49,11 @@ import java.time.ZoneId;
|
|||||||
*/
|
*/
|
||||||
public final class SnowflakeGuidCreator implements GuidCreator<Long> {
|
public final class SnowflakeGuidCreator implements GuidCreator<Long> {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(SnowflakeGuidCreator.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a SnowflakeGuidGenerator with the default start epoch and
|
* Constructs a SnowflakeGuidGenerator with the default start epoch and custom worker ID, data
|
||||||
* custom worker ID, data centre ID.
|
* centre ID.
|
||||||
*
|
*
|
||||||
* @param dataCentreId the data centre ID (between 0 and 31)
|
* @param dataCentreId the data centre ID (between 0 and 31)
|
||||||
* @param workerId the worker ID (between 0 and 31)
|
* @param workerId the worker ID (between 0 and 31)
|
||||||
@@ -61,16 +63,13 @@ public final class SnowflakeGuidCreator implements GuidCreator<Long> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a SnowflakeGuidGenerator with a custom epoch, worker ID, and
|
* Constructs a SnowflakeGuidGenerator with a custom epoch, worker ID, and data centre ID.
|
||||||
* data centre ID.
|
|
||||||
*
|
*
|
||||||
* @param dataCentreId the data centre ID (between 0 and 31)
|
* @param dataCentreId the data centre ID (between 0 and 31)
|
||||||
* @param workerId the worker ID (between 0 and 31)
|
* @param workerId the worker ID (between 0 and 31)
|
||||||
* @param startEpoch the custom epoch timestamp (in milliseconds) to
|
* @param startEpoch the custom epoch timestamp (in milliseconds) to start generating IDs from
|
||||||
* start generating IDs from
|
* @throws IllegalArgumentException if the start epoch is greater than the current timestamp,
|
||||||
* @throws IllegalArgumentException if the start epoch is greater than the
|
* or if the worker ID or data centre ID is out of range
|
||||||
* current timestamp, or if the worker ID
|
|
||||||
* or data centre ID is out of range
|
|
||||||
*/
|
*/
|
||||||
public SnowflakeGuidCreator(long dataCentreId, long workerId, long startEpoch) {
|
public SnowflakeGuidCreator(long dataCentreId, long workerId, long startEpoch) {
|
||||||
if (startEpoch > currentTimestamp()) {
|
if (startEpoch > currentTimestamp()) {
|
||||||
@@ -98,37 +97,37 @@ public final class SnowflakeGuidCreator implements GuidCreator<Long> {
|
|||||||
* Generates the next unique ID.
|
* Generates the next unique ID.
|
||||||
*
|
*
|
||||||
* @return the generated unique ID
|
* @return the generated unique ID
|
||||||
* @throws TimingException if the system clock moves backwards,
|
* @throws TimingException if the system clock moves backwards, indicating an invalid sequence
|
||||||
* indicating an invalid sequence of timestamps.
|
* of timestamps.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public synchronized Long nextId() {
|
public synchronized Long nextId() {
|
||||||
var timestamp = currentTimestamp();
|
var timestamp = currentTimestamp();
|
||||||
|
|
||||||
// If the current time is less than the timestamp of the last ID generation, it means that the system clock
|
// if the current time is less than the timestamp of the last ID generation, it means that
|
||||||
// has been set back and an exception should be thrown.
|
// the system clock has been set back and an exception should be thrown
|
||||||
if (timestamp < lastTimestamp) {
|
if (timestamp < lastTimestamp) {
|
||||||
throw new TimingException("Clock moved backwards. Refusing to generate id for %d milliseconds"
|
throw new TimingException("Clock moved backwards. Refusing to generate id for %d milliseconds"
|
||||||
.formatted(lastTimestamp - timestamp));
|
.formatted(lastTimestamp - timestamp));
|
||||||
}
|
}
|
||||||
|
|
||||||
// If generated at the same time, perform intra-millisecond sequences
|
// if generated at the same time, perform intra-millisecond sequences
|
||||||
long sequenceBits = 12L;
|
long sequenceBits = 12L;
|
||||||
if (lastTimestamp == timestamp) {
|
if (lastTimestamp == timestamp) {
|
||||||
long sequenceMask = ~(-1L << sequenceBits);
|
long sequenceMask = ~(-1L << sequenceBits);
|
||||||
sequence = (sequence + 1) & sequenceMask;
|
sequence = (sequence + 1) & sequenceMask;
|
||||||
// Sequence overflow in milliseconds
|
// sequence overflow in milliseconds
|
||||||
if (sequence == 0) {
|
if (sequence == 0) {
|
||||||
// Block to the next millisecond, get a new timestamp
|
// block to the next millisecond, get a new timestamp
|
||||||
timestamp = awaitToNextMillis(lastTimestamp);
|
timestamp = awaitToNextMillis(lastTimestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Timestamp change, sequence reset in milliseconds
|
// timestamp change, sequence reset in milliseconds
|
||||||
else {
|
else {
|
||||||
sequence = 0L;
|
sequence = 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Timestamp of last ID generation
|
// timestamp of last ID generation
|
||||||
lastTimestamp = timestamp;
|
lastTimestamp = timestamp;
|
||||||
|
|
||||||
// shifted and put together by or operations to form a 64-bit ID
|
// shifted and put together by or operations to form a 64-bit ID
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The package provides a set of tools for generating globally unique
|
|
||||||
* identifiers (GUIDs).
|
|
||||||
* <p>
|
|
||||||
* The goal of this library is to provide an efficient, reliable way to
|
|
||||||
* generate globally unique identifiers without requiring any specific
|
|
||||||
* environment or configuration.
|
|
||||||
* <p>
|
|
||||||
* Key features include:
|
|
||||||
* <ul>
|
|
||||||
* <li>Efficient generation of globally unique identifiers</li>
|
|
||||||
* <li>High performance and quick response</li>
|
|
||||||
* <li>Easy to integrate</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.guid;
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!--
|
<!--
|
||||||
~ Copyright (C) 2023-2024 OnixByte.
|
~ Copyright (C) 2024-2025 OnixByte.
|
||||||
~
|
~
|
||||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
~ you may not use this file except in compliance with the License.
|
~ you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,7 +18,8 @@
|
|||||||
package com.onixbyte.security;
|
package com.onixbyte.security;
|
||||||
|
|
||||||
import com.onixbyte.security.exception.KeyLoadingException;
|
import com.onixbyte.security.exception.KeyLoadingException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.security.KeyFactory;
|
import java.security.KeyFactory;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
@@ -30,15 +31,36 @@ import java.security.spec.X509EncodedKeySpec;
|
|||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* KeyLoader can load key pairs from PEM formated content.
|
* The {@code KeyLoader} class provides utility methods for loading ECDSA keys from PEM-formatted
|
||||||
|
* key text. This class supports loading both private and public keys.
|
||||||
|
* <p>
|
||||||
|
* The utility methods in this class are useful for scenarios where ECDSA keys need to be loaded
|
||||||
|
* from PEM-formatted strings for cryptographic operations.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>{@code
|
||||||
|
* String pemPrivateKey = """
|
||||||
|
* -----BEGIN PRIVATE KEY-----
|
||||||
|
* ...
|
||||||
|
* -----END PRIVATE KEY-----""";
|
||||||
|
* ECPrivateKey privateKey = KeyLoader.loadEcdsaPrivateKey(pemPrivateKey);
|
||||||
|
*
|
||||||
|
* String pemPublicKey = """
|
||||||
|
* -----BEGIN PUBLIC KEY-----
|
||||||
|
* ...
|
||||||
|
* -----END PUBLIC KEY-----""";
|
||||||
|
* ECPublicKey publicKey = KeyLoader.loadEcdsaPublicKey(pemPublicKey);
|
||||||
|
* }</pre>
|
||||||
*
|
*
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
* @version 1.6.0
|
* @version 1.6.0
|
||||||
* @since 1.6.0
|
* @since 1.6.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
public class KeyLoader {
|
public class KeyLoader {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(KeyLoader.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor prevents from being initialised.
|
* Private constructor prevents from being initialised.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+18
-2
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,7 +18,23 @@
|
|||||||
package com.onixbyte.security.exception;
|
package com.onixbyte.security.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code KeyLoadingException} is an exception indicating an error occurred while loading a key.
|
* The {@code KeyLoadingException} class represents an exception that is thrown when there is an
|
||||||
|
* error loading cryptographic keys. This exception can be used to indicate various issues such as
|
||||||
|
* invalid key specifications, unsupported key algorithms, or other key loading errors.
|
||||||
|
* <p>
|
||||||
|
* This class extends {@link RuntimeException}, allowing it to be thrown without being declared in
|
||||||
|
* a method's {@code throws} clause.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>{@code
|
||||||
|
* try {
|
||||||
|
* ECPrivateKey privateKey = KeyLoader.loadEcdsaPrivateKey(pemPrivateKey);
|
||||||
|
* } catch (KeyLoadingException e) {
|
||||||
|
* // Handle the exception
|
||||||
|
* e.printStackTrace();
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
*
|
*
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
* @version 1.6.0
|
* @version 1.6.0
|
||||||
|
|||||||
+43
-10
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,25 +17,60 @@
|
|||||||
|
|
||||||
package com.onixbyte.devkit.utils.unsafe;
|
package com.onixbyte.devkit.utils.unsafe;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code MapUtil} is a utility class that provides methods for converting objects to maps and
|
* The {@link ReflectMapUtil} class provides utility methods for converting between objects and maps
|
||||||
* maps to objects.
|
* using reflection. This class allows for dynamic mapping of object fields to map entries and
|
||||||
|
* vice versa.
|
||||||
* <p>
|
* <p>
|
||||||
* It also provides methods for getting and setting field values using reflection.
|
* The utility methods in this class are useful for scenarios where objects need to be represented
|
||||||
|
* as maps for serialization, deserialization, or other purposes, and where the structure of the
|
||||||
|
* objects is not known at compile time.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>
|
||||||
|
* {@code
|
||||||
|
* public class User {
|
||||||
|
* private String name;
|
||||||
|
* private int age;
|
||||||
|
*
|
||||||
|
* // getters and setters
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public class Example {
|
||||||
|
* public static void main(String[] args) throws IllegalAccessException {
|
||||||
|
* User user = new User();
|
||||||
|
* user.setName("John");
|
||||||
|
* user.setAge(30);
|
||||||
|
*
|
||||||
|
* // Convert object to map
|
||||||
|
* Map<String, Object> userMap = ReflectMapUtil.objectToMap(user);
|
||||||
|
* System.out.println(userMap); // Output: {name=John, age=30}
|
||||||
|
*
|
||||||
|
* // Convert map to object
|
||||||
|
* User newUser = ReflectMapUtil.mapToObject(userMap, User.class);
|
||||||
|
* System.out.println(newUser.getName()); // Output: John
|
||||||
|
* System.out.println(newUser.getAge()); // Output: 30
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author zihluwang
|
* @author zihluwang
|
||||||
* @version 1.4.2
|
* @version 1.4.2
|
||||||
* @since 1.4.2
|
* @since 1.4.2
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
public final class ReflectMapUtil {
|
public final class ReflectMapUtil {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(ReflectMapUtil.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an object to a map by mapping the field names to their corresponding values.
|
* Converts an object to a map by mapping the field names to their corresponding values.
|
||||||
*
|
*
|
||||||
@@ -131,10 +166,8 @@ public final class ReflectMapUtil {
|
|||||||
* @param <T> the type of the field value
|
* @param <T> the type of the field value
|
||||||
* @return the value of the field in the object, or null if the field does
|
* @return the value of the field in the object, or null if the field does
|
||||||
* not exist or cannot be accessed
|
* not exist or cannot be accessed
|
||||||
* @throws IllegalAccessException if an error occurs while accessing the
|
* @throws IllegalAccessException if an error occurs while accessing the field
|
||||||
* field
|
* @throws InvocationTargetException if an error occurs while invoking the field getter method
|
||||||
* @throws InvocationTargetException if an error occurs while invoking the
|
|
||||||
* field getter method
|
|
||||||
* @throws NoSuchMethodException if the specified getter is not present
|
* @throws NoSuchMethodException if the specified getter is not present
|
||||||
*/
|
*/
|
||||||
public static <T> T getFieldValue(Object obj, String fieldName, Class<T> fieldType)
|
public static <T> T getFieldValue(Object obj, String fieldName, Class<T> fieldType)
|
||||||
|
|||||||
@@ -17,8 +17,10 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<property name="COLOURFUL_OUTPUT" value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
<property name="COLOURFUL_OUTPUT"
|
||||||
<property name="STANDARD_OUTPUT" value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
||||||
|
<property name="STANDARD_OUTPUT"
|
||||||
|
value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
||||||
|
|
||||||
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,7 +17,8 @@
|
|||||||
|
|
||||||
package com.onixbyte.nums;
|
package com.onixbyte.nums;
|
||||||
|
|
||||||
import lombok.Getter;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
@@ -90,9 +91,10 @@ import java.util.function.Function;
|
|||||||
* @see BigDecimal
|
* @see BigDecimal
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Getter
|
|
||||||
public final class ChainedCalcUtil {
|
public final class ChainedCalcUtil {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(ChainedCalcUtil.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@code ChainedCalcUtil} instance with the specified initial value.
|
* Creates a {@code ChainedCalcUtil} instance with the specified initial value.
|
||||||
*
|
*
|
||||||
@@ -235,6 +237,15 @@ public final class ChainedCalcUtil {
|
|||||||
return value.setScale(scale, RoundingMode.HALF_UP);
|
return value.setScale(scale, RoundingMode.HALF_UP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current value as a {@link BigDecimal}.
|
||||||
|
*
|
||||||
|
* @return the current value as a {@link BigDecimal}
|
||||||
|
*/
|
||||||
|
public BigDecimal getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current value as a {@link Double}.
|
* Returns the current value as a {@link Double}.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,15 +14,18 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.onixbyte.nums;
|
package com.onixbyte.nums;
|
||||||
|
|
||||||
import com.onixbyte.nums.model.QuartileBounds;
|
import com.onixbyte.nums.model.QuartileBounds;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A utility class that provides methods for calculating percentiles and interquartile range (IQR) bounds
|
* A utility class that provides methods for calculating percentiles and interquartile range (IQR)
|
||||||
* for a dataset.
|
* bounds for a dataset.
|
||||||
* <p>
|
* <p>
|
||||||
* This class contains static methods to:
|
* This class contains static methods to:
|
||||||
* <ul>
|
* <ul>
|
||||||
@@ -48,6 +51,8 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public final class PercentileCalculator {
|
public final class PercentileCalculator {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(PercentileCalculator.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor prevents from being initialised.
|
* Private constructor prevents from being initialised.
|
||||||
*/
|
*/
|
||||||
@@ -57,12 +62,13 @@ public final class PercentileCalculator {
|
|||||||
/**
|
/**
|
||||||
* Calculates the specified percentile from a list of values.
|
* Calculates the specified percentile from a list of values.
|
||||||
* <p>
|
* <p>
|
||||||
* This method takes a list of double values and calculates the given percentile using linear interpolation between
|
* This method takes a list of double values and calculates the given percentile using linear
|
||||||
* the two closest ranks. The list is first sorted in ascending order, and the specified percentile is
|
* interpolation between the two closest ranks. The list is first sorted in ascending order,
|
||||||
* then calculated.
|
* and the specified percentile is then calculated.
|
||||||
*
|
*
|
||||||
* @param values a list of {@code Double} values from which the percentile is calculated.
|
* @param values a list of {@code Double} values from which the percentile is calculated.
|
||||||
* @param percentile a {@code Double} representing the percentile to be calculated (e.g., 50.0 for the median)
|
* @param percentile a {@code Double} representing the percentile to be calculated (e.g., 50.0
|
||||||
|
* for the median)
|
||||||
* @return a {@code Double} value representing the calculated percentile
|
* @return a {@code Double} value representing the calculated percentile
|
||||||
*/
|
*/
|
||||||
public static Double calculatePercentile(List<Double> values, Double percentile) {
|
public static Double calculatePercentile(List<Double> values, Double percentile) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -20,9 +20,9 @@ package com.onixbyte.nums.model;
|
|||||||
/**
|
/**
|
||||||
* A record representing the quartile bounds of a dataset.
|
* A record representing the quartile bounds of a dataset.
|
||||||
* <p>
|
* <p>
|
||||||
* This class encapsulates the lower and upper bounds of a dataset, which are typically used for detecting outliers in
|
* This class encapsulates the lower and upper bounds of a dataset, which are typically used for
|
||||||
* the data. The bounds are calculated based on the interquartile range (IQR) of the dataset. Values below the lower
|
* detecting outliers in the data. The bounds are calculated based on the interquartile range (IQR)
|
||||||
* bound or above the upper bound may be considered outliers.
|
* of the dataset. Values below the lower bound or above the upper bound may be considered outliers.
|
||||||
* <p>
|
* <p>
|
||||||
* Quartile bounds consist of:
|
* Quartile bounds consist of:
|
||||||
* <ul>
|
* <ul>
|
||||||
@@ -52,8 +52,8 @@ public record QuartileBounds(
|
|||||||
/**
|
/**
|
||||||
* Creates a new {@link Builder} instance for building a {@code QuartileBounds} object.
|
* Creates a new {@link Builder} instance for building a {@code QuartileBounds} object.
|
||||||
* <p>
|
* <p>
|
||||||
* The {@link Builder} pattern is used to construct the {@code QuartileBounds} object with optional values for the
|
* The {@link Builder} pattern is used to construct the {@code QuartileBounds} object with
|
||||||
* upper and lower bounds.
|
* optional values for the upper and lower bounds.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @return a new instance of the {@link Builder} class
|
* @return a new instance of the {@link Builder} class
|
||||||
@@ -65,9 +65,10 @@ public record QuartileBounds(
|
|||||||
/**
|
/**
|
||||||
* A builder class for constructing instances of the {@code QuartileBounds} record.
|
* A builder class for constructing instances of the {@code QuartileBounds} record.
|
||||||
* <p>
|
* <p>
|
||||||
* The {@link Builder} pattern allows for the step-by-step construction of a {@code QuartileBounds} object,
|
* The {@link Builder} pattern allows for the step-by-step construction of a
|
||||||
* providing a flexible way to set values for the lower and upper bounds. Once the builder has the required values,
|
* {@code QuartileBounds} object, providing a flexible way to set values for the lower and
|
||||||
* the {@link #build()} method creates and returns a new {@code QuartileBounds} object.
|
* upper bounds. Once the builder has the required values, the {@link #build()} method creates
|
||||||
|
* and returns a new {@code QuartileBounds} object.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* Example usage:
|
* Example usage:
|
||||||
@@ -114,7 +115,8 @@ public record QuartileBounds(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds and returns a new {@code QuartileBounds} instance with the specified upper and lower bounds.
|
* Builds and returns a new {@code QuartileBounds} instance with the specified upper and
|
||||||
|
* lower bounds.
|
||||||
*
|
*
|
||||||
* @return a new {@code QuartileBounds} object containing the specified bounds
|
* @return a new {@code QuartileBounds} object containing the specified bounds
|
||||||
*/
|
*/
|
||||||
|
|||||||
+21
-22
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,6 +18,8 @@
|
|||||||
package com.onixbyte.propertyguard.autoconfiguration;
|
package com.onixbyte.propertyguard.autoconfiguration;
|
||||||
|
|
||||||
import com.onixbyte.devkit.utils.AesUtil;
|
import com.onixbyte.devkit.utils.AesUtil;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
||||||
@@ -29,39 +31,34 @@ import java.util.HashMap;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code PropertyGuard} is a utility class designed for encrypting
|
* {@code PropertyGuard} is a utility class designed for encrypting configuration properties in
|
||||||
* configuration properties in Spring Boot applications.
|
* Spring Boot applications.
|
||||||
* <p>
|
* <p>
|
||||||
* Spring Boot applications often need to store sensitive configuration details
|
* Spring Boot applications often need to store sensitive configuration details such as database
|
||||||
* such as database passwords, API keys, etc. To ensure that these sensitive
|
* passwords, API keys, etc. To ensure that these sensitive pieces of information are not exposed
|
||||||
* pieces of information are not exposed to the public, developers can utilize
|
* to the public, developers can utilize the {@code PropertyGuard} class to encrypt and store them
|
||||||
* the {@code PropertyGuard} class to encrypt and store them within
|
* within configuration files.
|
||||||
* configuration files.
|
|
||||||
* <p>
|
* <p>
|
||||||
* <b>Usage</b>
|
* <b>Usage</b>
|
||||||
* You need a 16-char long secret for encrypting a configuration property. You
|
* You need a 16-char long secret for encrypting a configuration property. You can get this secret
|
||||||
* can get this secret on your own, or use the helper utility class by the
|
* on your own, or use the helper utility class by the following code:
|
||||||
* following code:
|
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
* var secret = AesUtil.generateRandomSecret(); // Let's presume the result is
|
* var secret = AesUtil.generateRandomSecret(); // Let's presume the result is "3856faef0d2d4f33"
|
||||||
* // "3856faef0d2d4f33"
|
|
||||||
* }</pre>
|
* }</pre>
|
||||||
* <p>
|
* <p>
|
||||||
* Then, in {@code application.yml} or {@code application.properties}, change
|
* Then, in {@code application.yml} or {@code application.properties}, change the original value
|
||||||
* the original value from plain text to encrypted value with the prefix
|
* from plain text to encrypted value with the prefix "<code>pg:</code>".
|
||||||
* "<code>pg:</code>".
|
* <pre>{@code
|
||||||
* <pre>
|
|
||||||
* # original
|
* # original
|
||||||
* app.example-properties=Sample Value
|
* app.example-properties=Sample Value
|
||||||
*
|
*
|
||||||
* # encrypted with key 3856faef0d2d4f33
|
* # encrypted with key 3856faef0d2d4f33
|
||||||
* app.example-properties=pg:t4YBfv8M9ZmTzWgTi2gJqg==
|
* app.example-properties=pg:t4YBfv8M9ZmTzWgTi2gJqg==
|
||||||
* </pre>
|
* }</pre>
|
||||||
* After that, before running, you need to add the command line arguments
|
* After that, before running, you need to add the command line arguments "pg.key" as the following
|
||||||
* "pg.key" as the following codes: {@code --pg.key=<the secret>}.
|
* codes: {@code --pg.key=<the secret>}.
|
||||||
* <p>
|
* <p>
|
||||||
* This class is extracted from <a href="https://baomidou.com/pages/e0a5ce/"
|
* This class is extracted from <a href="https://baomidou.com/pages/e0a5ce/">MyBatis-Plus</a>.
|
||||||
* >MyBatis-Plus</a>.
|
|
||||||
*
|
*
|
||||||
* @author hubin@baomidou
|
* @author hubin@baomidou
|
||||||
* @version 1.1.0
|
* @version 1.1.0
|
||||||
@@ -70,6 +67,8 @@ import java.util.Optional;
|
|||||||
*/
|
*/
|
||||||
public class PropertyGuard implements EnvironmentPostProcessor {
|
public class PropertyGuard implements EnvironmentPostProcessor {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(PropertyGuard.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a {@link PropertyGuard} instance.
|
* Create a {@link PropertyGuard} instance.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+5
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -38,7 +38,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
|||||||
import com.onixbyte.simplejwt.exceptions.IllegalKeyPairException;
|
import com.onixbyte.simplejwt.exceptions.IllegalKeyPairException;
|
||||||
import com.onixbyte.simplejwt.exceptions.IllegalSecretException;
|
import com.onixbyte.simplejwt.exceptions.IllegalSecretException;
|
||||||
import com.onixbyte.simplejwt.exceptions.UnsupportedAlgorithmException;
|
import com.onixbyte.simplejwt.exceptions.UnsupportedAlgorithmException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.security.interfaces.ECPrivateKey;
|
import java.security.interfaces.ECPrivateKey;
|
||||||
@@ -98,9 +99,10 @@ import java.util.function.Function;
|
|||||||
* @see JWTCreator.Builder
|
* @see JWTCreator.Builder
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
public class AuthzeroTokenResolver implements TokenResolver<DecodedJWT> {
|
public class AuthzeroTokenResolver implements TokenResolver<DecodedJWT> {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(AuthzeroTokenResolver.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a builder of {@link AuthzeroTokenResolver}.
|
* Create a builder of {@link AuthzeroTokenResolver}.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package contains classes related to the integration of the {@code com.auth0:java-jwt}
|
|
||||||
* library in the Simple JWT project. {@code com.auth0:java-jwt} is a powerful and widely-used
|
|
||||||
* identity as a Service (IDaaS) platform that provides secure authentication and authorisation
|
|
||||||
* solutions for web and mobile applications. The classes in this package provide the necessary
|
|
||||||
* functionality to handle JSON Web Tokens (JWTs) using the {@code com.auth0:java-jwt} library.
|
|
||||||
* <p>
|
|
||||||
* The main class in this package is the {@link
|
|
||||||
* com.onixbyte.simplejwt.authzero.AuthzeroTokenResolver}, which implements the
|
|
||||||
* {@link com.onixbyte.simplejwt.TokenResolver} interface and uses the {@code com.auth0:java-jwt}
|
|
||||||
* library to handle JWT operations. It provides the functionality to create, validate, and extract
|
|
||||||
* JWTs using the {@code com.auth0:java-jwt} library. Developers can use this class as the main
|
|
||||||
* token resolver in the Simple JWT project when integrating {@code com.auth0:java-jwt} as the
|
|
||||||
* JWT management library.
|
|
||||||
* <p>
|
|
||||||
* The {@link com.onixbyte.simplejwt.authzero.AuthzeroTokenResolver} relies on the
|
|
||||||
* {@code com.auth0:java-jwt} library to handle the underlying JWT operations, including token
|
|
||||||
* creation, validation, and extraction. It utilizes the {@code com.auth0:java-jwt}
|
|
||||||
* {@link com.auth0.jwt.algorithms.Algorithm} class to define and use different algorithms for
|
|
||||||
* JWT signing and verification.
|
|
||||||
* <p>
|
|
||||||
* Developers using the {@code com.auth0:java-jwt} integration should be familiar with the concepts
|
|
||||||
* and usage of the {@code com.auth0:java-jwt} library and follow the official
|
|
||||||
* {@code com.auth0:java-jwt} documentation for best practices and security considerations.
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.simplejwt.authzero;
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,6 +18,8 @@
|
|||||||
package com.onixbyte.simplejwt;
|
package com.onixbyte.simplejwt;
|
||||||
|
|
||||||
import com.onixbyte.simplejwt.exceptions.WeakSecretException;
|
import com.onixbyte.simplejwt.exceptions.WeakSecretException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@@ -32,6 +34,8 @@ import java.util.Random;
|
|||||||
*/
|
*/
|
||||||
public final class SecretCreator {
|
public final class SecretCreator {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(SecretCreator.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a secure secret with the specified length and character sets.
|
* Generates a secure secret with the specified length and character sets.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
package com.onixbyte.simplejwt;
|
package com.onixbyte.simplejwt;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code TokenPayload} interface is used to mark a data class as suitable
|
* {@code TokenPayload} interface is used to mark a data class as suitable
|
||||||
* for being used as the payload in a JSON Web Token (JWT). Any class
|
* for being used as the payload in a JSON Web Token (JWT). Any class
|
||||||
@@ -44,7 +42,4 @@ import java.util.Map;
|
|||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public interface TokenPayload {
|
public interface TokenPayload {
|
||||||
|
|
||||||
// Marker interface for JWT payload data classes
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package contains annotation classes that are used to prevent annotated
|
|
||||||
* properties from being automatically injected into the JSON Web Token (JWT)
|
|
||||||
* payload during token generation. These annotations can be applied to
|
|
||||||
* properties of a data class to exclude them from being included as part
|
|
||||||
* of the JWT payload.
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.simplejwt.annotations;
|
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-4
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
package com.onixbyte.simplejwt.constants;
|
package com.onixbyte.simplejwt.constants;
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,7 +43,6 @@ import java.util.List;
|
|||||||
* @version 1.1.0
|
* @version 1.1.0
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Getter
|
|
||||||
public enum TokenAlgorithm {
|
public enum TokenAlgorithm {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,14 +17,11 @@
|
|||||||
|
|
||||||
package com.onixbyte.simplejwt.constants;
|
package com.onixbyte.simplejwt.constants;
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base data types used to process enum data.
|
* The base data types used to process enum data.
|
||||||
*
|
*
|
||||||
* @author Zihlu Wang
|
* @author Zihlu Wang
|
||||||
*/
|
*/
|
||||||
@Getter
|
|
||||||
public enum TokenDataType {
|
public enum TokenDataType {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,4 +67,12 @@ public enum TokenDataType {
|
|||||||
this.mappedClass = mappedClass;
|
this.mappedClass = mappedClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the target mapped class.
|
||||||
|
*
|
||||||
|
* @return mapped class
|
||||||
|
*/
|
||||||
|
public Class<?> getMappedClass() {
|
||||||
|
return mappedClass;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package contains constant values related to JSON Web Token (JWT)
|
|
||||||
* processing. These constants define various aspects of JWT, such as the
|
|
||||||
* algorithms being used for token signing and verification, and other
|
|
||||||
* configuration parameters.
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.simplejwt.constants;
|
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The {@code cn.org.codecrafters.simplejwt.exceptions} package contains
|
|
||||||
* custom exception classes related to the
|
|
||||||
* {@code cn.org.codecrafters:simple-jwt-facade} library. These exceptions are
|
|
||||||
* thrown when there are issues or errors during the generation , validation,
|
|
||||||
* or processing of JSON Web Tokens (JWTs) in Java applications.
|
|
||||||
* <p>
|
|
||||||
* Custom exception classes in this package are designed to enhance the
|
|
||||||
* robustness and reliability of the JWT handling process by providing clear
|
|
||||||
* and meaningful error messages to the developers. They help developers
|
|
||||||
* identify and troubleshoot issues related to JWT generation, validation, or
|
|
||||||
* extraction and ensure smooth operation and error handling in
|
|
||||||
* their applications.
|
|
||||||
* <p>
|
|
||||||
* Developers using the {@code cn.org.codecrafters:simple-jwt-facade} library
|
|
||||||
* should be aware of the possible exceptions that can be thrown and handle
|
|
||||||
* them appropriately to ensure secure and reliable JWT handling in their
|
|
||||||
* Java applications.
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.simplejwt.exceptions;
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The {@code cn.org.codecrafters.simplejwt} package is the core package of the
|
|
||||||
* <b>Simple JWT</b> project, which provides a lightweight and easy-to-use
|
|
||||||
* library for working with JSON Web Tokens (JWTs) in Java applications. JWT is
|
|
||||||
* a widely-used standard for representing claims between two parties,
|
|
||||||
* typically used to secure web and mobile applications. This library aims to
|
|
||||||
* simplify the JWT handling process and provide convenient abstractions for
|
|
||||||
* JWT generation, validation, and extraction.
|
|
||||||
* <p>
|
|
||||||
* The <b>Simple JWT</b> library is designed to be flexible and customisable,
|
|
||||||
* allowing developers to use different algorithms, token resolvers, and token
|
|
||||||
* payload classes based on their specific application requirements. It aims to
|
|
||||||
* simplify the JWT handling process while maintaining security and best
|
|
||||||
* practices for working with JWTs.
|
|
||||||
* <p>
|
|
||||||
* Developers should refer to the official documentation and examples for the
|
|
||||||
* <b>Simple JWT</b> project to understand how to use the library effectively
|
|
||||||
* and securely in their Java applications.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.simplejwt;
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+5
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -24,7 +24,8 @@ import com.onixbyte.simplejwt.autoconfiguration.properties.SimpleJwtProperties;
|
|||||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.onixbyte.simplejwt.constants.TokenAlgorithm;
|
import com.onixbyte.simplejwt.constants.TokenAlgorithm;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
@@ -55,7 +56,6 @@ import org.springframework.context.annotation.Bean;
|
|||||||
* @version 1.6.0
|
* @version 1.6.0
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
@EnableConfigurationProperties(value = {SimpleJwtProperties.class})
|
@EnableConfigurationProperties(value = {SimpleJwtProperties.class})
|
||||||
@ConditionalOnClass({DecodedJWT.class, AuthzeroTokenResolver.class})
|
@ConditionalOnClass({DecodedJWT.class, AuthzeroTokenResolver.class})
|
||||||
@@ -64,6 +64,8 @@ import org.springframework.context.annotation.Bean;
|
|||||||
@AutoConfigureAfter(value = GuidAutoConfiguration.class)
|
@AutoConfigureAfter(value = GuidAutoConfiguration.class)
|
||||||
public class AuthzeroTokenResolverAutoConfiguration {
|
public class AuthzeroTokenResolverAutoConfiguration {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(AuthzeroTokenResolverAutoConfiguration.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new {@code SimpleJwtAutoConfiguration} instance with the
|
* Constructs a new {@code SimpleJwtAutoConfiguration} instance with the
|
||||||
* provided SimpleJwtProperties.
|
* provided SimpleJwtProperties.
|
||||||
|
|||||||
+5
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,8 @@ package com.onixbyte.simplejwt.autoconfiguration;
|
|||||||
|
|
||||||
import com.onixbyte.guid.GuidCreator;
|
import com.onixbyte.guid.GuidCreator;
|
||||||
import com.onixbyte.simplejwt.autoconfiguration.conditions.GuidCreatorCondition;
|
import com.onixbyte.simplejwt.autoconfiguration.conditions.GuidCreatorCondition;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
@@ -33,10 +34,11 @@ import java.util.UUID;
|
|||||||
* @version 1.1.0
|
* @version 1.1.0
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
public class GuidAutoConfiguration {
|
public class GuidAutoConfiguration {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(GuidAutoConfiguration.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor.
|
* Default constructor.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+5
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,7 +18,8 @@
|
|||||||
package com.onixbyte.simplejwt.autoconfiguration.conditions;
|
package com.onixbyte.simplejwt.autoconfiguration.conditions;
|
||||||
|
|
||||||
import com.onixbyte.guid.GuidCreator;
|
import com.onixbyte.guid.GuidCreator;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.context.annotation.Condition;
|
import org.springframework.context.annotation.Condition;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
@@ -32,9 +33,10 @@ import java.util.Objects;
|
|||||||
* @version 1.1.0
|
* @version 1.1.0
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
|
||||||
public class GuidCreatorCondition implements Condition {
|
public class GuidCreatorCondition implements Condition {
|
||||||
|
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(GuidCreatorCondition.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor.
|
* Default constructor.
|
||||||
*/
|
*/
|
||||||
|
|||||||
-38
@@ -1,38 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>
|
|
||||||
* The classes in this package are responsible for automatically configuring
|
|
||||||
* the Simple JWT library when it is used in a Spring Boot application. They
|
|
||||||
* provide default settings and configurations to ensure that the library works
|
|
||||||
* smoothly and seamlessly without requiring developers to manually configure
|
|
||||||
* it.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* Developers using the Simple JWT library with Spring Boot do not need to
|
|
||||||
* explicitly configure the library, as the auto-configuration classes take
|
|
||||||
* care of setting up the necessary components and configurations
|
|
||||||
* automatically. However, developers still have the flexibility to customize
|
|
||||||
* the behavior of the library by providing their own configurations and
|
|
||||||
* properties.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.simplejwt.autoconfiguration;
|
|
||||||
+90
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -20,7 +20,6 @@ package com.onixbyte.simplejwt.autoconfiguration.properties;
|
|||||||
import com.onixbyte.simplejwt.SecretCreator;
|
import com.onixbyte.simplejwt.SecretCreator;
|
||||||
import com.onixbyte.simplejwt.autoconfiguration.AuthzeroTokenResolverAutoConfiguration;
|
import com.onixbyte.simplejwt.autoconfiguration.AuthzeroTokenResolverAutoConfiguration;
|
||||||
import com.onixbyte.simplejwt.constants.TokenAlgorithm;
|
import com.onixbyte.simplejwt.constants.TokenAlgorithm;
|
||||||
import lombok.Data;
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,7 +41,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||||||
* @version 1.1.0
|
* @version 1.1.0
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@Data
|
|
||||||
@ConfigurationProperties(prefix = "onixbyte.simple-jwt")
|
@ConfigurationProperties(prefix = "onixbyte.simple-jwt")
|
||||||
public class SimpleJwtProperties {
|
public class SimpleJwtProperties {
|
||||||
|
|
||||||
@@ -79,5 +77,94 @@ public class SimpleJwtProperties {
|
|||||||
*/
|
*/
|
||||||
private String publicKey;
|
private String publicKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Algorithm getter.
|
||||||
|
*
|
||||||
|
* @return algorithm
|
||||||
|
*/
|
||||||
|
public TokenAlgorithm getAlgorithm() {
|
||||||
|
return algorithm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Algorithm setter.
|
||||||
|
*
|
||||||
|
* @param algorithm the algorithm
|
||||||
|
*/
|
||||||
|
public void setAlgorithm(TokenAlgorithm algorithm) {
|
||||||
|
this.algorithm = algorithm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Issuer getter.
|
||||||
|
*
|
||||||
|
* @return issuer
|
||||||
|
*/
|
||||||
|
public String getIssuer() {
|
||||||
|
return issuer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Issuer setter.
|
||||||
|
*
|
||||||
|
* @param issuer the issuer
|
||||||
|
*/
|
||||||
|
public void setIssuer(String issuer) {
|
||||||
|
this.issuer = issuer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Secret setter.
|
||||||
|
*
|
||||||
|
* @return secret
|
||||||
|
*/
|
||||||
|
public String getSecret() {
|
||||||
|
return secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Secret setter.
|
||||||
|
*
|
||||||
|
* @param secret the secret
|
||||||
|
*/
|
||||||
|
public void setSecret(String secret) {
|
||||||
|
this.secret = secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private key getter.
|
||||||
|
*
|
||||||
|
* @return private key
|
||||||
|
*/
|
||||||
|
public String getPrivateKey() {
|
||||||
|
return privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private key setter.
|
||||||
|
*
|
||||||
|
* @param privateKey private key
|
||||||
|
*/
|
||||||
|
public void setPrivateKey(String privateKey) {
|
||||||
|
this.privateKey = privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public key getter.
|
||||||
|
*
|
||||||
|
* @return public key
|
||||||
|
*/
|
||||||
|
public String getPublicKey() {
|
||||||
|
return publicKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public key setter.
|
||||||
|
*
|
||||||
|
* @param publicKey public key
|
||||||
|
*/
|
||||||
|
public void setPublicKey(String publicKey) {
|
||||||
|
this.publicKey = publicKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
-35
@@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2024 OnixByte.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
*
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The "cn.org.codecrafters.simplejwt.autoconfiguration.properties" package
|
|
||||||
* contains configuration properties classes used for Simple JWT library
|
|
||||||
* autoconfiguration. These classes define the properties that can be
|
|
||||||
* configured in the application's properties file (e.g.,
|
|
||||||
* application.properties) to customize the behavior and settings of the Simple
|
|
||||||
* JWT library.
|
|
||||||
* <p>
|
|
||||||
* Developers can customize the JWT algorithm, issuer, and secret by setting
|
|
||||||
* the corresponding properties in the application's properties file with the
|
|
||||||
* prefix "code-crafters.simple-jwt". The SimpleJwtAutoConfiguration class
|
|
||||||
* reads these properties and uses them to create the {@link
|
|
||||||
* com.onixbyte.simplejwt.TokenResolver} bean with the desired
|
|
||||||
* configuration.
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
package com.onixbyte.simplejwt.autoconfiguration.properties;
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!--
|
<!--
|
||||||
~ Copyright (C) 2023-2024 OnixByte.
|
~ Copyright (C) 2024-2025 OnixByte.
|
||||||
~
|
~
|
||||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
~ you may not use this file except in compliance with the License.
|
~ you may not use this file except in compliance with the License.
|
||||||
|
|||||||
Reference in New Issue
Block a user