Skip to content

Commit

Permalink
Handle text/* in response content type, so we can show message for e.…
Browse files Browse the repository at this point in the history
…g. text/html
  • Loading branch information
hmusum committed Feb 9, 2025
1 parent b7ca21b commit 484b7cd
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down

0 comments on commit 484b7cd

Please sign in to comment.