根据参数查询会话列表及执行器代码的优化

This commit is contained in:
lqyan
2024-02-04 12:02:06 +08:00
parent fe225c6ba8
commit fe8fb7f2ec
6 changed files with 282 additions and 30 deletions

View File

@@ -5,12 +5,15 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.jhinno.sdk.openapi.ArgsException; import com.jhinno.sdk.openapi.ArgsException;
import com.jhinno.sdk.openapi.CommonConstant; import com.jhinno.sdk.openapi.CommonConstant;
import com.jhinno.sdk.openapi.ServiceException; import com.jhinno.sdk.openapi.ServiceException;
import com.jhinno.sdk.openapi.api.app.AppStartedInfo;
import com.jhinno.sdk.openapi.api.auth.AuthPathConstant; import com.jhinno.sdk.openapi.api.auth.AuthPathConstant;
import com.jhinno.sdk.openapi.api.auth.Token; import com.jhinno.sdk.openapi.api.auth.Token;
import com.jhinno.sdk.openapi.client.JHApiClient; import com.jhinno.sdk.openapi.client.JHApiClient;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@@ -103,12 +106,8 @@ public class JHApiExecution {
String base64 = aes.encryptBase64(String.format("%s,%s", username, System.currentTimeMillis())); String base64 = aes.encryptBase64(String.format("%s,%s", username, System.currentTimeMillis()));
params.put("username", base64); params.put("username", base64);
String url = JHApiClient.getUrl(AuthPathConstant.AUTH_TOKEN_PATH, params); String url = JHApiClient.getUrl(AuthPathConstant.AUTH_TOKEN_PATH, params);
ResponseResult<Token> result = jhApiClient.get(url, new TypeReference<ResponseResult<Token>>() { Token token = get(url, new TypeReference<ResponseResult<Token>>() {
}); });
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
throw new ServiceException(AuthPathConstant.AUTH_TOKEN_PATH, result.getCode(), result.getMessage());
}
Token token = result.getData();
tokenInfo = new TokenInfo(); tokenInfo = new TokenInfo();
tokenInfo.setUserName(username); tokenInfo.setUserName(username);
tokenInfo.setToken(token.getToken()); tokenInfo.setToken(token.getToken());
@@ -136,9 +135,78 @@ public class JHApiExecution {
* @return 请求头 * @return 请求头
*/ */
protected Map<String, String> getHeaders(String username) { protected Map<String, String> getHeaders(String username) {
if (StringUtils.isBlank(username)) {
return new HashMap<>();
}
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put("token", getToken(username)); headers.put("token", getToken(username));
return headers; return headers;
} }
/**
* 发起一个匿名的GET请求
*
* @param path 请求路径
* @param type 响应数据类型
* @param <T> 数据类型
* @return 响应数据
*/
public <T> T get(String path, TypeReference<ResponseResult<T>> type) {
return get(path, null, type);
}
/**
* 发起携带token的GET请求
*
* @param username 用户名
* @param path 请求路径
* @param type 响应的数据类型
* @param <T> 数据类型
* @return 返回的数据
*/
public <T> T get(String path, String username, TypeReference<ResponseResult<T>> type) {
ResponseResult<T> result = jhApiClient.get(path, getHeaders(username), type);
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
throw new ServiceException(AuthPathConstant.AUTH_TOKEN_PATH, result.getCode(), result.getMessage());
}
return result.getData();
}
/**
* 发起一个有返回值的POST请求
*
* @param path 请求路径
* @param username 用户名
* @param body 请求体
* @param type 强求提的数据类型
* @param <R> 返回的数据类型
* @param <B> 请求体的数据类型
* @return 请求后的数据
*/
public <R, B> R post(String path, String username, B body, TypeReference<ResponseResult<R>> type) {
ResponseResult<R> result = jhApiClient.post(path, body, getHeaders(username), type);
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
throw new ServiceException(path, result.getCode(), result.getMessage());
}
return result.getData();
}
/**
* 发起一个没有返回值的POST请求
*
* @param path 请求路径
* @param username 用户名
* @param body 请求体
* @param <B> 请求体数据类型
*/
public <B> void post(String path, String username, B body) {
ResponseResult<?> result = jhApiClient.post(path, body, getHeaders(username), new TypeReference<ResponseResult<?>>() {
});
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
throw new ServiceException(path, result.getCode(), result.getMessage());
}
}
} }

