update(REQ-2516) - 完善记录接口调用方

This commit is contained in:
wangli 2024-06-18 09:57:43 +08:00
parent 0892ed394b
commit 7319ab90ad

View File

@ -3,6 +3,7 @@ package cn.axzo.workflow.server.common.interceptor;
import cn.axzo.workflow.core.common.exception.WorkflowEngineException;
import cn.axzo.workflow.core.repository.entity.ExtAxProperty;
import cn.axzo.workflow.core.service.ExtAxPropertyService;
import cn.axzo.workflow.server.common.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.springframework.beans.factory.annotation.Autowired;
@ -12,8 +13,10 @@ import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Duration;
import java.util.Enumeration;
import java.util.Objects;
import java.util.Optional;
import static cn.axzo.workflow.client.config.WorkflowRequestInterceptor.HEADER_API_VERSION;
import static cn.axzo.workflow.client.config.WorkflowRequestInterceptor.HEADER_HTTP_CLIENT;
@ -36,6 +39,8 @@ public class RequestHeaderContextInterceptor implements HandlerInterceptor {
private String serviceVersion;
@Autowired
private ExtAxPropertyService extAxPropertyService;
private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
private static final String REPEAT_KEY = "global:api_application:";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
@ -78,27 +83,47 @@ public class RequestHeaderContextInterceptor implements HandlerInterceptor {
if (!StringUtils.hasText(request.getHeader(HEADER_SERVER_NAME))) {
return;
}
ExtAxProperty property = extAxPropertyService.getByName(request.getHeader(HEADER_SERVER_NAME)).map(entity -> {
entity.setCreated(!Objects.equals(entity.getValue(), serviceVersion));
entity.setValue(headerClientVersion);
return entity;
}).orElseGet(() -> {
ExtAxProperty extAxProperty = new ExtAxProperty();
extAxProperty.setCreated(true);
extAxProperty.setName(request.getHeader(HEADER_SERVER_NAME));
extAxProperty.setValue(clientVersion.toString());
return extAxProperty;
});
if (property.getCreated()) {
if (Objects.isNull(property.getId())) {
extAxPropertyService.add(property);
} else {
extAxPropertyService.update(property);
}
String requestApplicationName = request.getHeader(HEADER_SERVER_NAME);
Optional<ExtAxProperty> extAxProperty = extAxPropertyService.getByName(requestApplicationName);
String cacheRepeatKey = REPEAT_KEY + requestApplicationName;
log.info("repeatApi key: {}", cacheRepeatKey);
String key = RedisUtils.getCacheObject(cacheRepeatKey);
if (Objects.isNull(key)) {
RedisUtils.setCacheObject(cacheRepeatKey, "", Duration.ofSeconds(5));
KEY_CACHE.set(cacheRepeatKey);
insert(extAxProperty, headerClientVersion, clientVersion);
} else {
update(extAxProperty, headerClientVersion, clientVersion);
}
}
private void update(Optional<ExtAxProperty> extAxProperty, String requestApplicationName, DefaultArtifactVersion clientVersion) {
if (!extAxProperty.isPresent()) {
return;
}
ExtAxProperty property = extAxProperty.get();
if (Objects.equals(property.getValue(), clientVersion.toString())) {
return;
}
property.setValue(clientVersion.toString());
extAxPropertyService.update(property);
}
private void insert(Optional<ExtAxProperty> extAxProperty, String requestApplicationName, DefaultArtifactVersion clientVersion) {
if (extAxProperty.isPresent()) {
return;
}
extAxPropertyService.add(extAxProperty.orElseGet(() -> {
ExtAxProperty property = new ExtAxProperty();
property.setCreated(true);
property.setName(requestApplicationName);
property.setValue(clientVersion.toString());
return property;
}));
}
private void printHeader(HttpServletRequest request) {
Enumeration<String> headerNames = request.getHeaderNames();
log.info("parse header start, current uri: {}", request.getRequestURI());