XuqmGroup-Server/update-service/src/main/java/com/xuqm/update/service/ConnectivityValidationService.java
XuqmGroup f6c06db04b feat(deploy): 添加生产环境部署配置和联调环境切换功能
- 新增 .env.production.example 配置文件,包含所有微服务的数据库和Redis配置
- 添加 compose.production.yaml Docker Compose部署文件,定义web和各服务容器
- 实现Android SDK环境切换功能,支持外部服务和本地联调模式切换
- 添加推送注册状态管理和接收开关设置界面
- 集成演示服务的应用密钥客户端和认证服务实现
- 完善文档说明各SDK模块的集成和使用方法
2026-04-30 11:47:01 +08:00

54 行
2.1 KiB
Java

package com.xuqm.update.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;
@Service
public class ConnectivityValidationService {
private final HttpClient httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.build();
private final ObjectMapper objectMapper = new ObjectMapper();
public void validateCallbackUrl(String url, String label) {
if (url == null || url.isBlank()) {
return;
}
String normalized = url.trim();
if (!(normalized.startsWith("http://") || normalized.startsWith("https://"))) {
throw new IllegalArgumentException(label + " must start with http:// or https://");
}
try {
Map<String, Object> body = new LinkedHashMap<>();
body.put("probe", true);
body.put("label", label);
body.put("timestamp", System.currentTimeMillis());
String payload = objectMapper.writeValueAsString(body);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(normalized))
.timeout(Duration.ofSeconds(5))
.header("Content-Type", "application/json")
.header("X-Xuqm-Connectivity-Check", "true")
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
int status = response.statusCode();
if (status < 200 || status >= 400) {
throw new IllegalStateException("connectivity check failed with HTTP " + status);
}
} catch (Exception e) {
throw new IllegalArgumentException("cannot connect to " + label + ": " + normalized, e);
}
}
}