From f18de8de9fe55304ed6c76209f14ad199d222401 Mon Sep 17 00:00:00 2001 From: Chris Tremblay Date: Thu, 8 Feb 2024 19:26:03 -0500 Subject: [PATCH] "host" header breaking certain ingress controllers, its only used by APIM so remove it when APIM isn't used --- .../java/com/chtrembl/petstoreapp/model/WebRequest.java | 9 +++++++++ .../petstoreapp/service/PetStoreServiceImpl.java | 5 +---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/petstore/petstoreapp/src/main/java/com/chtrembl/petstoreapp/model/WebRequest.java b/petstore/petstoreapp/src/main/java/com/chtrembl/petstoreapp/model/WebRequest.java index cb4863de..2c4f6e6c 100644 --- a/petstore/petstoreapp/src/main/java/com/chtrembl/petstoreapp/model/WebRequest.java +++ b/petstore/petstoreapp/src/main/java/com/chtrembl/petstoreapp/model/WebRequest.java @@ -2,6 +2,8 @@ import java.io.Serializable; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.stereotype.Component; @@ -14,7 +16,14 @@ public class WebRequest implements Serializable { private MultiValueMap headers = new LinkedMultiValueMap<>(); + @Autowired + private ContainerEnvironment containerEnvironment; + public MultiValueMap getHeaders() { + if(!StringUtils.isEmpty(this.containerEnvironment.getPetstoreAPIMHost()) && this.headers.get("host") == null) + { + this.headers.add("host", this.containerEnvironment.getPetstoreAPIMHost()); + } return this.headers; } diff --git a/petstore/petstoreapp/src/main/java/com/chtrembl/petstoreapp/service/PetStoreServiceImpl.java b/petstore/petstoreapp/src/main/java/com/chtrembl/petstoreapp/service/PetStoreServiceImpl.java index d7b7644b..70f9daca 100644 --- a/petstore/petstoreapp/src/main/java/com/chtrembl/petstoreapp/service/PetStoreServiceImpl.java +++ b/petstore/petstoreapp/src/main/java/com/chtrembl/petstoreapp/service/PetStoreServiceImpl.java @@ -73,11 +73,11 @@ public Collection getPets(String category) { this.sessionUser.getCustomEventProperties(), null); try { Consumer consumer = it -> it.addAll(this.webRequest.getHeaders()); + pets = this.petServiceWebClient.get().uri("petstorepetservice/v2/pet/findByStatus?status=available") .accept(MediaType.APPLICATION_JSON) .headers(consumer) .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) - .header("host", this.containerEnvironment.getPetstoreAPIMHost()) .header("session-id", this.sessionUser.getSessionId()) .header("Ocp-Apim-Subscription-Key", this.containerEnvironment.getPetStoreServicesSubscriptionKey()) .header("Cache-Control", "no-cache").header("Ocp-Apim-Trace", "true").retrieve() @@ -136,7 +136,6 @@ public Collection getProducts(String category, List tags) { .accept(MediaType.APPLICATION_JSON) .headers(consumer) .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) - .header("host", this.containerEnvironment.getPetstoreAPIMHost()) .header("session-id", this.sessionUser.getSessionId()) .header("Ocp-Apim-Subscription-Key", this.containerEnvironment.getPetStoreServicesSubscriptionKey()) .header("Cache-Control", "no-cache").header("Ocp-Apim-Trace", "true").retrieve() @@ -224,7 +223,6 @@ public void updateOrder(long productId, int quantity, boolean completeOrder) { .accept(MediaType.APPLICATION_JSON) .headers(consumer) .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) - .header("host", this.containerEnvironment.getPetstoreAPIMHost()) .header("session-id", this.sessionUser.getSessionId()) .header("Ocp-Apim-Subscription-Key", this.containerEnvironment.getPetStoreServicesSubscriptionKey()) .header("Cache-Control", "no-cache").header("Ocp-Apim-Trace", "true").retrieve() @@ -251,7 +249,6 @@ public Order retrieveOrder(String orderId) { .accept(MediaType.APPLICATION_JSON) .headers(consumer) .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) - .header("host", this.containerEnvironment.getPetstoreAPIMHost()) .header("session-id", this.sessionUser.getSessionId()) .header("Ocp-Apim-Subscription-Key", this.containerEnvironment.getPetStoreServicesSubscriptionKey()) .header("Cache-Control", "no-cache").header("Ocp-Apim-Trace", "true").retrieve()