From 93c735c1122aeb336bd65bc45d6b14eaa980df06 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Thu, 18 Nov 2021 15:03:28 +0100 Subject: add method to inject http headers into Apache HTTP-Client requests --- .../at/gv/egiz/eaaf/core/impl/http/HttpUtils.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'eaaf_core_utils/src/main/java/at/gv/egiz/eaaf') diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java index 3058c9b5..dd6f69ee 100644 --- a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java @@ -34,6 +34,7 @@ import javax.net.ssl.SSLContext; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; @@ -48,6 +49,7 @@ import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; import at.gv.egiz.eaaf.core.exceptions.EaafFactoryException; import at.gv.egiz.eaaf.core.impl.data.Pair; import at.gv.egiz.eaaf.core.impl.data.Triple; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -183,6 +185,23 @@ public class HttpUtils { } } + /** + * Inject HTTP header into http request. + * + *
The header is only set if HeaderValue is not null
+ * + * @param req Http request object + * @param headerName HeaderName + * @param headerValue HeaderValue + */ + public static void addHeaderIfNotEmpty(@NonNull HttpRequest req, @NonNull String headerName, + @Nullable String headerValue) { + if (StringUtils.isNotEmpty(headerValue)) { + req.addHeader(headerName, headerValue); + + } + } + /** * Initialize a {@link SSLContext} with a {@link KeyStore} that uses X509 Client * authentication. -- cgit v1.2.3