mirror of
https://github.com/yanlongqi/jhinno-openapi-java-sdk.git
synced 2026-03-22 06:15:10 +08:00
Compare commits
26 Commits
release-1.
...
release-2.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
753a40a6d0 | ||
|
|
0c9e98247c | ||
|
|
d134b8dc70 | ||
|
|
3686d17f9d | ||
|
|
75c7e02253 | ||
|
|
8ed0c93867 | ||
|
|
79a73fed6b | ||
|
|
1ddd65e34e | ||
|
|
89e858cc39 | ||
|
|
9bc5138a18 | ||
|
|
688617b530 | ||
|
|
56b9683cf3 | ||
|
|
0ac882f645 | ||
|
|
40df7537d1 | ||
|
|
94599f04f0 | ||
|
|
646fb869dc | ||
|
|
1dafc6dac8 | ||
|
|
193e7b4fc2 | ||
|
|
ec7d38f9dc | ||
|
|
dda983c9e8 | ||
|
|
cb765320ab | ||
|
|
2ab5c20fb5 | ||
|
|
51c068c28d | ||
|
|
f3cb13703a | ||
|
|
78ff3feb90 | ||
|
|
1ff758a31a |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -4,10 +4,7 @@ target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
/.idea/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
8
.idea/.gitignore
generated
vendored
8
.idea/.gitignore
generated
vendored
@@ -1,8 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
6
.idea/JavaSceneConfigState.xml
generated
6
.idea/JavaSceneConfigState.xml
generated
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SmartInputSourceJavaSceneConfigState">
|
||||
<option name="customChineseScenes" value="{"capsLockState":false,"code":";ArgsException(errorMessage);setUserNameCn(userNameCn)","enable":true,"languageType":"CHINESE","name":"自定义中文切换","tip":""}" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/encodings.xml
generated
8
.idea/encodings.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
<file url="PROJECT" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/inspectionProfiles/Project_Default.xml
generated
8
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -1,8 +0,0 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="JavadocDeclaration" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ADDITIONAL_TAGS" value="date" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
13
.idea/misc.xml
generated
13
.idea/misc.xml
generated
@@ -1,13 +0,0 @@
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
124
.idea/uiDesigner.xml
generated
124
.idea/uiDesigner.xml
generated
@@ -1,124 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
258
README.md
258
README.md
@@ -1,27 +1,32 @@
|
||||
# Jhinno OpenAPI SDK for Java
|
||||
# 1. Jhinno OpenAPI SDK for Java
|
||||
|
||||
针对Java的景行API SDK使Java开发人员能够轻松使用景行API接口。您可以在几分钟内开始通过Maven或jar文件使用它。
|
||||
|
||||
- [仓库地址:https://github.com/yanlongqi/jhinno-openapi-java-sdk](https://github.com/yanlongqi/jhinno-openapi-java-sdk)
|
||||
- [开发文档:https://jhinno-sdk-doc.yuchat.top/apidocs](https://jhinno-sdk-doc.yuchat.top/apidocs)
|
||||
- [最新jar包:jhinno-openapi-java-sdk-1.0.0.jar](https://jhinno-jenkins.yuchat.top/job/jhinno-openapi-java-sdk/lastSuccessfulBuild/artifact/target/jhinno-openapi-java-sdk-1.0.0.jar)
|
||||
- [最新源码包:jhinno-openapi-java-sdk-1.0.0-sources.jar](https://jhinno-jenkins.yuchat.top/job/jhinno-openapi-java-sdk/lastSuccessfulBuild/artifact/target/jhinno-openapi-java-sdk-1.0.0-sources.jar)
|
||||
- [jar包下载:https://github.com/yanlongqi/jhinno-openapi-java-sdk/releases](https://github.com/yanlongqi/jhinno-openapi-java-sdk/releases)
|
||||
|
||||
## 必要条件
|
||||
## 1.1 必要条件
|
||||
|
||||
- Java 1.8 or later
|
||||
- Maven
|
||||
|
||||
## 支持的Appform的版本
|
||||
## 1.2 支持的Appform的版本
|
||||
|
||||
1. JH_Appform_6.0_Release
|
||||
2. JH_Appform_6.0_SP1_Release
|
||||
3. JH_Appform_6.1_Release
|
||||
3. JH_Appform_6.2_Release(6.2以前用1.x.x版本)
|
||||
|
||||
## 安装
|
||||
# 2. 快速开始
|
||||
|
||||
建议在您的项目中使用Jhinno OpenAPI SDK for Java的方法是从Maven中使用它。导入方法如下:
|
||||
## 2.1 SpringBoot
|
||||
|
||||
方法一:通过Maven仓库安装
|
||||
### 2.1.1 安装
|
||||
|
||||
建议在您的项目中使用Jhinno OpenAPI SDK for Java的方法是从Maven中使用它。:
|
||||
|
||||
#### 方法一:通过Maven仓库安装
|
||||
|
||||
> 添加仓库地址(改方案为备用方案,后续会上传至Maven中央仓库,目前不影响正常使用)
|
||||
|
||||
@@ -36,21 +41,120 @@
|
||||
</repositories>
|
||||
```
|
||||
|
||||
添加依赖
|
||||
#### 方法二:通过命令将jar包导入本地Maven仓库
|
||||
|
||||
```shell
|
||||
|
||||
# SDK Client的jar包的导入,其中<path-to-dir>为jar的路径,<path-to-dir>为jar包路径
|
||||
mvn org.apache.maven.plugins:maven-install-plugin:3.1.1:install-file -Dfile="<path-to-dir>/jhinno-openapi-java-sdk-x.x.x.jar" -Dsources="<path-to-dir>/jhinno-openapi-java-sdk-x.x.x-sources.jar"
|
||||
# 例如
|
||||
mvn org.apache.maven.plugins:maven-install-plugin:3.1.1:install-file -Dfile="E:/下载/jhinno-openapi-java-sdk-2.0.0.jar" -Dsources="E:/下载/jhinno-openapi-java-sdk-2.0.0-sources.jar"
|
||||
|
||||
# SDK SpringBoot Starter的jar包的导入,其中<path-to-dir>为jar的路径,<path-to-dir>为jar包路径
|
||||
mvn org.apache.maven.plugins:maven-install-plugin:3.1.1:install-file -Dfile="<path-to-dir>/jhinno-openapi-sdk-spring-boot-starter-x.x.x.jar" -Dsources="<path-to-dir>/jhinno-openapi-sdk-spring-boot-starter-x.x.x-sources.jar"
|
||||
# 例如
|
||||
mvn org.apache.maven.plugins:maven-install-plugin:3.1.1:install-file -Dfile="E:/下载/jhinno-openapi-sdk-spring-boot-starter-2.0.0.jar" -Dsources="E:/下载/jhinno-openapi-sdk-spring-boot-starter-2.0.0-sources.jar"
|
||||
```
|
||||
|
||||
> 注:
|
||||
> - jhinno-openapi-java-sdk-x.x.x.jar 为SDK的jar包。
|
||||
> - jhinno-openapi-java-sdk-x.x.x-sources.jar 为源码包,添加可方便查看SDK代码的注释。
|
||||
> - jhinno-openapi-sdk-spring-boot-starter-x.x.x.jar 为SDK的spring-boot-starter的jar包。
|
||||
> - jhinno-openapi-sdk-spring-boot-starter-x.x.x-sources.jar 为SDK的spring-boot-starter的源码包。
|
||||
|
||||
### 2.1.2 引入`jhinno-openapi-sdk-spring-boot-starter`坐标
|
||||
|
||||
```xml
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jhinno</groupId>
|
||||
<artifactId>jhinno-openapi-java-sdk</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<artifactId>jhinno-openapi-sdk-spring-boot-starter</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
方法二:通过命令将jar包导入本地Maven仓库
|
||||
### 2.1.3 配置
|
||||
|
||||
在SpringBoot的`application.properties`或`application.yml`里面配置
|
||||
|
||||
#### 2.1.3.1 application.yaml
|
||||
|
||||
```yaml
|
||||
jhinno:
|
||||
openapi:
|
||||
server-url: https://172.17.0.5/appform
|
||||
access-key: xxxxx
|
||||
access-key-secret: xxxx
|
||||
auth-type: access_secret_mode
|
||||
```
|
||||
|
||||
#### 2.1.3.2 application.properties
|
||||
|
||||
```properties
|
||||
jhinno.openapi.server-url=https://{appform服务器的地址}/appform
|
||||
jhinno.openapi.access-key=xxxxx
|
||||
jhinno.openapi.access-key-secret=xxxx
|
||||
jhinno.openapi.auth-type=access_secret_mode
|
||||
|
||||
```
|
||||
|
||||
> 注:
|
||||
> - 其中`jhinno.openapi.server-url`为景行接口服务的BaseUrl;
|
||||
> - `auth-type` 认证类型,`token_mode`(Token认证) 和 `access_secret_mode`(AccessKey认证);Appform Release 6.2 `token_mode`作为过渡,将会弃用;
|
||||
> - `jhinno.openapi.used-server-time`是否获取服务器时间来请求token,关闭可提高获取token的时间,但打开有可能因为服务器时间不准确而导致token获取失败的问题。
|
||||
> - `access-key` 和 `access-key-secret` 作为访问接口的凭证,需要提供集成商名称、系统名称、负责人姓名、负责电话电话信息申请。
|
||||
> - 更多配置见`com.jhinno.sdk.openapi.autoconfigure.JHOpenapiProperties`源码。
|
||||
|
||||
### 2.1.4 使用
|
||||
|
||||
在`com.jhinno.sdk.openapi.api`包下面对应`app`、`data`、`file`、`job`、`organization`这几个子包,分别代表景行`Appform`
|
||||
的应用、作业数据、文件、作业操作、组织等接口资源。开发者只需要使用接口的执行器(`JHxxxApiExecution`
|
||||
)即可使用资源,如:`JHFileApiExecution`
|
||||
|
||||
```java
|
||||
public class DemoUserSDK {
|
||||
|
||||
/**
|
||||
* 注入要调用的执行器
|
||||
*/
|
||||
@Autowired
|
||||
private JHFileApiExecution fileApiExecution;
|
||||
|
||||
void contextLoads() {
|
||||
|
||||
// 调用执行其中想要调用的方法
|
||||
List<FileInfo> list = fileApiExecution.getFileList("jhadmin", "$HOME");
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2.2 Spring
|
||||
|
||||
### 2.2.1 安装
|
||||
|
||||
建议在您的项目中使用Jhinno OpenAPI SDK for Java的方法是从Maven中使用它。:
|
||||
|
||||
#### 方法一:通过Maven仓库安装
|
||||
|
||||
> 添加仓库地址(改方案为备用方案,后续会上传至Maven中央仓库,目前不影响正常使用)
|
||||
|
||||
```xml
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jhinno-nexus</id>
|
||||
<name>jhinno-nexus</name>
|
||||
<url>https://jhinno-nexus.yuchat.top/repository/maven-releases</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
```
|
||||
|
||||
#### 方法二:通过命令将jar包导入本地Maven仓库
|
||||
|
||||
```shell
|
||||
# 其中<path-to-dir>为jar的路径,<path-to-dir>为jar包路径
|
||||
|
||||
# SDK Client的jar包的导入,其中<path-to-dir>为jar的路径,<path-to-dir>为jar包路径
|
||||
mvn org.apache.maven.plugins:maven-install-plugin:3.1.1:install-file -Dfile="<path-to-dir>/jhinno-openapi-java-sdk-1.0.0.jar" -Dsources="<path-to-dir>/jhinno-openapi-java-sdk-1.0.0-sources.jar"
|
||||
# 例如
|
||||
mvn org.apache.maven.plugins:maven-install-plugin:3.1.1:install-file -Dfile="E:/下载/jhinno-openapi-java-sdk-1.0.0.jar" -Dsources="E:/下载/jhinno-openapi-java-sdk-1.0.0-sources.jar"
|
||||
@@ -60,44 +164,91 @@ mvn org.apache.maven.plugins:maven-install-plugin:3.1.1:install-file -Dfile="E:/
|
||||
> - jhinno-openapi-java-sdk-x.x.x.jar 为SDK的jar包。
|
||||
> - jhinno-openapi-java-sdk-x.x.x-sources.jar 为源码包,添加可方便查看SDK代码的注释。
|
||||
|
||||
然后在`pom.xml`添加
|
||||
### 2.2.2 引入`jhinno-openapi-java-sdk`坐标
|
||||
|
||||
```xml
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jhinno</groupId>
|
||||
<artifactId>jhinno-openapi-java-sdk</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
方法三:直接在`pom.xml`引用jar(改方法可能会出现一些莫名其妙的问题,所以不建议使用)
|
||||
### 2.2.3 配置
|
||||
|
||||
spring.xml添加以下内容
|
||||
|
||||
```xml
|
||||
<!-- 其中{path-to-file}为jar的位置 -->
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<beans>
|
||||
<bean id="apiClient" class="com.jhinno.sdk.openapi.client.JHApiClient" init-method="initDefaultApiClient">
|
||||
<constructor-arg value="https://172.17.0.5/appform"/>
|
||||
</bean>
|
||||
|
||||
<bean id="appApiExecution" class="com.jhinno.sdk.openapi.api.app.JHAppApiExecution">
|
||||
<constructor-arg ref="apiClient"/>
|
||||
</bean>
|
||||
<bean id="dataApiExecution" class="com.jhinno.sdk.openapi.api.data.JHDataApiExecution">
|
||||
<constructor-arg ref="apiClient"/>
|
||||
</bean>
|
||||
<bean id="fileApiExecution" class="com.jhinno.sdk.openapi.api.file.JHFileApiExecution">
|
||||
<constructor-arg ref="apiClient"/>
|
||||
</bean>
|
||||
<bean id="jhJobApiExecution" class="com.jhinno.sdk.openapi.api.job.JHJobApiExecution">
|
||||
<constructor-arg ref="apiClient"/>
|
||||
</bean>
|
||||
<bean id="departmentApiExecution" class="com.jhinno.sdk.openapi.api.organization.JHDepartmentApiExecution">
|
||||
<constructor-arg ref="apiClient"/>
|
||||
</bean>
|
||||
<bean id="userApiExecution" class="com.jhinno.sdk.openapi.api.organization.JHUserApiExecution">
|
||||
<constructor-arg ref="apiClient"/>
|
||||
</bean>
|
||||
</beans>
|
||||
```
|
||||
|
||||
### 2.2.4 使用
|
||||
|
||||
在`com.jhinno.sdk.openapi.api`包下面对应`app`、`data`、`file`、`job`、`organization`这几个子包,分别代表景行`Appform`
|
||||
的应用、作业数据、文件、作业操作、组织等接口资源。开发者只需要使用接口的执行器(`JHxxxApiExecution`
|
||||
)即可使用资源,如:`JHAppApiExecution`
|
||||
|
||||
```java
|
||||
public class DemoUserSDK {
|
||||
|
||||
/**
|
||||
* 注入要调用的执行器
|
||||
*/
|
||||
@Autowired
|
||||
private JHFileApiExecution fileApiExecution;
|
||||
|
||||
void contextLoads() {
|
||||
|
||||
// 调用执行其中想要调用的方法
|
||||
List<FileInfo> list = fileApiExecution.getFileList("jhadmin", "$HOME");
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2.3 普通java项目
|
||||
|
||||
### 2.3.1 安装
|
||||
|
||||
- 同 2.2.1 安装一样
|
||||
|
||||
### 2.3.2 引入`jhinno-openapi-java-sdk`坐标
|
||||
|
||||
```xml
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jhinno</groupId>
|
||||
<artifactId>jhinno-openapi-java-sdk</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>{path-to-file}</systemPath>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
例如
|
||||
|
||||
```xml
|
||||
<!-- 其中{path-to-file}为jar的位置 -->
|
||||
<dependency>
|
||||
<groupId>com.jhinno</groupId>
|
||||
<artifactId>jhinno-openapi-java-sdk</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>D:/jar/jhinno-openapi-java-sdk-1.0.0.jar</systemPath>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
## 使用
|
||||
### 2.3.3 使用
|
||||
|
||||
在`com.jhinno.sdk.openapi.api`包下面对应`app`、`data`、`file`、`job`、`organization`这几个子包,分别代表景行`Appform`
|
||||
的应用、作业数据、文件、作业操作、组织等接口资源。开发者只需要使用接口的执行器(`JHxxxApiExecution`
|
||||
@@ -115,16 +266,39 @@ public class DemoUserSDK {
|
||||
|
||||
/**
|
||||
* JHApiClient 是一个HTTP连接池,开发者需要复用
|
||||
* 其中https://192.168.87.25/appform为景行API服务的地址
|
||||
* 其中https://172.17.0.5/appform为景行API服务的地址
|
||||
* 注意: JHApiClient为内置的http连接池,系统只需要初始化一份即可(单例调用)。
|
||||
*/
|
||||
private static final JHApiClient client = JHApiClient.build("https://192.168.87.25/appform");
|
||||
public static final JHApiClient client = new JHApiClient("https://172.17.0.5/appform");
|
||||
|
||||
public static final Map<Class<? extends JHApiExecution>, JHApiExecution> jhApiClientMap = new HashMap<>();
|
||||
|
||||
public static final String ACCESS_KEY = "3f03747f147942bd8debd81b6c9c6a80";
|
||||
|
||||
public static final String ACCESS_KEY_SECRET = "e0681859b91c499eb1d2c8e09cea3242";
|
||||
|
||||
static {
|
||||
client.initDefaultApiClient();
|
||||
jhApiClientMap.put(JHAppApiExecution.class, new JHAppApiExecution());
|
||||
jhApiClientMap.put(JHDataApiExecution.class, new JHDataApiExecution());
|
||||
jhApiClientMap.put(JHFileApiExecution.class, new JHFileApiExecution());
|
||||
jhApiClientMap.put(JHJobApiExecution.class, new JHJobApiExecution());
|
||||
jhApiClientMap.put(JHDepartmentApiExecution.class, new JHDepartmentApiExecution());
|
||||
jhApiClientMap.put(JHUserApiExecution.class, new JHUserApiExecution());
|
||||
|
||||
jhApiClientMap.forEach((k, v) -> {
|
||||
v.setJhApiClient(client);
|
||||
v.setAuthType(AuthType.ACCESS_SECRET_MODE);
|
||||
v.setAccessKey(ACCESS_KEY);
|
||||
v.setAccessKeySecret(ACCESS_KEY_SECRET);
|
||||
v.setUsedServerTime(true);
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// 初始化一个调用调用景行会话服务接口执行器
|
||||
JHAppApiExecution jhAppApiExecution = new JHAppApiExecution(client);
|
||||
JHAppApiExecution jhAppApiExecution = (JHAppApiExecution) jhApiClientMap.get(JHAppApiExecution.class);
|
||||
|
||||
// 调用启动会话的接口
|
||||
AppStartedInfo appStartedInfo = jhAppApiExecution.desktopStart("jhadmin", "linux_desktop", new AppStartRequest());
|
||||
@@ -137,7 +311,7 @@ public class DemoUserSDK {
|
||||
|
||||
```
|
||||
|
||||
## 支持SDK的扩展
|
||||
# 3. 支持SDK的扩展
|
||||
|
||||
如果是基于景行定制的接口,本SDK没有包含这些方法,因此您可以基于`JHApiExecution`快速进行扩展,具体的扩展步骤如下:
|
||||
|
||||
@@ -170,7 +344,7 @@ public class JHAppApiExecution extends JHApiExecution {
|
||||
|
||||
```
|
||||
|
||||
## 构建
|
||||
# 4. 构建
|
||||
|
||||
一旦您检出代码,就可以使用Maven构建它。使用以下命令进行构建:
|
||||
|
||||
@@ -178,17 +352,17 @@ public class JHAppApiExecution extends JHApiExecution {
|
||||
mvn clean package -DskipTests -P product
|
||||
```
|
||||
|
||||
## 代码贡献
|
||||
# 5. 代码贡献
|
||||
|
||||
- 代码必须格式化,使用IDEA自带的格式即可;
|
||||
- 请求路径必须放在一个单独的const类中统一维护,具体可参考现有代码提交;
|
||||
- 封装新的接口是需要继承`JHApiExecution`,调用父类的辅助方法辅助封装;
|
||||
|
||||
## 作者
|
||||
# 6. 作者
|
||||
|
||||
- [yanlongqi](https://github.com/yanlongqi)
|
||||
|
||||
## 支持
|
||||
# 7. 支持
|
||||
|
||||
- 电话(同微信):18794888087
|
||||
- 邮箱:lqyan@jhinno.com
|
||||
164
jhinno-openapi-java-sdk/pom.xml
Normal file
164
jhinno-openapi-java-sdk/pom.xml
Normal file
@@ -0,0 +1,164 @@
|
||||
<?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>
|
||||
|
||||
<artifactId>jhinno-openapi-java-sdk</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>Jhinno OpenAPI SDK for Java</name>
|
||||
<description>The Jhinno OpenAPI SDK for Java used for accessing Jhinno OpenApi Service</description>
|
||||
<url>http://jhinno.com</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.jhinno</groupId>
|
||||
<artifactId>jhinno-openapi-java-sdk-parent</artifactId>
|
||||
<version>2.0.2</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>jhinno-releases</id>
|
||||
<url>http://192.168.87.22:8081/repository/maven-releases</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok-maven-plugin</artifactId>
|
||||
<version>1.18.0.0</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<configuration>
|
||||
<sourcepath>target/generated-sources/delombok</sourcepath>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
<docencoding>${project.build.sourceEncoding}</docencoding>
|
||||
<charset>${project.build.sourceEncoding}</charset>
|
||||
<doclint>none</doclint>
|
||||
<tags>
|
||||
<tag>
|
||||
<name>date</name>
|
||||
<placement>X</placement>
|
||||
</tag>
|
||||
</tags>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>lqyan</id>
|
||||
<name>lqyan</name>
|
||||
<email>lqyan@jhinno.com</email>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.jhinno.sdk.openapi;
|
||||
|
||||
/**
|
||||
* 目前Appform的认证类型有两个,Token模式和AK/SK的模式。
|
||||
* 推荐使用AK/SK的模式,Token模式后续将被弃用
|
||||
*/
|
||||
public enum AuthType {
|
||||
|
||||
/**
|
||||
* Token模式
|
||||
*/
|
||||
TOKEN_MODE,
|
||||
|
||||
/**
|
||||
* AK/SK模式
|
||||
*/
|
||||
ACCESS_SECRET_MODE,
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.jhinno.sdk.openapi;
|
||||
|
||||
import org.omg.CosNaming.NamingContextPackage.NotFound;
|
||||
|
||||
/**
|
||||
* 客户端错误相关常亮的定义
|
||||
*
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.jhinno.sdk.openapi;
|
||||
|
||||
import com.fasterxml.jackson.databind.util.StdDateFormat;
|
||||
|
||||
import java.text.DateFormat;
|
||||
|
||||
/**
|
||||
* @author yanlongqi
|
||||
* @date 2024/1/31 10:17
|
||||
*/
|
||||
public class CommonConstant {
|
||||
|
||||
/**
|
||||
* 请求成功时的标识
|
||||
*/
|
||||
public static final String SUCCESS = "success";
|
||||
|
||||
/**
|
||||
* 请求失败时的标识
|
||||
*/
|
||||
public static final String FAILED = "failed";
|
||||
|
||||
/**
|
||||
* AES获取Token格式
|
||||
*/
|
||||
public static final String TokenUserFormat = "%s,%s";
|
||||
|
||||
|
||||
/**
|
||||
* 获取token时AES加密的默认key
|
||||
*/
|
||||
public static final String DEFAULT_AES_KEY = "jin5no@aqec8gtw6";
|
||||
|
||||
/**
|
||||
* 字符逗号
|
||||
*/
|
||||
public static final String NORMAL_CHARACTER_COMMA = ",";
|
||||
|
||||
/**
|
||||
* 签名key
|
||||
*/
|
||||
|
||||
public static final String SIGNATURE = "signature";
|
||||
|
||||
/**
|
||||
* 集成方Key
|
||||
*/
|
||||
public static final String ACCESS_KEY = "accessKey";
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
public static final String USERNAME = "username";
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
public static final String CURRENT_TIME_MILLIS = "currentTimeMillis";
|
||||
|
||||
|
||||
/**
|
||||
* TOKEN
|
||||
*/
|
||||
public static final String TOKEN = "token";
|
||||
|
||||
/**
|
||||
* 签名加密格式
|
||||
* <ul>
|
||||
* <li> accessKey </li>
|
||||
* <li> username </li>
|
||||
* <li> currentTimeMillis </li>
|
||||
* </ul>
|
||||
*/
|
||||
public static final String SIGNATURE_FORMAT = "#%s#%s#%s#";
|
||||
|
||||
/**
|
||||
* HmacSHA256 算法
|
||||
*/
|
||||
public static final String HMAC_SHA_256_ALGORITHM = "HmacSHA256";
|
||||
|
||||
/**
|
||||
* AES 算法
|
||||
*/
|
||||
public static final String AES_ALGORITHM = "AES";
|
||||
|
||||
/**
|
||||
* AES ECB Padding
|
||||
*/
|
||||
public static final String AES_ECB_PADDING = "AES/ECB/PKCS5Padding";
|
||||
|
||||
/**
|
||||
* 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss
|
||||
*/
|
||||
public static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
/**
|
||||
* 时间转换
|
||||
*/
|
||||
public static final DateFormat HTTP_DATETIME_FORMAT = new StdDateFormat();
|
||||
|
||||
|
||||
}
|
||||
@@ -1,17 +1,23 @@
|
||||
package com.jhinno.sdk.openapi.api;
|
||||
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import cn.hutool.http.ContentType;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
import com.jhinno.sdk.openapi.ServiceException;
|
||||
import com.jhinno.sdk.openapi.*;
|
||||
import com.jhinno.sdk.openapi.api.app.JHAppApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.auth.AuthPathConstant;
|
||||
import com.jhinno.sdk.openapi.client.DefaultHttpClientConfig;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.jhinno.sdk.openapi.api.app.AppPathConstant;
|
||||
import org.apache.http.entity.ContentType;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -33,22 +39,52 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* @date 2024/1/30 19:39
|
||||
* @see JHAppApiExecution
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class JHApiExecution {
|
||||
|
||||
|
||||
/**
|
||||
* JHApiClient实例
|
||||
*/
|
||||
public JHApiClient jhApiClient;
|
||||
private JHApiClient jhApiClient;
|
||||
|
||||
/**
|
||||
* token的超时时间
|
||||
*/
|
||||
private int tokenTimeout = CommonConstant.DEFAULT_TOKEN_EFFECTIVE_TIME;
|
||||
private int tokenTimeout = DefaultHttpClientConfig.DEFAULT_TOKEN_EFFECTIVE_TIME;
|
||||
|
||||
/**
|
||||
* token提前获取的时间
|
||||
*/
|
||||
private int tokenResidueTime = CommonConstant.DEFAULT_TOKEN_RESIDUE_TIME;
|
||||
private int tokenResidueTime = DefaultHttpClientConfig.DEFAULT_TOKEN_RESIDUE_TIME;
|
||||
|
||||
|
||||
/**
|
||||
* 是否使用服务器时间,开启可能会导致请求过慢,但是不会太慢,默认token会有缓存
|
||||
*/
|
||||
private boolean isUsedServerTime = DefaultHttpClientConfig.DEFAULT_IS_USED_SERVER_TIME;
|
||||
|
||||
/**
|
||||
* 是否强制获取用户的token,默认{@link DefaultHttpClientConfig#DEFAULT_IS_FORCE_GET_TOKEN},
|
||||
* 如果强制获取token,则每次请求都去获取token;
|
||||
*/
|
||||
private boolean isForceGetToken = DefaultHttpClientConfig.DEFAULT_IS_FORCE_GET_TOKEN;
|
||||
|
||||
/**
|
||||
* 接口请求的认证类型
|
||||
*/
|
||||
private AuthType authType = AuthType.ACCESS_SECRET_MODE;
|
||||
|
||||
/**
|
||||
* 访问密钥
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* 访问密钥密码
|
||||
*/
|
||||
private String accessKeySecret;
|
||||
|
||||
|
||||
/**
|
||||
@@ -60,47 +96,28 @@ public class JHApiExecution {
|
||||
this.jhApiClient = jhApiClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 用户令牌的缓存
|
||||
*/
|
||||
private static final Map<String, TokenInfo> TOKEN_INFO_MAP = new ConcurrentHashMap<>(20);
|
||||
|
||||
/**
|
||||
* 设置在JHApiClient实例的实例
|
||||
* 设置是否使用服务器时间
|
||||
*
|
||||
* @param jhApiClient 客户端实例
|
||||
* @param usedServerTime 是否使用服务器时间
|
||||
*/
|
||||
public void setJHApiClient(JHApiClient jhApiClient) {
|
||||
this.jhApiClient = jhApiClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置token超时的时间,单位:分钟
|
||||
*
|
||||
* @param tokenTimeout token的超时时间
|
||||
*/
|
||||
public void setTokenTimeout(int tokenTimeout) {
|
||||
this.tokenTimeout = tokenTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置提前获取token的时间,单位:分钟
|
||||
*
|
||||
* @param tokenResidueTime 提前获取token的时间
|
||||
*/
|
||||
public void setTokenResidueTime(int tokenResidueTime) {
|
||||
this.tokenResidueTime = tokenResidueTime;
|
||||
public void setUsedServerTime(boolean usedServerTime) {
|
||||
isUsedServerTime = usedServerTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户的Token
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param isForce 是否强制获取toke
|
||||
* @return 用户的token
|
||||
*/
|
||||
public String getToken(String username, boolean isForce) {
|
||||
public String getToken(String username) {
|
||||
if (StringUtils.isBlank(username)) {
|
||||
throw new ArgsException("用户名称不能为空!");
|
||||
}
|
||||
@@ -108,14 +125,23 @@ public class JHApiExecution {
|
||||
|
||||
// 防止因为服务器时间的问题二导致token不可用,可以通过此配置提前获取token
|
||||
int tokenEffectiveTime = (tokenTimeout - tokenResidueTime) * 60 * 1000;
|
||||
|
||||
// 如果是强制获取、用户令牌为空、用户令牌过期等,则获取令牌
|
||||
if (isForce || tokenInfo == null || System.currentTimeMillis() - tokenInfo.getCurrentTimestamp() < tokenEffectiveTime) {
|
||||
if (isForceGetToken || tokenInfo == null || System.currentTimeMillis() - tokenInfo.getCurrentTimestamp() > tokenEffectiveTime) {
|
||||
Map<String, Object> params = new HashMap<>(2);
|
||||
params.put("timeout", tokenTimeout);
|
||||
String currentTimeMillis = getCurrentTimeMillis();
|
||||
String beforeEncryption = String.format(CommonConstant.TokenUserFormat, username, currentTimeMillis);
|
||||
try {
|
||||
SecretKeySpec secretKey = new SecretKeySpec(CommonConstant.DEFAULT_AES_KEY.getBytes(StandardCharsets.UTF_8), CommonConstant.AES_ALGORITHM);
|
||||
Cipher cipher = Cipher.getInstance(CommonConstant.AES_ECB_PADDING);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||
byte[] encryptBytes = cipher.doFinal(beforeEncryption.getBytes(StandardCharsets.UTF_8));
|
||||
params.put("username", Base64.getEncoder().encodeToString(encryptBytes));
|
||||
} catch (Exception e) {
|
||||
throw new ClientException("AES加密失败,失败原因:" + e.getMessage(), e);
|
||||
}
|
||||
|
||||
AES aes = new AES(CommonConstant.DEFAULT_AES_KEY.getBytes());
|
||||
String base64 = aes.encryptBase64(String.format("%s,%s", username, System.currentTimeMillis()));
|
||||
params.put("username", base64);
|
||||
String url = JHApiClient.getUrl(AuthPathConstant.AUTH_TOKEN_PATH, params);
|
||||
Map<String, String> token = get(url, new TypeReference<ResponseResult<Map<String, String>>>() {
|
||||
});
|
||||
@@ -128,14 +154,15 @@ public class JHApiExecution {
|
||||
return tokenInfo.getToken();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户的Token,获取缓存,不强制获取
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return 用户的token
|
||||
* @return
|
||||
*/
|
||||
public String getToken(String username) {
|
||||
return getToken(username, false);
|
||||
public String getCurrentTimeMillis() {
|
||||
if (authType == AuthType.ACCESS_SECRET_MODE || !isUsedServerTime) {
|
||||
return String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
return jhApiClient.getAppformServerCurrentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
@@ -143,20 +170,57 @@ public class JHApiExecution {
|
||||
* 构建一个带token的请求头
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param isContentType 是否携带默认的Content-type,默认为{@link ContentType#JSON}
|
||||
* @param isContentType 是否携带默认的Content-type,默认为{@link ContentType#APPLICATION_JSON}
|
||||
* @return 请求头
|
||||
*/
|
||||
protected Map<String, String> getHeaders(String username, boolean isContentType) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
protected Map<String, Object> getHeaders(String username, boolean isContentType) {
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
// 默认请求json数据
|
||||
if (isContentType) {
|
||||
headers.put("Content-type", ContentType.JSON.getValue());
|
||||
headers.put("Content-type", ContentType.APPLICATION_JSON.getMimeType());
|
||||
}
|
||||
if (authType == AuthType.ACCESS_SECRET_MODE) {
|
||||
if (StringUtils.isBlank(accessKey)) {
|
||||
throw new ClientException("AccessKey不能为空");
|
||||
}
|
||||
if (StringUtils.isBlank(accessKeySecret)) {
|
||||
throw new ClientException("AccessKeySecret不能为空");
|
||||
}
|
||||
headers.put(CommonConstant.ACCESS_KEY, accessKey);
|
||||
|
||||
if (StringUtils.isBlank(username)) {
|
||||
username = StringUtils.EMPTY;
|
||||
}
|
||||
headers.put(CommonConstant.USERNAME, username);
|
||||
|
||||
String currentTimeMillis = getCurrentTimeMillis();
|
||||
headers.put(CommonConstant.CURRENT_TIME_MILLIS, currentTimeMillis);
|
||||
headers.put(CommonConstant.SIGNATURE, getsSignature(username, currentTimeMillis));
|
||||
} else if (authType == AuthType.TOKEN_MODE && StringUtils.isNotBlank(username)) {
|
||||
headers.put(CommonConstant.TOKEN, getToken(username));
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
headers.put("token", getToken(username));
|
||||
return headers;
|
||||
|
||||
|
||||
/**
|
||||
* 获取签名
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param currentTimeMillis 时间戳
|
||||
* @return 签名
|
||||
*/
|
||||
public String getsSignature(String username, String currentTimeMillis) {
|
||||
SecretKeySpec secretKey = new SecretKeySpec(accessKeySecret.getBytes(StandardCharsets.UTF_8), CommonConstant.HMAC_SHA_256_ALGORITHM);
|
||||
try {
|
||||
Mac mac = Mac.getInstance(CommonConstant.HMAC_SHA_256_ALGORITHM);
|
||||
mac.init(secretKey);
|
||||
String beforeSignature = String.format(CommonConstant.SIGNATURE_FORMAT, accessKey, username, currentTimeMillis);
|
||||
byte[] digest = mac.doFinal(beforeSignature.getBytes(StandardCharsets.UTF_8));
|
||||
return Hex.encodeHexString(digest);
|
||||
} catch (Exception e) {
|
||||
throw new ClientException("签名加密失败,失败信息:" + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +229,7 @@ public class JHApiExecution {
|
||||
* @param username 用户名
|
||||
* @return 请求头
|
||||
*/
|
||||
protected Map<String, String> getHeaders(String username) {
|
||||
protected Map<String, Object> getHeaders(String username) {
|
||||
return getHeaders(username, true);
|
||||
}
|
||||
|
||||
@@ -358,7 +422,7 @@ public class JHApiExecution {
|
||||
|
||||
|
||||
/**
|
||||
* 退出用户的登录,释放许可
|
||||
* 退出用户的登录,释放许可,当用户退出登录后,建议清除用户的token信息
|
||||
*
|
||||
* @param username 用户名
|
||||
*/
|
||||
@@ -91,4 +91,26 @@ public class AppPathConstant {
|
||||
* 获取应用链接URL
|
||||
*/
|
||||
public static final String APPS_GET_URL_PATH = "/ws/api/apps/{appName}/url";
|
||||
|
||||
|
||||
/**
|
||||
* WEB启动会话URL
|
||||
*/
|
||||
public static final String WEB_SESSION_URL_PATH = "/pageapi/apps/webclient/gui/{desktopId}";
|
||||
|
||||
|
||||
/**
|
||||
* 根据用途查询应用
|
||||
* <p/>
|
||||
* 该接口目前在Solutions下面维护,未经过产品的测试
|
||||
*/
|
||||
public static final String APP_USE_LABEL_PATH = "/ws/api/app/use_label/apps";
|
||||
|
||||
|
||||
/**
|
||||
* 根据文件后缀查询应用
|
||||
* <p/>
|
||||
* 该接口目前在Solutions下面维护,未经过产品的测试
|
||||
*/
|
||||
public static final String APPS_SUFFIXES_PATH = "/ws/api/apps/suffixes";
|
||||
}
|
||||
@@ -34,4 +34,9 @@ public class AppStartedInfo {
|
||||
* 作业id(有可能出现,但不是太明白)
|
||||
*/
|
||||
private String jobId;
|
||||
|
||||
/**
|
||||
* WEB 启动会话URL
|
||||
*/
|
||||
private String webSessionUrl;
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.jhinno.sdk.openapi.api.app;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 应用
|
||||
*/
|
||||
@Data
|
||||
public class AppstoreAppInfo {
|
||||
|
||||
/**
|
||||
* 应ID
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 应用名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 应用图标
|
||||
*/
|
||||
private String image;
|
||||
|
||||
/**
|
||||
* 应用类型
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 应用版本
|
||||
*/
|
||||
private String version;
|
||||
|
||||
/**
|
||||
* 应用状态
|
||||
*/
|
||||
private Boolean status;
|
||||
|
||||
/**
|
||||
* 是否 使用
|
||||
*/
|
||||
private Boolean isUsed;
|
||||
|
||||
/**
|
||||
* CPU使用
|
||||
*/
|
||||
private String cpuUsage;
|
||||
|
||||
/**
|
||||
* 空闲内存
|
||||
*/
|
||||
private String idleMem;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private String idleTime;
|
||||
|
||||
/**
|
||||
* 应用路径
|
||||
*/
|
||||
private String appPath;
|
||||
|
||||
/**
|
||||
* 工作路径
|
||||
*/
|
||||
private String appCwd;
|
||||
|
||||
/**
|
||||
* 发布时间
|
||||
*/
|
||||
private String appReleaseTime;
|
||||
|
||||
/**
|
||||
* 启动选项
|
||||
*/
|
||||
private String appStartOpt;
|
||||
|
||||
/**
|
||||
* 启动前缀
|
||||
*/
|
||||
private String appStartPrefix;
|
||||
|
||||
/**
|
||||
* 启动后缀
|
||||
*/
|
||||
private String appStartSuffix;
|
||||
|
||||
/**
|
||||
* 应用详情
|
||||
*/
|
||||
private String appDetail;
|
||||
|
||||
/**
|
||||
* 启动数量
|
||||
*/
|
||||
private String startNum;
|
||||
|
||||
/**
|
||||
* 用途英文名
|
||||
*/
|
||||
private String useLabelEnStr;
|
||||
|
||||
/**
|
||||
* 用途中文名
|
||||
*/
|
||||
private String useLabelCnStr;
|
||||
|
||||
/**
|
||||
* 应用系统
|
||||
*/
|
||||
private String appOs;
|
||||
|
||||
/**
|
||||
* 申请状态
|
||||
*/
|
||||
private String applyStatus;
|
||||
|
||||
}
|
||||
@@ -1,15 +1,18 @@
|
||||
package com.jhinno.sdk.openapi.api.app;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.AuthType;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
import com.jhinno.sdk.openapi.ServiceException;
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import com.jhinno.sdk.openapi.utils.CollectionUtil;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -20,6 +23,7 @@ import java.util.Map;
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/1 16:26
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class JHAppApiExecution extends JHApiExecution {
|
||||
|
||||
/**
|
||||
@@ -33,8 +37,10 @@ public class JHAppApiExecution extends JHApiExecution {
|
||||
|
||||
/**
|
||||
* 启动一个会话
|
||||
*
|
||||
* <h4>一、通过JHClient启动</h4>
|
||||
* <p>
|
||||
* 改方法返回一个重要的参数{@link AppStartedInfo#getJhappUrl()}(拉起景行客户端协议的URL)。
|
||||
* 方法返回一个重要的参数{@link AppStartedInfo#getJhappUrl()}(拉起景行客户端协议的URL)。
|
||||
* <p>
|
||||
* 测试:将该URL复制粘贴到浏览器的地址栏进行访问即可启动会话。
|
||||
*
|
||||
@@ -59,6 +65,16 @@ public class JHAppApiExecution extends JHApiExecution {
|
||||
* iframe.src = "{@link AppStartedInfo#getJhappUrl()}";
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* 注意:如果使用JHAppClient启动应用的,并且没有做浏览器端和服务器没有做时间同步,
|
||||
* 那么 {@link AppStartRequest#setCurrentTimestamp(String)} 参数必传,
|
||||
* 并使用js生产的时间,具体的参数见 {@link AppStartRequest#setCurrentTimestamp(String)}
|
||||
*
|
||||
* <h4>通过浏览器启动</h4>
|
||||
* <pre class="code">
|
||||
* window.open("{@link AppStartedInfo#getWebSessionUrl()}}")
|
||||
* </pre>
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param appId 应用拆
|
||||
* @param appStartRequest 启动参数
|
||||
@@ -71,7 +87,26 @@ public class JHAppApiExecution extends JHApiExecution {
|
||||
if (CollectionUtil.isEmpty(data)) {
|
||||
throw new ServiceException(path, 500, "获取到的会话信息为空");
|
||||
}
|
||||
return data.get(0);
|
||||
AppStartedInfo appStartedInfo = data.get(0);
|
||||
appStartedInfo.setWebSessionUrl(getWebSessionUrl(username, appStartedInfo.getDesktopId()));
|
||||
return appStartedInfo;
|
||||
}
|
||||
|
||||
public String getWebSessionUrl(String username, String desktopId) {
|
||||
String webSessionUrlPath = AppPathConstant.WEB_SESSION_URL_PATH.replace("{desktopId}", desktopId);
|
||||
String url = getJhApiClient().getUrl(webSessionUrlPath);
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
AuthType authType = getAuthType();
|
||||
if (authType == AuthType.TOKEN_MODE) {
|
||||
params.put(CommonConstant.TOKEN, getToken(username));
|
||||
} else if (authType == AuthType.ACCESS_SECRET_MODE) {
|
||||
params.put(CommonConstant.USERNAME, username);
|
||||
params.put(CommonConstant.ACCESS_KEY, getAccessKey());
|
||||
String currentTimeMillis = getCurrentTimeMillis();
|
||||
params.put(CommonConstant.CURRENT_TIME_MILLIS, currentTimeMillis);
|
||||
params.put(CommonConstant.SIGNATURE, getsSignature(username, currentTimeMillis));
|
||||
}
|
||||
return JHApiClient.getUrl(url, params);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -264,7 +299,10 @@ public class JHAppApiExecution extends JHApiExecution {
|
||||
if (CollectionUtil.isEmpty(list)) {
|
||||
throw new ServiceException(path, 500, "获取到的会话信息为空");
|
||||
}
|
||||
return list.get(0);
|
||||
|
||||
AppStartedInfo appStartedInfo = list.get(0);
|
||||
appStartedInfo.setWebSessionUrl(getWebSessionUrl(username, sessionId));
|
||||
return appStartedInfo;
|
||||
}
|
||||
|
||||
|
||||
@@ -356,7 +394,7 @@ public class JHAppApiExecution extends JHApiExecution {
|
||||
/**
|
||||
* 获取应用链接
|
||||
*
|
||||
* @param username 应户名
|
||||
* @param username 用户名
|
||||
* @param appName 应用名
|
||||
* @return 应用链接地址
|
||||
*/
|
||||
@@ -372,4 +410,61 @@ public class JHAppApiExecution extends JHApiExecution {
|
||||
}
|
||||
return apps.get(0).get("url");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件后缀查询应用
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param suffixes 文件后缀列表
|
||||
* @return 应用列表
|
||||
*/
|
||||
public List<AppstoreAppInfo> getAppInfoSuffixList(String username, String... suffixes) {
|
||||
return getAppInfoSuffixList(username, Arrays.asList(suffixes));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件后缀查询应用
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param suffixes 文件后缀列表
|
||||
* @return 应用列表
|
||||
*/
|
||||
public List<AppstoreAppInfo> getAppInfoSuffixList(String username, List<String> suffixes) {
|
||||
Map<String, Object> params = new HashMap<>(1);
|
||||
if (CollectionUtil.isNotEmpty(suffixes)) {
|
||||
params.put("suffixes", String.join(CommonConstant.NORMAL_CHARACTER_COMMA, suffixes));
|
||||
}
|
||||
String path = JHApiClient.getUrl(AppPathConstant.APPS_SUFFIXES_PATH, params);
|
||||
return get(path, username, new TypeReference<ResponseResult<List<AppstoreAppInfo>>>() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用途查询应用
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param labels 用途列表
|
||||
* @return 应用列表
|
||||
*/
|
||||
public List<UseLabelInfo> getUseLabelList(String username, String... labels) {
|
||||
return getUseLabelList(username, Arrays.asList(labels));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据用途查询应用
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param labels 用途列表
|
||||
* @return 应用列表
|
||||
*/
|
||||
public List<UseLabelInfo> getUseLabelList(String username, List<String> labels) {
|
||||
Map<String, Object> params = new HashMap<>(1);
|
||||
if (CollectionUtil.isNotEmpty(labels)) {
|
||||
params.put("use_labels", String.join(CommonConstant.NORMAL_CHARACTER_COMMA, labels));
|
||||
}
|
||||
String path = JHApiClient.getUrl(AppPathConstant.APP_USE_LABEL_PATH, params);
|
||||
return get(path, username, new TypeReference<ResponseResult<List<UseLabelInfo>>>() {
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.jhinno.sdk.openapi.api.app;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class UseLabelInfo {
|
||||
/**
|
||||
* 用途id
|
||||
*/
|
||||
private String useLabelId;
|
||||
|
||||
/**
|
||||
* 用途名称
|
||||
*/
|
||||
private String useLabelName;
|
||||
|
||||
/**
|
||||
* 用途图片
|
||||
*/
|
||||
private String useLabelIcon;
|
||||
|
||||
/**
|
||||
* 用途app列表
|
||||
*/
|
||||
private List<AppstoreAppInfo> apps;
|
||||
}
|
||||
@@ -24,5 +24,9 @@ public class AuthPathConstant {
|
||||
*/
|
||||
public static final String AUTH_REGISTER = "/ws/api/auth/register";
|
||||
|
||||
/**
|
||||
* 测试服务器是否可用
|
||||
*/
|
||||
public static final String PING = "/ws/api/ping";
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.jhinno.sdk.openapi.api.data;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
@@ -8,6 +7,8 @@ import com.jhinno.sdk.openapi.ServiceException;
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import com.jhinno.sdk.openapi.utils.CollectionUtil;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
@@ -21,6 +22,7 @@ import java.util.Map;
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/4 17:09
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class JHDataApiExecution extends JHApiExecution {
|
||||
|
||||
/**
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.jhinno.sdk.openapi.api.file;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 文件信息
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/5 10:47
|
||||
*/
|
||||
@Data
|
||||
public class FileInfo {
|
||||
|
||||
|
||||
/**
|
||||
* 绝对路径
|
||||
*/
|
||||
private String absolutePath;
|
||||
|
||||
/**
|
||||
* 是否为空
|
||||
*/
|
||||
private Boolean empty;
|
||||
|
||||
|
||||
/**
|
||||
* 文件密级
|
||||
*/
|
||||
private Integer fileConfLevel;
|
||||
|
||||
/**
|
||||
* 文件ID
|
||||
*/
|
||||
private String fileId;
|
||||
|
||||
/**
|
||||
* 是否文件夹
|
||||
*/
|
||||
private Boolean folder;
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 最后修改时间
|
||||
*/
|
||||
private Date lastModified;
|
||||
|
||||
/**
|
||||
* 权限
|
||||
*/
|
||||
private String permission;
|
||||
|
||||
/**
|
||||
* 文件大小
|
||||
*/
|
||||
private Long size;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
* <ul>
|
||||
* <li>directory:文件夹</li>
|
||||
* <li>file:文件</li>
|
||||
* </ul>
|
||||
*/
|
||||
private String fileType;
|
||||
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 文件属主
|
||||
*/
|
||||
private String owner;
|
||||
|
||||
/**
|
||||
* 组
|
||||
*/
|
||||
private String group;
|
||||
|
||||
/**
|
||||
* 其他
|
||||
*/
|
||||
private String other;
|
||||
|
||||
/**
|
||||
* 是否可读
|
||||
*/
|
||||
private Boolean read;
|
||||
|
||||
/**
|
||||
* 是否可写
|
||||
*/
|
||||
private Boolean write;
|
||||
|
||||
/**
|
||||
* 是否可执行
|
||||
*/
|
||||
private Boolean execute;
|
||||
|
||||
/**
|
||||
* 是否软链接
|
||||
*/
|
||||
private Integer link;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.jhinno.sdk.openapi.api.file;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
@@ -8,14 +7,14 @@ import com.jhinno.sdk.openapi.ServiceException;
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import com.jhinno.sdk.openapi.utils.CollectionUtil;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -26,6 +25,7 @@ import java.util.Map;
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/4 18:58
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class JHFileApiExecution extends JHApiExecution {
|
||||
|
||||
public JHFileApiExecution(JHApiClient jhApiClient) {
|
||||
@@ -189,18 +189,23 @@ public class JHFileApiExecution extends JHApiExecution {
|
||||
if (StringUtils.isBlank(uploadPath)) {
|
||||
throw new ArgsException("uploadPath是必填参数");
|
||||
}
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.setCharset(StandardCharsets.UTF_8);
|
||||
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||||
builder.addBinaryBody("file", is, ContentType.MULTIPART_FORM_DATA, fileName);
|
||||
Map<String, Object> body = new HashMap<>(3);
|
||||
|
||||
if (isCover != null) {
|
||||
builder.addTextBody("isCover", isCover.toString());
|
||||
body.put("isCover", isCover);
|
||||
}
|
||||
builder.addTextBody("uploadPath", uploadPath);
|
||||
HttpPost httpPost = new HttpPost(jhApiClient.getUrl(FilePathConstant.FILE_UPLOAD_PATH));
|
||||
httpPost.setEntity(builder.build());
|
||||
ResponseResult<Object> result = jhApiClient.request(httpPost, getHeaders(username, false), new TypeReference<ResponseResult<Object>>() {
|
||||
});
|
||||
body.put("uploadPath", uploadPath);
|
||||
|
||||
ResponseResult<Object> result = getJhApiClient().upload(
|
||||
FilePathConstant.FILE_UPLOAD_PATH,
|
||||
"file",
|
||||
fileName,
|
||||
is,
|
||||
getHeaders(username, false),
|
||||
body,
|
||||
new TypeReference<ResponseResult<Object>>() {
|
||||
}
|
||||
);
|
||||
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
|
||||
throw new ServiceException(FilePathConstant.FILE_UPLOAD_PATH, result.getCode(), result.getMessage());
|
||||
}
|
||||
@@ -220,6 +225,55 @@ public class JHFileApiExecution extends JHApiExecution {
|
||||
uploadFile(username, is, fileName, uploadPath, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username 用户名
|
||||
* @param path 本地文件路径
|
||||
* @param fileName 文件名
|
||||
* @param uploadPath 上传路径,服务器路径
|
||||
* @param isCover 是否覆盖(非必填,默认:false)
|
||||
*/
|
||||
public void uploadFile(String username, String path, String fileName, String uploadPath, Boolean isCover) throws FileNotFoundException {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("path是必填参数");
|
||||
}
|
||||
File file = new File(path);
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
uploadFile(username, fileInputStream, fileName, uploadPath, isCover);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param username 用户名
|
||||
* @param path 本地文件路径
|
||||
* @param fileName 文件名
|
||||
* @param uploadPath 上传路径,服务器路径
|
||||
*/
|
||||
public void uploadFile(String username, String path, String fileName, String uploadPath) throws FileNotFoundException {
|
||||
uploadFile(username, path, fileName, uploadPath, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param username 用户名
|
||||
* @param path 本地文件路径
|
||||
* @param uploadPath 上传路径,服务器路径
|
||||
* @param isCover 是否覆盖(非必填,默认:false)
|
||||
*/
|
||||
public void uploadFile(String username, String path, String uploadPath, Boolean isCover) throws FileNotFoundException {
|
||||
File file = new File(path);
|
||||
uploadFile(username, path, file.getName(), uploadPath, isCover);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username 用户名
|
||||
* @param path 本地文件路径
|
||||
* @param uploadPath 上传路径,服务器路径
|
||||
*/
|
||||
public void uploadFile(String username, String path, String uploadPath) throws FileNotFoundException {
|
||||
File file = new File(path);
|
||||
uploadFile(username, path, file.getName(), uploadPath, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取文件下载地址
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.jhinno.sdk.openapi.api.job;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
@@ -10,7 +8,10 @@ import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.api.file.FileInfo;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import com.jhinno.sdk.openapi.utils.JsonUtil;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.jhinno.sdk.openapi.utils.CollectionUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -20,6 +21,7 @@ import java.util.Map;
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/5 18:44
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class JHJobApiExecution extends JHApiExecution {
|
||||
|
||||
/**
|
||||
@@ -48,7 +50,7 @@ public class JHJobApiExecution extends JHApiExecution {
|
||||
}
|
||||
Map<String, Object> map = new HashMap<>(2);
|
||||
map.put("appId", appId);
|
||||
map.put("params", JSONUtil.toJsonStr(params));
|
||||
map.put("params", JsonUtil.objectToString(params));
|
||||
String path = JHApiClient.getUrl(JobPathConstant.JOB_SUBMIT_PATH, map);
|
||||
List<Map<String, String>> result = post(path, username, new TypeReference<ResponseResult<List<Map<String, String>>>>() {
|
||||
});
|
||||
@@ -102,7 +104,7 @@ public class JHJobApiExecution extends JHApiExecution {
|
||||
params.put("status", status.getStatus());
|
||||
}
|
||||
if (CollectionUtil.isNotEmpty(condition)) {
|
||||
params.put("condition", JSONUtil.toJsonStr(params));
|
||||
params.put("condition", JsonUtil.objectToString(params));
|
||||
}
|
||||
String path = JHApiClient.getUrl(JobPathConstant.JOB_PAGE_PATH, params);
|
||||
return get(path, username, new TypeReference<ResponseResult<PageJobInfo>>() {
|
||||
@@ -354,7 +356,7 @@ public class JHJobApiExecution extends JHApiExecution {
|
||||
throw new ArgsException("jobId不能为空!");
|
||||
}
|
||||
String path = JobPathConstant.JOB_PEEK_PATH.replace("{jobId}", jobId);
|
||||
ResponseResult<String> result = jhApiClient.get(path, getHeaders(username), new TypeReference<ResponseResult<String>>() {
|
||||
ResponseResult<String> result = getJhApiClient().get(path, getHeaders(username), new TypeReference<ResponseResult<String>>() {
|
||||
});
|
||||
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
|
||||
throw new ServiceException(path, result.getCode(), result.getMessage());
|
||||
@@ -383,4 +385,19 @@ public class JHJobApiExecution extends JHApiExecution {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户作业表单信息
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param appId 用户ID
|
||||
*/
|
||||
public List<JobAppFormItemInfo> getSimulationAppForm(String username, String appId) {
|
||||
if (StringUtils.isBlank(appId)) {
|
||||
throw new ArgsException("appId不能为空!");
|
||||
}
|
||||
String path = JobPathConstant.JOB_GET_APP_FORM_PATH.replace("{appId}", appId);
|
||||
return get(path, username, new TypeReference<ResponseResult<List<JobAppFormItemInfo>>>() {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.jhinno.sdk.openapi.api.job;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class JobAppFormItemInfo {
|
||||
|
||||
/**
|
||||
* 参数描述
|
||||
*/
|
||||
private String valueDesc;
|
||||
|
||||
/**
|
||||
* 参数key
|
||||
*/
|
||||
private String valueKey;
|
||||
|
||||
/**
|
||||
* 参数正则
|
||||
*/
|
||||
private String validateRegXp;
|
||||
|
||||
/**
|
||||
* 参数类型
|
||||
* 从参数范围内多选(多个值用英文逗号隔开)还是单选,
|
||||
* string、singleSelect、multiSelect
|
||||
* <ol>
|
||||
* <li>选择: select</li>
|
||||
* <li>开关: switch</li>
|
||||
* <li>值: text</li>
|
||||
* <li>文件: upload</li>
|
||||
* </ol>
|
||||
*/
|
||||
|
||||
private String valueType;
|
||||
|
||||
/**
|
||||
* 参数范围
|
||||
*/
|
||||
private List<ParamsValueScope> valueScope;
|
||||
|
||||
/**
|
||||
* 是否必填
|
||||
*/
|
||||
private boolean required;
|
||||
|
||||
/**
|
||||
* 默认值
|
||||
*/
|
||||
private String valueDefault;
|
||||
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class ParamsValueScope {
|
||||
private String name;
|
||||
private String value;
|
||||
}
|
||||
}
|
||||
@@ -82,4 +82,12 @@ public class JobPathConstant {
|
||||
* 连接作业会话
|
||||
*/
|
||||
public static final String JOB_CONNECT_SESSION_PATH = "/ws/api/jobs/{jobId}/connect";
|
||||
|
||||
|
||||
/**
|
||||
* 获取作业表单
|
||||
* <p/>
|
||||
* 该接口目前在Solutions下面维护,未经过产品的测试
|
||||
*/
|
||||
public static final String JOB_GET_APP_FORM_PATH = "/ws/api/jobs/{appId}/form_params";
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
@@ -18,6 +19,7 @@ import java.util.Map;
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/6 17:37
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class JHDepartmentApiExecution extends JHApiExecution {
|
||||
|
||||
/**
|
||||
@@ -6,6 +6,7 @@ import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.PageResult;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -19,6 +20,7 @@ import java.util.Map;
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/6 17:37
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class JHUserApiExecution extends JHApiExecution {
|
||||
/**
|
||||
* 获取一个执行器的实例
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.jhinno.sdk.openapi.client;
|
||||
|
||||
/**
|
||||
* 默认的HTTP客户端请求配置
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/1/30 18:12
|
||||
*/
|
||||
public class DefaultHttpClientConfig {
|
||||
|
||||
/**
|
||||
* 默认设置最大连接数
|
||||
*/
|
||||
public static int MAX_TOTAL = 200;
|
||||
|
||||
/**
|
||||
* 默认服务每次能并行接收的请求数量
|
||||
*/
|
||||
public static int MAX_PER_ROUTE = 20;
|
||||
|
||||
|
||||
/**
|
||||
* 默认socket连接超时的时间(单位:毫秒)
|
||||
*/
|
||||
public static final int SOCKET_TIMEOUT = 5000;
|
||||
|
||||
/**
|
||||
* 默认连接超时的时间(单位:毫秒)
|
||||
*/
|
||||
public static final int CONNECT_TIMEOUT = 5000;
|
||||
|
||||
/**
|
||||
* 默认请求超时的时间(单位:毫秒)
|
||||
*/
|
||||
public static final int CONNECTION_REQUEST_TIMEOUT = 5000;
|
||||
|
||||
/**
|
||||
* 是否使用服务器的时间
|
||||
*/
|
||||
public static final boolean DEFAULT_IS_USED_SERVER_TIME = false;
|
||||
|
||||
/**
|
||||
* 默认不强制获取token
|
||||
*/
|
||||
public static final boolean DEFAULT_IS_FORCE_GET_TOKEN = false;
|
||||
|
||||
|
||||
/**
|
||||
* 默认的token有效时间(单位:分钟)
|
||||
*/
|
||||
public static final int DEFAULT_TOKEN_EFFECTIVE_TIME = 30;
|
||||
|
||||
|
||||
/**
|
||||
* token 默认剩余时间
|
||||
*/
|
||||
public static final int DEFAULT_TOKEN_RESIDUE_TIME = 5;
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
package com.jhinno.sdk.openapi.client;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.ClientErrorCode;
|
||||
import com.jhinno.sdk.openapi.ClientException;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.api.auth.AuthPathConstant;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* 提供请求的工具
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/1/29 10:31
|
||||
*/
|
||||
@Data
|
||||
public class JHApiClient {
|
||||
|
||||
/**
|
||||
* 基础的请求URL地址
|
||||
* <p>
|
||||
* 如:https:// 192.168.3.12/appform
|
||||
* </p>
|
||||
*/
|
||||
private String baseUrl;
|
||||
|
||||
|
||||
/**
|
||||
* 设置自定义的jackson序列化配置
|
||||
*/
|
||||
private ObjectMapper mapper;
|
||||
|
||||
|
||||
/**
|
||||
* API HTTP 客户端
|
||||
*/
|
||||
private JHApiHttpClient apiHttpClient;
|
||||
|
||||
|
||||
public JHApiClient(String baseUrl) {
|
||||
if (StringUtils.isBlank(baseUrl)) {
|
||||
throw new ClientException("服务器的BaseUrl不能为空!");
|
||||
}
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
||||
}
|
||||
this.baseUrl = baseUrl;
|
||||
mapper = new ObjectMapper();
|
||||
mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||
mapper.setDateFormat(new SimpleDateFormat(CommonConstant.NORM_DATETIME_PATTERN));
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
}
|
||||
|
||||
public void initDefaultApiClient() {
|
||||
JHApiHttpClientImpl jhApiHttpClient = new JHApiHttpClientImpl();
|
||||
jhApiHttpClient.init();
|
||||
jhApiHttpClient.createHttpClients();
|
||||
this.apiHttpClient = jhApiHttpClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送一个get请求
|
||||
*
|
||||
* @param path 接口路径
|
||||
* @param headers 请求头
|
||||
* @param type 返回数据类型
|
||||
* @param <T> 返回数据类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public <T> T get(String path, Map<String, Object> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("url不能为空");
|
||||
}
|
||||
try {
|
||||
InputStream content = apiHttpClient.get(getUrl(path), headers);
|
||||
return mapper.readValue(content, type);
|
||||
} catch (IOException e) {
|
||||
throw new ClientException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起一个get请求
|
||||
*
|
||||
* @param path 接口地址
|
||||
* @param type 返回数据的类型
|
||||
* @param <T> 返回数据的类型
|
||||
* @return 请求的数据
|
||||
*/
|
||||
public <T> T get(String path, TypeReference<T> type) {
|
||||
return get(path, null, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获的一个url
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param params 请求参数
|
||||
* @return 添加路径参数后的URL
|
||||
*/
|
||||
public static String getUrl(String path, Map<String, Object> params) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ClientException("path不能为空");
|
||||
}
|
||||
if (params == null || params.isEmpty()) {
|
||||
return path;
|
||||
}
|
||||
StringBuilder urlBuilder = new StringBuilder(path + "?");
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
try {
|
||||
Object value = entry.getValue();
|
||||
// 如果值为空,则不添加该字段
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
urlBuilder.append(entry.getKey()).append("=");
|
||||
if (value instanceof String) {
|
||||
urlBuilder.append(URLEncoder.encode((String) value, StandardCharsets.UTF_8.name()));
|
||||
} else if (value instanceof Date) {
|
||||
SimpleDateFormat format = new SimpleDateFormat(CommonConstant.NORM_DATETIME_PATTERN);
|
||||
urlBuilder.append(URLEncoder.encode(format.format(value), StandardCharsets.UTF_8.name()));
|
||||
} else {
|
||||
urlBuilder.append(value);
|
||||
}
|
||||
urlBuilder.append("&");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new ClientException("url参数编码失败", ClientErrorCode.UNKNOWN, e);
|
||||
}
|
||||
}
|
||||
urlBuilder.setLength(urlBuilder.length() - 1);
|
||||
return urlBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取完整的请求路径
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return 请求URL
|
||||
*/
|
||||
public String getUrl(String path) {
|
||||
return baseUrl + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送post请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param body 请求体
|
||||
* @param headers 请求头
|
||||
* @param type 请求数据类型
|
||||
* @param <T> t 返回的数据的类型
|
||||
* @param <K> k body的类型
|
||||
* @return t
|
||||
*/
|
||||
public <T, K> T post(String path, K body, Map<String, Object> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("path不能为空");
|
||||
}
|
||||
try {
|
||||
String bodyStr = null;
|
||||
if (body != null) {
|
||||
bodyStr = mapper.writeValueAsString(body);
|
||||
}
|
||||
InputStream content = apiHttpClient.post(getUrl(path), bodyStr, headers);
|
||||
return mapper.readValue(content, type);
|
||||
} catch (IOException e) {
|
||||
throw new ClientException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起put请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param params 请求参数
|
||||
* @param body 请求体
|
||||
* @param headers 请求头
|
||||
* @param type 请求数据类型
|
||||
* @param <T> t 返回的数据的类型
|
||||
* @param <K> k body的类型
|
||||
* @return 请求返回的数据
|
||||
*/
|
||||
public <T, K> T put(String path, Map<String, Object> params, K body, Map<String, Object> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("path不能为空");
|
||||
}
|
||||
try {
|
||||
String bodyStr = null;
|
||||
if (body != null) {
|
||||
bodyStr = mapper.writeValueAsString(body);
|
||||
}
|
||||
InputStream content = apiHttpClient.put(getUrl(path), bodyStr, headers);
|
||||
return mapper.readValue(content, type);
|
||||
} catch (IOException e) {
|
||||
throw new ClientException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起put请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param body 请求体
|
||||
* @param headers 请求头
|
||||
* @param type 请求数据类型
|
||||
* @param <T> t 返回的数据的类型
|
||||
* @param <K> k body的类型
|
||||
* @return 请求返回的数据
|
||||
*/
|
||||
public <T, K> T put(String path, K body, Map<String, Object> headers, TypeReference<T> type) {
|
||||
return put(path, null, body, headers, type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起put请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param body 请求体
|
||||
* @param type 请求数据类型
|
||||
* @param <T> t 返回的数据的类型
|
||||
* @param <K> k body的类型
|
||||
* @return 请求返回的数据
|
||||
*/
|
||||
public <T, K> T put(String path, K body, TypeReference<T> type) {
|
||||
return put(path, body, null, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path 请求地址
|
||||
* @param body 请求体
|
||||
* @param type 响应类型
|
||||
* @param <T> 相应返回数据类型
|
||||
* @param <K> 请求体数据类型
|
||||
* @return 响应数据
|
||||
*/
|
||||
public <T, K> T post(String path, K body, TypeReference<T> type) {
|
||||
return post(path, body, null, type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起delete请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param headers 请求头
|
||||
* @param type 响应类型
|
||||
* @param <T> 响应数据类型
|
||||
* @return 响应数据
|
||||
*/
|
||||
public <T> T delete(String path, Map<String, Object> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("path不能为空");
|
||||
}
|
||||
try {
|
||||
InputStream content = apiHttpClient.delete(getUrl(path), headers);
|
||||
return mapper.readValue(content, type);
|
||||
} catch (IOException e) {
|
||||
throw new ClientException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起delete请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param type 响应类型
|
||||
* @param <T> 响应数据类型
|
||||
* @return 响应数据
|
||||
*/
|
||||
public <T> T delete(String path, TypeReference<T> type) {
|
||||
return delete(path, null, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path 请求路径
|
||||
* @param keyName 文件key
|
||||
* @param fileName 文件名
|
||||
* @param is 文件流
|
||||
* @param headers 请求头
|
||||
* @param body 请求体其他数据
|
||||
* @param type 返回数据类型
|
||||
* @return 请求类型
|
||||
*/
|
||||
public ResponseResult<Object> upload(String path,
|
||||
String keyName,
|
||||
String fileName,
|
||||
InputStream is,
|
||||
Map<String, Object> headers,
|
||||
Map<String, Object> body,
|
||||
TypeReference<ResponseResult<Object>> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("path不能为空");
|
||||
}
|
||||
try {
|
||||
InputStream content = apiHttpClient.upload(getUrl(path), keyName, fileName, is, body, headers);
|
||||
return mapper.readValue(content, type);
|
||||
} catch (IOException e) {
|
||||
throw new ClientException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getAppformServerCurrentTimeMillis() {
|
||||
try {
|
||||
long currentTimeMillis = apiHttpClient.getAppformServerCurrentTimeMillis(getUrl(AuthPathConstant.PING));
|
||||
return String.valueOf(currentTimeMillis);
|
||||
} catch (IOException e) {
|
||||
throw new ClientException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.jhinno.sdk.openapi.client;
|
||||
|
||||
|
||||
import com.jhinno.sdk.openapi.AuthType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 该接口用户实现发送HTTP请求的方法
|
||||
*/
|
||||
public interface JHApiHttpClient {
|
||||
|
||||
/**
|
||||
* 发起一个get请求
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param headers 请求头
|
||||
* @return 数据流
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
InputStream get(String url, Map<String, Object> headers) throws IOException;
|
||||
|
||||
/**
|
||||
* 发起一个post请求
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param body 请求体
|
||||
* @param headers 请求头
|
||||
* @return 数据流
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
InputStream post(String url, String body, Map<String, Object> headers) throws IOException;
|
||||
|
||||
|
||||
/**
|
||||
* 发起一个put请求
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param body 请求体
|
||||
* @param headers 请求头
|
||||
* @return 数据流
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
InputStream put(String url, String body, Map<String, Object> headers) throws IOException;
|
||||
|
||||
/**
|
||||
* 发起一个GET请求
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param headers 请求头
|
||||
* @return 数据流
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
InputStream delete(String url, Map<String, Object> headers) throws IOException;
|
||||
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param keyName 文件key
|
||||
* @param is 文件流
|
||||
* @param body 请求体
|
||||
* @param headers 请求头
|
||||
* @return 响应
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
InputStream upload(String url, String keyName, String fileName, InputStream is, Map<String, Object> body, Map<String, Object> headers) throws IOException;
|
||||
|
||||
|
||||
/**
|
||||
* 获取Appform服务器当前的时间
|
||||
* <p/>
|
||||
* {@link AuthType#TOKEN_MODE}模式,并且打开了获取服务器时间的开关需要实现改方法
|
||||
*
|
||||
* @return 服务器的时间
|
||||
*/
|
||||
default long getAppformServerCurrentTimeMillis(String url) throws IOException {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
package com.jhinno.sdk.openapi.client;
|
||||
|
||||
import com.jhinno.sdk.openapi.ClientErrorCode;
|
||||
import com.jhinno.sdk.openapi.ClientException;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.*;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.ParseException;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class JHApiHttpClientImpl implements JHApiHttpClient {
|
||||
|
||||
|
||||
/**
|
||||
* HTTP的连接客户端
|
||||
*/
|
||||
private CloseableHttpClient closeableHttpClient;
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 每次发送请求的配置,如果该配置未进行设置则走 {@link DefaultHttpClientConfig} 中的默认配置
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* {@link JHApiClient} 默认只配置了 socket连接超时的时间(socketTimeout) 、连接超时的时间(connectTimeout)、
|
||||
* 请求超时的时间(connectionRequestTimeout)这三项,其默认配置在{@link DefaultHttpClientConfig} 中。
|
||||
* 如果你要自定义你自己的配置,则可以通过{@link HttpClients }构建自己的RequestConfig来请求接口
|
||||
* </p>
|
||||
*
|
||||
* @param requestConfig HTTP请求的配置
|
||||
*/
|
||||
private RequestConfig requestConfig;
|
||||
|
||||
|
||||
/**
|
||||
* Socket连接超时的时间(单位:毫秒,默认:{@link DefaultHttpClientConfig#SOCKET_TIMEOUT})
|
||||
*/
|
||||
private int socketTimeout = DefaultHttpClientConfig.SOCKET_TIMEOUT;
|
||||
|
||||
/**
|
||||
* 连接超时的时间(单位:毫秒,默认:{@link DefaultHttpClientConfig#CONNECT_TIMEOUT})
|
||||
*/
|
||||
private int connectTimeout = DefaultHttpClientConfig.CONNECT_TIMEOUT;
|
||||
|
||||
/**
|
||||
* 默认请求超时的时间(单位:毫秒,默认:{@link DefaultHttpClientConfig#CONNECTION_REQUEST_TIMEOUT})
|
||||
*/
|
||||
private int connectRequestTimeout = DefaultHttpClientConfig.CONNECTION_REQUEST_TIMEOUT;
|
||||
|
||||
/**
|
||||
* 设置最大连接数(默认:{@link DefaultHttpClientConfig#MAX_TOTAL})
|
||||
*/
|
||||
private int maxTotal = DefaultHttpClientConfig.MAX_TOTAL;
|
||||
|
||||
/**
|
||||
* 服务每次能并行接收的请求数量(默认:{@link DefaultHttpClientConfig#MAX_PER_ROUTE})
|
||||
*/
|
||||
private int maxPerRoute = DefaultHttpClientConfig.MAX_PER_ROUTE;
|
||||
|
||||
/**
|
||||
* 初始化一个HTTP客户端实例
|
||||
*
|
||||
* @return 返回一个可关闭的HTTP客户端示例
|
||||
*/
|
||||
public void createHttpClients() {
|
||||
SSLContextBuilder builder = new SSLContextBuilder();
|
||||
try {
|
||||
builder.loadTrustMaterial(null, (x509Certificates, s) -> true);
|
||||
SSLConnectionSocketFactory sslref = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
|
||||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", sslref).build();
|
||||
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
|
||||
cm.setMaxTotal(maxTotal);
|
||||
cm.setDefaultMaxPerRoute(maxPerRoute);
|
||||
closeableHttpClient = HttpClients.custom().setSSLSocketFactory(sslref).setConnectionManager(cm).setConnectionManagerShared(true).build();
|
||||
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
|
||||
throw new ClientException(e.getMessage(), ClientErrorCode.SSL_EXCEPTION, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化客户端
|
||||
*/
|
||||
public void init() {
|
||||
this.requestConfig = RequestConfig.custom()
|
||||
.setSocketTimeout(socketTimeout)
|
||||
.setConnectTimeout(connectTimeout)
|
||||
.setConnectionRequestTimeout(connectRequestTimeout)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 原始发送请求
|
||||
*
|
||||
* @param httpRequest 请求体
|
||||
* @param headers 请求头
|
||||
* @return 响应体
|
||||
*/
|
||||
public HttpEntity request(HttpRequestBase httpRequest, Map<String, Object> headers) {
|
||||
if (requestConfig == null) {
|
||||
throw new ClientException("请配置requestConfig");
|
||||
}
|
||||
|
||||
if (httpRequest == null) {
|
||||
throw new ClientException("httpRequest不能为空");
|
||||
}
|
||||
httpRequest.setConfig(requestConfig);
|
||||
// 添加请求头
|
||||
if (headers != null && !headers.isEmpty()) {
|
||||
for (Map.Entry<String, Object> entry : headers.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
httpRequest.setHeader(key, value.toString());
|
||||
}
|
||||
}
|
||||
try {
|
||||
HttpResponse response = closeableHttpClient.execute(httpRequest);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
|
||||
httpRequest.releaseConnection();
|
||||
throw new ClientException("发送HTTP请求失败,请求码:" + statusCode, ClientErrorCode.REQUEST_ERROR);
|
||||
}
|
||||
return response.getEntity();
|
||||
} catch (IOException e) {
|
||||
httpRequest.releaseConnection();
|
||||
throw new ClientException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public InputStream get(String url, Map<String, Object> headers) throws IOException {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
return request(httpGet, headers).getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream post(String url, String body, Map<String, Object> headers) throws IOException {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
httpPost.setEntity(new StringEntity(body, StandardCharsets.UTF_8));
|
||||
}
|
||||
return request(httpPost, headers).getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream put(String url, String body, Map<String, Object> headers) throws IOException {
|
||||
HttpPut httpPut = new HttpPut(url);
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
httpPut.setEntity(new StringEntity(body, StandardCharsets.UTF_8));
|
||||
}
|
||||
return request(httpPut, headers).getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream delete(String url, Map<String, Object> headers) throws IOException {
|
||||
HttpDelete httpDelete = new HttpDelete(url);
|
||||
return request(httpDelete, headers).getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream upload(String url, String keyName, String fileName, InputStream is, Map<String, Object> body, Map<String, Object> headers) throws IOException {
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.setCharset(StandardCharsets.UTF_8);
|
||||
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||||
builder.addBinaryBody(keyName, is, ContentType.MULTIPART_FORM_DATA, fileName);
|
||||
if (body != null && !body.isEmpty()) {
|
||||
for (Map.Entry<String, Object> entry : body.entrySet()) {
|
||||
builder.addTextBody(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.setEntity(builder.build());
|
||||
return request(httpPost, headers).getContent();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getAppformServerCurrentTimeMillis(String url) throws IOException {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.setConfig(requestConfig);
|
||||
try {
|
||||
HttpResponse response = closeableHttpClient.execute(httpGet);
|
||||
Header header = response.getFirstHeader("Date");
|
||||
if (header == null) {
|
||||
throw new ClientException("获取时间戳响应为空!");
|
||||
}
|
||||
String value = header.getValue();
|
||||
if (StringUtils.isBlank(value)) {
|
||||
throw new ClientException("获取时间戳响应头为空!");
|
||||
}
|
||||
return CommonConstant.HTTP_DATETIME_FORMAT.parse(value).getTime();
|
||||
} catch (ParseException e) {
|
||||
throw new ClientException("时间格式获取失败,失败原因:" + e.getMessage(), e);
|
||||
}finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.jhinno.sdk.openapi.utils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public class CollectionUtil {
|
||||
|
||||
/**
|
||||
* Map是否为空
|
||||
*
|
||||
* @param map 集合
|
||||
* @return 是否为空
|
||||
*/
|
||||
public static boolean isEmpty(Map<?, ?> map) {
|
||||
return map == null || map.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Map是否为非空
|
||||
*
|
||||
* @param map 集合
|
||||
* @return 是否为非空
|
||||
*/
|
||||
public static boolean isNotEmpty(Map<?, ?> map) {
|
||||
return !isEmpty(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collection是否为空
|
||||
*
|
||||
* @param collection 集合
|
||||
* @return 是否为非空
|
||||
*/
|
||||
public static boolean isEmpty(Collection<?> collection) {
|
||||
return collection == null || collection.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Collection是否为非空
|
||||
*
|
||||
* @param collection 集合
|
||||
* @return 是否为非空
|
||||
*/
|
||||
public static boolean isNotEmpty(Collection<?> collection) {
|
||||
return !isEmpty(collection);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.jhinno.sdk.openapi.utils;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class JsonUtil {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
static {
|
||||
OBJECT_MAPPER.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat(CommonConstant.NORM_DATETIME_PATTERN));
|
||||
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static <T> T stringToObject(String str, Class<T> cls) {
|
||||
if (StringUtils.isBlank(str)) {
|
||||
return null;
|
||||
}
|
||||
return OBJECT_MAPPER.readValue(str, cls);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static <T> T stringToObject(String str, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(str)) {
|
||||
return null;
|
||||
}
|
||||
return OBJECT_MAPPER.readValue(str, type);
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public static <T> T isToObject(InputStream is, Class<T> cls) {
|
||||
if (is == null) {
|
||||
return null;
|
||||
}
|
||||
return OBJECT_MAPPER.readValue(is, cls);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static String objectToString(Object object) {
|
||||
return OBJECT_MAPPER.writeValueAsString(object);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.jhinno.sdk.openapi.test;
|
||||
|
||||
import com.jhinno.sdk.openapi.AuthType;
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.app.JHAppApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.data.JHDataApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.file.JHFileApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.job.JHJobApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.organization.JHDepartmentApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.organization.JHUserApiExecution;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* SDK Client 的配置
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/4 17:11
|
||||
*/
|
||||
public class JHClientConfig {
|
||||
|
||||
/**
|
||||
* 初始化JHApi客户端
|
||||
*/
|
||||
public static final JHApiClient client = new JHApiClient("https://192.168.38.160/appform");
|
||||
|
||||
public static final Map<Class<? extends JHApiExecution>, JHApiExecution> jhApiClientMap = new HashMap<>();
|
||||
|
||||
public static final String ACCESS_KEY = "35154207caa94be582e75da31ec8e9f2";
|
||||
|
||||
public static final String ACCESS_KEY_SECRET = "2cdb5ecbe51543ad9a08ad21c32e2dd9";
|
||||
|
||||
static {
|
||||
client.initDefaultApiClient();
|
||||
jhApiClientMap.put(JHAppApiExecution.class, new JHAppApiExecution());
|
||||
jhApiClientMap.put(JHDataApiExecution.class, new JHDataApiExecution());
|
||||
jhApiClientMap.put(JHFileApiExecution.class, new JHFileApiExecution());
|
||||
jhApiClientMap.put(JHJobApiExecution.class, new JHJobApiExecution());
|
||||
jhApiClientMap.put(JHDepartmentApiExecution.class, new JHDepartmentApiExecution());
|
||||
jhApiClientMap.put(JHUserApiExecution.class, new JHUserApiExecution());
|
||||
|
||||
jhApiClientMap.forEach((k, v) -> {
|
||||
v.setJhApiClient(client);
|
||||
v.setAuthType(AuthType.ACCESS_SECRET_MODE);
|
||||
v.setAccessKey(ACCESS_KEY);
|
||||
v.setAccessKeySecret(ACCESS_KEY_SECRET);
|
||||
v.setUsedServerTime(true);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.jhinno.sdk.openapi.test.app;
|
||||
|
||||
import cn.hutool.core.lang.ConsoleTable;
|
||||
import com.jhinno.sdk.openapi.api.app.*;
|
||||
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||
import org.junit.Test;
|
||||
@@ -21,7 +20,7 @@ public class AppApiTest {
|
||||
/**
|
||||
* 获得一个调用应用接口的执行器
|
||||
*/
|
||||
public static final JHAppApiExecution jhAppApiExecution = new JHAppApiExecution(JHClientConfig.client);
|
||||
public static final JHAppApiExecution jhAppApiExecution = (JHAppApiExecution) JHClientConfig.jhApiClientMap.get(JHAppApiExecution.class);
|
||||
|
||||
/**
|
||||
* 测测试使用自定义的参数启动jhadmin的Linux桌面
|
||||
@@ -157,14 +156,7 @@ public class AppApiTest {
|
||||
@Test
|
||||
public void testGetAppList() {
|
||||
List<AppInfo> appList = jhAppApiExecution.getAppList("jhadmin");
|
||||
|
||||
ConsoleTable consoleTable = ConsoleTable.create();
|
||||
consoleTable.setSBCMode(false);
|
||||
consoleTable.addHeader("id", "name", "type", "category", "protocol");
|
||||
for (AppInfo appInfo : appList) {
|
||||
consoleTable.addBody(appInfo.getId(), appInfo.getName(), appInfo.getType(), appInfo.getCategory(), appInfo.getProtocol());
|
||||
}
|
||||
consoleTable.print();
|
||||
System.out.println(appList);
|
||||
}
|
||||
|
||||
|
||||
@@ -176,4 +168,21 @@ public class AppApiTest {
|
||||
System.out.println(jhAppApiExecution.getAppUrl("jhadmin", "myjobmana"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试根据文件后缀取应用列表
|
||||
*/
|
||||
@Test
|
||||
public void testGetAppInfoSuffixList() {
|
||||
System.out.println(jhAppApiExecution.getAppInfoSuffixList("test", ".sh"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试根据用途获取应用列表
|
||||
*/
|
||||
@Test
|
||||
public void testGetUseLabelList() {
|
||||
System.out.println(jhAppApiExecution.getUseLabelList("jhadmin"));
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class DataApiTest {
|
||||
|
||||
public static final JHDataApiExecution execution = new JHDataApiExecution(JHClientConfig.client);
|
||||
public static final JHDataApiExecution execution = (JHDataApiExecution) JHClientConfig.jhApiClientMap.get(JHDataApiExecution.class);
|
||||
|
||||
/**
|
||||
* 测试获取作业数据区目录列表
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.jhinno.sdk.openapi.test.file;
|
||||
|
||||
import cn.hutool.core.lang.ConsoleTable;
|
||||
|
||||
import com.jhinno.sdk.openapi.api.file.FileInfo;
|
||||
import com.jhinno.sdk.openapi.api.file.JHFileApiExecution;
|
||||
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||
@@ -17,7 +17,7 @@ import java.util.List;
|
||||
*/
|
||||
public class FileApiTest {
|
||||
|
||||
private static final JHFileApiExecution execution = new JHFileApiExecution(JHClientConfig.client);
|
||||
private static final JHFileApiExecution execution = (JHFileApiExecution) JHClientConfig.jhApiClientMap.get(JHFileApiExecution.class);
|
||||
|
||||
|
||||
/**
|
||||
@@ -50,14 +50,8 @@ public class FileApiTest {
|
||||
*/
|
||||
@Test
|
||||
public void testGetFileList() {
|
||||
List<FileInfo> fileList = execution.getFileList("lqyan", "$HOME");
|
||||
ConsoleTable consoleTable = ConsoleTable.create();
|
||||
consoleTable.setSBCMode(false);
|
||||
consoleTable.addHeader("fileName", "fileType", "owner", "read", "write", "execute", "path");
|
||||
for (FileInfo fileInfo : fileList) {
|
||||
consoleTable.addBody(fileInfo.getFileName(), fileInfo.getFileType(), fileInfo.getOwner(), fileInfo.getRead().toString(), fileInfo.getWrite().toString(), fileInfo.getExecute().toString(), fileInfo.getPath());
|
||||
}
|
||||
consoleTable.print();
|
||||
List<FileInfo> fileList = execution.getFileList("jhadmin", "$HOME");
|
||||
System.out.println(fileList);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +59,7 @@ public class FileApiTest {
|
||||
*/
|
||||
@Test
|
||||
public void testMkdir() {
|
||||
System.out.println(execution.mkdir("jhadmin", "$HOME/test", true));
|
||||
System.out.println(execution.mkdir("jhadmin", "$HOMEtest1", true));
|
||||
}
|
||||
|
||||
|
||||
@@ -92,9 +86,9 @@ public class FileApiTest {
|
||||
*/
|
||||
@Test
|
||||
public void testUploadFile() throws IOException {
|
||||
File file = new File("C:\\Users\\yanlongqi\\Desktop\\双色球.xls");
|
||||
File file = new File("C:\\Users\\yanlongqi\\Desktop\\LdapAdminv1830.exe");
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
execution.uploadFile("jhadmin", fileInputStream, file.getName(), "$HOME/temp", true);
|
||||
execution.uploadFile("jhadmin", fileInputStream, file.getName(), "$HOME", true);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.jhinno.sdk.openapi.test.job;
|
||||
|
||||
import com.jhinno.sdk.openapi.api.job.JHJobApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.job.JobHistoryInfo;
|
||||
import com.jhinno.sdk.openapi.api.job.JobStatusEnum;
|
||||
import com.jhinno.sdk.openapi.api.job.PageJobInfo;
|
||||
import com.jhinno.sdk.openapi.api.job.*;
|
||||
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -20,7 +17,7 @@ import java.util.Map;
|
||||
*/
|
||||
public class JobApiTest {
|
||||
|
||||
private static final JHJobApiExecution execution = new JHJobApiExecution(JHClientConfig.client);
|
||||
private static final JHJobApiExecution execution = (JHJobApiExecution) JHClientConfig.jhApiClientMap.get(JHJobApiExecution.class);
|
||||
|
||||
/**
|
||||
* 测试提交作业
|
||||
@@ -125,4 +122,13 @@ public class JobApiTest {
|
||||
System.out.println(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试获取作业表单
|
||||
*/
|
||||
@Test
|
||||
public void testGetSimulationAppForm() {
|
||||
List<JobAppFormItemInfo> simulationAppForm = execution.getSimulationAppForm("jhadmin", "common_sub");
|
||||
System.out.println(simulationAppForm);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import org.junit.Test;
|
||||
*/
|
||||
public class DepartmentApiTest {
|
||||
|
||||
private static final JHDepartmentApiExecution execution = new JHDepartmentApiExecution(JHClientConfig.client);
|
||||
private static final JHDepartmentApiExecution execution = (JHDepartmentApiExecution) JHClientConfig.jhApiClientMap.get(JHDepartmentApiExecution.class);
|
||||
|
||||
|
||||
/**
|
||||
@@ -15,7 +15,7 @@ import org.junit.Test;
|
||||
*/
|
||||
public class UserApiTest {
|
||||
|
||||
private static final JHUserApiExecution execution = new JHUserApiExecution(JHClientConfig.client);
|
||||
private static final JHUserApiExecution execution = (JHUserApiExecution) JHClientConfig.jhApiClientMap.get(JHUserApiExecution.class);
|
||||
|
||||
|
||||
/**
|
||||
136
jhinno-openapi-sdk-spring-boot-starter/pom.xml
Normal file
136
jhinno-openapi-sdk-spring-boot-starter/pom.xml
Normal file
@@ -0,0 +1,136 @@
|
||||
<?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>
|
||||
|
||||
<artifactId>jhinno-openapi-sdk-spring-boot-starter</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>Jhinno OpenAPI SDK for Java SpringBoot Starter</name>
|
||||
<description>The Jhinno OpenAPI SDK for Java used for accessing Jhinno OpenApi Service</description>
|
||||
<url>http://jhinno.com</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.jhinno</groupId>
|
||||
<artifactId>jhinno-openapi-java-sdk-parent</artifactId>
|
||||
<version>2.0.2</version>
|
||||
</parent>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.jhinno</groupId>
|
||||
<artifactId>jhinno-openapi-java-sdk</artifactId>
|
||||
<version>2.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>jhinno-releases</id>
|
||||
<url>http://192.168.87.22:8081/repository/maven-releases</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok-maven-plugin</artifactId>
|
||||
<version>1.18.0.0</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<configuration>
|
||||
<sourcepath>target/generated-sources/delombok</sourcepath>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
<docencoding>${project.build.sourceEncoding}</docencoding>
|
||||
<charset>${project.build.sourceEncoding}</charset>
|
||||
<doclint>none</doclint>
|
||||
<tags>
|
||||
<tag>
|
||||
<name>date</name>
|
||||
<placement>X</placement>
|
||||
</tag>
|
||||
</tags>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>lqyan</id>
|
||||
<name>lqyan</name>
|
||||
<email>lqyan@jhinno.com</email>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.jhinno.sdk.openapi.autoconfigure;
|
||||
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import com.jhinno.sdk.openapi.client.JHApiHttpClientImpl;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* openapi客户端自动配置
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/6/4 16:01
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(JHOpenapiProperties.class)
|
||||
public class JHOpenapiClientAutoConfigure {
|
||||
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public JHApiClient jhApiClient(JHOpenapiProperties properties) {
|
||||
JHApiClient jhApiClient = new JHApiClient(properties.getServerUrl());
|
||||
JHApiHttpClientImpl jhApiHttpClient = new JHApiHttpClientImpl();
|
||||
jhApiHttpClient.setMaxPerRoute(properties.getMaxPerRout());
|
||||
jhApiHttpClient.setSocketTimeout(properties.getSocketTimeout());
|
||||
jhApiHttpClient.setMaxTotal(properties.getMaxTotal());
|
||||
jhApiHttpClient.setConnectTimeout(properties.getConnectTimeout());
|
||||
jhApiHttpClient.setConnectRequestTimeout(properties.getConnectRequestTimeout());
|
||||
jhApiHttpClient.init();
|
||||
jhApiHttpClient.createHttpClients();
|
||||
jhApiClient.setApiHttpClient(jhApiHttpClient);
|
||||
return jhApiClient;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.jhinno.sdk.openapi.autoconfigure;
|
||||
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.app.JHAppApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.data.JHDataApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.file.JHFileApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.job.JHJobApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.organization.JHDepartmentApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.organization.JHUserApiExecution;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* openapi执行器自动配置
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/6/4 17:27
|
||||
*/
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JHOpenapiExecutionAutoconfigure {
|
||||
|
||||
|
||||
private final JHOpenapiProperties properties;
|
||||
private final JHApiClient client;
|
||||
|
||||
|
||||
public void init(JHApiExecution apiExecution) {
|
||||
apiExecution.setJhApiClient(client);
|
||||
apiExecution.setForceGetToken(properties.isForceGetToken());
|
||||
apiExecution.setAuthType(properties.getAuthType());
|
||||
apiExecution.setAccessKey(properties.getAccessKey());
|
||||
apiExecution.setAccessKeySecret(properties.getAccessKeySecret());
|
||||
apiExecution.setTokenTimeout(properties.getTokenTimeout());
|
||||
apiExecution.setTokenResidueTime(properties.getTokenResidueTime());
|
||||
apiExecution.setUsedServerTime(properties.isUsedServerTime());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JHAppApiExecution appApiExecution() {
|
||||
JHAppApiExecution jhAppApiExecution = new JHAppApiExecution();
|
||||
init(jhAppApiExecution);
|
||||
return jhAppApiExecution;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JHDataApiExecution dataApiExecution() {
|
||||
JHDataApiExecution dataApiExecution = new JHDataApiExecution();
|
||||
init(dataApiExecution);
|
||||
return dataApiExecution;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public JHFileApiExecution fileApiExecution() {
|
||||
JHFileApiExecution fileApiExecution = new JHFileApiExecution();
|
||||
init(fileApiExecution);
|
||||
return fileApiExecution;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JHJobApiExecution jobApiExecution() {
|
||||
JHJobApiExecution jobApiExecution = new JHJobApiExecution();
|
||||
init(jobApiExecution);
|
||||
return jobApiExecution;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JHDepartmentApiExecution departmentApiExecution() {
|
||||
JHDepartmentApiExecution departmentApiExecution = new JHDepartmentApiExecution();
|
||||
init(departmentApiExecution);
|
||||
return departmentApiExecution;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JHUserApiExecution userApiExecution() {
|
||||
JHUserApiExecution userApiExecution = new JHUserApiExecution();
|
||||
init(userApiExecution);
|
||||
return userApiExecution;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.jhinno.sdk.openapi.autoconfigure;
|
||||
|
||||
import com.jhinno.sdk.openapi.AuthType;
|
||||
import com.jhinno.sdk.openapi.client.DefaultHttpClientConfig;
|
||||
import com.jhinno.sdk.openapi.constant.CommonConstants;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author yanlongqi
|
||||
* @date 2024/6/4 16:03
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = CommonConstants.CONFIG_PREFIX)
|
||||
public class JHOpenapiProperties {
|
||||
|
||||
/**
|
||||
* 接口服务的BaseURL, 列如:https://192.168.87.20/appform
|
||||
*/
|
||||
private String serverUrl;
|
||||
|
||||
/**
|
||||
* 设置连接池的最大连接数,默认{@link DefaultHttpClientConfig#MAX_TOTAL}
|
||||
*/
|
||||
private int maxTotal = DefaultHttpClientConfig.MAX_TOTAL;
|
||||
|
||||
|
||||
/**
|
||||
* 设置服务每次能并行接收的请求数量,默认{@link DefaultHttpClientConfig#MAX_PER_ROUTE}
|
||||
*/
|
||||
private int maxPerRout = DefaultHttpClientConfig.MAX_PER_ROUTE;
|
||||
|
||||
/**
|
||||
* 设置服务socket连接超时的时间(单位:毫秒),默认{@link DefaultHttpClientConfig#SOCKET_TIMEOUT}
|
||||
*/
|
||||
private int socketTimeout = DefaultHttpClientConfig.SOCKET_TIMEOUT;
|
||||
|
||||
/**
|
||||
* 设置服务连接超时的时间(单位:毫秒),默认{@link DefaultHttpClientConfig#CONNECT_TIMEOUT}
|
||||
*/
|
||||
private int connectTimeout = DefaultHttpClientConfig.CONNECT_TIMEOUT;
|
||||
|
||||
|
||||
/**
|
||||
* 设置服务请求超时的时间(单位:毫秒),默认{@link DefaultHttpClientConfig#CONNECTION_REQUEST_TIMEOUT}
|
||||
*/
|
||||
private int connectRequestTimeout = DefaultHttpClientConfig.CONNECTION_REQUEST_TIMEOUT;
|
||||
|
||||
|
||||
/**
|
||||
* token的超时时间(单位:分钟)
|
||||
*/
|
||||
private int tokenTimeout = DefaultHttpClientConfig.DEFAULT_TOKEN_EFFECTIVE_TIME;
|
||||
|
||||
/**
|
||||
* token提前获取的时间(单位:分钟)
|
||||
*/
|
||||
private int tokenResidueTime = DefaultHttpClientConfig.DEFAULT_TOKEN_RESIDUE_TIME;
|
||||
|
||||
|
||||
/**
|
||||
* 是否使用服务器时间
|
||||
*/
|
||||
private boolean usedServerTime = DefaultHttpClientConfig.DEFAULT_IS_USED_SERVER_TIME;
|
||||
|
||||
/**
|
||||
* 是否强制获取用户的token,默认{@link DefaultHttpClientConfig#DEFAULT_IS_FORCE_GET_TOKEN},
|
||||
* 如果强制获取token,则每次请求都去获取token;
|
||||
*/
|
||||
private boolean isForceGetToken = DefaultHttpClientConfig.DEFAULT_IS_FORCE_GET_TOKEN;
|
||||
|
||||
|
||||
/**
|
||||
* 接口请求的认证类型
|
||||
*/
|
||||
private AuthType authType = AuthType.ACCESS_SECRET_MODE;
|
||||
|
||||
/**
|
||||
* 访问密钥
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* 访问密钥密码
|
||||
*/
|
||||
private String accessKeySecret;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.jhinno.sdk.openapi.constant;
|
||||
|
||||
/**
|
||||
* @author yanlongqi
|
||||
* @date 2024/6/4 16:12
|
||||
*/
|
||||
public class CommonConstants {
|
||||
|
||||
|
||||
/**
|
||||
* 配置的前缀
|
||||
*/
|
||||
public static final String CONFIG_PREFIX = "jhinno.openapi";
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.jhinno.sdk.openapi.autoconfigure.JHOpenapiClientAutoConfigure,\
|
||||
com.jhinno.sdk.openapi.autoconfigure.JHOpenapiExecutionAutoconfigure
|
||||
@@ -0,0 +1,2 @@
|
||||
com.jhinno.sdk.openapi.autoconfigure.JHOpenapiClientAutoConfigure
|
||||
com.jhinno.sdk.openapi.autoconfigure.JHOpenapiExecutionAutoconfigure
|
||||
143
pom.xml
143
pom.xml
@@ -5,10 +5,10 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.jhinno</groupId>
|
||||
<artifactId>jhinno-openapi-java-sdk</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>Jhinno OpenAPI SDK for Java</name>
|
||||
<artifactId>jhinno-openapi-java-sdk-parent</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<packaging>pom</packaging>
|
||||
<name>Jhinno OpenAPI SDK for Java parent</name>
|
||||
<description>The Jhinno OpenAPI SDK for Java used for accessing Jhinno OpenApi Service</description>
|
||||
<url>http://jhinno.com</url>
|
||||
|
||||
@@ -21,70 +21,12 @@
|
||||
<maven.test.skip>true</maven.test.skip>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.18</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.13.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.15</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.15.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.15.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.25</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.28</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
@@ -93,70 +35,11 @@
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.6.2</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<modules>
|
||||
<module>jhinno-openapi-java-sdk</module>
|
||||
<module>jhinno-openapi-sdk-spring-boot-starter</module>
|
||||
</modules>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok-maven-plugin</artifactId>
|
||||
<version>1.18.0.0</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<configuration>
|
||||
<sourcepath>target/generated-sources/delombok</sourcepath>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
<docencoding>${project.build.sourceEncoding}</docencoding>
|
||||
<charset>${project.build.sourceEncoding}</charset>
|
||||
<doclint>none</doclint>
|
||||
<tags>
|
||||
<tag>
|
||||
<name>date</name>
|
||||
<placement>X</placement>
|
||||
</tag>
|
||||
</tags>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.jhinno.sdk.openapi;
|
||||
|
||||
/**
|
||||
* @author yanlongqi
|
||||
* @date 2024/1/31 10:17
|
||||
*/
|
||||
public class CommonConstant {
|
||||
|
||||
/**
|
||||
* 请求成功时的标识
|
||||
*/
|
||||
public static final String SUCCESS = "success";
|
||||
|
||||
/**
|
||||
* 请求失败时的标识
|
||||
*/
|
||||
public static final String FAILED = "failed";
|
||||
|
||||
|
||||
/**
|
||||
* 默认的token有效时间(单位:分钟)
|
||||
*/
|
||||
public static final int DEFAULT_TOKEN_EFFECTIVE_TIME = 30;
|
||||
|
||||
|
||||
/**
|
||||
* token 默认剩余时间
|
||||
*/
|
||||
public static final int DEFAULT_TOKEN_RESIDUE_TIME = 5;
|
||||
|
||||
/**
|
||||
* 获取token时AES加密的默认key
|
||||
*/
|
||||
public static final String DEFAULT_AES_KEY = "jin5no@aqec8gtw6";
|
||||
|
||||
/**
|
||||
* 字符逗号
|
||||
*/
|
||||
public static final String NORMAL_CHARACTER_COMMA = ",";
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package com.jhinno.sdk.openapi.api.file;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 文件信息
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/5 10:47
|
||||
*/
|
||||
@Data
|
||||
public class FileInfo {
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
* <ul>
|
||||
* <li>directory:文件夹</li>
|
||||
* <li>file:文件</li>
|
||||
* </ul>
|
||||
*/
|
||||
private String fileType;
|
||||
|
||||
/**
|
||||
* 文件属主
|
||||
*/
|
||||
private String owner;
|
||||
|
||||
/**
|
||||
* 是否可读
|
||||
*/
|
||||
private Boolean read;
|
||||
|
||||
/**
|
||||
* 是否可写
|
||||
*/
|
||||
private Boolean write;
|
||||
|
||||
/**
|
||||
* 是否可执行
|
||||
*/
|
||||
private Boolean execute;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.jhinno.sdk.openapi.client;
|
||||
|
||||
/**
|
||||
* 默认的HTTP客户端请求配置
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/1/30 18:12
|
||||
*/
|
||||
public class DefaultHttpClientConfig {
|
||||
|
||||
/**
|
||||
* 默认设置最大连接数
|
||||
*/
|
||||
public static int MAX_TOTAL = 200;
|
||||
|
||||
/**
|
||||
* 默认服务每次能并行接收的请求数量
|
||||
*/
|
||||
public static int MAX_PER_ROUT = 20;
|
||||
|
||||
|
||||
/**
|
||||
* 默认socket连接超时的时间(单位:秒)
|
||||
*/
|
||||
public static final int SOCKET_TIMEOUT = 5000;
|
||||
|
||||
/**
|
||||
* 默认连接超时的时间(单位:秒)
|
||||
*/
|
||||
public static final int CONNECT_TIMEOUT = 5000;
|
||||
|
||||
/**
|
||||
* 默认请求超时的时间(单位:秒)
|
||||
*/
|
||||
public static final int CONNECTION_REQUEST_TIMEOUT = 5000;
|
||||
}
|
||||
@@ -1,441 +0,0 @@
|
||||
package com.jhinno.sdk.openapi.client;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.ClientErrorCode;
|
||||
import com.jhinno.sdk.openapi.ClientException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.*;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* 提供请求的工具
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/1/29 10:31
|
||||
*/
|
||||
public class JHApiClient {
|
||||
|
||||
/**
|
||||
* 基础的请求URL地址
|
||||
* <p>
|
||||
* 如:<a href="https://192.168.3.12/appform">https://192.168.3.12/appform</a>
|
||||
* </p>
|
||||
*/
|
||||
private final String baseUrl;
|
||||
|
||||
|
||||
/**
|
||||
* 对象转换器
|
||||
*/
|
||||
private ObjectMapper mapper;
|
||||
|
||||
/**
|
||||
* 初始化一个JHApiClient的实例,可使用自定义的客户端
|
||||
*
|
||||
* @param baseUrl 景行接口服务的基础地址
|
||||
* @param closeableHttpClient 可关闭的HTTP客户端
|
||||
*/
|
||||
private JHApiClient(CloseableHttpClient closeableHttpClient, String baseUrl) {
|
||||
this.baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
|
||||
this.closeableHttpClient = closeableHttpClient;
|
||||
this.requestConfig = RequestConfig.custom().setSocketTimeout(DefaultHttpClientConfig.SOCKET_TIMEOUT).setConnectTimeout(DefaultHttpClientConfig.CONNECT_TIMEOUT).setConnectionRequestTimeout(DefaultHttpClientConfig.CONNECTION_REQUEST_TIMEOUT).build();
|
||||
mapper = new ObjectMapper();
|
||||
mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||
mapper.setDateFormat(new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* HTTP的连接客户端
|
||||
*/
|
||||
private final CloseableHttpClient closeableHttpClient;
|
||||
|
||||
|
||||
/**
|
||||
* 每次发送请求的配置,如果该配置未进行设置则走 {@link DefaultHttpClientConfig} 中的默认配置
|
||||
*/
|
||||
private RequestConfig requestConfig;
|
||||
|
||||
|
||||
/**
|
||||
* 通过最大连接数和服务每次能并行接收的请求数量构建一个JHApiClient实例
|
||||
*
|
||||
* @param maxTotal 最大连接数
|
||||
* @param maxPerRout 服务每次能并行接收的请求数量
|
||||
* @param baseUrl 景行接口服务的基础地址
|
||||
* @return JHApiClient的实例
|
||||
*/
|
||||
public static JHApiClient build(int maxTotal, int maxPerRout, String baseUrl) {
|
||||
return build(createHttpClients(maxTotal, maxPerRout), baseUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过{@link DefaultHttpClientConfig}默认配置的最大连接数和服务每次能并行接收的请求数量构建一个JHApiClient实例
|
||||
* <p>
|
||||
*
|
||||
* @param baseUrl 景行接口服务的基础地址
|
||||
* @return JHApiClient的实例
|
||||
*/
|
||||
public static JHApiClient build(String baseUrl) {
|
||||
return build(createHttpClients(DefaultHttpClientConfig.MAX_TOTAL, DefaultHttpClientConfig.MAX_PER_ROUT), baseUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过外部传入的{@link CloseableHttpClient}构建一个请求客户端.
|
||||
* <p>
|
||||
*
|
||||
* @param httpClient 请求连接池
|
||||
* @param baseUrl 景行接口服务的基础地址
|
||||
* @return JHApiClient的实例
|
||||
*/
|
||||
public static JHApiClient build(CloseableHttpClient httpClient, String baseUrl) {
|
||||
return new JHApiClient(httpClient, baseUrl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化一个HTTP客户端实例
|
||||
*
|
||||
* @param maxTotal 设置最大连接数
|
||||
* @param maxPerRoute 服务每次能并行接收的请求数量
|
||||
* @return 返回一个可关闭的HTTP客户端示例
|
||||
*/
|
||||
public static CloseableHttpClient createHttpClients(int maxTotal, int maxPerRoute) {
|
||||
SSLContextBuilder builder = new SSLContextBuilder();
|
||||
try {
|
||||
builder.loadTrustMaterial(null, (x509Certificates, s) -> true);
|
||||
SSLConnectionSocketFactory sslref = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
|
||||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", sslref).build();
|
||||
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
|
||||
cm.setMaxTotal(maxTotal);
|
||||
cm.setDefaultMaxPerRoute(maxPerRoute);
|
||||
return HttpClients.custom().setSSLSocketFactory(sslref).setConnectionManager(cm).setConnectionManagerShared(true).build();
|
||||
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
|
||||
throw new ClientException(e.getMessage(), ClientErrorCode.SSL_EXCEPTION, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭JHApiClient实例 (释放所有资源)
|
||||
* 调用JHApiClient实例的shutdown() 后,JHApiClient实例不可用。
|
||||
* 如果客户端不存在则不进行任何操作
|
||||
*/
|
||||
public void shutdown() {
|
||||
try {
|
||||
if (closeableHttpClient != null) {
|
||||
closeableHttpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ClientException("关闭JHApiClient失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置自定义的jackson序列化配置
|
||||
*
|
||||
* @param mapper 序列化器
|
||||
*/
|
||||
public void setMapper(ObjectMapper mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 设置一个HTTP请求的配置
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* {@link JHApiClient} 默认只配置了 socket连接超时的时间(socketTimeout) 、连接超时的时间(connectTimeout)、
|
||||
* 请求超时的时间(connectionRequestTimeout)这三项,其默认配置在{@link DefaultHttpClientConfig} 中。
|
||||
* 如果你要自定义你自己的配置,则可以通过{@link HttpClients}构建自己的RequestConfig来请求接口
|
||||
* </p>
|
||||
*
|
||||
* @param requestConfig HTTP请求的配置
|
||||
*/
|
||||
public void setRequestConfig(RequestConfig requestConfig) {
|
||||
this.requestConfig = requestConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 原始发送请求
|
||||
*
|
||||
* @param httpRequest 请求体
|
||||
* @param headers 请求头
|
||||
* @return 响应体
|
||||
*/
|
||||
public HttpEntity request(HttpRequestBase httpRequest, Map<String, String> headers) {
|
||||
if (requestConfig == null) {
|
||||
throw new ClientException("请配置requestConfig");
|
||||
}
|
||||
|
||||
if (httpRequest == null) {
|
||||
throw new ClientException("httpRequest不能为空");
|
||||
}
|
||||
httpRequest.setConfig(requestConfig);
|
||||
|
||||
// 添加请求头
|
||||
if (CollectionUtil.isNotEmpty(headers)) {
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
httpRequest.setHeader(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
HttpResponse response = closeableHttpClient.execute(httpRequest);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
|
||||
httpRequest.releaseConnection();
|
||||
throw new ClientException("发送HTTP请求失败,请求码:" + statusCode, ClientErrorCode.REQUEST_ERROR);
|
||||
}
|
||||
return response.getEntity();
|
||||
} catch (IOException e) {
|
||||
throw new ClientException(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
*
|
||||
* @param httpRequest HttpRequestBase
|
||||
* @param headers 请求头
|
||||
* @param type 返回数据类型
|
||||
* @param <T> 返回的数据类型
|
||||
* @return 返回的接口数据
|
||||
*/
|
||||
public <T> T request(HttpRequestBase httpRequest, Map<String, String> headers, TypeReference<T> type) {
|
||||
try {
|
||||
InputStream content = request(httpRequest, headers).getContent();
|
||||
return mapper.readValue(content, type);
|
||||
} catch (IOException e) {
|
||||
throw new ClientException(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送一个get请求
|
||||
*
|
||||
* @param path 接口地址
|
||||
* @param headers 请求头
|
||||
* @param type 请求返回值类型
|
||||
* @param <T> t
|
||||
* @return t
|
||||
*/
|
||||
public <T> T get(String path, Map<String, String> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("url不能为空");
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(getUrl(path));
|
||||
return request(httpGet, headers, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起一个get请求
|
||||
*
|
||||
* @param path 接口地址
|
||||
* @param type 返回数据的类型
|
||||
* @param <T> 返回数据的类型
|
||||
* @return 请求的数据
|
||||
*/
|
||||
public <T> T get(String path, TypeReference<T> type) {
|
||||
return get(path, null, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获的一个url
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param params 请求参数
|
||||
* @return 添加路径参数后的URL
|
||||
*/
|
||||
public static String getUrl(String path, Map<String, Object> params) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ClientException("path不能为空");
|
||||
}
|
||||
if (params == null || params.isEmpty()) {
|
||||
return path;
|
||||
}
|
||||
StringBuilder urlBuilder = new StringBuilder(path + "?");
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
try {
|
||||
Object value = entry.getValue();
|
||||
// 如果值为空,则不添加该字段
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
urlBuilder.append(entry.getKey()).append("=");
|
||||
if (value instanceof String) {
|
||||
urlBuilder.append(URLEncoder.encode((String) value, StandardCharsets.UTF_8.name()));
|
||||
} else if (value instanceof Date) {
|
||||
SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
|
||||
urlBuilder.append(URLEncoder.encode(format.format(value), "utf-8"));
|
||||
} else {
|
||||
urlBuilder.append(value);
|
||||
}
|
||||
urlBuilder.append("&");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new ClientException("url参数编码失败", ClientErrorCode.UNKNOWN, e);
|
||||
}
|
||||
}
|
||||
urlBuilder.setLength(urlBuilder.length() - 1);
|
||||
return urlBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取完整的请求路径
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return 请求URL
|
||||
*/
|
||||
public String getUrl(String path) {
|
||||
return baseUrl + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送post请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param body 请求体
|
||||
* @param headers 请求头
|
||||
* @param type 请求数据类型
|
||||
* @param <T> t 返回的数据的类型
|
||||
* @param <K> k body的类型
|
||||
* @return t
|
||||
*/
|
||||
public <T, K> T post(String path, K body, Map<String, String> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("path不能为空");
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(getUrl(path));
|
||||
try {
|
||||
if (body != null) {
|
||||
String bodyStr = mapper.writeValueAsString(body);
|
||||
httpPost.setEntity(new StringEntity(bodyStr, "utf-8"));
|
||||
}
|
||||
return request(httpPost, headers, type);
|
||||
} catch (Exception e) {
|
||||
throw new ClientException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起put请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param body 请求体
|
||||
* @param headers 请求头
|
||||
* @param type 请求数据类型
|
||||
* @param <T> t 返回的数据的类型
|
||||
* @param <K> k body的类型
|
||||
* @return 请求返回的数据
|
||||
*/
|
||||
public <T, K> T put(String path, K body, Map<String, String> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("url不能为空");
|
||||
}
|
||||
HttpPut httpPost = new HttpPut(getUrl(path));
|
||||
try {
|
||||
if (body != null) {
|
||||
String bodyStr = mapper.writeValueAsString(body);
|
||||
httpPost.setEntity(new StringEntity(bodyStr, "utf-8"));
|
||||
}
|
||||
return request(httpPost, headers, type);
|
||||
} catch (Exception e) {
|
||||
throw new ClientException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起put请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param body 请求体
|
||||
* @param type 请求数据类型
|
||||
* @param <T> t 返回的数据的类型
|
||||
* @param <K> k body的类型
|
||||
* @return 请求返回的数据
|
||||
*/
|
||||
public <T, K> T put(String path, K body, TypeReference<T> type) {
|
||||
return put(path, body, null, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param path 请求地址
|
||||
* @param body 请求体
|
||||
* @param type 响应类型
|
||||
* @param <T> 相应返回数据类型
|
||||
* @param <K> 请求体数据类型
|
||||
* @return 响应数据
|
||||
*/
|
||||
public <T, K> T post(String path, K body, TypeReference<T> type) {
|
||||
return post(path, body, null, type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起delete请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param headers 请求头
|
||||
* @param type 响应类型
|
||||
* @param <T> 响应数据类型
|
||||
* @return 响应数据
|
||||
*/
|
||||
public <T> T delete(String path, Map<String, String> headers, TypeReference<T> type) {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new ArgsException("url不能为空");
|
||||
}
|
||||
HttpDelete httpDelete = new HttpDelete(getUrl(path));
|
||||
return request(httpDelete, headers, type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发起delete请求
|
||||
*
|
||||
* @param path 请求地址
|
||||
* @param type 响应类型
|
||||
* @param <T> 响应数据类型
|
||||
* @return 响应数据
|
||||
*/
|
||||
public <T> T delete(String path, TypeReference<T> type) {
|
||||
return delete(path, null, type);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.jhinno.sdk.openapi.test;
|
||||
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
|
||||
/**
|
||||
* SDK Client 的配置
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/4 17:11
|
||||
*/
|
||||
public class JHClientConfig {
|
||||
|
||||
/**
|
||||
* 初始化JHApi客户端
|
||||
*/
|
||||
public static final JHApiClient client = JHApiClient.build("https://192.168.87.25/appform");
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.jhinno.sdk.openapi.test.auth;
|
||||
|
||||
import com.jhinno.sdk.openapi.api.app.JHAppApiExecution;
|
||||
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 鉴权相关测试累类
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/1 18:06
|
||||
*/
|
||||
public class AuthApiTest {
|
||||
|
||||
JHAppApiExecution jhAppApiExecution = new JHAppApiExecution(JHClientConfig.client);
|
||||
|
||||
/**
|
||||
* 测试获取token
|
||||
*/
|
||||
@Test
|
||||
public void testGetToken() {
|
||||
String token = jhAppApiExecution.getToken("jhadmin");
|
||||
System.out.println("token:" + token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试退出登录
|
||||
*/
|
||||
@Test
|
||||
public void testLogout(){
|
||||
jhAppApiExecution.logout("lqyan");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user