Browse Source

add-D4Metric-6.2

Qyanger 6 months ago
parent
commit
b9c813d216

+ 2 - 0
.gitignore

@@ -1,4 +1,6 @@
 HELP.md
+AGENTS.md
+README-API.md
 target/
 .mvn/wrapper/maven-wrapper.jar
 !**/src/main/**/target/

+ 26 - 16
src/main/java/com/demo/seecanalysisbackend/config/ElasticsearchConfig.java

@@ -1,10 +1,13 @@
 package com.demo.seecanalysisbackend.config;
 
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.protocol.HttpContext;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.data.elasticsearch.client.ClientConfiguration;
 import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
-import org.springframework.data.elasticsearch.support.HttpHeaders;
+import org.springframework.data.elasticsearch.client.elc.rest5_client.Rest5Clients;
 
 import java.net.URI;
 import java.time.Duration;
@@ -22,15 +25,15 @@ public class ElasticsearchConfig extends ElasticsearchConfiguration {
     @Value("${spring.elasticsearch.password:}")
     private String password;
 
-    @Value("${spring.elasticsearch.client.compatibility-mode:true}")
-    private boolean compatibilityMode;
-
     @Value("${spring.elasticsearch.client.socket-timeout:${spring.elasticsearch.socket-timeout:60s}}")
     private Duration socketTimeout;
 
     @Value("${spring.elasticsearch.client.connection-timeout:${spring.elasticsearch.connection-timeout:5s}}")
     private Duration connectionTimeout;
 
+    @Value("${spring.elasticsearch.client.force-json-media-type:true}")
+    private boolean forceJsonMediaType;
+
     @Override
     public ClientConfiguration clientConfiguration() {
         String[] hosts = Arrays.stream(uris.split(","))
@@ -42,8 +45,18 @@ public class ElasticsearchConfig extends ElasticsearchConfiguration {
         ClientConfiguration.TerminalClientConfigurationBuilder builder = ClientConfiguration.builder()
             .connectedTo(hosts)
             .withConnectTimeout(connectionTimeout)
-            .withSocketTimeout(socketTimeout)
-            .withDefaultHeaders(defaultHeaders());
+            .withSocketTimeout(socketTimeout);
+
+        if (forceJsonMediaType) {
+            builder = builder.withClientConfigurer(
+                Rest5Clients.ElasticsearchRest5ClientConfigurationCallback.from(restClientBuilder -> {
+                    restClientBuilder.setHttpClientConfigCallback(httpClientBuilder ->
+                        httpClientBuilder.addRequestInterceptorLast(this::rewriteMediaTypeHeaders)
+                    );
+                    return restClientBuilder;
+                })
+            );
+        }
 
         if (username != null && !username.isBlank()) {
             return builder.withBasicAuth(username, password == null ? "" : password).build();
@@ -52,17 +65,14 @@ public class ElasticsearchConfig extends ElasticsearchConfiguration {
         return builder.build();
     }
 
-    private HttpHeaders defaultHeaders() {
-        HttpHeaders headers = new HttpHeaders();
-        if (compatibilityMode) {
-            headers.add("Accept", "application/vnd.elasticsearch+json;compatible-with=7");
-            headers.add("Content-Type", "application/vnd.elasticsearch+json;compatible-with=7");
-            return headers;
-        }
+    private void rewriteMediaTypeHeaders(HttpRequest request, EntityDetails entityDetails, HttpContext context) {
+        request.removeHeaders("Accept");
+        request.setHeader("Accept", "application/json");
 
-        headers.add("Accept", "application/json");
-        headers.add("Content-Type", "application/json");
-        return headers;
+        if (entityDetails != null) {
+            request.removeHeaders("Content-Type");
+            request.setHeader("Content-Type", "application/json");
+        }
     }
 
     private String toHostPort(String uriText) {

+ 2 - 1
src/main/resources/application.yml

@@ -9,7 +9,8 @@ spring:
     connection-timeout: ${ELASTICSEARCH_CONNECTION_TIMEOUT:5s}
     socket-timeout: ${ELASTICSEARCH_SOCKET_TIMEOUT:10s}
     client:
-      compatibility-mode: ${ELASTICSEARCH_COMPATIBILITY_MODE:true}
+      compatibility-mode: ${ELASTICSEARCH_COMPATIBILITY_MODE:false}
+      force-json-media-type: ${ELASTICSEARCH_FORCE_JSON_MEDIA_TYPE:true}
       connection-timeout: ${ELASTICSEARCH_CLIENT_CONNECTION_TIMEOUT:5s}
       socket-timeout: ${ELASTICSEARCH_CLIENT_SOCKET_TIMEOUT:60s}