View File

@@ -17,4 +17,9 @@ public class AppPathConstant {
* 查询会列表 * 查询会列表
*/ */
public static final String APPS_SESSIONS_ALL_PATH = "/ws/api/apps/sessions/all"; public static final String APPS_SESSIONS_ALL_PATH = "/ws/api/apps/sessions/all";
/**
* 使用参数查询会话列表
*/
public static final String APPS_SESSIONS_PATH = "/ws/api/apps/sessions";
} }

View File

@@ -2,13 +2,13 @@ package com.jhinno.sdk.openapi.api.app;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.jhinno.sdk.openapi.CommonConstant;
import com.jhinno.sdk.openapi.ServiceException; import com.jhinno.sdk.openapi.ServiceException;
import com.jhinno.sdk.openapi.api.JHApiExecution; import com.jhinno.sdk.openapi.api.JHApiExecution;
import com.jhinno.sdk.openapi.api.ResponseResult; import com.jhinno.sdk.openapi.api.ResponseResult;
import com.jhinno.sdk.openapi.client.JHApiClient; import com.jhinno.sdk.openapi.client.JHApiClient;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -37,14 +37,10 @@ public class JHAppApiExecution extends JHApiExecution {
*/ */
public AppStartedInfo desktopStart(String username, String appId, AppStartRequest appStartRequest) { public AppStartedInfo desktopStart(String username, String appId, AppStartRequest appStartRequest) {
String path = AppPathConstant.APPS_START_PATH.replace("{appId}", appId); String path = AppPathConstant.APPS_START_PATH.replace("{appId}", appId);
ResponseResult<List<AppStartedInfo>> result = jhApiClient.post(path, appStartRequest, getHeaders(username), new TypeReference<ResponseResult<List<AppStartedInfo>>>() { List<AppStartedInfo> data = post(path, username, appStartRequest, new TypeReference<ResponseResult<List<AppStartedInfo>>>() {
}); });
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
throw new ServiceException(path, result.getCode(), result.getMessage());
}
List<AppStartedInfo> data = result.getData();
if (CollectionUtil.isEmpty(data)) { if (CollectionUtil.isEmpty(data)) {
throw new ServiceException(path, result.getCode(), "获取到的会话信息为空"); throw new ServiceException(path, 500, "获取到的会话信息为空");
} }
return data.get(0); return data.get(0);
} }
@@ -68,18 +64,35 @@ public class JHAppApiExecution extends JHApiExecution {
* *
* @return 会话列表 * @return 会话列表
*/ */
public List<Map<String, Object>> getDesktopList(String username) { public List<SessionInfo> getDesktopList(String username) {
ResponseResult<List<Map<String, Object>>> result = jhApiClient.get( return get(AppPathConstant.APPS_SESSIONS_ALL_PATH, username, new TypeReference<ResponseResult<List<SessionInfo>>>() {
AppPathConstant.APPS_SESSIONS_ALL_PATH,
getHeaders(username),
new TypeReference<ResponseResult<List<Map<String, Object>>>>() {
}); });
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
throw new ServiceException(AppPathConstant.APPS_SESSIONS_ALL_PATH, result.getCode(), result.getMessage());
}
return result.getData();
} }
/**
* 使用参数查询会话列表
*
* <p>
* 注sessionIds 和 sessionName 不能同时为空
* </p>
*
* @param username 用户名
* @param sessionIds 会话id列表 (非必填)
* @param sessionName 会话名称 (非必填)
* @return 会话列表
*/
public List<SessionInfo> getDesktopsByParams(String username, List<String> sessionIds, String sessionName) {
Map<String, Object> params = new HashMap<>(2);
if (CollectionUtil.isNotEmpty(sessionIds)) {
params.put("sessionIds", String.join(",", sessionIds));
}
if (StringUtils.isNotBlank(sessionName)) {
params.put("sessionName", sessionName);
}
String path = JHApiClient.getUrl(AppPathConstant.APPS_SESSIONS_PATH, params);
return get(path, username, new TypeReference<ResponseResult<List<SessionInfo>>>() {
});
}
} }

