From 8a9c6ddb20a17f7f972fed8ef0621c3a99b80bb6 Mon Sep 17 00:00:00 2001 From: Harald Musum Date: Sat, 8 Feb 2025 16:22:18 +0100 Subject: [PATCH] Handle text/* in response content type, so we can show message for e.g. text/html --- .../feed/client/impl/HttpFeedClient.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/impl/HttpFeedClient.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/impl/HttpFeedClient.java index ed1918051465..53024e337695 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/impl/HttpFeedClient.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/impl/HttpFeedClient.java @@ -17,7 +17,6 @@ import com.fasterxml.jackson.core.StreamReadConstraints; import java.io.IOException; -import java.io.UncheckedIOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.time.Duration; @@ -155,14 +154,7 @@ private void verifyConnection(FeedClientBuilderImpl builder, ClusterFactory clus cluster.dispatch(request, future); HttpResponse response = future.get(20, TimeUnit.SECONDS); if (response.code() != 200) { - String message; - if (response.body() != null) switch (response.contentType()) { - case "application/json": message = parseMessage(response.body()); break; - case "text/plain": message = new String(response.body(), UTF_8); break; - default: message = response.toString(); break; - } - else message = response.toString(); - + String message = getMessageFromResponse(response); // Old server ignores ?dryRun=true, but getting this particular error message means everything else is OK. if (response.code() == 400 && "Could not read document, no document?".equals(message)) { if (builder.speedTest) throw new FeedException("server does not support speed test; upgrade to a newer version"); @@ -184,6 +176,22 @@ private void verifyConnection(FeedClientBuilderImpl builder, ClusterFactory clus } } + private static String getMessageFromResponse(HttpResponse response) { + String message; + byte[] body = response.body(); + if (body != null) { + String contentType = response.contentType(); + if (contentType.equals("application/json")) + message = parseMessage(body); + else if (contentType.startsWith("text/")) + message = new String(body, UTF_8); + else + message = response.toString(); + } + else message = response.toString(); + return message; + } + private static String parseMessage(byte[] json) { try { return parse(null, json).message;