|
|
@@ -1,15 +1,22 @@
|
|
|
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 lombok.extern.slf4j.Slf4j;
|
|
|
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.client.elc.rest5_client.Rest5Clients;
|
|
|
|
|
|
import java.net.URI;
|
|
|
import java.time.Duration;
|
|
|
+import java.util.Locale;
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
@Configuration
|
|
|
+@Slf4j
|
|
|
public class ElasticsearchConfig extends ElasticsearchConfiguration {
|
|
|
|
|
|
@Value("${spring.elasticsearch.uris}")
|
|
|
@@ -27,6 +34,9 @@ public class ElasticsearchConfig extends ElasticsearchConfiguration {
|
|
|
@Value("${spring.elasticsearch.client.connection-timeout:${spring.elasticsearch.connection-timeout:5s}}")
|
|
|
private Duration connectionTimeout;
|
|
|
|
|
|
+ @Value("${spring.elasticsearch.client.media-type-mode:json}")
|
|
|
+ private String mediaTypeMode;
|
|
|
+
|
|
|
@Override
|
|
|
public ClientConfiguration clientConfiguration() {
|
|
|
String[] hosts = Arrays.stream(uris.split(","))
|
|
|
@@ -40,6 +50,18 @@ public class ElasticsearchConfig extends ElasticsearchConfiguration {
|
|
|
.withConnectTimeout(connectionTimeout)
|
|
|
.withSocketTimeout(socketTimeout);
|
|
|
|
|
|
+ String normalizedMode = mediaTypeMode == null ? "json" : mediaTypeMode.trim().toLowerCase(Locale.ROOT);
|
|
|
+ if (!"default".equals(normalizedMode)) {
|
|
|
+ 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();
|
|
|
}
|
|
|
@@ -47,6 +69,34 @@ public class ElasticsearchConfig extends ElasticsearchConfiguration {
|
|
|
return builder.build();
|
|
|
}
|
|
|
|
|
|
+ private void rewriteMediaTypeHeaders(HttpRequest request, EntityDetails entityDetails, HttpContext context) {
|
|
|
+ String normalizedMode = mediaTypeMode == null ? "json" : mediaTypeMode.trim().toLowerCase(Locale.ROOT);
|
|
|
+
|
|
|
+ if ("default".equals(normalizedMode)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ request.removeHeaders("Accept");
|
|
|
+
|
|
|
+ if ("compat7".equals(normalizedMode)) {
|
|
|
+ request.setHeader("Accept", "application/vnd.elasticsearch+json;compatible-with=7");
|
|
|
+ if (entityDetails != null) {
|
|
|
+ request.removeHeaders("Content-Type");
|
|
|
+ request.setHeader("Content-Type", "application/vnd.elasticsearch+json;compatible-with=7");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ request.setHeader("Accept", "application/json");
|
|
|
+ if (entityDetails != null) {
|
|
|
+ request.removeHeaders("Content-Type");
|
|
|
+ request.setHeader("Content-Type", "application/json");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("ES media type mode={} for request {} {}", normalizedMode, request.getMethod(), request.getRequestUri());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private String toHostPort(String uriText) {
|
|
|
URI uri = URI.create(uriText);
|
|
|
int port = uri.getPort();
|