View File

@@ -0,0 +1,138 @@
package com.jhinno.sdk.openapi.api.app;
import com.sun.java.swing.plaf.windows.resources.windows;
import lombok.Data;
import java.util.Date;
/**
* 会话信息
*
* @author yanlongqi
* @date 2024/2/4 10:32
*/
@Data
public class SessionInfo {
/**
* 最后使用时间
*/
private String lastUseTime;
/**
* 转移操作员权限
*/
private String transfer_operator_right;
/**
* 操作员
*/
private String operator;
/**
* 协议
* <ul>
* <li>jhapp</li>
* <li>其他</li>
* </ul>
*/
private String protocol;
/**
* 主机
*/
private String host;
/**
*
*/
private String backend;
/**
* 会话id
*/
private String id;
/**
* 应用id
*/
private String app_id;
/**
*
*/
private Boolean isMtUsing;
/**
* 桌面类型
*/
private String desktop_type;
/**
* 密级英文名
*/
private String confidentialEn;
/**
* 创建时间
*/
private String createDate;
/**
* 密级
*/
private String confidential;
/**
* 会话属主
*/
private String owner;
/**
* 密级中文名称
*/
private String confidential_cn;
/**
* 操作系统
*/
private String os;
/**
* 启动模式
*/
private String startmode;
/**
* JHClient的地址用于拉起对应会话的JHClient客户端
* <ul>
* <li>
* 测试时:可将其粘贴纸浏览器的地址栏里面拉起JHClient客户端
* </li>
* <li>
* 开发时:通过使用a标签或者使用iframe的方式拉起JHClient客户端
* </li>
* </ul>
*/
private String jhappUrl;
/**
* 会话属主姓名
*/
private String ownername;
/**
* 是否分享给我
*/
private Boolean shareMe;
/**
* 会话id
*/
private String session_id;
/**
* 执行时间
*/
private String executionTime;
/**
* 是否docker会话
*/
private String isDocker;
/**
* 会话名称
*/
private String name;
/**
* 是否共享
*/
private Boolean isShare;
/**
* 状态
*/
private String status;
}

View File

