-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
REST client: introduce a strict deprecation mode #33708
Changes from 2 commits
47f45cb
ef1c4ea
38202c9
514ef27
486698e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,11 @@ | |
import org.apache.http.RequestLine; | ||
import org.apache.http.StatusLine; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Holds an elasticsearch response. It wraps the {@link HttpResponse} returned and associates it with | ||
|
@@ -96,6 +100,40 @@ public HttpEntity getEntity() { | |
return response.getEntity(); | ||
} | ||
|
||
private static Pattern WARNING_HEADER_PATTERN = Pattern.compile( | ||
"299 " + // warn code | ||
"Elasticsearch-\\d+\\.\\d+\\.\\d+(?:-(?:alpha|beta|rc)\\d+)?(?:-SNAPSHOT)?-(?:[a-f0-9]{7}|Unknown) " + // warn agent | ||
"\"((?:\t| |!|[\\x23-\\x5B]|[\\x5D-\\x7E]|[\\x80-\\xFF]|\\\\|\\\\\")*)\" " + // quoted warning value, captured | ||
// quoted RFC 1123 date format | ||
"\"" + // opening quote | ||
"(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), " + // weekday | ||
"\\d{2} " + // 2-digit day | ||
"(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) " + // month | ||
"\\d{4} " + // 4-digit year | ||
"\\d{2}:\\d{2}:\\d{2} " + // (two-digit hour):(two-digit minute):(two-digit second) | ||
"GMT" + // GMT | ||
"\""); // closing quote | ||
|
||
|
||
public List<String> getWarnings() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add javadoc for this? |
||
List<String> warnings = new ArrayList<>(); | ||
for (Header header : response.getHeaders("Warning")) { | ||
String warning = header.getValue(); | ||
final Matcher matcher = WARNING_HEADER_PATTERN.matcher(warning); | ||
if (matcher.matches()) { | ||
warnings.add(matcher.group(1)); | ||
continue; | ||
} | ||
warnings.add(warning); | ||
} | ||
return warnings; | ||
} | ||
|
||
public boolean hasWarnings() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And could you add javadoc for this too? |
||
Header[] warnings = response.getHeaders("Warning"); | ||
return warnings != null && warnings.length > 0; | ||
} | ||
|
||
HttpResponse getHttpResponse() { | ||
return response; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,10 @@ private static String buildMessage(Response response) throws IOException { | |
response.getStatusLine().toString() | ||
); | ||
|
||
if (response.hasWarnings()) { | ||
message += "\n" + "Warnings: " + response.getWarnings(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
|
||
HttpEntity entity = response.getEntity(); | ||
if (entity != null) { | ||
if (entity.isRepeatable() == false) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ public abstract class ESRestTestCase extends ESTestCase { | |
public static final String CLIENT_RETRY_TIMEOUT = "client.retry.timeout"; | ||
public static final String CLIENT_SOCKET_TIMEOUT = "client.socket.timeout"; | ||
public static final String CLIENT_PATH_PREFIX = "client.path.prefix"; | ||
public static final String STRICT_DEPRECATION_MODE = "strict.deprecation.mode"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd save this for a follow up change. I suspect we'll want to control this some other way in tests. We might be able to get away with a method on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I am removing it. My thinking here was to introduce the hook even if not used ;) |
||
|
||
/** | ||
* Convert the entity from a {@link Response} into a map of maps. | ||
|
@@ -498,6 +499,9 @@ protected static void configureClient(RestClientBuilder builder, Settings settin | |
if (settings.hasValue(CLIENT_PATH_PREFIX)) { | ||
builder.setPathPrefix(settings.get(CLIENT_PATH_PREFIX)); | ||
} | ||
if (settings.hasValue(STRICT_DEPRECATION_MODE)) { | ||
builder.setStrictDeprecationMode(Boolean.parseBoolean(settings.get(STRICT_DEPRECATION_MODE))); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you line all of these up vertically?