Skip to content

Commit

Permalink
Add XContentHelper shims for move to specifying deprecation logger
Browse files Browse the repository at this point in the history
This adds deprecated shims for the `convertToMap` method in `XContentHelper`.
These shims will be removed once all callers switch to passing in the
`DeprecationHandler` when converting to a map.

Relates to elastic#28504
  • Loading branch information
dakrone committed Feb 12, 2018
1 parent 4e0c146 commit 504b2d4
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,38 @@ public static XContentParser createParser(NamedXContentRegistry xContentRegistry
@Deprecated
public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered)
throws ElasticsearchParseException {
return convertToMap(bytes, ordered, null);
return convertToMap(bytes, ordered, null, LoggingDeprecationHandler.INSTANCE);
}

/**
* Converts the given bytes into a map that is optionally ordered.
* @deprecated this method relies on auto-detection of content type.
* Use {@link #convertToMap(BytesReference, boolean, XContentType, DeprecationHandler)}
* instead with the proper {@link XContentType}
*/
@Deprecated
public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered,
LoggingDeprecationHandler deprecationHandler)
throws ElasticsearchParseException {
return convertToMap(bytes, ordered, null, deprecationHandler);
}

/**
* Converts the given bytes into a map that is optionally ordered. The provided {@link XContentType} must be non-null.
* @deprecated Use {@link #convertToMap(BytesReference, boolean, XContentType, DeprecationHandler)} instead
*/
@Deprecated
public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered, XContentType xContentType)
throws ElasticsearchParseException {
return convertToMap(bytes, ordered, xContentType, LoggingDeprecationHandler.INSTANCE);
}

/**
* Converts the given bytes into a map that is optionally ordered. The provided {@link XContentType} must be non-null.
*/
public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered, XContentType xContentType,
DeprecationHandler deprecationHandler)
throws ElasticsearchParseException {
try {
final XContentType contentType;
InputStream input;
Expand All @@ -105,7 +129,8 @@ public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReferen
input = bytes.streamInput();
}
contentType = xContentType != null ? xContentType : XContentFactory.xContentType(input);
return new Tuple<>(Objects.requireNonNull(contentType), convertToMap(XContentFactory.xContent(contentType), input, ordered));
return new Tuple<>(Objects.requireNonNull(contentType),
convertToMap(XContentFactory.xContent(contentType), input, ordered, deprecationHandler));
} catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse content to map", e);
}
Expand All @@ -114,10 +139,21 @@ public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReferen
/**
* Convert a string in some {@link XContent} format to a {@link Map}. Throws an {@link ElasticsearchParseException} if there is any
* error.
* @deprecated will be removed in favor of {@link #convertToMap(XContent, String, boolean, DeprecationHandler)}
*/
@Deprecated
public static Map<String, Object> convertToMap(XContent xContent, String string, boolean ordered) throws ElasticsearchParseException {
return convertToMap(xContent, string, ordered, LoggingDeprecationHandler.INSTANCE);
}

/**
* Convert a string in some {@link XContent} format to a {@link Map}. Throws an {@link ElasticsearchParseException} if there is any
* error.
*/
public static Map<String, Object> convertToMap(XContent xContent, String string, boolean ordered,
DeprecationHandler deprecationHandler) throws ElasticsearchParseException {
// It is safe to use EMPTY here because this never uses namedObject
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, string)) {
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, deprecationHandler, string)) {
return ordered ? parser.mapOrdered() : parser.map();
} catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse content to map", e);
Expand All @@ -127,11 +163,22 @@ public static Map<String, Object> convertToMap(XContent xContent, String string,
/**
* Convert a string in some {@link XContent} format to a {@link Map}. Throws an {@link ElasticsearchParseException} if there is any
* error. Note that unlike {@link #convertToMap(BytesReference, boolean)}, this doesn't automatically uncompress the input.
* @deprecated will be removed in favor of {@link #convertToMap(XContent, InputStream, boolean, DeprecationHandler)}
*/
@Deprecated
public static Map<String, Object> convertToMap(XContent xContent, InputStream input, boolean ordered)
throws ElasticsearchParseException {
return convertToMap(xContent, input, ordered, LoggingDeprecationHandler.INSTANCE);
}

/**
* Convert a string in some {@link XContent} format to a {@link Map}. Throws an {@link ElasticsearchParseException} if there is any
* error. Note that unlike {@link #convertToMap(BytesReference, boolean)}, this doesn't automatically uncompress the input.
*/
public static Map<String, Object> convertToMap(XContent xContent, InputStream input, boolean ordered, DeprecationHandler deprecationHandler)
throws ElasticsearchParseException {
// It is safe to use EMPTY here because this never uses namedObject
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, input)) {
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, deprecationHandler, input)) {
return ordered ? parser.mapOrdered() : parser.map();
} catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse content to map", e);
Expand Down

0 comments on commit 504b2d4

Please sign in to comment.