@@ -1,5 +1,6 @@
package com.jhinno.sdk.openapi.client; package com.jhinno.sdk.openapi.client;
import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.jhinno.sdk.openapi.ClientErrorCode; import com.jhinno.sdk.openapi.ClientErrorCode;
@@ -29,8 +30,10 @@ import java.net.URLEncoder;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.TimeZone;
/** /**
* 提供请求的工具 * 提供请求的工具
@@ -48,6 +51,12 @@ public class JHApiClient {
*/ */
private final String baseUrl; private final String baseUrl;
/**
* 对象转换器
*/
private ObjectMapper mapper;
/** /**
* 初始化一个JHApiClient的实例可使用自定义的客户端 * 初始化一个JHApiClient的实例可使用自定义的客户端
* *
@@ -58,6 +67,9 @@ public class JHApiClient {
this.baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; this.baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
this.closeableHttpClient = closeableHttpClient; this.closeableHttpClient = closeableHttpClient;
this.requestConfig = RequestConfig.custom().setSocketTimeout(DefaultHttpClientConfig.SOCKET_TIMEOUT).setConnectTimeout(DefaultHttpClientConfig.CONNECT_TIMEOUT).setConnectionRequestTimeout(DefaultHttpClientConfig.CONNECTION_REQUEST_TIMEOUT).build(); 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));
} }
@@ -147,6 +159,16 @@ public class JHApiClient {
} }
} }
/**
* 设置自定义的jackson序列化配置
*
* @param mapper 序列化器
*/
public void setMapper(ObjectMapper mapper) {
this.mapper = mapper;
}
/** /**
* <p> * <p>
* 设置一个HTTP请求的配置 * 设置一个HTTP请求的配置
@@ -217,7 +239,6 @@ public class JHApiClient {
public <T> T request(HttpRequestBase httpRequest, Map<String, String> headers, TypeReference<T> type) { public <T> T request(HttpRequestBase httpRequest, Map<String, String> headers, TypeReference<T> type) {
try { try {
InputStream content = request(httpRequest, headers).getContent(); InputStream content = request(httpRequest, headers).getContent();
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(content, type); return mapper.readValue(content, type);
} catch (Exception e) { } catch (Exception e) {
throw new ClientException(e.getMessage()); throw new ClientException(e.getMessage());
@@ -310,7 +331,6 @@ public class JHApiClient {
HttpPost httpPost = new HttpPost(baseUrl + path); HttpPost httpPost = new HttpPost(baseUrl + path);
try { try {
if (body != null) { if (body != null) {
ObjectMapper mapper = new ObjectMapper();
String bodyStr = mapper.writeValueAsString(body); String bodyStr = mapper.writeValueAsString(body);
httpPost.setEntity(new StringEntity(bodyStr, "utf-8")); httpPost.setEntity(new StringEntity(bodyStr, "utf-8"));
} }
@@ -339,7 +359,6 @@ public class JHApiClient {
HttpPut httpPost = new HttpPut(baseUrl + path); HttpPut httpPost = new HttpPut(baseUrl + path);
try { try {
if (body != null) { if (body != null) {
ObjectMapper mapper = new ObjectMapper();
String bodyStr = mapper.writeValueAsString(body); String bodyStr = mapper.writeValueAsString(body);
httpPost.setEntity(new StringEntity(bodyStr, "utf-8")); httpPost.setEntity(new StringEntity(bodyStr, "utf-8"));
} }

View File

@@ -3,11 +3,11 @@ package com.jhinno.sdk.openapi.test.app;
import com.jhinno.sdk.openapi.api.app.AppStartRequest; import com.jhinno.sdk.openapi.api.app.AppStartRequest;
import com.jhinno.sdk.openapi.api.app.AppStartedInfo; import com.jhinno.sdk.openapi.api.app.AppStartedInfo;
import com.jhinno.sdk.openapi.api.app.JHAppApiExecution; import com.jhinno.sdk.openapi.api.app.JHAppApiExecution;
import com.jhinno.sdk.openapi.api.app.SessionInfo;
import com.jhinno.sdk.openapi.client.JHApiClient; import com.jhinno.sdk.openapi.client.JHApiClient;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.*;
import java.util.Map;
/** /**
* 会话启动相关单元测试 * 会话启动相关单元测试
@@ -21,7 +21,7 @@ public class AppApiTest {
/** /**
* 初始化JHApi客户端 * 初始化JHApi客户端
*/ */
public static final JHApiClient client = JHApiClient.build("https://192.168.87.25/appform"); public static final JHApiClient client = JHApiClient.build("https://192.168.0.22/appform");
/** /**
@@ -55,7 +55,16 @@ public class AppApiTest {
*/ */
@Test @Test
public void testGetSessionsList() { public void testGetSessionsList() {
List<Map<String, Object>> desktopList = jhAppApiExecution.getDesktopList("jhadmin"); List<SessionInfo> desktopList = jhAppApiExecution.getDesktopList("jhadmin");
System.out.println(desktopList);
}
/**
* 测试根据参数查询会话列表
*/
@Test
public void testGetDesktopsByParams() {
List<SessionInfo> desktopList = jhAppApiExecution.getDesktopsByParams("jhadmin", null, "Windows桌面");
System.out.println(desktopList); System.out.println(desktopList);
} }
} }