-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from xianzhi-group/main
release 1.0.0
- Loading branch information
Showing
27 changed files
with
4,857 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,4 @@ | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
replay_pid* | ||
*/target | ||
*/*/target | ||
../.idea | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.xianzhi</groupId> | ||
<artifactId>xianzhi-dependencies</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<name>XianZhi common</name> | ||
<description>Some modules of public code or tool classes</description> | ||
<url>https://github.com/xianzhi-group/xianzhi-dependencies</url> | ||
<inceptionYear>2024</inceptionYear> | ||
<licenses> | ||
<license> | ||
<name>Apache License, Version 2.0</name> | ||
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
<developers> | ||
<developer> | ||
<id>Ethan Wang</id> | ||
<name>Ethan Wang</name> | ||
<email>[email protected]</email> | ||
<roles> | ||
<role>Developer</role> | ||
<role>Tech Leader</role> | ||
</roles> | ||
</developer> | ||
</developers> | ||
<scm> | ||
<connection>scm:git:git://github.com/xianzhi-group/xianzhi-dependencies</connection> | ||
<developerConnection>scm:git:git://github.com/xianzhi-group/xianzhi-dependencies</developerConnection> | ||
<url>https://github.com/xianzhi-group/xianzhi-dependencies</url> | ||
</scm> | ||
<organization> | ||
<name>XianZhi Group</name> | ||
<url>https://github.com/xianzhi-group</url> | ||
</organization> | ||
<issueManagement> | ||
<system>GitHub Issues</system> | ||
<url>https://github.com/xianzhi-group/xianzhi-dependencies/issues</url> | ||
</issueManagement> | ||
|
||
<groupId>io.xianzhi.common</groupId> | ||
<artifactId>xianzhi-common</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.fastjson2</groupId> | ||
<artifactId>fastjson2</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.fastjson2</groupId> | ||
<artifactId>fastjson2-extension</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.fastjson2</groupId> | ||
<artifactId>fastjson2-extension-spring6</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.hutool</groupId> | ||
<artifactId>hutool-all</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
120 changes: 120 additions & 0 deletions
120
xianzhi-common/src/main/java/io/xianzhi/common/code/CommonCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (c) 2023-2024 XianZhi Group All Rights | ||
* | ||
* 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 io.xianzhi.common.code; | ||
|
||
import io.xianzhi.common.result.Result; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
/** | ||
* Define some public custom response information<br> | ||
* | ||
* @author Ethan Wang | ||
* @since 1.0.0 | ||
*/ | ||
@Getter | ||
@AllArgsConstructor | ||
public enum CommonCode implements Result { | ||
/** | ||
* Operation successful | ||
*/ | ||
SUCCESS("200", true, "success"), | ||
/** | ||
* Operation failed, and the reason for the failure is foreseeable | ||
*/ | ||
FAIL("500", false, "fail"), | ||
/** | ||
* Operation failed, unforeseeable error | ||
*/ | ||
ERROR("-1", false, "error"), | ||
/** | ||
* unauthorized | ||
*/ | ||
UNAUTHORIZED("401", false, "unauthorized"), | ||
/** | ||
* forbidden | ||
*/ | ||
FORBIDDEN("403", false, "forbidden"), | ||
/** | ||
* parameter check fail | ||
*/ | ||
PARAMETER_CHECK_FAILED("001", false, "parameter.check.failed"), | ||
/** | ||
* http media type not supported exception | ||
*/ | ||
HTTP_MEDIA_TYPE_NOT_SUPPORTED_EXCEPTION("002", false, "http.media.type.not.supported"), | ||
/** | ||
* missing servlet request parameter exception | ||
*/ | ||
MISSING_SERVLET_REQUEST_PARAMETER_EXCEPTION("003", false, "missing.servlet.request.parameter"), | ||
/** | ||
* http request method not supported exception | ||
*/ | ||
HTTP_REQUEST_METHOD_NOT_SUPPORTED_EXCEPTION("004", false, "http.request.method.not.supported"), | ||
/** | ||
* http message not readable exception | ||
*/ | ||
HTTP_MESSAGE_NOT_READABLE_EXCEPTION("005", false, "http.message.not.readable"), | ||
|
||
; | ||
|
||
|
||
/** | ||
* custom response status code | ||
*/ | ||
private final String code; | ||
/** | ||
* whether the operation was successful or not | ||
*/ | ||
private final boolean success; | ||
/** | ||
* custom prompt information | ||
*/ | ||
private final String message; | ||
|
||
|
||
/** | ||
* This method returns custom response status codes, | ||
* non HTTP status codes, or other response status codes | ||
* | ||
* @return custom response status code | ||
*/ | ||
@Override | ||
public String code() { | ||
return this.code; | ||
} | ||
|
||
/** | ||
* This method returns whether the operation was successful or not | ||
* | ||
* @return whether the operation was successful or not | ||
*/ | ||
@Override | ||
public boolean success() { | ||
return this.success; | ||
} | ||
|
||
/** | ||
* This method returns custom prompt information | ||
* | ||
* @return custom prompt information | ||
*/ | ||
@Override | ||
public String message() { | ||
return this.message; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
xianzhi-common/src/main/java/io/xianzhi/common/context/UserBO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2023-2024 XianZhi Group All Rights | ||
* | ||
* 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 io.xianzhi.common.context; | ||
|
||
/** | ||
* Context user information<br> | ||
* | ||
* @author Ethan Wang | ||
* @since 1.0.0 | ||
*/ | ||
public interface UserBO { | ||
|
||
/** | ||
* Get User id | ||
* | ||
* @return user id | ||
*/ | ||
String getUserId(); | ||
} |
54 changes: 54 additions & 0 deletions
54
xianzhi-common/src/main/java/io/xianzhi/common/context/UserContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2023-2024 XianZhi Group All Rights | ||
* | ||
* 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 io.xianzhi.common.context; | ||
|
||
import io.xianzhi.common.code.CommonCode; | ||
import io.xianzhi.common.exception.BizException; | ||
|
||
/** | ||
* User Information Context<br> | ||
* | ||
* @author Ethan Wang | ||
* @since 1.0.0 | ||
*/ | ||
public class UserContext { | ||
|
||
/** | ||
* User Information Context | ||
*/ | ||
private static final ThreadLocal<UserBO> USER_BO_THREAD_LOCAL = new InheritableThreadLocal<>(); | ||
|
||
|
||
/** | ||
* Get the current user ID | ||
* | ||
* @return current user ID | ||
*/ | ||
public static String getCurrentUserId() { | ||
UserBO userBO = USER_BO_THREAD_LOCAL.get(); | ||
if (null == userBO) { | ||
throw new BizException(CommonCode.UNAUTHORIZED); | ||
} | ||
return userBO.getUserId(); | ||
} | ||
|
||
|
||
public static void set(UserBO userBO) { | ||
USER_BO_THREAD_LOCAL.set(userBO); | ||
} | ||
} | ||
|
65 changes: 65 additions & 0 deletions
65
xianzhi-common/src/main/java/io/xianzhi/common/exception/BizException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2023-2024 XianZhi Group All Rights | ||
* | ||
* 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 io.xianzhi.common.exception; | ||
|
||
|
||
import io.xianzhi.common.result.ResponseResult; | ||
import io.xianzhi.common.result.Result; | ||
import lombok.Getter; | ||
import org.springframework.context.i18n.LocaleContextHolder; | ||
|
||
/** | ||
* Project custom business exception. Usually a foreseeable anomaly.<br> | ||
* | ||
* @author Ethan Wang | ||
* @since 1.0.0 2024-02-29 13:28 | ||
*/ | ||
@Getter | ||
public class BizException extends RuntimeException { | ||
|
||
/** | ||
* Corresponding response information can be obtained in case of abnormalities | ||
*/ | ||
private final transient Result result; | ||
|
||
|
||
/** | ||
* Constructs a new runtime exception with {@code null} as its | ||
* detail message. The cause is not initialized, and may subsequently be | ||
* initialized by a call to {@link #initCause}. | ||
*/ | ||
public BizException(Result result) { | ||
super(result.message()); | ||
this.result = new Result() { | ||
@Override | ||
public String code() { | ||
return result.code(); | ||
} | ||
|
||
@Override | ||
public boolean success() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return ResponseResult.messageSource.getMessage(result.message(), null, LocaleContextHolder.getLocale()); | ||
} | ||
}; | ||
|
||
} | ||
} |
Oops, something went wrong.