diff --git a/.github/workflows/checkstyle.yml b/.github/workflows/checkstyle.yml deleted file mode 100644 index eb0ea0b887..0000000000 --- a/.github/workflows/checkstyle.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: Checkstyle - -on: [push, pull_request] - -jobs: - checkstyle: - runs-on: ubuntu-latest - strategy: - matrix: - java: [ 11 ] - steps: - - name: Checkout Java Client - uses: actions/checkout@v3 - - - name: Set up JDK ${{ matrix.java }} - uses: actions/setup-java@v3 - with: - java-version: ${{ matrix.java }} - distribution: 'temurin' - cache: 'gradle' - - - name: Check style and license headers - run: | - ./gradlew checkstyleMain checkstyleTest diff --git a/CHANGELOG.md b/CHANGELOG.md index 05862df114..3d06e232b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Dependencies ### Changed +- Migrated from checkstyle to spotless ([#648](https://github.com/opensearch-project/opensearch-java/pull/648)) ### Deprecated diff --git a/DEVELOPER_GUIDE.md b/DEVELOPER_GUIDE.md index c90e138b40..6969aba856 100644 --- a/DEVELOPER_GUIDE.md +++ b/DEVELOPER_GUIDE.md @@ -97,11 +97,28 @@ Follow links in the [Java Tutorial](https://code.visualstudio.com/docs/java/java ## Java Language Formatting Guidelines -Java files in the opensearch-java codebase are formatted with the [checkstyle plugin](https://docs.gradle.org/current/userguide/checkstyle_plugin.html). This plugin is configured using [checkstyle.xml](config/checkstyle/checkstyle.xml). To run the formatting checks: +Java files in the OpenSearch codebase are formatted with the Eclipse JDT formatter, using the [Spotless Gradle](https://github.com/diffplug/spotless/tree/master/plugin-gradle) plugin. This plugin is configured on a project-by-project basis, via `build.gradle.kts`. So long as at least one project is configured, the formatting check can be run explicitly with: -``` -./gradlew checkstyleMain checkstyleTest -``` + ./gradlew spotlessJavaCheck + +The code can be formatted with: + + ./gradlew spotlessApply + +These tasks can also be run for specific subprojects, e.g. + + ./gradlew :java-client:spotlessJavaCheck + ./gradlew :samples:spotlessJavaCheck + +Please follow these formatting guidelines: + +* Java indent is 4 spaces +* Line width is 140 characters +* Lines of code surrounded by `// tag::NAME` and `// end::NAME` comments are included in the documentation and should only be 76 characters wide not counting leading indentation. Such regions of code are not formatted automatically as it is not possible to change the line length rule of the formatter for part of a file. Please format such sections sympathetically with the rest of the code, while keeping lines to maximum length of 76 characters. +* Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause the build to fail. +* If *absolutely* necessary, you can disable formatting for regions of code with the `// tag::NAME` and `// end::NAME` directives, but note that these are intended for use in documentation, so please make it clear what you have done, and only do this where the benefit clearly outweighs the decrease in consistency. +* Note that JavaDoc and block comments i.e. `/* ... */` are not formatted, but line comments i.e `// ...` are. +* There is an implicit rule that negative boolean expressions should use the form `foo == false` instead of `!foo` for better readability of the code. While this isn't strictly enforced, if might get called out in PR reviews as something to change. ## Submitting Changes diff --git a/README.md b/README.md index 06ec9c85cd..755e3d13cd 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -[![Checkstyle](https://github.com/opensearch-project/opensearch-java/actions/workflows/checkstyle.yml/badge.svg?branch=main)](https://github.com/opensearch-project/opensearch-java/actions/workflows/checkstyle.yml) [![Build](https://github.com/opensearch-project/opensearch-java/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/opensearch-project/opensearch-java/actions/workflows/build.yml) [![Integration Tests](https://github.com/opensearch-project/opensearch-java/actions/workflows/test-integration.yml/badge.svg?branch=main)](https://github.com/opensearch-project/opensearch-java/actions/workflows/test-integration.yml) ![Maven Central](https://img.shields.io/maven-central/v/org.opensearch.client/opensearch-java) @@ -14,6 +13,7 @@ OpenSearch Java Client - [Project Resources](#project-resources) - [Code of Conduct](#code-of-conduct) - [User Guide](#user-guide) +- [Snapshot Builds](#snapshot-builds) - [Compatibility with OpenSearch](#compatibility-with-opensearch) - [Security](#security) - [License](#license) diff --git a/build.gradle.kts b/build.gradle.kts index df7f0dc13b..7779a42fd3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,8 +42,6 @@ allprojects { mavenCentral() maven(url = "https://plugins.gradle.org/m2/") } - - apply(plugin = "checkstyle") } // Find git information. diff --git a/buildSrc/formatterConfig.xml b/buildSrc/formatterConfig.xml new file mode 100644 index 0000000000..425f9acfea --- /dev/null +++ b/buildSrc/formatterConfig.xml @@ -0,0 +1,362 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml deleted file mode 100644 index 7739883f14..0000000000 --- a/config/checkstyle/checkstyle.xml +++ /dev/null @@ -1,156 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/config/checkstyle/checkstyle_suppressions.xml b/config/checkstyle/checkstyle_suppressions.xml deleted file mode 100644 index edfa8870aa..0000000000 --- a/config/checkstyle/checkstyle_suppressions.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/config/checkstyle/header.java.txt b/config/checkstyle/header.java.txt deleted file mode 100644 index b5b39ea4ed..0000000000 --- a/config/checkstyle/header.java.txt +++ /dev/null @@ -1,7 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ diff --git a/java-client/build.gradle.kts b/java-client/build.gradle.kts index 7d2946497f..b357788591 100644 --- a/java-client/build.gradle.kts +++ b/java-client/build.gradle.kts @@ -49,10 +49,10 @@ buildscript { plugins { java `java-library` - checkstyle `maven-publish` id("com.github.jk1.dependency-license-report") version "2.5" id("org.owasp.dependencycheck") version "8.4.0" + id("com.diffplug.spotless") version "6.22.0" } apply(plugin = "opensearch.repositories") apply(plugin = "org.owasp.dependencycheck") @@ -63,10 +63,6 @@ configurations { } } -checkstyle { - toolVersion = "10.12.3" -} - java { targetCompatibility = JavaVersion.VERSION_11 sourceCompatibility = JavaVersion.VERSION_11 @@ -116,6 +112,10 @@ tasks.withType { } } +tasks.build { + dependsOn("spotlessJavaCheck") +} + tasks.test { systemProperty("tests.security.manager", "false") @@ -269,6 +269,22 @@ tasks.withType { } } +spotless { + java { + + target("**/json/**/*.java", "**/transport/**/*.java", "**/util/**/*.java") + + // Use the default importOrder configuration + importOrder() + removeUnusedImports() + + eclipse().configFile("../buildSrc/formatterConfig.xml") + + trimTrailingWhitespace() + endWithNewline() + } +} + publishing { repositories{ if (version.toString().endsWith("SNAPSHOT")) { diff --git a/java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java b/java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java index 92eb9d0b8f..baf834324a 100644 --- a/java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java +++ b/java-client/src/main/java/org/opensearch/client/json/AttributedJsonpMapper.java @@ -72,7 +72,7 @@ public boolean ignoreUnknownFields() { @SuppressWarnings("unchecked") public T attribute(String name) { if (this.name.equals(name)) { - return (T)this.value; + return (T) this.value; } else { return mapper.attribute(name); } diff --git a/java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java index 677fb94098..aeb22671c2 100644 --- a/java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/BuildFunctionDeserializer.java @@ -33,7 +33,6 @@ package org.opensearch.client.json; import jakarta.json.stream.JsonParser; - import java.util.function.Function; /** diff --git a/java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java index 84e96ef5dd..1cf864be7b 100644 --- a/java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/DelegatingDeserializer.java @@ -33,7 +33,6 @@ package org.opensearch.client.json; import jakarta.json.stream.JsonParser; - import java.util.EnumSet; public abstract class DelegatingDeserializer implements JsonpDeserializer { @@ -68,7 +67,7 @@ public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event eve */ public static JsonpDeserializer unwrap(JsonpDeserializer deserializer) { while (deserializer instanceof DelegatingDeserializer) { - deserializer = ((DelegatingDeserializer) deserializer).unwrap(); + deserializer = ((DelegatingDeserializer) deserializer).unwrap(); } return deserializer; } diff --git a/java-client/src/main/java/org/opensearch/client/json/DelegatingJsonParser.java b/java-client/src/main/java/org/opensearch/client/json/DelegatingJsonParser.java index b34606e20a..c1b5b5c303 100644 --- a/java-client/src/main/java/org/opensearch/client/json/DelegatingJsonParser.java +++ b/java-client/src/main/java/org/opensearch/client/json/DelegatingJsonParser.java @@ -37,7 +37,6 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonLocation; import jakarta.json.stream.JsonParser; - import java.math.BigDecimal; import java.util.Map; import java.util.stream.Stream; @@ -134,4 +133,4 @@ public void skipObject() { public void close() { parser.close(); } -} \ No newline at end of file +} diff --git a/java-client/src/main/java/org/opensearch/client/json/ExternallyTaggedUnion.java b/java-client/src/main/java/org/opensearch/client/json/ExternallyTaggedUnion.java index 94627156e8..2878c7e127 100644 --- a/java-client/src/main/java/org/opensearch/client/json/ExternallyTaggedUnion.java +++ b/java-client/src/main/java/org/opensearch/client/json/ExternallyTaggedUnion.java @@ -32,13 +32,11 @@ package org.opensearch.client.json; -import org.opensearch.client.util.TaggedUnion; - +import static jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParsingException; - import java.util.ArrayList; import java.util.EnumSet; import java.util.HashMap; @@ -46,10 +44,8 @@ import java.util.Map; import java.util.function.BiFunction; import java.util.function.Function; - import javax.annotation.Nullable; - -import static jakarta.json.stream.JsonParser.Event; +import org.opensearch.client.util.TaggedUnion; /** * Utilities for union types whose discriminant is not directly part of the structure, either as an enclosing property name or as @@ -71,15 +67,17 @@ public static class Deserializer, Member> { @Nullable private final BiFunction unKnownUnionCtor; - public Deserializer(Map> deserializers, - Function unionCtor) { + public Deserializer(Map> deserializers, Function unionCtor) { this.deserializers = deserializers; this.unionCtor = unionCtor; this.unKnownUnionCtor = null; } - - public Deserializer(Map> deserializers, - Function unionCtor, BiFunction unKnownUnionCtor) { + + public Deserializer( + Map> deserializers, + Function unionCtor, + BiFunction unKnownUnionCtor + ) { this.deserializers = deserializers; this.unionCtor = unionCtor; this.unKnownUnionCtor = unKnownUnionCtor; @@ -127,9 +125,9 @@ public void deserializeEntry(String key, JsonParser parser, JsonpMapper mapper, int hashPos = key.indexOf('#'); if (hashPos == -1) { throw new JsonParsingException( - "Property name '" + key - + "' is not in the 'type#name' format. Make sure the request has 'typed_keys' set.", - parser.getLocation()); + "Property name '" + key + "' is not in the 'type#name' format. Make sure the request has 'typed_keys' set.", + parser.getLocation() + ); } String type = key.substring(0, hashPos); @@ -138,43 +136,44 @@ public void deserializeEntry(String key, JsonParser parser, JsonpMapper mapper, targetMap.put(name, deserializer.deserialize(type, parser, mapper, parser.next())); } } - + public static > JsonpDeserializer>> arrayDeserializer( - TypedKeysDeserializer deserializer) { - return JsonpDeserializer.of( - EnumSet.of(Event.START_OBJECT), - (parser, mapper, event) -> { - Map> result = new HashMap<>(); - String key = null; - while ((event = parser.next()) != Event.END_OBJECT) { - JsonpUtils.expectEvent(parser, event, Event.KEY_NAME); - // Split key and type - key = parser.getString(); - int hashPos = key.indexOf('#'); - - String type = key.substring(0, hashPos); - String name = key.substring(hashPos + 1); - - List list = new ArrayList<>(); - JsonpUtils.expectNextEvent(parser, Event.START_ARRAY); - try { - while ((event = parser.next()) != Event.END_ARRAY) { - list.add(deserializer.deserializer.deserialize(type, parser, mapper, event)); - } - } catch (Exception e) { - throw e; - } - result.put(name, list); - } - return result; - }); + TypedKeysDeserializer deserializer + ) { + return JsonpDeserializer.of(EnumSet.of(Event.START_OBJECT), (parser, mapper, event) -> { + Map> result = new HashMap<>(); + String key = null; + while ((event = parser.next()) != Event.END_OBJECT) { + JsonpUtils.expectEvent(parser, event, Event.KEY_NAME); + // Split key and type + key = parser.getString(); + int hashPos = key.indexOf('#'); + + String type = key.substring(0, hashPos); + String name = key.substring(hashPos + 1); + + List list = new ArrayList<>(); + JsonpUtils.expectNextEvent(parser, Event.START_ARRAY); + try { + while ((event = parser.next()) != Event.END_ARRAY) { + list.add(deserializer.deserializer.deserialize(type, parser, mapper, event)); + } + } catch (Exception e) { + throw e; + } + result.put(name, list); + } + return result; + }); } /** * Serialize an externally tagged union using the typed keys encoding. */ public static > void serializeTypedKeys( - Map map, JsonGenerator generator, JsonpMapper mapper + Map map, + JsonGenerator generator, + JsonpMapper mapper ) { generator.writeStartObject(); serializeTypedKeysInner(map, generator, mapper); @@ -185,9 +184,11 @@ public void deserializeEntry(String key, JsonParser parser, JsonpMapper mapper, * Serialize an externally tagged union using the typed keys encoding, without the enclosing start/end object. */ public static > void serializeTypedKeysInner( - Map map, JsonGenerator generator, JsonpMapper mapper + Map map, + JsonGenerator generator, + JsonpMapper mapper ) { - for (Map.Entry entry: map.entrySet()) { + for (Map.Entry entry : map.entrySet()) { T value = entry.getValue(); generator.writeKey(value._kind().jsonValue() + "#" + entry.getKey()); value.serialize(generator, mapper); diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonData.java b/java-client/src/main/java/org/opensearch/client/json/JsonData.java index 3e2e95748e..ad907650bb 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonData.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonData.java @@ -34,7 +34,6 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonParser; - import java.util.EnumSet; /** @@ -71,19 +70,19 @@ public interface JsonData extends JsonpSerializable { /** * Converts this object to a target class. */ - T to(Class clazz, JsonpMapper mapper); + T to(Class clazz, JsonpMapper mapper); /** * Converts this object using a deserializer. A mapper must have been provided at creation time. * * @throws IllegalStateException if no mapper was provided at creation time. */ - T deserialize(JsonpDeserializer deserializer); + T deserialize(JsonpDeserializer deserializer); /** * Converts this object using a deserializer. */ - T deserialize(JsonpDeserializer deserializer, JsonpMapper mapper); + T deserialize(JsonpDeserializer deserializer, JsonpMapper mapper); /** * Creates a raw JSON value from an existing object. A mapper will be needed to convert the result. @@ -109,7 +108,5 @@ static JsonData from(JsonParser parser, JsonpMapper mapper) { return of(parser.getValue(), mapper); } - JsonpDeserializer _DESERIALIZER = JsonpDeserializer.of( - EnumSet.allOf(JsonParser.Event.class), JsonData::from - ); + JsonpDeserializer _DESERIALIZER = JsonpDeserializer.of(EnumSet.allOf(JsonParser.Event.class), JsonData::from); } diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java b/java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java index 56c7c8d0d3..72c9e0235b 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonDataImpl.java @@ -35,7 +35,6 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; - import java.io.StringReader; import java.io.StringWriter; @@ -67,8 +66,9 @@ public JsonValue toJson(JsonpMapper mapper) { // Provided mapper has precedence over the one that was optionally set at creation time mapper = mapper != null ? mapper : this.mapper; if (mapper == null) { - throw new IllegalStateException("Contains a '" + value.getClass().getName() + - "' that cannot be converted to a JsonValue without a mapper"); + throw new IllegalStateException( + "Contains a '" + value.getClass().getName() + "' that cannot be converted to a JsonValue without a mapper" + ); } final JsonParser parser = getParser(mapper); diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonEnum.java b/java-client/src/main/java/org/opensearch/client/json/JsonEnum.java index ab63641594..0965041d7e 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonEnum.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonEnum.java @@ -35,12 +35,11 @@ import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParsingException; - -import javax.annotation.Nullable; import java.util.EnumSet; import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; +import javax.annotation.Nullable; /** * Base interface for enumerations in API types. Members have a JSON representation and also accept diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializable.java b/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializable.java index 5616c31a8b..dc05f477ca 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializable.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializable.java @@ -41,7 +41,7 @@ /** * Indicates that a class has a {@link JsonpDeserializer} as a static field. */ -@Target({ElementType.TYPE}) +@Target({ ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface JsonpDeserializable { diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializer.java index 3c12aa2884..e35079ffd7 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializer.java @@ -32,16 +32,15 @@ package org.opensearch.client.json; -import org.opensearch.client.util.TriFunction; import jakarta.json.JsonValue; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; - import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.function.BiFunction; import java.util.function.Supplier; +import org.opensearch.client.util.TriFunction; public interface JsonpDeserializer { @@ -98,13 +97,13 @@ default V deserialize(JsonParser parser, JsonpMapper mapper) { */ V deserialize(JsonParser parser, JsonpMapper mapper, Event event); - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- /** * Creates a deserializer for a class that delegates to the mapper provided to * {@link #deserialize(JsonParser, JsonpMapper)}. */ - static JsonpDeserializer of (Class clazz) { + static JsonpDeserializer of(Class clazz) { return new JsonpDeserializerBase(EnumSet.allOf(JsonParser.Event.class)) { @Override public T deserialize(JsonParser parser, JsonpMapper mapper) { @@ -145,7 +144,7 @@ static JsonpDeserializer lazy(Supplier> ctor) { return new LazyDeserializer<>(ctor); } - //----- Builtin types + // ----- Builtin types static JsonpDeserializer fixedValue(T value) { return new JsonpDeserializerBase(EnumSet.noneOf(Event.class)) { @@ -229,7 +228,8 @@ static JsonpDeserializer> stringMapDeserializer(JsonpDeserial } static JsonpDeserializer> enumMapDeserializer( - JsonpDeserializer keyDeserializer, JsonpDeserializer valueDeserializer + JsonpDeserializer keyDeserializer, + JsonpDeserializer valueDeserializer ) { return new JsonpDeserializerBase.EnumMapDeserializer(keyDeserializer, valueDeserializer); } diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializerBase.java b/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializerBase.java index d706f8ec72..df405110eb 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializerBase.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonpDeserializerBase.java @@ -37,7 +37,6 @@ import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonParsingException; - import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; @@ -65,14 +64,9 @@ protected JsonpDeserializerBase(EnumSet acceptedEvents, EnumSet na /** Combines accepted events from a number of deserializers */ protected static EnumSet allAcceptedEvents(JsonpDeserializer... deserializers) { EnumSet result = EnumSet.noneOf(Event.class); - for (JsonpDeserializer deserializer: deserializers) { + for (JsonpDeserializer deserializer : deserializers) { EnumSet set = deserializer.acceptedEvents(); - // Disabled for now. Only happens with the experimental Union2 and is caused by string and number - // parsers leniency. Need to be replaced with a check on a preferred event type. - //if (!Collections.disjoint(result, set)) { - // throw new IllegalArgumentException("Deserializer accepted events are not disjoint"); - //} result.addAll(set); } @@ -98,16 +92,14 @@ public final boolean accepts(Event event) { return acceptedEvents.contains(event); } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- - //----- Builtin types + // ----- Builtin types static final JsonpDeserializer STRING = // String parsing is lenient and accepts any other primitive type - new JsonpDeserializerBase(EnumSet.of( - Event.KEY_NAME, Event.VALUE_STRING, Event.VALUE_NUMBER, - Event.VALUE_FALSE, Event.VALUE_TRUE - ), + new JsonpDeserializerBase( + EnumSet.of(Event.KEY_NAME, Event.VALUE_STRING, Event.VALUE_NUMBER, Event.VALUE_FALSE, Event.VALUE_TRUE), EnumSet.of(Event.VALUE_STRING) ) { @Override @@ -122,77 +114,72 @@ public String deserialize(JsonParser parser, JsonpMapper mapper, Event event) { } }; - static final JsonpDeserializer INTEGER = - new JsonpDeserializerBase( - EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), - EnumSet.of(Event.VALUE_NUMBER) - ) { - @Override - public Integer deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - if (event == Event.VALUE_STRING) { - return Integer.valueOf(parser.getString()); - } - return parser.getInt(); + static final JsonpDeserializer INTEGER = new JsonpDeserializerBase( + EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), + EnumSet.of(Event.VALUE_NUMBER) + ) { + @Override + public Integer deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + if (event == Event.VALUE_STRING) { + return Integer.valueOf(parser.getString()); } - }; + return parser.getInt(); + } + }; - static final JsonpDeserializer BOOLEAN = - new JsonpDeserializerBase( - EnumSet.of(Event.VALUE_FALSE, Event.VALUE_TRUE, Event.VALUE_STRING), - EnumSet.of(Event.VALUE_FALSE, Event.VALUE_TRUE) - ) { - @Override - public Boolean deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - if (event == Event.VALUE_STRING) { - return Boolean.parseBoolean(parser.getString()); - } else { - return event == Event.VALUE_TRUE; - } + static final JsonpDeserializer BOOLEAN = new JsonpDeserializerBase( + EnumSet.of(Event.VALUE_FALSE, Event.VALUE_TRUE, Event.VALUE_STRING), + EnumSet.of(Event.VALUE_FALSE, Event.VALUE_TRUE) + ) { + @Override + public Boolean deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + if (event == Event.VALUE_STRING) { + return Boolean.parseBoolean(parser.getString()); + } else { + return event == Event.VALUE_TRUE; } - }; + } + }; - static final JsonpDeserializer LONG = - new JsonpDeserializerBase( - EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), - EnumSet.of(Event.VALUE_NUMBER) - ) { - @Override - public Long deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - if (event == Event.VALUE_STRING) { - return Long.valueOf(parser.getString()); - } - return parser.getLong(); + static final JsonpDeserializer LONG = new JsonpDeserializerBase( + EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), + EnumSet.of(Event.VALUE_NUMBER) + ) { + @Override + public Long deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + if (event == Event.VALUE_STRING) { + return Long.valueOf(parser.getString()); } - }; + return parser.getLong(); + } + }; - static final JsonpDeserializer FLOAT = - new JsonpDeserializerBase( - EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), - EnumSet.of(Event.VALUE_NUMBER) + static final JsonpDeserializer FLOAT = new JsonpDeserializerBase( + EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), + EnumSet.of(Event.VALUE_NUMBER) - ) { - @Override - public Float deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - if (event == Event.VALUE_STRING) { - return Float.valueOf(parser.getString()); - } - return parser.getBigDecimal().floatValue(); + ) { + @Override + public Float deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + if (event == Event.VALUE_STRING) { + return Float.valueOf(parser.getString()); } - }; + return parser.getBigDecimal().floatValue(); + } + }; - static final JsonpDeserializer DOUBLE = - new JsonpDeserializerBase( - EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), - EnumSet.of(Event.VALUE_NUMBER) - ) { - @Override - public Double deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - if (event == Event.VALUE_STRING) { - return Double.valueOf(parser.getString()); - } - return parser.getBigDecimal().doubleValue(); + static final JsonpDeserializer DOUBLE = new JsonpDeserializerBase( + EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), + EnumSet.of(Event.VALUE_NUMBER) + ) { + @Override + public Double deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + if (event == Event.VALUE_STRING) { + return Double.valueOf(parser.getString()); } - }; + return parser.getBigDecimal().doubleValue(); + } + }; static final class DoubleOrNullDeserializer extends JsonpDeserializerBase { static final EnumSet nativeEvents = EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_NULL); @@ -240,8 +227,14 @@ public Integer deserialize(JsonParser parser, JsonpMapper mapper, Event event) { static final class StringOrNullDeserializer extends JsonpDeserializerBase { static final EnumSet nativeEvents = EnumSet.of(Event.VALUE_STRING, Event.VALUE_NULL); - static final EnumSet acceptedEvents = EnumSet.of(Event.KEY_NAME, Event.VALUE_STRING, - Event.VALUE_NUMBER, Event.VALUE_FALSE, Event.VALUE_TRUE, Event.VALUE_NULL); + static final EnumSet acceptedEvents = EnumSet.of( + Event.KEY_NAME, + Event.VALUE_STRING, + Event.VALUE_NUMBER, + Event.VALUE_FALSE, + Event.VALUE_TRUE, + Event.VALUE_NULL + ); StringOrNullDeserializer() { super(acceptedEvents, nativeEvents); @@ -262,50 +255,43 @@ public String deserialize(JsonParser parser, JsonpMapper mapper, Event event) { } } - static final JsonpDeserializer DOUBLE_OR_NAN = - new JsonpDeserializerBase( - EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING, Event.VALUE_NULL), - EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_NULL) - ) { - @Override - public Double deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - if (event == Event.VALUE_NULL) { - return Double.NaN; - } - if (event == Event.VALUE_STRING) { - return Double.valueOf(parser.getString()); - } - return parser.getBigDecimal().doubleValue(); + static final JsonpDeserializer DOUBLE_OR_NAN = new JsonpDeserializerBase( + EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING, Event.VALUE_NULL), + EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_NULL) + ) { + @Override + public Double deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + if (event == Event.VALUE_NULL) { + return Double.NaN; } - }; - - static final JsonpDeserializer NUMBER = - new JsonpDeserializerBase( - EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), - EnumSet.of(Event.VALUE_NUMBER) - ) { - @Override - public Number deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - if (event == Event.VALUE_STRING) { - return Double.valueOf(parser.getString()); - } - return ((JsonNumber)parser.getValue()).numberValue(); + if (event == Event.VALUE_STRING) { + return Double.valueOf(parser.getString()); } - }; + return parser.getBigDecimal().doubleValue(); + } + }; - static final JsonpDeserializer JSON_VALUE = - new JsonpDeserializerBase( - EnumSet.allOf(Event.class) - ) { - @Override - public JsonValue deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - return parser.getValue(); + static final JsonpDeserializer NUMBER = new JsonpDeserializerBase( + EnumSet.of(Event.VALUE_NUMBER, Event.VALUE_STRING), + EnumSet.of(Event.VALUE_NUMBER) + ) { + @Override + public Number deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + if (event == Event.VALUE_STRING) { + return Double.valueOf(parser.getString()); } - }; + return ((JsonNumber) parser.getValue()).numberValue(); + } + }; - static final JsonpDeserializer VOID = new JsonpDeserializerBase( - EnumSet.noneOf(Event.class) - ) { + static final JsonpDeserializer JSON_VALUE = new JsonpDeserializerBase(EnumSet.allOf(Event.class)) { + @Override + public JsonValue deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + return parser.getValue(); + } + }; + + static final JsonpDeserializer VOID = new JsonpDeserializerBase(EnumSet.noneOf(Event.class)) { @Override public Void deserialize(JsonParser parser, JsonpMapper mapper) { throw new JsonParsingException("Void types should not have any value", parser.getLocation()); @@ -317,7 +303,7 @@ public Void deserialize(JsonParser parser, JsonpMapper mapper, Event event) { } }; - //----- Collections + // ----- Collections static class ArrayDeserializer implements JsonpDeserializer> { private final JsonpDeserializer itemDeserializer; diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonpMapperBase.java b/java-client/src/main/java/org/opensearch/client/json/JsonpMapperBase.java index 2ce6e7f788..9e3295f984 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonpMapperBase.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonpMapperBase.java @@ -35,9 +35,8 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; - -import javax.annotation.Nullable; import java.lang.reflect.Field; +import javax.annotation.Nullable; public abstract class JsonpMapperBase implements JsonpMapper { @@ -61,7 +60,7 @@ public static JsonpDeserializer findDeserializer(Class clazz) { if (annotation != null) { try { Field field = clazz.getDeclaredField(annotation.field()); - return (JsonpDeserializer)field.get(null); + return (JsonpDeserializer) field.get(null); } catch (Exception e) { throw new RuntimeException("No deserializer found in '" + clazz.getName() + "." + annotation.field() + "'"); } diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java b/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java index c6e1938999..604bf2791e 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java @@ -32,7 +32,6 @@ package org.opensearch.client.json; -import org.opensearch.client.util.ObjectBuilder; import jakarta.json.JsonObject; import jakarta.json.JsonString; import jakarta.json.JsonValue; @@ -41,12 +40,12 @@ import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonParsingException; - -import javax.annotation.Nullable; import java.io.StringReader; import java.util.AbstractMap; import java.util.Map; import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.opensearch.client.util.ObjectBuilder; public class JsonpUtils { @@ -94,7 +93,7 @@ public static void skipValue(JsonParser parser) { * Skip the value at the current position of the parser. */ public static void skipValue(JsonParser parser, Event event) { - switch(event) { + switch (event) { case START_OBJECT: parser.skipObject(); break; @@ -111,7 +110,7 @@ public static void skipValue(JsonParser parser, Event event) { public static T buildVariant(JsonParser parser, ObjectBuilder builder) { if (builder == null) { - throw new JsonParsingException("No variant found" , parser.getLocation()); + throw new JsonParsingException("No variant found", parser.getLocation()); } return builder.build(); } @@ -134,7 +133,10 @@ public static void serialize(T value, JsonGenerator generator, @Nullable Jso * (the object has been consumed from the original one). */ public static Map.Entry lookAheadFieldValue( - String name, String defaultValue, JsonParser parser, JsonpMapper mapper + String name, + String defaultValue, + JsonParser parser, + JsonpMapper mapper ) { JsonLocation location = parser.getLocation(); @@ -192,17 +194,15 @@ public static JsonParser objectParser(JsonObject object, JsonpMapper mapper) { } public static String toString(JsonValue value) { - switch(value.getValueType()) { + switch (value.getValueType()) { case OBJECT: throw new IllegalArgumentException("Json objects cannot be used as string"); case ARRAY: - return value.asJsonArray().stream() - .map(JsonpUtils::toString) - .collect(Collectors.joining(",")); + return value.asJsonArray().stream().map(JsonpUtils::toString).collect(Collectors.joining(",")); case STRING: - return ((JsonString)value).getString(); + return ((JsonString) value).getString(); case TRUE: return "true"; diff --git a/java-client/src/main/java/org/opensearch/client/json/LookAheadJsonParser.java b/java-client/src/main/java/org/opensearch/client/json/LookAheadJsonParser.java index b4a4231d14..63e9a9f318 100644 --- a/java-client/src/main/java/org/opensearch/client/json/LookAheadJsonParser.java +++ b/java-client/src/main/java/org/opensearch/client/json/LookAheadJsonParser.java @@ -33,7 +33,6 @@ package org.opensearch.client.json; import jakarta.json.stream.JsonParser; - import java.util.Map; public interface LookAheadJsonParser extends JsonParser { diff --git a/java-client/src/main/java/org/opensearch/client/json/NamedDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/NamedDeserializer.java index 309295e313..e162d2a783 100644 --- a/java-client/src/main/java/org/opensearch/client/json/NamedDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/NamedDeserializer.java @@ -35,7 +35,6 @@ import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonParsingException; - import java.util.EnumSet; /** diff --git a/java-client/src/main/java/org/opensearch/client/json/ObjectBuilderDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/ObjectBuilderDeserializer.java index 112686f7c9..4943e83eb3 100644 --- a/java-client/src/main/java/org/opensearch/client/json/ObjectBuilderDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/ObjectBuilderDeserializer.java @@ -32,12 +32,11 @@ package org.opensearch.client.json; -import org.opensearch.client.util.ObjectBuilder; import jakarta.json.stream.JsonParser; - import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; +import org.opensearch.client.util.ObjectBuilder; /** * An object deserializer based on an {@link ObjectBuilder}. @@ -63,10 +62,10 @@ public static JsonpDeserializer lazy( Function buildFn ) { return new LazyDeserializer<>(() -> { - ObjectDeserializer builderDeser = new ObjectDeserializer(builderCtor); - builderDeserializerSetup.accept(builderDeser); - return new BuildFunctionDeserializer<>(builderDeser, buildFn); - }); + ObjectDeserializer builderDeser = new ObjectDeserializer(builderCtor); + builderDeserializerSetup.accept(builderDeser); + return new BuildFunctionDeserializer<>(builderDeser, buildFn); + }); } public static > JsonpDeserializer createForObject( diff --git a/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java index d7e3c59317..316b0239e3 100644 --- a/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java @@ -32,12 +32,9 @@ package org.opensearch.client.json; -import org.opensearch.client.util.QuadConsumer; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonParsingException; - -import javax.annotation.Nullable; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; @@ -47,6 +44,8 @@ import java.util.function.BiConsumer; import java.util.function.ObjIntConsumer; import java.util.function.Supplier; +import javax.annotation.Nullable; +import org.opensearch.client.util.QuadConsumer; public class ObjectDeserializer implements JsonpDeserializer { @@ -68,10 +67,7 @@ public static class FieldObjectDeserializer extends Field private final BiConsumer setter; private final JsonpDeserializer deserializer; - public FieldObjectDeserializer( - BiConsumer setter, JsonpDeserializer deserializer, - String name - ) { + public FieldObjectDeserializer(BiConsumer setter, JsonpDeserializer deserializer, String name) { super(name); this.setter = setter; this.deserializer = deserializer; @@ -106,7 +102,7 @@ public void deserialize(JsonParser parser, JsonpMapper mapper, String fieldName, } }; - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- private static final EnumSet EventSetObject = EnumSet.of(Event.START_OBJECT, Event.KEY_NAME); private static final EnumSet EventSetObjectAndString = EnumSet.of(Event.START_OBJECT, Event.VALUE_STRING, Event.KEY_NAME); @@ -222,7 +218,7 @@ protected void parseUnknownField(JsonParser parser, JsonpMapper mapper, String f } else { throw new JsonParsingException( - "Unknown field '" + fieldName + "' for type '" + object.getClass().getName() +"'", + "Unknown field '" + fieldName + "' for type '" + object.getClass().getName() + "'", parser.getLocation() ); } @@ -246,27 +242,22 @@ public void shortcutProperty(String name) { acceptedEvents = EventSetObjectAndString; } - //----- Object types + // ----- Object types - public void add( - BiConsumer setter, - JsonpDeserializer deserializer, - String name - ) { - FieldObjectDeserializer fieldDeserializer = - new FieldObjectDeserializer<>(setter, deserializer, name); + public void add(BiConsumer setter, JsonpDeserializer deserializer, String name) { + FieldObjectDeserializer fieldDeserializer = new FieldObjectDeserializer<>(setter, deserializer, name); this.fieldDeserializers.put(name, fieldDeserializer); } public void add( BiConsumer setter, JsonpDeserializer deserializer, - String name, String... aliases + String name, + String... aliases ) { - FieldObjectDeserializer fieldDeserializer = - new FieldObjectDeserializer<>(setter, deserializer, name); + FieldObjectDeserializer fieldDeserializer = new FieldObjectDeserializer<>(setter, deserializer, name); this.fieldDeserializers.put(name, fieldDeserializer); - for (String alias: aliases) { + for (String alias : aliases) { this.fieldDeserializers.put(alias, fieldDeserializer); } } @@ -280,33 +271,11 @@ public void setTypeProperty(String name, String defaultType) { this.defaultType = defaultType; } - //----- Primitive types + // ----- Primitive types public void add(ObjIntConsumer setter, String name, String... deprecatedNames) { // FIXME (perf): add specialized deserializer to avoid intermediate boxing add(setter::accept, JsonpDeserializer.integerDeserializer(), name, deprecatedNames); } -// Experiment: avoid boxing, allow multiple primitive parsers (e.g. int as number & string) -// public void add( -// ObjIntConsumer setter, -// JsonpIntParser vp, -// String name, String... deprecatedNames -// ) { -// this.fieldDeserializers.put(name, new FieldDeserializer(name, deprecatedNames) { -// @Override -// public void deserialize(JsonParser parser, JsonpMapper mapper, String fieldName, ObjectType object) { -// JsonpUtils.expectNextEvent(parser, Event.VALUE_NUMBER); -// setter.accept(object, vp.parse(parser)); -// } -// }); -// } -// -// public static class JsonpIntParser { -// public int parse(JsonParser parser) { -// JsonpUtils.expectNextEvent(parser, Event.VALUE_NUMBER); -// return parser.getInt(); -// } -// } - } diff --git a/java-client/src/main/java/org/opensearch/client/json/UnexpectedJsonEventException.java b/java-client/src/main/java/org/opensearch/client/json/UnexpectedJsonEventException.java index e717f73a9e..c7aa821133 100644 --- a/java-client/src/main/java/org/opensearch/client/json/UnexpectedJsonEventException.java +++ b/java-client/src/main/java/org/opensearch/client/json/UnexpectedJsonEventException.java @@ -35,7 +35,6 @@ import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonParsingException; - import java.util.EnumSet; public class UnexpectedJsonEventException extends JsonParsingException { diff --git a/java-client/src/main/java/org/opensearch/client/json/UnionDeserializer.java b/java-client/src/main/java/org/opensearch/client/json/UnionDeserializer.java index 0e77419d46..55527cefc0 100644 --- a/java-client/src/main/java/org/opensearch/client/json/UnionDeserializer.java +++ b/java-client/src/main/java/org/opensearch/client/json/UnionDeserializer.java @@ -32,13 +32,11 @@ package org.opensearch.client.json; -import org.opensearch.client.util.ObjectBuilder; import jakarta.json.JsonObject; import jakarta.json.stream.JsonLocation; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonParsingException; - import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -51,6 +49,7 @@ import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; +import org.opensearch.client.util.ObjectBuilder; public class UnionDeserializer implements JsonpDeserializer { @@ -62,6 +61,7 @@ public AmbiguousUnionException(String message) { private abstract static class EventHandler { abstract Union deserialize(JsonParser parser, JsonpMapper mapper, Event event, BiFunction buildFn); + abstract EnumSet nativeEvents(); } @@ -102,7 +102,7 @@ private static class MultiMemberHandler extends EventHandle @Override EnumSet nativeEvents() { EnumSet result = EnumSet.noneOf(Event.class); - for (SingleMemberHandler smh: handlers) { + for (SingleMemberHandler smh : handlers) { result.addAll(smh.deserializer.nativeEvents()); } return result; @@ -111,10 +111,10 @@ EnumSet nativeEvents() { @Override Union deserialize(JsonParser parser, JsonpMapper mapper, Event event, BiFunction buildFn) { RuntimeException exception = null; - for (EventHandler d: handlers) { + for (EventHandler d : handlers) { try { return d.deserialize(parser, mapper, event, buildFn); - } catch(RuntimeException ex) { + } catch (RuntimeException ex) { exception = ex; } } @@ -172,8 +172,11 @@ public Builder addMember(Kind tag, JsonpDeserializer unwrapped = DelegatingDeserializer.unwrap(deserializer); if (unwrapped instanceof ObjectDeserializer) { ObjectDeserializer od = (ObjectDeserializer) unwrapped; - UnionDeserializer.SingleMemberHandler member = - new SingleMemberHandler<>(tag, deserializer, new HashSet<>(od.fieldNames())); + UnionDeserializer.SingleMemberHandler member = new SingleMemberHandler<>( + tag, + deserializer, + new HashSet<>(od.fieldNames()) + ); objectMembers.add(member); if (od.shortcutProperty() != null) { // also add it as a string @@ -181,7 +184,7 @@ public Builder addMember(Kind tag, JsonpDeserializer member = new SingleMemberHandler<>(tag, deserializer); - for (Event e: deserializer.nativeEvents()) { + for (Event e : deserializer.nativeEvents()) { addMember(e, tag, member); } } @@ -191,18 +194,20 @@ public Builder addMember(Kind tag, JsonpDeserializer build() { - Map fieldFrequencies = objectMembers.stream().flatMap(m -> m.fields.stream()) - .collect( Collectors.groupingBy(Function.identity(), Collectors.counting())); - Set duplicateFields = fieldFrequencies.entrySet().stream() - .filter(entry -> entry.getValue() > 1) - .map(Map.Entry::getKey) - .collect(Collectors.toSet()); - for (UnionDeserializer.SingleMemberHandler member: objectMembers) { + Map fieldFrequencies = objectMembers.stream() + .flatMap(m -> m.fields.stream()) + .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); + Set duplicateFields = fieldFrequencies.entrySet() + .stream() + .filter(entry -> entry.getValue() > 1) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); + for (UnionDeserializer.SingleMemberHandler member : objectMembers) { member.fields.removeAll(duplicateFields); } // Check that no object member had all its fields removed - for (UnionDeserializer.SingleMemberHandler member: objectMembers) { + for (UnionDeserializer.SingleMemberHandler member : objectMembers) { if (member.fields.isEmpty()) { throw new AmbiguousUnionException("All properties of '" + member.tag + "' also exist in other object members"); } @@ -213,10 +218,6 @@ public JsonpDeserializer build() { otherMembers.put(Event.START_OBJECT, objectMembers.remove(0)); } -// if (objectMembers.size() > 1) { -// System.out.println("multiple objects in " + buildFn); -// } - return new UnionDeserializer<>(objectMembers, otherMembers, buildFn); } } @@ -239,8 +240,8 @@ public UnionDeserializer( this.objectMembers = Collections.emptyMap(); } else { this.objectMembers = new HashMap<>(); - for (SingleMemberHandler member: objectMembers) { - for (String field: member.fields) { + for (SingleMemberHandler member : objectMembers) { + for (String field : member.fields) { this.objectMembers.put(field, member); } } @@ -249,7 +250,7 @@ public UnionDeserializer( this.nonObjectMembers = nonObjectMembers; this.nativeEvents = EnumSet.noneOf(Event.class); - for (EventHandler member: nonObjectMembers.values()) { + for (EventHandler member : nonObjectMembers.values()) { this.nativeEvents.addAll(member.nativeEvents()); } @@ -286,8 +287,9 @@ public Union deserialize(JsonParser parser, JsonpMapper mapper, Event event) { if (member == null && event == Event.START_OBJECT && !objectMembers.isEmpty()) { if (parser instanceof LookAheadJsonParser) { - Map.Entry, JsonParser> memberAndParser = - ((LookAheadJsonParser) parser).findVariant(objectMembers); + Map.Entry, JsonParser> memberAndParser = ((LookAheadJsonParser) parser).findVariant( + objectMembers + ); member = memberAndParser.getKey(); // Parse the buffered parser @@ -297,7 +299,7 @@ public Union deserialize(JsonParser parser, JsonpMapper mapper, Event event) { // Parse as an object to find matching field names JsonObject object = parser.getObject(); - for (String field: object.keySet()) { + for (String field : object.keySet()) { member = objectMembers.get(field); if (member != null) { break; diff --git a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonProvider.java b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonProvider.java index 56045e4e51..bc14bf1537 100644 --- a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonProvider.java +++ b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonProvider.java @@ -47,7 +47,6 @@ import jakarta.json.stream.JsonGeneratorFactory; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParserFactory; - import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -81,7 +80,7 @@ public JsonFactory jacksonJsonFactory() { return this.jsonFactory; } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- // Parser private final ParserFactory defaultParserFactory = new ParserFactory(null); @@ -166,7 +165,7 @@ public JsonParser createParser(JsonArray array) { } } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- // Generator private final JsonGeneratorFactory defaultGeneratorFactory = new GeneratorFactory(null); @@ -233,7 +232,7 @@ public JsonGenerator createGenerator(OutputStream out, Charset charset) { } } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- // Unsupported operations /** diff --git a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpGenerator.java b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpGenerator.java index 5b6e5efdff..50dc7f2f8f 100644 --- a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpGenerator.java +++ b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpGenerator.java @@ -38,7 +38,6 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonGenerationException; import jakarta.json.stream.JsonGenerator; - import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; @@ -339,10 +338,10 @@ public void flush() { } private void writeValue(JsonValue value) throws IOException { - switch(value.getValueType()) { + switch (value.getValueType()) { case OBJECT: generator.writeStartObject(); - for (Map.Entry entry: value.asJsonObject().entrySet()) { + for (Map.Entry entry : value.asJsonObject().entrySet()) { generator.writeFieldName(entry.getKey()); writeValue(entry.getValue()); } @@ -351,14 +350,14 @@ private void writeValue(JsonValue value) throws IOException { case ARRAY: generator.writeStartArray(); - for (JsonValue item: value.asJsonArray()) { + for (JsonValue item : value.asJsonArray()) { writeValue(item); } generator.writeEndArray(); break; case STRING: - generator.writeString(((JsonString)value).getString()); + generator.writeString(((JsonString) value).getString()); break; case FALSE: diff --git a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpMapper.java b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpMapper.java index 3a92266ca0..186018a0d2 100644 --- a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpMapper.java +++ b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpMapper.java @@ -32,11 +32,6 @@ package org.opensearch.client.json.jackson; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpDeserializerBase; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.JsonpMapperBase; -import org.opensearch.client.json.JsonpSerializer; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.databind.ObjectMapper; @@ -44,9 +39,13 @@ import jakarta.json.spi.JsonProvider; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; - import java.io.IOException; import java.util.EnumSet; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpDeserializerBase; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.JsonpMapperBase; +import org.opensearch.client.json.JsonpSerializer; public class JacksonJsonpMapper extends JsonpMapperBase { @@ -64,9 +63,8 @@ public JacksonJsonpMapper(ObjectMapper objectMapper, JsonFactory jsonFactory) { } public JacksonJsonpMapper() { - this(new ObjectMapper() - .configure(SerializationFeature.INDENT_OUTPUT, false) - .setSerializationInclusion(JsonInclude.Include.NON_NULL) + this( + new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false).setSerializationInclusion(JsonInclude.Include.NON_NULL) ); } @@ -83,7 +81,7 @@ public JsonProvider jsonProvider() { } @Override - protected JsonpDeserializer getDefaultDeserializer(Class clazz) { + protected JsonpDeserializer getDefaultDeserializer(Class clazz) { return new JacksonValueParser<>(clazz); } @@ -100,7 +98,7 @@ public void serialize(T value, JsonGenerator generator) { return; } - com.fasterxml.jackson.core.JsonGenerator jkGenerator = ((JacksonJsonpGenerator)generator).jacksonGenerator(); + com.fasterxml.jackson.core.JsonGenerator jkGenerator = ((JacksonJsonpGenerator) generator).jacksonGenerator(); try { objectMapper.writeValue(jkGenerator, value); } catch (IOException ioe) { @@ -124,11 +122,11 @@ public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event eve throw new IllegalArgumentException("Jackson's ObjectMapper can only be used with the JacksonJsonpProvider"); } - com.fasterxml.jackson.core.JsonParser jkParser = ((JacksonJsonpParser)parser).jacksonParser(); + com.fasterxml.jackson.core.JsonParser jkParser = ((JacksonJsonpParser) parser).jacksonParser(); try { return objectMapper.readValue(jkParser, clazz); - } catch(IOException ioe) { + } catch (IOException ioe) { throw JacksonUtils.convertException(ioe); } } diff --git a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpParser.java b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpParser.java index eb75a2bb63..8597b81604 100644 --- a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpParser.java +++ b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpParser.java @@ -41,9 +41,6 @@ import jakarta.json.stream.JsonLocation; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParsingException; -import org.opensearch.client.json.LookAheadJsonParser; -import org.opensearch.client.json.UnexpectedJsonEventException; - import java.io.IOException; import java.math.BigDecimal; import java.util.AbstractMap; @@ -51,6 +48,8 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.stream.Stream; +import org.opensearch.client.json.LookAheadJsonParser; +import org.opensearch.client.json.UnexpectedJsonEventException; /** * A JSONP parser implementation on top of Jackson. @@ -105,7 +104,7 @@ private JsonParsingException convertException(IOException ioe) { private JsonToken fetchNextToken() { try { return parser.nextToken(); - } catch(IOException e) { + } catch (IOException e) { throw convertException(e); } } @@ -142,7 +141,7 @@ public Event next() { Event result = tokenToEvent.get(token); if (result == null) { - throw new JsonParsingException("Unsupported Jackson event type '"+ token + "'", getLocation()); + throw new JsonParsingException("Unsupported Jackson event type '" + token + "'", getLocation()); } return result; @@ -214,8 +213,7 @@ public void close() { public JsonObject getObject() { ensureTokenIsCurrent(); if (parser.currentToken() != JsonToken.START_OBJECT) { - throw new IllegalStateException("Unexpected event '" + parser.currentToken() + - "' at " + parser.getTokenLocation()); + throw new IllegalStateException("Unexpected event '" + parser.currentToken() + "' at " + parser.getTokenLocation()); } if (valueParser == null) { valueParser = new JsonValueParser(); @@ -234,8 +232,7 @@ public JsonArray getArray() { valueParser = new JsonValueParser(); } if (parser.currentToken() != JsonToken.START_ARRAY) { - throw new IllegalStateException("Unexpected event '" + parser.currentToken() + - "' at " + parser.getTokenLocation()); + throw new IllegalStateException("Unexpected event '" + parser.currentToken() + "' at " + parser.getTokenLocation()); } try { return valueParser.parseArray(parser); @@ -277,7 +274,7 @@ public void skipObject() { depth--; break; } - } while(!(token == JsonToken.END_OBJECT && depth == 0)); + } while (!(token == JsonToken.END_OBJECT && depth == 0)); } catch (IOException e) { throw convertException(e); } @@ -303,7 +300,7 @@ public void skipArray() { depth--; break; } - } while(!(token == JsonToken.END_ARRAY && depth == 0)); + } while (!(token == JsonToken.END_ARRAY && depth == 0)); } catch (IOException e) { throw convertException(e); } @@ -327,7 +324,7 @@ public Stream getValueStream() { return LookAheadJsonParser.super.getValueStream(); } - //----- Look ahead methods + // ----- Look ahead methods public Map.Entry lookAheadFieldValue(String name, String defaultValue) { @@ -349,8 +346,8 @@ public Map.Entry lookAheadFieldValue(String name, String def tb.copyCurrentEvent(parser); return new AbstractMap.SimpleImmutableEntry<>( - parser.getText(), - new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) + parser.getText(), + new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) ); } else { tb.copyCurrentStructure(parser); @@ -364,8 +361,8 @@ public Map.Entry lookAheadFieldValue(String name, String def // Field not found return new AbstractMap.SimpleImmutableEntry<>( - defaultValue, - new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) + defaultValue, + new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) ); } @@ -386,8 +383,8 @@ public Map.Entry findVariant(Map if (variant != null) { tb.copyCurrentEvent(parser); return new AbstractMap.SimpleImmutableEntry<>( - variant, - new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) + variant, + new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) ); } else { tb.copyCurrentStructure(parser); @@ -401,8 +398,8 @@ public Map.Entry findVariant(Map // No variant found: return the buffered parser and let the caller decide what to do. return new AbstractMap.SimpleImmutableEntry<>( - null, - new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) + null, + new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) ); } @@ -420,4 +417,3 @@ private void expectEvent(JsonToken expected) { } } } - diff --git a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonUtils.java b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonUtils.java index 988474f7ce..7c1894ea95 100644 --- a/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonUtils.java +++ b/java-client/src/main/java/org/opensearch/client/json/jackson/JacksonUtils.java @@ -36,7 +36,6 @@ import jakarta.json.JsonException; import jakarta.json.stream.JsonGenerationException; import jakarta.json.stream.JsonParsingException; - import java.io.IOException; class JacksonUtils { diff --git a/java-client/src/main/java/org/opensearch/client/json/jackson/JsonValueParser.java b/java-client/src/main/java/org/opensearch/client/json/jackson/JsonValueParser.java index 565e38e35d..3b4b9dd0d1 100644 --- a/java-client/src/main/java/org/opensearch/client/json/jackson/JsonValueParser.java +++ b/java-client/src/main/java/org/opensearch/client/json/jackson/JsonValueParser.java @@ -41,7 +41,6 @@ import jakarta.json.JsonValue; import jakarta.json.spi.JsonProvider; import jakarta.json.stream.JsonParsingException; - import java.io.IOException; /** @@ -58,7 +57,7 @@ public JsonObject parseObject(JsonParser parser) throws IOException { JsonObjectBuilder ob = DefaultJsonProvider.INSTANCE.createObjectBuilder(); JsonToken token; - while((token = parser.nextToken()) != JsonToken.END_OBJECT) { + while ((token = parser.nextToken()) != JsonToken.END_OBJECT) { if (token != JsonToken.FIELD_NAME) { throw new JsonParsingException("Expected a property name", new JacksonJsonpLocation(parser)); } @@ -72,7 +71,7 @@ public JsonObject parseObject(JsonParser parser) throws IOException { public JsonArray parseArray(JsonParser parser) throws IOException { JsonArrayBuilder ab = DefaultJsonProvider.INSTANCE.createArrayBuilder(); - while(parser.nextToken() != JsonToken.END_ARRAY) { + while (parser.nextToken() != JsonToken.END_ARRAY) { ab.add(parseValue(parser)); } return ab.build(); @@ -100,7 +99,7 @@ public JsonValue parseValue(JsonParser parser) throws IOException { case VALUE_NUMBER_FLOAT: case VALUE_NUMBER_INT: - switch(parser.getNumberType()) { + switch (parser.getNumberType()) { case INT: return DefaultJsonProvider.INSTANCE.createValue(parser.getIntValue()); case LONG: diff --git a/java-client/src/main/java/org/opensearch/client/json/jsonb/JsonbJsonpMapper.java b/java-client/src/main/java/org/opensearch/client/json/jsonb/JsonbJsonpMapper.java index a01d478326..0a7c98e2da 100644 --- a/java-client/src/main/java/org/opensearch/client/json/jsonb/JsonbJsonpMapper.java +++ b/java-client/src/main/java/org/opensearch/client/json/jsonb/JsonbJsonpMapper.java @@ -32,21 +32,20 @@ package org.opensearch.client.json.jsonb; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpDeserializerBase; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.JsonpMapperBase; -import org.opensearch.client.json.JsonpSerializable; import jakarta.json.bind.Jsonb; import jakarta.json.bind.spi.JsonbProvider; import jakarta.json.spi.JsonProvider; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; - import java.io.CharArrayReader; import java.io.CharArrayWriter; import java.util.EnumSet; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpDeserializerBase; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.JsonpMapperBase; +import org.opensearch.client.json.JsonpSerializable; public class JsonbJsonpMapper extends JsonpMapperBase { @@ -74,7 +73,7 @@ protected JsonpDeserializer getDefaultDeserializer(Class clazz) { @Override public void serialize(T value, JsonGenerator generator) { if (value instanceof JsonpSerializable) { - ((JsonpSerializable)value).serialize(generator, this); + ((JsonpSerializable) value).serialize(generator, this); return; } @@ -126,7 +125,7 @@ private void transferAll(JsonParser from, JsonGenerator to) { */ private void transferAll(JsonParser from, JsonParser.Event event, JsonGenerator to) { transferEvent(from, event, to); - switch(event) { + switch (event) { case START_OBJECT: { int depth = 1; do { @@ -140,9 +139,9 @@ private void transferAll(JsonParser from, JsonParser.Event event, JsonGenerator depth--; break; } - } while(!(event == Event.END_OBJECT && depth == 0)); + } while (!(event == Event.END_OBJECT && depth == 0)); } - break; + break; case START_ARRAY: { int depth = 1; @@ -157,9 +156,9 @@ private void transferAll(JsonParser from, JsonParser.Event event, JsonGenerator depth--; break; } - } while(!(event == Event.END_ARRAY && depth == 0)); + } while (!(event == Event.END_ARRAY && depth == 0)); } - break; + break; default: // nothing more diff --git a/java-client/src/main/java/org/opensearch/client/transport/Endpoint.java b/java-client/src/main/java/org/opensearch/client/transport/Endpoint.java index b0fdf982e6..0e74bf2519 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/Endpoint.java +++ b/java-client/src/main/java/org/opensearch/client/transport/Endpoint.java @@ -32,12 +32,11 @@ package org.opensearch.client.transport; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.NdJsonpSerializable; - -import javax.annotation.Nullable; import java.util.Collections; import java.util.Map; +import javax.annotation.Nullable; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.NdJsonpSerializable; /** * An endpoint links requests and responses to HTTP protocol encoding. It also defines the error response @@ -54,41 +53,41 @@ */ public interface Endpoint { - /** - * Get the endpoint's HTTP method for a request. - */ - String method(RequestT request); + /** + * Get the endpoint's HTTP method for a request. + */ + String method(RequestT request); - /** - * Get the URL path for a request. - */ - String requestUrl(RequestT request); + /** + * Get the URL path for a request. + */ + String requestUrl(RequestT request); - /** - * Get the query parameters for a request. - */ - default Map queryParameters(RequestT request) { - return Collections.emptyMap(); - } + /** + * Get the query parameters for a request. + */ + default Map queryParameters(RequestT request) { + return Collections.emptyMap(); + } - /** - * Get the HTTP headers for a request. - */ - default Map headers(RequestT request) { - return Collections.emptyMap(); - } + /** + * Get the HTTP headers for a request. + */ + default Map headers(RequestT request) { + return Collections.emptyMap(); + } - boolean hasRequestBody(); + boolean hasRequestBody(); - /** - * Is this status code to be considered as an error? - */ - boolean isError(int statusCode); + /** + * Is this status code to be considered as an error? + */ + boolean isError(int statusCode); - /** - * The entity parser for the error response body. Can be {@code null} to indicate that there's no error body. - */ - @Nullable - JsonpDeserializer errorDeserializer(int statusCode); + /** + * The entity parser for the error response body. Can be {@code null} to indicate that there's no error body. + */ + @Nullable + JsonpDeserializer errorDeserializer(int statusCode); } diff --git a/java-client/src/main/java/org/opensearch/client/transport/Transport.java b/java-client/src/main/java/org/opensearch/client/transport/Transport.java index e1167437d4..5596991030 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/Transport.java @@ -32,13 +32,12 @@ package org.opensearch.client.transport; -import org.opensearch.client.ApiClient; -import org.opensearch.client.json.JsonpMapper; - -import javax.annotation.Nullable; import java.io.Closeable; import java.io.IOException; import java.util.concurrent.CompletableFuture; +import javax.annotation.Nullable; +import org.opensearch.client.ApiClient; +import org.opensearch.client.json.JsonpMapper; /** * The transport layer that allows {@link ApiClient}s to send requests. diff --git a/java-client/src/main/java/org/opensearch/client/transport/TransportException.java b/java-client/src/main/java/org/opensearch/client/transport/TransportException.java index d15ff4f584..954eb4aeac 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/TransportException.java +++ b/java-client/src/main/java/org/opensearch/client/transport/TransportException.java @@ -36,7 +36,6 @@ public class TransportException extends IOException { - public TransportException(String message) { this(message, null); } diff --git a/java-client/src/main/java/org/opensearch/client/transport/TransportHeaders.java b/java-client/src/main/java/org/opensearch/client/transport/TransportHeaders.java index b21360a903..1d011ea56d 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/TransportHeaders.java +++ b/java-client/src/main/java/org/opensearch/client/transport/TransportHeaders.java @@ -12,6 +12,5 @@ public final class TransportHeaders { public static final String ACCEPT = "Accept"; public static final String USER_AGENT = "User-Agent"; - private TransportHeaders() { - } + private TransportHeaders() {} } diff --git a/java-client/src/main/java/org/opensearch/client/transport/TransportOptions.java b/java-client/src/main/java/org/opensearch/client/transport/TransportOptions.java index 581922e24c..18c9d51065 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/TransportOptions.java +++ b/java-client/src/main/java/org/opensearch/client/transport/TransportOptions.java @@ -32,8 +32,6 @@ package org.opensearch.client.transport; -import org.opensearch.client.util.ObjectBuilder; - import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -42,6 +40,7 @@ import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; +import org.opensearch.client.util.ObjectBuilder; /** * Container for all application-specific or request-specific options, including headers, query parameters and warning handlers. @@ -77,18 +76,17 @@ interface Builder extends ObjectBuilder { class BuilderImpl implements Builder { protected List> headers = Collections.emptyList(); - protected Map queryParameters = Collections.emptyMap(); + protected Map queryParameters = Collections.emptyMap(); protected Function, Boolean> onWarnings = null; - public BuilderImpl() { - } + public BuilderImpl() {} public BuilderImpl(TransportOptions src) { Collection> srcHeaders = src.headers(); if (srcHeaders != null && !srcHeaders.isEmpty()) { headers = new ArrayList<>(srcHeaders); } - Map srcParams = src.queryParameters(); + Map srcParams = src.queryParameters(); if (srcParams != null && !srcParams.isEmpty()) { queryParameters = new HashMap<>(srcParams); } @@ -138,9 +136,7 @@ class DefaultImpl implements TransportOptions { protected DefaultImpl(BuilderImpl builder) { this.headers = builder.headers.isEmpty() ? Collections.emptyList() : List.copyOf(builder.headers); - this.params = builder.queryParameters.isEmpty() ? - Collections.emptyMap() : - Map.copyOf(builder.queryParameters); + this.params = builder.queryParameters.isEmpty() ? Collections.emptyMap() : Map.copyOf(builder.queryParameters); this.onWarnings = builder.onWarnings; } diff --git a/java-client/src/main/java/org/opensearch/client/transport/Version.java b/java-client/src/main/java/org/opensearch/client/transport/Version.java index 3fb527a08d..42967ffd57 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/Version.java +++ b/java-client/src/main/java/org/opensearch/client/transport/Version.java @@ -32,12 +32,11 @@ package org.opensearch.client.transport; -import org.opensearch.client.ApiClient; - -import javax.annotation.Nullable; import java.io.InputStream; import java.util.Objects; import java.util.Properties; +import javax.annotation.Nullable; +import org.opensearch.client.ApiClient; /** * This class represents a SemVer version, with an optional patch revision. @@ -60,8 +59,7 @@ public static Version parse(String version) { if (hyphen >= 0) { version = version.substring(0, hyphen); isPreRelease = true; - } - else { + } else { isPreRelease = false; } @@ -71,8 +69,7 @@ public static Version parse(String version) { int minor = (bits.length >= 2) ? Integer.parseInt(bits[1]) : 0; int maintenance = (bits.length >= 3) ? Integer.parseInt(bits[2]) : -1; return new Version(major, minor, maintenance, isPreRelease); - } - catch(NumberFormatException ex) { + } catch (NumberFormatException ex) { return null; } } @@ -105,10 +102,7 @@ public boolean equals(Object other) { if (this == other) return true; if (!(other instanceof Version)) return false; Version that = (Version) other; - return (major == that.major && - minor == that.minor && - maintenance == that.maintenance && - isPreRelease == that.isPreRelease); + return (major == that.major && minor == that.minor && maintenance == that.maintenance && isPreRelease == that.isPreRelease); } @Override diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncByteArrayContentPublisher.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncByteArrayContentPublisher.java index 5fd975c836..ee0ac0d5c0 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncByteArrayContentPublisher.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncByteArrayContentPublisher.java @@ -8,14 +8,13 @@ package org.opensearch.client.transport.aws; +import java.nio.ByteBuffer; +import java.util.Optional; +import javax.annotation.CheckForNull; import org.reactivestreams.Subscriber; import software.amazon.awssdk.core.async.AsyncRequestBody; import software.amazon.awssdk.http.async.SdkHttpContentPublisher; -import javax.annotation.CheckForNull; -import java.nio.ByteBuffer; -import java.util.Optional; - /** * An implementation of AWS {@SdkHttpContentPublisher} that transfers a pre-existing * byte array diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncCapturingResponseHandler.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncCapturingResponseHandler.java index 5e9589f131..88efc9c0ba 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncCapturingResponseHandler.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncCapturingResponseHandler.java @@ -8,13 +8,12 @@ package org.opensearch.client.transport.aws; -import org.reactivestreams.Publisher; -import software.amazon.awssdk.http.SdkHttpResponse; -import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler; - import java.nio.ByteBuffer; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; +import org.reactivestreams.Publisher; +import software.amazon.awssdk.http.SdkHttpResponse; +import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler; /** * An implementation of AWS {@link SdkAsyncHttpResponseHandler} that captures the response, diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncCapturingSubscriber.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncCapturingSubscriber.java index c84da81c13..751e587c61 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncCapturingSubscriber.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AsyncCapturingSubscriber.java @@ -8,12 +8,11 @@ package org.opensearch.client.transport.aws; -import org.reactivestreams.Subscriber; -import org.reactivestreams.Subscription; - import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.util.concurrent.CompletableFuture; +import org.reactivestreams.Subscriber; +import org.reactivestreams.Subscription; /** * A reactive subscriber that captures a byte stream into a byte array diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java index 4609fbd6a1..2e4145f3bb 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java @@ -10,6 +10,23 @@ import jakarta.json.JsonObject; import jakarta.json.stream.JsonParser; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.zip.GZIPInputStream; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.jackson.JacksonJsonpMapper; @@ -40,25 +57,6 @@ import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.utils.SdkAutoCloseable; -import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.Collection; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.zip.GZIPInputStream; - - /** * Implementation of the OpenSearchTransport interface that sends signed requests using * the AWS v2 SDK HTTP clients, to connect to an AWS OpenSearch service using IAM authentication. @@ -92,10 +90,11 @@ public class AwsSdk2Transport implements OpenSearchTransport { * compression options, etc. */ public AwsSdk2Transport( - @CheckForNull SdkAsyncHttpClient asyncHttpClient, - @Nonnull String host, - @Nonnull Region signingRegion, - @CheckForNull AwsSdk2TransportOptions options) { + @CheckForNull SdkAsyncHttpClient asyncHttpClient, + @Nonnull String host, + @Nonnull Region signingRegion, + @CheckForNull AwsSdk2TransportOptions options + ) { this(asyncHttpClient, host, "es", signingRegion, options); } @@ -110,10 +109,11 @@ public AwsSdk2Transport( * compression options, etc. */ public AwsSdk2Transport( - @CheckForNull SdkHttpClient syncHttpClient, - @Nonnull String host, - @Nonnull Region signingRegion, - @CheckForNull AwsSdk2TransportOptions options) { + @CheckForNull SdkHttpClient syncHttpClient, + @Nonnull String host, + @Nonnull Region signingRegion, + @CheckForNull AwsSdk2TransportOptions options + ) { this(syncHttpClient, host, "es", signingRegion, options); } @@ -132,11 +132,12 @@ public AwsSdk2Transport( * compression options, etc. */ public AwsSdk2Transport( - @CheckForNull SdkAsyncHttpClient asyncHttpClient, - @Nonnull String host, - @Nonnull String signingServiceName, - @Nonnull Region signingRegion, - @CheckForNull AwsSdk2TransportOptions options) { + @CheckForNull SdkAsyncHttpClient asyncHttpClient, + @Nonnull String host, + @Nonnull String signingServiceName, + @Nonnull Region signingRegion, + @CheckForNull AwsSdk2TransportOptions options + ) { this((SdkAutoCloseable) asyncHttpClient, host, signingServiceName, signingRegion, options); } @@ -152,36 +153,36 @@ public AwsSdk2Transport( * compression options, etc. */ public AwsSdk2Transport( - @CheckForNull SdkHttpClient syncHttpClient, - @Nonnull String host, - @Nonnull String signingServiceName, - @Nonnull Region signingRegion, - @CheckForNull AwsSdk2TransportOptions options) { + @CheckForNull SdkHttpClient syncHttpClient, + @Nonnull String host, + @Nonnull String signingServiceName, + @Nonnull Region signingRegion, + @CheckForNull AwsSdk2TransportOptions options + ) { this((SdkAutoCloseable) syncHttpClient, host, signingServiceName, signingRegion, options); } private AwsSdk2Transport( - @CheckForNull SdkAutoCloseable httpClient, - @Nonnull String host, - @Nonnull String signingServiceName, - @Nonnull Region signingRegion, - @CheckForNull AwsSdk2TransportOptions options) { + @CheckForNull SdkAutoCloseable httpClient, + @Nonnull String host, + @Nonnull String signingServiceName, + @Nonnull Region signingRegion, + @CheckForNull AwsSdk2TransportOptions options + ) { Objects.requireNonNull(host, "Target OpenSearch service host must not be null"); this.httpClient = httpClient; this.host = host; this.signingServiceName = signingServiceName; this.signingRegion = signingRegion; this.transportOptions = options != null ? options : AwsSdk2TransportOptions.builder().build(); - this.defaultMapper = Optional.ofNullable(options) - .map(AwsSdk2TransportOptions::mapper) - .orElse(new JacksonJsonpMapper()); + this.defaultMapper = Optional.ofNullable(options).map(AwsSdk2TransportOptions::mapper).orElse(new JacksonJsonpMapper()); } @Override public ResponseT performRequest( - RequestT request, - Endpoint endpoint, - @Nullable TransportOptions options + RequestT request, + Endpoint endpoint, + @Nullable TransportOptions options ) throws IOException { OpenSearchRequestBodyBuffer requestBody = prepareRequestBody(request, endpoint, options); @@ -214,9 +215,9 @@ public ResponseT performRequest( @Override public CompletableFuture performRequestAsync( - RequestT request, - Endpoint endpoint, - @Nullable TransportOptions options + RequestT request, + Endpoint endpoint, + @Nullable TransportOptions options ) { try { OpenSearchRequestBodyBuffer requestBody = prepareRequestBody(request, endpoint, options); @@ -247,25 +248,24 @@ public AwsSdk2TransportOptions options() { } @Override - public void close() { - } + public void close() {} @CheckForNull private OpenSearchRequestBodyBuffer prepareRequestBody( - RequestT request, - Endpoint endpoint, - TransportOptions options + RequestT request, + Endpoint endpoint, + TransportOptions options ) throws IOException { if (endpoint.hasRequestBody()) { final JsonpMapper mapper = Optional.ofNullable(options) - .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) - .map(AwsSdk2TransportOptions::mapper) - .orElse(defaultMapper); + .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) + .map(AwsSdk2TransportOptions::mapper) + .orElse(defaultMapper); final int maxUncompressedSize = Optional.ofNullable(options) - .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) - .map(AwsSdk2TransportOptions::requestCompressionSize) - .or(()->Optional.ofNullable(transportOptions.requestCompressionSize())) - .orElse(DEFAULT_REQUEST_COMPRESSION_SIZE); + .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) + .map(AwsSdk2TransportOptions::requestCompressionSize) + .or(() -> Optional.ofNullable(transportOptions.requestCompressionSize())) + .orElse(DEFAULT_REQUEST_COMPRESSION_SIZE); OpenSearchRequestBodyBuffer buffer = new OpenSearchRequestBodyBuffer(mapper, maxUncompressedSize); buffer.addContent(request); @@ -276,13 +276,12 @@ private OpenSearchRequestBodyBuffer prepareRequestBody( } private SdkHttpFullRequest prepareRequest( - RequestT request, - Endpoint endpoint, - @CheckForNull TransportOptions options, - @CheckForNull OpenSearchRequestBodyBuffer body + RequestT request, + Endpoint endpoint, + @CheckForNull TransportOptions options, + @CheckForNull OpenSearchRequestBodyBuffer body ) { - SdkHttpFullRequest.Builder req = SdkHttpFullRequest.builder() - .method(SdkHttpMethod.fromValue(endpoint.method(request))); + SdkHttpFullRequest.Builder req = SdkHttpFullRequest.builder().method(SdkHttpMethod.fromValue(endpoint.method(request))); StringBuilder url = new StringBuilder(); url.append("https://").append(host); @@ -323,10 +322,10 @@ private SdkHttpFullRequest prepareRequest( } boolean responseCompression = Optional.ofNullable(options) - .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) - .map(AwsSdk2TransportOptions::responseCompression) - .or(() -> Optional.ofNullable(transportOptions.responseCompression())) - .orElse(Boolean.TRUE); + .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) + .map(AwsSdk2TransportOptions::responseCompression) + .or(() -> Optional.ofNullable(transportOptions.responseCompression())) + .orElse(Boolean.TRUE); if (responseCompression) { req.putHeader("Accept-Encoding", "gzip"); } else { @@ -334,16 +333,16 @@ private SdkHttpFullRequest prepareRequest( } final AwsCredentialsProvider credentials = Optional.ofNullable(options) - .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) - .map(AwsSdk2TransportOptions::credentials) - .or(() -> Optional.ofNullable(transportOptions.credentials())) - .orElse(DefaultCredentialsProvider.create()); + .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) + .map(AwsSdk2TransportOptions::credentials) + .or(() -> Optional.ofNullable(transportOptions.credentials())) + .orElse(DefaultCredentialsProvider.create()); Aws4SignerParams signerParams = Aws4SignerParams.builder() - .awsCredentials(credentials.resolveCredentials()) - .signingName(this.signingServiceName) - .signingRegion(signingRegion) - .build(); + .awsCredentials(credentials.resolveCredentials()) + .signingName(this.signingServiceName) + .signingRegion(signingRegion) + .build(); return Aws4Signer.create().sign(req.build(), signerParams); } @@ -375,10 +374,10 @@ private void applyOptionsHeaders(SdkHttpFullRequest.Builder builder, TransportOp } private ResponseT executeSync( - SdkHttpClient syncHttpClient, - SdkHttpFullRequest httpRequest, - Endpoint endpoint, - TransportOptions options + SdkHttpClient syncHttpClient, + SdkHttpFullRequest httpRequest, + Endpoint endpoint, + TransportOptions options ) throws IOException { HttpExecuteRequest.Builder executeRequest = HttpExecuteRequest.builder().request(httpRequest); @@ -399,49 +398,46 @@ private ResponseT executeSync( } private CompletableFuture executeAsync( - SdkAsyncHttpClient asyncHttpClient, - SdkHttpFullRequest httpRequest, - @CheckForNull OpenSearchRequestBodyBuffer requestBody, - Endpoint endpoint, - TransportOptions options + SdkAsyncHttpClient asyncHttpClient, + SdkHttpFullRequest httpRequest, + @CheckForNull OpenSearchRequestBodyBuffer requestBody, + Endpoint endpoint, + TransportOptions options ) { byte[] requestBodyArray = requestBody == null ? NO_BYTES : requestBody.getByteArray(); final AsyncCapturingResponseHandler responseHandler = new AsyncCapturingResponseHandler(); AsyncExecuteRequest.Builder executeRequest = AsyncExecuteRequest.builder() - .request(httpRequest) - .requestContentPublisher(new AsyncByteArrayContentPublisher(requestBodyArray)) - .responseHandler(responseHandler); + .request(httpRequest) + .requestContentPublisher(new AsyncByteArrayContentPublisher(requestBodyArray)) + .responseHandler(responseHandler); CompletableFuture executeFuture = asyncHttpClient.execute(executeRequest.build()); - return executeFuture - .thenCompose(_v -> responseHandler.getHeaderPromise()) - .thenCompose(response -> responseHandler.getBodyPromise().thenCompose(responseBody -> { - CompletableFuture ret = new CompletableFuture<>(); - try { - InputStream bodyStream = new ByteArrayInputStream(responseBody); - ret.complete(parseResponse(response, bodyStream, endpoint, options)); - } catch (Throwable e) { - ret.completeExceptionally(e); - } - return ret; - })); + return executeFuture.thenCompose(_v -> responseHandler.getHeaderPromise()) + .thenCompose(response -> responseHandler.getBodyPromise().thenCompose(responseBody -> { + CompletableFuture ret = new CompletableFuture<>(); + try { + InputStream bodyStream = new ByteArrayInputStream(responseBody); + ret.complete(parseResponse(response, bodyStream, endpoint, options)); + } catch (Throwable e) { + ret.completeExceptionally(e); + } + return ret; + })); } private ResponseT parseResponse( - @Nonnull SdkHttpResponse httpResponse, - @CheckForNull InputStream bodyStream, - @Nonnull Endpoint endpoint, - @CheckForNull TransportOptions options + @Nonnull SdkHttpResponse httpResponse, + @CheckForNull InputStream bodyStream, + @Nonnull Endpoint endpoint, + @CheckForNull TransportOptions options ) throws IOException { final JsonpMapper mapper = Optional.ofNullable(options) - .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) - .map(AwsSdk2TransportOptions::mapper) - .orElse(defaultMapper); + .map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null) + .map(AwsSdk2TransportOptions::mapper) + .orElse(defaultMapper); int statusCode = httpResponse.statusCode(); - boolean isZipped = httpResponse.firstMatchingHeader("Content-Encoding") - .map(enc -> enc.contains("gzip")) - .orElse(Boolean.FALSE); + boolean isZipped = httpResponse.firstMatchingHeader("Content-Encoding").map(enc -> enc.contains("gzip")).orElse(Boolean.FALSE); if (bodyStream != null && isZipped) { bodyStream = new GZIPInputStream(bodyStream); } @@ -454,9 +450,7 @@ private ResponseT parseResponse( if (bodyStream != null) { try (JsonParser parser = mapper.jsonProvider().createParser(bodyStream)) { - JsonObject val = JsonpDeserializer.jsonValueDeserializer() - .deserialize(parser, mapper) - .asJsonObject(); + JsonObject val = JsonpDeserializer.jsonValueDeserializer().deserialize(parser, mapper).asJsonObject(); String message = null; if (val.get("error") instanceof JsonObject) { message = val.get("error").asJsonObject().getString("reason", null); @@ -471,7 +465,7 @@ private ResponseT parseResponse( cause.reason(message); } } catch (Exception e) { - // OK. We'll use default message + // OK. We'll use default message } } @@ -482,9 +476,7 @@ private ResponseT parseResponse( if (endpoint.isError(statusCode)) { JsonpDeserializer errorDeserializer = endpoint.errorDeserializer(statusCode); if (errorDeserializer == null || bodyStream == null) { - throw new TransportException( - "Request failed with status code '" + statusCode + "'" - ); + throw new TransportException("Request failed with status code '" + statusCode + "'"); } try { try (JsonParser parser = mapper.jsonProvider().createParser(bodyStream)) { diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2TransportOptions.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2TransportOptions.java index 187c1b69bd..1d10f9c424 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2TransportOptions.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2TransportOptions.java @@ -8,13 +8,12 @@ package org.opensearch.client.transport.aws; +import java.util.List; +import java.util.function.Function; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.transport.TransportOptions; import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; -import java.util.List; -import java.util.function.Function; - public interface AwsSdk2TransportOptions extends TransportOptions { /** @@ -103,8 +102,7 @@ class BuilderImpl extends TransportOptions.BuilderImpl implements Builder { protected Boolean responseCompression; protected JsonpMapper mapper; - public BuilderImpl() { - } + public BuilderImpl() {} public BuilderImpl(AwsSdk2TransportOptions src) { super(src); diff --git a/java-client/src/main/java/org/opensearch/client/transport/endpoints/BooleanEndpoint.java b/java-client/src/main/java/org/opensearch/client/transport/endpoints/BooleanEndpoint.java index 61d326bb9c..a6969671ac 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/endpoints/BooleanEndpoint.java +++ b/java-client/src/main/java/org/opensearch/client/transport/endpoints/BooleanEndpoint.java @@ -32,10 +32,9 @@ package org.opensearch.client.transport.endpoints; -import org.opensearch.client.json.JsonpDeserializer; - import java.util.Map; import java.util.function.Function; +import org.opensearch.client.json.JsonpDeserializer; public class BooleanEndpoint extends SimpleEndpoint { @@ -43,8 +42,7 @@ public BooleanEndpoint( String id, Function method, Function requestUrl, - Function> queryParameters, + Function> queryParameters, Function> headers, boolean hasRequestBody, // always false JsonpDeserializer responseParser // always null diff --git a/java-client/src/main/java/org/opensearch/client/transport/endpoints/DelegatingJsonEndpoint.java b/java-client/src/main/java/org/opensearch/client/transport/endpoints/DelegatingJsonEndpoint.java index 0b3fd1dd76..62efe29d3d 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/endpoints/DelegatingJsonEndpoint.java +++ b/java-client/src/main/java/org/opensearch/client/transport/endpoints/DelegatingJsonEndpoint.java @@ -32,12 +32,11 @@ package org.opensearch.client.transport.endpoints; +import java.util.Map; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.transport.JsonEndpoint; -import javax.annotation.Nullable; -import java.util.Map; - public class DelegatingJsonEndpoint implements JsonEndpoint { protected final JsonEndpoint endpoint; diff --git a/java-client/src/main/java/org/opensearch/client/transport/endpoints/DictionaryResponse.java b/java-client/src/main/java/org/opensearch/client/transport/endpoints/DictionaryResponse.java index 8275174318..d92c22700c 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/endpoints/DictionaryResponse.java +++ b/java-client/src/main/java/org/opensearch/client/transport/endpoints/DictionaryResponse.java @@ -32,6 +32,11 @@ package org.opensearch.client.transport.endpoints; +import jakarta.json.stream.JsonGenerator; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; @@ -39,12 +44,6 @@ import org.opensearch.client.json.JsonpUtils; import org.opensearch.client.json.ObjectDeserializer; import org.opensearch.client.util.ObjectBuilderBase; -import jakarta.json.stream.JsonGenerator; - -import javax.annotation.Nullable; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; /** * Base class for dictionary responses, i.e. a series of key/value pairs. @@ -98,8 +97,8 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { } } - protected abstract static class AbstractBuilder> - extends ObjectBuilderBase { + protected abstract static class AbstractBuilder> extends + ObjectBuilderBase { private Map result; @@ -154,11 +153,13 @@ public BuilderT tValueSerializer(@Nullable JsonpSerializer value) { // --------------------------------------------------------------------------------------------- protected static > void setupDictionaryResponseDeserializer( - ObjectDeserializer op, JsonpDeserializer tKeyParser, - JsonpDeserializer tValueParser) { - - op.setUnknownFieldHandler((builder, name, parser, params) -> { - builder.putResult(name, tValueParser.deserialize(parser, params)); - }); + ObjectDeserializer op, + JsonpDeserializer tKeyParser, + JsonpDeserializer tValueParser + ) { + + op.setUnknownFieldHandler( + (builder, name, parser, params) -> { builder.putResult(name, tValueParser.deserialize(parser, params)); } + ); } } diff --git a/java-client/src/main/java/org/opensearch/client/transport/endpoints/EndpointWithResponseMapperAttr.java b/java-client/src/main/java/org/opensearch/client/transport/endpoints/EndpointWithResponseMapperAttr.java index 08fba087b7..56bcc9d783 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/endpoints/EndpointWithResponseMapperAttr.java +++ b/java-client/src/main/java/org/opensearch/client/transport/endpoints/EndpointWithResponseMapperAttr.java @@ -32,13 +32,12 @@ package org.opensearch.client.transport.endpoints; +import jakarta.json.stream.JsonParser; +import javax.annotation.Nullable; import org.opensearch.client.json.DelegatingDeserializer; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.transport.JsonEndpoint; -import jakarta.json.stream.JsonParser; - -import javax.annotation.Nullable; /** * An endpoint wrapper that adds attributes to the JSON mapper used to deserialize its response. diff --git a/java-client/src/main/java/org/opensearch/client/transport/endpoints/SimpleEndpoint.java b/java-client/src/main/java/org/opensearch/client/transport/endpoints/SimpleEndpoint.java index 1f40ff5267..04ce725a00 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/endpoints/SimpleEndpoint.java +++ b/java-client/src/main/java/org/opensearch/client/transport/endpoints/SimpleEndpoint.java @@ -32,14 +32,13 @@ package org.opensearch.client.transport.endpoints; -import org.opensearch.client.opensearch._types.ErrorResponse; -import org.apache.hc.core5.net.URLEncodedUtils; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.transport.JsonEndpoint; - import java.util.Collections; import java.util.Map; import java.util.function.Function; +import org.apache.hc.core5.net.URLEncodedUtils; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.opensearch._types.ErrorResponse; +import org.opensearch.client.transport.JsonEndpoint; public class SimpleEndpoint implements JsonEndpoint { @@ -121,19 +120,16 @@ public JsonpDeserializer errorDeserializer(int statusCode) { public SimpleEndpoint withResponseDeserializer( JsonpDeserializer newResponseParser ) { - return new SimpleEndpoint<>( - method, - requestUrl, - queryParameters, - headers, - hasRequestBody, - newResponseParser - ); + return new SimpleEndpoint<>(method, requestUrl, queryParameters, headers, hasRequestBody, newResponseParser); } public static RuntimeException noPathTemplateFound(String what) { - return new RuntimeException("Could not find a request " + what + " with this set of properties. " + - "Please check the API documentation, or raise an issue if this should be a valid request."); + return new RuntimeException( + "Could not find a request " + + what + + " with this set of properties. " + + "Please check the API documentation, or raise an issue if this should be a valid request." + ); } public static void pathEncode(String src, StringBuilder dest) { diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Options.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Options.java index e10c32a577..fae5c41998 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Options.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Options.java @@ -8,6 +8,9 @@ package org.opensearch.client.transport.httpclient5; +import static org.opensearch.client.transport.TransportHeaders.ACCEPT; +import static org.opensearch.client.transport.TransportHeaders.USER_AGENT; + import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collection; @@ -15,11 +18,10 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.Map.Entry; +import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; - import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.message.BasicHeader; @@ -27,19 +29,16 @@ import org.opensearch.client.transport.TransportOptions; import org.opensearch.client.transport.Version; -import static org.opensearch.client.transport.TransportHeaders.ACCEPT; -import static org.opensearch.client.transport.TransportHeaders.USER_AGENT; - public class ApacheHttpClient5Options implements TransportOptions { /** * Default request options. */ public static final ApacheHttpClient5Options DEFAULT = new Builder( - Collections.emptyList(), - HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory.DEFAULT, - null, - null - ).build(); + Collections.emptyList(), + HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory.DEFAULT, + null, + null + ).build(); private final List
headers; private final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory; @@ -67,9 +66,7 @@ public RequestConfig getRequestConfig() { @Override public Collection> headers() { - return headers.stream() - .map(h -> new AbstractMap.SimpleImmutableEntry<>(h.getName(), h.getValue())) - .collect(Collectors.toList()); + return headers.stream().map(h -> new AbstractMap.SimpleImmutableEntry<>(h.getName(), h.getValue())).collect(Collectors.toList()); } @Override @@ -90,23 +87,22 @@ public Function, Boolean> onWarnings() { public Builder toBuilder() { return new Builder(headers, httpAsyncResponseConsumerFactory, warningsHandler, requestConfig); } - + public static class Builder implements TransportOptions.Builder { private final List
headers; private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory; private WarningsHandler warningsHandler; private RequestConfig requestConfig; - + private Builder(Builder builder) { - this(builder.headers, builder.httpAsyncResponseConsumerFactory, - builder.warningsHandler, builder.requestConfig); + this(builder.headers, builder.httpAsyncResponseConsumerFactory, builder.warningsHandler, builder.requestConfig); } private Builder( - List
headers, - HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, - WarningsHandler warningsHandler, - RequestConfig requestConfig + List
headers, + HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, + WarningsHandler warningsHandler, + RequestConfig requestConfig ) { this.headers = new ArrayList<>(headers); this.httpAsyncResponseConsumerFactory = httpAsyncResponseConsumerFactory; @@ -153,7 +149,7 @@ public TransportOptions.Builder onWarnings(Function, Boolean> liste return this; } - + /** * Set the {@link HttpAsyncResponseConsumerFactory} used to create one * {@link AsyncResponseConsumer} callback per retry. Controls how the @@ -219,15 +215,13 @@ static ApacheHttpClient5Options initialOptions() { ); return new ApacheHttpClient5Options( - DEFAULT.toBuilder() - .addHeader(USER_AGENT, ua) - .addHeader(ACCEPT, ApacheHttpClient5Transport.JsonContentType.toString()) + DEFAULT.toBuilder().addHeader(USER_AGENT, ua).addHeader(ACCEPT, ApacheHttpClient5Transport.JsonContentType.toString()) ); } - + static ApacheHttpClient5Options of(TransportOptions options) { if (options instanceof ApacheHttpClient5Options) { - return (ApacheHttpClient5Options)options; + return (ApacheHttpClient5Options) options; } else { final Builder builder = new Builder(DEFAULT.toBuilder()); @@ -237,7 +231,7 @@ static ApacheHttpClient5Options of(TransportOptions options) { return builder.build(); } } - + /** * Custom implementation of {@link BasicHeader} that overrides equals and * hashCode so it is easier to test equality of {@link ApacheHttpClient5Options}. diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java index ad47af52f3..c4142d7f85 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java @@ -8,6 +8,8 @@ package org.opensearch.client.transport.httpclient5; +import jakarta.json.stream.JsonGenerator; +import jakarta.json.stream.JsonParser; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -24,10 +26,10 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.Map.Entry; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; @@ -36,9 +38,7 @@ import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.GZIPOutputStream; - import javax.annotation.Nullable; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hc.client5.http.auth.AuthCache; @@ -87,9 +87,6 @@ import org.opensearch.client.transport.httpclient5.internal.NodeSelector; import org.opensearch.client.util.MissingRequiredPropertyException; -import jakarta.json.stream.JsonGenerator; -import jakarta.json.stream.JsonParser; - /** * Apache HttpClient 5 based client transport. */ @@ -111,10 +108,19 @@ public class ApacheHttpClient5Transport implements OpenSearchTransport { private final String pathPrefix; private final List
defaultHeaders; - public ApacheHttpClient5Transport(final CloseableHttpAsyncClient client, final Header[] defaultHeaders, - final List nodes, final JsonpMapper mapper, @Nullable TransportOptions options, final String pathPrefix, - final FailureListener failureListener, final NodeSelector nodeSelector, final boolean strictDeprecationMode, - final boolean compressionEnabled, final boolean chunkedEnabled) { + public ApacheHttpClient5Transport( + final CloseableHttpAsyncClient client, + final Header[] defaultHeaders, + final List nodes, + final JsonpMapper mapper, + @Nullable TransportOptions options, + final String pathPrefix, + final FailureListener failureListener, + final NodeSelector nodeSelector, + final boolean strictDeprecationMode, + final boolean compressionEnabled, + final boolean chunkedEnabled + ) { this.mapper = mapper; this.client = client; this.defaultHeaders = Collections.unmodifiableList(Arrays.asList(defaultHeaders)); @@ -129,8 +135,11 @@ public ApacheHttpClient5Transport(final CloseableHttpAsyncClient client, final H } @Override - public ResponseT performRequest(RequestT request, - Endpoint endpoint, TransportOptions options) throws IOException { + public ResponseT performRequest( + RequestT request, + Endpoint endpoint, + TransportOptions options + ) throws IOException { try { return performRequestAsync(request, endpoint, options).join(); } catch (final CompletionException ex) { @@ -140,29 +149,33 @@ public ResponseT performRequest(RequestT request, throw (IOException) ex.getCause(); } else { throw new IOException(ex.getCause()); - } + } } } @Override - public CompletableFuture performRequestAsync(RequestT request, - Endpoint endpoint, TransportOptions options) { + public CompletableFuture performRequestAsync( + RequestT request, + Endpoint endpoint, + TransportOptions options + ) { final ApacheHttpClient5Options requestOptions = (options == null) ? transportOptions : ApacheHttpClient5Options.of(options); final CompletableFuture future = new CompletableFuture<>(); final HttpUriRequestBase clientReq = prepareLowLevelRequest(request, endpoint, requestOptions); - final WarningsHandler warningsHandler = (requestOptions.getWarningsHandler() == null) ? - this.warningsHandler : requestOptions.getWarningsHandler(); + final WarningsHandler warningsHandler = (requestOptions.getWarningsHandler() == null) + ? this.warningsHandler + : requestOptions.getWarningsHandler(); try { performRequestAsync(nextNodes(), requestOptions, clientReq, warningsHandler, future); - } catch(final IOException ex) { + } catch (final IOException ex) { future.completeExceptionally(ex); } - + return future.thenApply(r -> { try { - return (ResponseT)prepareResponse(r, endpoint); + return (ResponseT) prepareResponse(r, endpoint); } catch (final IOException ex) { throw new CompletionException(ex); } @@ -184,8 +197,13 @@ public void close() throws IOException { client.close(); } - private void performRequestAsync(final NodeTuple> nodeTuple, final ApacheHttpClient5Options options, - final HttpUriRequestBase request, final WarningsHandler warningsHandler, final CompletableFuture listener) { + private void performRequestAsync( + final NodeTuple> nodeTuple, + final ApacheHttpClient5Options options, + final HttpUriRequestBase request, + final WarningsHandler warningsHandler, + final CompletableFuture listener + ) { final RequestContext context = createContextForNextAttempt(options, request, nodeTuple.nodes.next(), nodeTuple.authCache); Future future = client.execute( context.requestProducer, @@ -195,8 +213,12 @@ private void performRequestAsync(final NodeTuple> nodeTuple, fina @Override public void completed(ClassicHttpResponse httpResponse) { try { - ResponseOrResponseException responseOrResponseException = convertResponse(request, context.node, - httpResponse, warningsHandler); + ResponseOrResponseException responseOrResponseException = convertResponse( + request, + context.node, + httpResponse, + warningsHandler + ); if (responseOrResponseException.responseException == null) { listener.complete(responseOrResponseException.response); } else { @@ -236,7 +258,7 @@ public void cancelled() { request.setDependency((org.apache.hc.core5.concurrent.Cancellable) future); } } - + /** * Replaces the nodes with which the client communicates. * @@ -259,8 +281,12 @@ private void setNodes(Collection nodes) { this.denylist.clear(); } - private ResponseOrResponseException convertResponse(final HttpUriRequestBase request, final Node node, - final ClassicHttpResponse httpResponse, final WarningsHandler warningsHandler) throws IOException { + private ResponseOrResponseException convertResponse( + final HttpUriRequestBase request, + final Node node, + final ClassicHttpResponse httpResponse, + final WarningsHandler warningsHandler + ) throws IOException { int statusCode = httpResponse.getCode(); Optional.ofNullable(httpResponse.getEntity()) @@ -440,15 +466,17 @@ private void onFailure(Node node) { failureListener.onFailure(node); } - private RequestContext createContextForNextAttempt(final ApacheHttpClient5Options options, - final HttpUriRequestBase request, final Node node, final AuthCache authCache) { + private RequestContext createContextForNextAttempt( + final ApacheHttpClient5Options options, + final HttpUriRequestBase request, + final Node node, + final AuthCache authCache + ) { request.reset(); return new RequestContext(options, request, node, authCache); } - private ResponseT prepareResponse(Response clientResp, - Endpoint endpoint - ) throws IOException { + private ResponseT prepareResponse(Response clientResp, Endpoint endpoint) throws IOException { try { int statusCode = clientResp.getStatusLine().getStatusCode(); @@ -456,18 +484,12 @@ private ResponseT prepareResponse(Response clientResp, if (endpoint.isError(statusCode)) { JsonpDeserializer errorDeserializer = endpoint.errorDeserializer(statusCode); if (errorDeserializer == null) { - throw new TransportException( - "Request failed with status code '" + statusCode + "'", - new ResponseException(clientResp) - ); + throw new TransportException("Request failed with status code '" + statusCode + "'", new ResponseException(clientResp)); } HttpEntity entity = clientResp.getEntity(); if (entity == null) { - throw new TransportException( - "Expecting a response body, but none was sent", - new ResponseException(clientResp) - ); + throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp)); } // We may have to replay it. @@ -480,12 +502,12 @@ private ResponseT prepareResponse(Response clientResp, // TODO: have the endpoint provide the exception constructor throw new OpenSearchException((ErrorResponse) error); } - } catch(MissingRequiredPropertyException errorEx) { + } catch (MissingRequiredPropertyException errorEx) { // Could not decode exception, try the response type try { ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint); return response; - } catch(Exception respEx) { + } catch (Exception respEx) { // No better luck: throw the original error decoding exception throw new TransportException("Failed to decode error response", new ResponseException(clientResp)); } @@ -499,9 +521,9 @@ private ResponseT prepareResponse(Response clientResp, } private HttpUriRequestBase prepareLowLevelRequest( - RequestT request, - Endpoint endpoint, - @Nullable ApacheHttpClient5Options options + RequestT request, + Endpoint endpoint, + @Nullable ApacheHttpClient5Options options ) { final String method = endpoint.method(request); final String path = endpoint.requestUrl(request); @@ -565,6 +587,7 @@ private void setHeaders(HttpRequest httpRequest, Collection ResponseT decodeResponse( - int statusCode, @Nullable HttpEntity entity, Response clientResp, Endpoint endpoint - ) throws IOException { + int statusCode, + @Nullable HttpEntity entity, + Response clientResp, + Endpoint endpoint + ) throws IOException { if (endpoint instanceof BooleanEndpoint) { BooleanEndpoint bep = (BooleanEndpoint) endpoint; @@ -587,23 +613,21 @@ private ResponseT decodeResponse( ResponseT response = (ResponseT) new BooleanResponse(bep.getResult(statusCode)); return response; - } else if (endpoint instanceof JsonEndpoint){ - JsonEndpoint jsonEndpoint = (JsonEndpoint)endpoint; + } else if (endpoint instanceof JsonEndpoint) { + JsonEndpoint jsonEndpoint = (JsonEndpoint) endpoint; // Successful response ResponseT response = null; JsonpDeserializer responseParser = jsonEndpoint.responseDeserializer(); if (responseParser != null) { // Expecting a body if (entity == null) { - throw new TransportException( - "Expecting a response body, but none was sent", - new ResponseException(clientResp) - ); + throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp)); } InputStream content = entity.getContent(); try (JsonParser parser = mapper.jsonProvider().createParser(content)) { response = responseParser.deserialize(parser, mapper); - }; + } + ; } return response; } else { @@ -682,7 +706,7 @@ public void remove() { */ private void writeNdJson(NdJsonpSerializable value, ByteArrayOutputStream baos) { Iterator values = value._serializables(); - while(values.hasNext()) { + while (values.hasNext()) { Object item = values.next(); if (item instanceof NdJsonpSerializable && item != value) { // do not recurse on the item itself writeNdJson((NdJsonpSerializable) item, baos); @@ -694,7 +718,7 @@ private void writeNdJson(NdJsonpSerializable value, ByteArrayOutputStream baos) } } } - + private static URI buildUri(String pathPrefix, String path, Map params) { Objects.requireNonNull(path, "path must not be null"); try { @@ -732,18 +756,20 @@ private static class RequestContext { private final AsyncResponseConsumer asyncResponseConsumer; private final HttpClientContext context; - RequestContext(final ApacheHttpClient5Options options, final HttpUriRequestBase request, - final Node node, final AuthCache authCache) { + RequestContext( + final ApacheHttpClient5Options options, + final HttpUriRequestBase request, + final Node node, + final AuthCache authCache + ) { this.node = node; this.requestProducer = HttpUriRequestProducer.create(request, node.getHost()); - this.asyncResponseConsumer = options - .getHttpAsyncResponseConsumerFactory() - .createHttpAsyncResponseConsumer(); + this.asyncResponseConsumer = options.getHttpAsyncResponseConsumerFactory().createHttpAsyncResponseConsumer(); this.context = HttpClientContext.create(); context.setAuthCache(new WrappingAuthCache(context, authCache)); } } - + /** * The Apache HttpClient 5 adds "Authorization" header even if the credentials for basic authentication are not provided * (effectively, username and password are 'null'). To workaround that, wrapping the AuthCache around current HttpClientContext @@ -817,7 +843,7 @@ private static class ResponseOrResponseException { this.response = null; } } - + /** * Listener that allows to be notified whenever a failure happens. Useful when sniffing is enabled, so that we can sniff on failure. * The default implementation is a no-op. @@ -835,7 +861,7 @@ public FailureListener() {} */ public void onFailure(Node node) {} } - + /** * A gzip compressing entity that also implements {@code getContent()}. */ diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5TransportBuilder.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5TransportBuilder.java index bf75ba74b4..334a48a89b 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5TransportBuilder.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5TransportBuilder.java @@ -16,10 +16,8 @@ import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; - import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; - import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy; @@ -170,7 +168,7 @@ public ApacheHttpClient5TransportBuilder setPathPrefix(String pathPrefix) { } /** - * Sets the {@link JsonpMapper} instance to be used for parsing JSON payloads. If not provided + * Sets the {@link JsonpMapper} instance to be used for parsing JSON payloads. If not provided * the {@link JacksonJsonpMapper} is going to be used. * @param mapper the {@link JsonpMapper} instance */ @@ -279,18 +277,18 @@ public ApacheHttpClient5Transport build() { } final ApacheHttpClient5Transport transport = new ApacheHttpClient5Transport( - httpClient, - defaultHeaders, - nodes, - mapper, - options, - pathPrefix, - failureListener, - nodeSelector, - strictDeprecationMode, - compressionEnabled, - chunkedEnabled.orElse(false) - ); + httpClient, + defaultHeaders, + nodes, + mapper, + options, + pathPrefix, + failureListener, + nodeSelector, + strictDeprecationMode, + compressionEnabled, + chunkedEnabled.orElse(false) + ); httpClient.start(); return transport; @@ -403,5 +401,4 @@ public interface HttpClientConfigCallback { HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder); } - } diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/HttpAsyncResponseConsumerFactory.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/HttpAsyncResponseConsumerFactory.java index c9322f473e..3bddfd48c3 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/HttpAsyncResponseConsumerFactory.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/HttpAsyncResponseConsumerFactory.java @@ -47,7 +47,8 @@ public interface HttpAsyncResponseConsumerFactory { * Creates the default type of {@link AsyncResponseConsumer}, based on heap buffering with a buffer limit of 100MB. */ HttpAsyncResponseConsumerFactory DEFAULT = new HeapBufferedResponseConsumerFactory( - HeapBufferedResponseConsumerFactory.DEFAULT_BUFFER_LIMIT); + HeapBufferedResponseConsumerFactory.DEFAULT_BUFFER_LIMIT + ); /** * Creates the {@link AsyncResponseConsumer}, called once per request attempt. diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/Response.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/Response.java index 2f36d517de..66c3c1831b 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/Response.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/Response.java @@ -32,6 +32,11 @@ package org.opensearch.client.transport.httpclient5; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; @@ -40,12 +45,6 @@ import org.apache.hc.core5.http.message.RequestLine; import org.apache.hc.core5.http.message.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 opensearch response. It wraps the {@link HttpResponse} returned and associates it with * its corresponding {@link RequestLine} and {@link HttpHost}. diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ResponseException.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ResponseException.java index 6cd0358a93..212efcee64 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ResponseException.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ResponseException.java @@ -32,14 +32,13 @@ package org.opensearch.client.transport.httpclient5; +import java.io.IOException; +import java.util.Locale; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.ParseException; import org.apache.hc.core5.http.io.entity.BufferedHttpEntity; import org.apache.hc.core5.http.io.entity.EntityUtils; -import java.io.IOException; -import java.util.Locale; - /** * Exception thrown when an opensearch node responds to a request with a status code that indicates an error. * Holds the response that was returned. diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncEntityConsumer.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncEntityConsumer.java index e500cd9466..07cb155dea 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncEntityConsumer.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncEntityConsumer.java @@ -13,6 +13,9 @@ package org.opensearch.client.transport.httpclient5.internal; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.concurrent.atomic.AtomicReference; import org.apache.hc.core5.http.ContentTooLongException; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpException; @@ -20,10 +23,6 @@ import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityConsumer; import org.apache.hc.core5.util.ByteArrayBuffer; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.concurrent.atomic.AtomicReference; - /** * Default implementation of {@link AsyncEntityConsumer}. Buffers the whole * response content in heap memory, meaning that the size of the buffer is equal to the content-length of the response. diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncResponseConsumer.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncResponseConsumer.java index 4d0ceb66e7..2b26b1ed47 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncResponseConsumer.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncResponseConsumer.java @@ -32,6 +32,7 @@ package org.opensearch.client.transport.httpclient5.internal; +import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hc.core5.http.ClassicHttpResponse; @@ -46,8 +47,6 @@ import org.apache.hc.core5.http.nio.support.AbstractAsyncResponseConsumer; import org.apache.hc.core5.http.protocol.HttpContext; -import java.io.IOException; - /** * Default implementation of {@link AsyncResponseConsumer}. Buffers the whole * response content in heap memory, meaning that the size of the buffer is equal to the content-length of the response. diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HttpEntityAsyncEntityProducer.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HttpEntityAsyncEntityProducer.java index 1c669a55c7..2713f6182a 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HttpEntityAsyncEntityProducer.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/HttpEntityAsyncEntityProducer.java @@ -8,19 +8,18 @@ package org.opensearch.client.transport.httpclient5.internal; -import org.apache.hc.core5.http.HttpEntity; -import org.apache.hc.core5.http.nio.AsyncEntityProducer; -import org.apache.hc.core5.http.nio.DataStreamChannel; -import org.apache.hc.core5.http.nio.ResourceHolder; -import org.apache.hc.core5.util.Args; -import org.apache.hc.core5.util.Asserts; - import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.nio.AsyncEntityProducer; +import org.apache.hc.core5.http.nio.DataStreamChannel; +import org.apache.hc.core5.http.nio.ResourceHolder; +import org.apache.hc.core5.util.Args; +import org.apache.hc.core5.util.Asserts; /** * The {@link AsyncEntityProducer} implementation for {@link HttpEntity} diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/Node.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/Node.java index 8f55f67cde..7f0e7e8a26 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/Node.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/internal/Node.java @@ -32,13 +32,12 @@ package org.opensearch.client.transport.httpclient5.internal; -import org.apache.hc.core5.http.HttpHost; - import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.TreeSet; +import org.apache.hc.core5.http.HttpHost; /** * Metadata about an {@link HttpHost} running OpenSearch. diff --git a/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientOptions.java b/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientOptions.java index 2574c0fd91..8fa20977c8 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientOptions.java +++ b/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientOptions.java @@ -32,10 +32,8 @@ package org.opensearch.client.transport.rest_client; -import org.opensearch.client.transport.TransportOptions; -import org.opensearch.client.transport.Version; -import org.opensearch.client.RequestOptions; -import org.opensearch.client.WarningsHandler; +import static org.opensearch.client.transport.TransportHeaders.ACCEPT; +import static org.opensearch.client.transport.TransportHeaders.USER_AGENT; import java.util.AbstractMap; import java.util.Collection; @@ -44,9 +42,10 @@ import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; - -import static org.opensearch.client.transport.TransportHeaders.ACCEPT; -import static org.opensearch.client.transport.TransportHeaders.USER_AGENT; +import org.opensearch.client.RequestOptions; +import org.opensearch.client.WarningsHandler; +import org.opensearch.client.transport.TransportOptions; +import org.opensearch.client.transport.Version; /** * The {@link RestClientTransport} is deprecated and is scheduled for removal in later versions. Please @@ -59,7 +58,7 @@ public class RestClientOptions implements TransportOptions { static RestClientOptions of(TransportOptions options) { if (options instanceof RestClientOptions) { - return (RestClientOptions)options; + return (RestClientOptions) options; } else { final Builder builder = new Builder(RequestOptions.DEFAULT.toBuilder()); @@ -87,15 +86,16 @@ public RequestOptions restClientRequestOptions() { @Override public Collection> headers() { - return options.getHeaders().stream() + return options.getHeaders() + .stream() .map(h -> new AbstractMap.SimpleImmutableEntry<>(h.getName(), h.getValue())) .collect(Collectors.toList()); } @Override public Map queryParameters() { - // TODO - param not available -// return options.getParameters(); + // TODO - param not available + // return options.getParameters(); return null; } @@ -138,7 +138,7 @@ public TransportOptions.Builder addHeader(String name, String value) { // We must filter out our own user-agent from the options or they'll end up as multiple values for the header RequestOptions options = builder.build(); builder = RequestOptions.DEFAULT.toBuilder(); -// options.getParameters().forEach((k, v) -> builder.addParameter(k, v)); + // options.getParameters().forEach((k, v) -> builder.addParameter(k, v)); options.getHeaders().forEach(h -> { if (!h.getName().equalsIgnoreCase(USER_AGENT)) { builder.addHeader(h.getName(), h.getValue()); @@ -155,8 +155,8 @@ public TransportOptions.Builder addHeader(String name, String value) { @Override public TransportOptions.Builder setParameter(String name, String value) { - //TODO - param not available -// builder.addParameter(name, value); + // TODO - param not available + // builder.addParameter(name, value); return this; } diff --git a/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientTransport.java b/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientTransport.java index e6b724df42..c43504483e 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientTransport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientTransport.java @@ -32,26 +32,18 @@ package org.opensearch.client.transport.rest_client; -import org.apache.hc.core5.http.HttpStatus; -import org.opensearch.client.opensearch._types.OpenSearchException; -import org.opensearch.client.opensearch._types.ErrorResponse; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.NdJsonpSerializable; -import org.opensearch.client.transport.JsonEndpoint; -import org.opensearch.client.transport.TransportException; -import org.opensearch.client.transport.endpoints.BooleanEndpoint; -import org.opensearch.client.transport.endpoints.BooleanResponse; -import org.opensearch.client.transport.OpenSearchTransport; -import org.opensearch.client.transport.Endpoint; -import org.opensearch.client.transport.TransportOptions; -import org.opensearch.client.util.ApiTypeHelper; -import org.opensearch.client.util.MissingRequiredPropertyException; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; - +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import javax.annotation.Nullable; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.io.entity.BufferedHttpEntity; import org.apache.hc.core5.http.io.entity.ByteArrayEntity; import org.apache.hc.core5.http.io.entity.EntityUtils; @@ -61,14 +53,20 @@ import org.opensearch.client.ResponseException; import org.opensearch.client.ResponseListener; import org.opensearch.client.RestClient; - -import javax.annotation.Nullable; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.CompletableFuture; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.NdJsonpSerializable; +import org.opensearch.client.opensearch._types.ErrorResponse; +import org.opensearch.client.opensearch._types.OpenSearchException; +import org.opensearch.client.transport.Endpoint; +import org.opensearch.client.transport.JsonEndpoint; +import org.opensearch.client.transport.OpenSearchTransport; +import org.opensearch.client.transport.TransportException; +import org.opensearch.client.transport.TransportOptions; +import org.opensearch.client.transport.endpoints.BooleanEndpoint; +import org.opensearch.client.transport.endpoints.BooleanResponse; +import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.MissingRequiredPropertyException; /** * The {@link RestClientTransport} is deprecated and is scheduled for removal in later versions. Please @@ -165,8 +163,7 @@ public CompletableFuture performRequest future.cancellable = restClient.performRequestAsync(clientReq, new ResponseListener() { @Override public void onSuccess(Response clientResp) { - try (ApiTypeHelper.DisabledChecksHandle h = - ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck(disableRequiredChecks)) { + try (ApiTypeHelper.DisabledChecksHandle h = ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck(disableRequiredChecks)) { ResponseT response = getHighLevelResponse(clientResp, endpoint); future.complete(response); @@ -196,9 +193,9 @@ private org.opensearch.client.Request prepareLowLevelRequest( org.opensearch.client.Request clientReq = new org.opensearch.client.Request(method, path); - RequestOptions restOptions = options == null ? - transportOptions.restClientRequestOptions() : - RestClientOptions.of(options).restClientRequestOptions(); + RequestOptions restOptions = options == null + ? transportOptions.restClientRequestOptions() + : RestClientOptions.of(options).restClientRequestOptions(); if (restOptions != null) { clientReq.setOptions(restOptions); @@ -231,7 +228,7 @@ private org.opensearch.client.Request prepareLowLevelRequest( */ private void writeNdJson(NdJsonpSerializable value, ByteArrayOutputStream baos) { Iterator values = value._serializables(); - while(values.hasNext()) { + while (values.hasNext()) { Object item = values.next(); if (item instanceof NdJsonpSerializable && item != value) { // do not recurse on the item itself writeNdJson((NdJsonpSerializable) item, baos); @@ -259,18 +256,12 @@ private ResponseT getHighLevelResponse( } else if (endpoint.isError(statusCode)) { JsonpDeserializer errorDeserializer = endpoint.errorDeserializer(statusCode); if (errorDeserializer == null) { - throw new TransportException( - "Request failed with status code '" + statusCode + "'", - new ResponseException(clientResp) - ); + throw new TransportException("Request failed with status code '" + statusCode + "'", new ResponseException(clientResp)); } HttpEntity entity = clientResp.getEntity(); if (entity == null) { - throw new TransportException( - "Expecting a response body, but none was sent", - new ResponseException(clientResp) - ); + throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp)); } // We may have to replay it. @@ -283,12 +274,12 @@ private ResponseT getHighLevelResponse( // TODO: have the endpoint provide the exception constructor throw new OpenSearchException((ErrorResponse) error); } - } catch(MissingRequiredPropertyException errorEx) { + } catch (MissingRequiredPropertyException errorEx) { // Could not decode exception, try the response type try { ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint); return response; - } catch(Exception respEx) { + } catch (Exception respEx) { // No better luck: throw the original error decoding exception throw new TransportException("Failed to decode error response", new ResponseException(clientResp)); } @@ -302,7 +293,10 @@ private ResponseT getHighLevelResponse( } private ResponseT decodeResponse( - int statusCode, @Nullable HttpEntity entity, Response clientResp, Endpoint endpoint + int statusCode, + @Nullable HttpEntity entity, + Response clientResp, + Endpoint endpoint ) throws IOException { if (endpoint instanceof BooleanEndpoint) { @@ -312,24 +306,22 @@ private ResponseT decodeResponse( ResponseT response = (ResponseT) new BooleanResponse(bep.getResult(statusCode)); return response; - } else if (endpoint instanceof JsonEndpoint){ + } else if (endpoint instanceof JsonEndpoint) { @SuppressWarnings("unchecked") - JsonEndpoint jsonEndpoint = (JsonEndpoint)endpoint; + JsonEndpoint jsonEndpoint = (JsonEndpoint) endpoint; // Successful response ResponseT response = null; JsonpDeserializer responseParser = jsonEndpoint.responseDeserializer(); if (responseParser != null) { // Expecting a body if (entity == null) { - throw new TransportException( - "Expecting a response body, but none was sent", - new ResponseException(clientResp) - ); + throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp)); } InputStream content = entity.getContent(); try (JsonParser parser = mapper.jsonProvider().createParser(content)) { response = responseParser.deserialize(parser, mapper); - }; + } + ; } return response; } else { diff --git a/java-client/src/main/java/org/opensearch/client/util/ActionStatusOptions.java b/java-client/src/main/java/org/opensearch/client/util/ActionStatusOptions.java index e4e5cdf3c2..0d824a46e3 100644 --- a/java-client/src/main/java/org/opensearch/client/util/ActionStatusOptions.java +++ b/java-client/src/main/java/org/opensearch/client/util/ActionStatusOptions.java @@ -35,7 +35,6 @@ import org.opensearch.client.json.JsonEnum; import org.opensearch.client.json.JsonpDeserializable; - @JsonpDeserializable public enum ActionStatusOptions implements JsonEnum { Success("success"), @@ -59,5 +58,6 @@ public String jsonValue() { } public static final JsonEnum.Deserializer _DESERIALIZER = new JsonEnum.Deserializer<>( - ActionStatusOptions.values()); -} \ No newline at end of file + ActionStatusOptions.values() + ); +} diff --git a/java-client/src/main/java/org/opensearch/client/util/ApiTypeHelper.java b/java-client/src/main/java/org/opensearch/client/util/ApiTypeHelper.java index e804c3f832..1122975bc6 100644 --- a/java-client/src/main/java/org/opensearch/client/util/ApiTypeHelper.java +++ b/java-client/src/main/java/org/opensearch/client/util/ApiTypeHelper.java @@ -32,7 +32,6 @@ package org.opensearch.client.util; -import javax.annotation.Nullable; import java.util.AbstractList; import java.util.AbstractMap; import java.util.Collections; @@ -40,6 +39,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.annotation.Nullable; /** * Utility functions for API model types @@ -48,7 +48,7 @@ public class ApiTypeHelper { private ApiTypeHelper() {} - //----- Required properties + // ----- Required properties private static final ThreadLocal disableRequiredPropertiesCheck = new ThreadLocal<>(); @@ -62,6 +62,7 @@ public static class DisabledChecksHandle implements AutoCloseable { public DisabledChecksHandle(Boolean value) { this.value = value; } + @Override public void close() { disableRequiredPropertiesCheck.set(value); @@ -90,7 +91,7 @@ public static T requireNonNull(T value, Object obj, String name) { return value; } - //----- Lists + // ----- Lists /** Immutable empty list implementation so that we can create marker list objects */ static class EmptyList extends AbstractList { @@ -118,7 +119,7 @@ public Iterator iterator() { */ @SuppressWarnings("unchecked") public static List undefinedList() { - return (List)UNDEFINED_LIST; + return (List) UNDEFINED_LIST; } /** @@ -154,7 +155,7 @@ public static List unmodifiableRequired(List list, Object obj, String return Collections.unmodifiableList(list); } - //----- Maps + // ----- Maps static class EmptyMap extends AbstractMap { @Override @@ -171,7 +172,7 @@ public Set> entrySet() { */ @SuppressWarnings("unchecked") public static Map undefinedMap() { - return (Map)UNDEFINED_MAP; + return (Map) UNDEFINED_MAP; } /** diff --git a/java-client/src/main/java/org/opensearch/client/util/ListBuilder.java b/java-client/src/main/java/org/opensearch/client/util/ListBuilder.java index 886af4f808..8f03233bfa 100644 --- a/java-client/src/main/java/org/opensearch/client/util/ListBuilder.java +++ b/java-client/src/main/java/org/opensearch/client/util/ListBuilder.java @@ -59,7 +59,7 @@ public ListBuilder add(Function> fn) { } public ListBuilder addAll(Iterable iterable) { - for (T item: iterable) { + for (T item : iterable) { list.add(item); } return this; diff --git a/java-client/src/main/java/org/opensearch/client/util/MapBuilder.java b/java-client/src/main/java/org/opensearch/client/util/MapBuilder.java index 529b601018..e3b81ad493 100644 --- a/java-client/src/main/java/org/opensearch/client/util/MapBuilder.java +++ b/java-client/src/main/java/org/opensearch/client/util/MapBuilder.java @@ -62,7 +62,7 @@ public MapBuilder putAll(Map map) { } public MapBuilder putAll(Iterable> entries) { - for (Map.Entry entry: entries) { + for (Map.Entry entry : entries) { this.map.put(entry.getKey(), entry.getValue()); } return this; @@ -94,12 +94,12 @@ public static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4 } private static Map makeMap(Object... values) { - Map result = new HashMap<>(values.length/2); - for (int i = 0; i < values.length; i+=2) { + Map result = new HashMap<>(values.length / 2); + for (int i = 0; i < values.length; i += 2) { @SuppressWarnings("unchecked") - K k = (K)values[i]; + K k = (K) values[i]; @SuppressWarnings("unchecked") - V v = (V)values[i+1]; + V v = (V) values[i + 1]; result.put(k, v); } return result; diff --git a/java-client/src/main/java/org/opensearch/client/util/MissingRequiredPropertyException.java b/java-client/src/main/java/org/opensearch/client/util/MissingRequiredPropertyException.java index c17ba1b9ff..9dea4f4660 100644 --- a/java-client/src/main/java/org/opensearch/client/util/MissingRequiredPropertyException.java +++ b/java-client/src/main/java/org/opensearch/client/util/MissingRequiredPropertyException.java @@ -41,6 +41,7 @@ public class MissingRequiredPropertyException extends RuntimeException { private Class clazz; private String property; + public MissingRequiredPropertyException(Object obj, String property) { super("Missing required property '" + obj.getClass().getSimpleName() + "." + property + "'"); this.clazz = obj.getClass(); diff --git a/java-client/src/main/java/org/opensearch/client/util/ObjectBuilder.java b/java-client/src/main/java/org/opensearch/client/util/ObjectBuilder.java index 3bf1e28e0f..c2d3a6399f 100644 --- a/java-client/src/main/java/org/opensearch/client/util/ObjectBuilder.java +++ b/java-client/src/main/java/org/opensearch/client/util/ObjectBuilder.java @@ -38,5 +38,5 @@ * @param the type that will be built. */ public interface ObjectBuilder { - T build(); + T build(); } diff --git a/java-client/src/main/java/org/opensearch/client/util/ObjectBuilderBase.java b/java-client/src/main/java/org/opensearch/client/util/ObjectBuilderBase.java index 605d6763a5..788078c9dd 100644 --- a/java-client/src/main/java/org/opensearch/client/util/ObjectBuilderBase.java +++ b/java-client/src/main/java/org/opensearch/client/util/ObjectBuilderBase.java @@ -50,12 +50,11 @@ protected void _checkSingleUse() { this._used = true; } - //----- List utilities + // ----- List utilities /** A private extension of ArrayList so that we can recognize our own creations */ static final class InternalList extends ArrayList { - InternalList() { - } + InternalList() {} InternalList(Collection c) { super(c); @@ -98,12 +97,11 @@ protected static List _listAddAll(List list, List values) { } } - //----- Map utilities + // ----- Map utilities /** A private extension of HashMap so that we can recognize our own creations */ private static final class InternalMap extends HashMap { - InternalMap() { - } + InternalMap() {} InternalMap(Map m) { super(m); diff --git a/java-client/src/main/java/org/opensearch/client/util/OpenSearchRequestBodyBuffer.java b/java-client/src/main/java/org/opensearch/client/util/OpenSearchRequestBodyBuffer.java index 8302ccd148..012e90af08 100644 --- a/java-client/src/main/java/org/opensearch/client/util/OpenSearchRequestBodyBuffer.java +++ b/java-client/src/main/java/org/opensearch/client/util/OpenSearchRequestBodyBuffer.java @@ -9,12 +9,6 @@ package org.opensearch.client.util; import jakarta.json.stream.JsonGenerator; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.NdJsonpSerializable; -import org.opensearch.client.transport.OpenSearchTransport; - -import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -22,6 +16,11 @@ import java.io.OutputStream; import java.util.Iterator; import java.util.zip.GZIPOutputStream; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.NdJsonpSerializable; +import org.opensearch.client.transport.OpenSearchTransport; /** * Serializes and captures an OpenSearch request body, and then provides access to it in convenient @@ -220,8 +219,7 @@ public void write(int b) throws IOException { } @Override - public void close() throws IOException { - } + public void close() throws IOException {} } private static class CompressingOutputBuffer extends OutputStream { diff --git a/java-client/src/main/java/org/opensearch/client/util/TaggedUnionUtils.java b/java-client/src/main/java/org/opensearch/client/util/TaggedUnionUtils.java index 49d1b02fb2..1e437e24ce 100644 --- a/java-client/src/main/java/org/opensearch/client/util/TaggedUnionUtils.java +++ b/java-client/src/main/java/org/opensearch/client/util/TaggedUnionUtils.java @@ -32,10 +32,9 @@ package org.opensearch.client.util; -import org.opensearch.client.json.NdJsonpSerializable; - import java.util.Collections; import java.util.Iterator; +import org.opensearch.client.json.NdJsonpSerializable; public class TaggedUnionUtils { public static , Tag extends Enum, Value> Value get(Union union, Tag kind) { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java index b42c9b7897..af17210b97 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java @@ -35,6 +35,7 @@ import jakarta.json.JsonString; import jakarta.json.JsonValue; import jakarta.json.stream.JsonParser; +import java.io.StringReader; import org.junit.Assert; import org.junit.Test; import org.opensearch.client.json.JsonData; @@ -42,16 +43,13 @@ import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import org.opensearch.client.opensearch.model.ModelTestCase; -import java.io.StringReader; - - public class JsonDataTest extends Assert { @Test public void testParsing() { JsonpMapper mapper = new JsonbJsonpMapper(); - String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + - "\"stringValue\":\"foo\"}"; + String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + + "\"stringValue\":\"foo\"}"; JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json)); @@ -66,8 +64,8 @@ public void testParsing() { public void testSerialize() { JsonpMapper mapper = new JsonbJsonpMapper(); - String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + - "\"stringValue\":\"foo\"}"; + String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + + "\"stringValue\":\"foo\"}"; JsonpMapperTest.SomeClass sc = ModelTestCase.fromJson(json, JsonpMapperTest.SomeClass.class, mapper); @@ -89,6 +87,6 @@ public void testConvert() { final JsonValue value = json.toJson(new JsonbJsonpMapper()); assertEquals(JsonValue.ValueType.STRING, value.getValueType()); - assertEquals("foo", ((JsonString)value).getString()); + assertEquals("foo", ((JsonString) value).getString()); } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java index 2cdb878e7e..256eb9b81f 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonpMapperTest.java @@ -42,6 +42,13 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.Assert; import org.junit.Test; import org.opensearch.client.json.JsonpDeserializer; @@ -51,18 +58,9 @@ import org.opensearch.client.opensearch.IOUtils; import org.opensearch.client.opensearch.model.ModelTestCase; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.Writer; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicInteger; - public class JsonpMapperTest extends Assert { - String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + - "\"stringValue\":\"foo\"}"; + String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + "\"stringValue\":\"foo\"}"; @Test public void testJsonb() { @@ -86,8 +84,8 @@ public void testJacksonCustomMapper() { JacksonJsonpMapper mapper = new JacksonJsonpMapper(jkMapper); - String json = "{\"children\":[{\"double_value\":3.2,\"int_value\":2}],\"double_value\":2.1,\"int_value\":1," + - "\"string_value\":\"foo\"}"; + String json = "{\"children\":[{\"double_value\":3.2,\"int_value\":2}],\"double_value\":2.1,\"int_value\":1," + + "\"string_value\":\"foo\"}"; testSerialize(mapper, json); testDeserialize(mapper, json); @@ -100,18 +98,14 @@ public void testJacksonCustomJsonFactory() { final JacksonJsonpMapper mapper = new JacksonJsonpMapper(jkMapper, jsonFactory); final JsonValue json = Json.createObjectBuilder() - .add("children", Json - .createArrayBuilder() - .add(Json.createObjectBuilder() - .add("doubleValue", 3.2) - .add("intValue", 2))) + .add("children", Json.createArrayBuilder().add(Json.createObjectBuilder().add("doubleValue", 3.2).add("intValue", 2))) .add("doubleValue", "2.1") .add("intValue", 1) .add("stringValue", "foo") .build(); final Writer writer = new StringWriter(); - try (JsonGenerator generator = mapper.jsonProvider().createGenerator(writer)){ + try (JsonGenerator generator = mapper.jsonProvider().createGenerator(writer)) { generator.write(json); } @@ -134,7 +128,7 @@ public void testConcurrentLazyResolve() throws Exception { return JsonpDeserializer.integerDeserializer(); }); - // Two threads will attempt to deserialize. They should both be successful + // Two threads will attempt to deserialize. They should both be successful final AtomicInteger successes = new AtomicInteger(0); final JsonpMapper mapper = new JsonbJsonpMapper(); Runnable threadProc = () -> { @@ -142,7 +136,7 @@ public void testConcurrentLazyResolve() throws Exception { try { // Prior to fix, one of these would throw NPE because its // LazyDeserializer resolution would return null - deserializer.deserialize(parser,mapper); + deserializer.deserialize(parser, mapper); successes.incrementAndGet(); } catch (Throwable e) { // We'll notice that we failed to increment successes @@ -172,7 +166,6 @@ public void testConcurrentLazyResolve() throws Exception { assertEquals(2, successes.get()); } - private void testSerialize(JsonpMapper mapper, String expected) { SomeClass something = new SomeClass(); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/jackson/JacksonJsonpParserTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/jackson/JacksonJsonpParserTest.java index 21d7ff3fa2..be80a45c8b 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/json/jackson/JacksonJsonpParserTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/jackson/JacksonJsonpParserTest.java @@ -33,6 +33,14 @@ package org.opensearch.client.opensearch.json.jackson; import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.json.stream.JsonParser; +import jakarta.json.stream.JsonParser.Event; +import java.io.StringReader; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import javax.annotation.Nullable; +import org.junit.Test; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpMapperBase; @@ -40,15 +48,6 @@ import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.core.MsearchResponse; import org.opensearch.client.opensearch.model.ModelTestCase; -import jakarta.json.stream.JsonParser; -import jakarta.json.stream.JsonParser.Event; -import org.junit.Test; - -import javax.annotation.Nullable; -import java.io.StringReader; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; public class JacksonJsonpParserTest extends ModelTestCase { @@ -114,72 +113,73 @@ public void testForbidValueGettersAfterHasNext() { try { assertEquals("fooValue", parser.getString()); fail(); - } catch(IllegalStateException e) { + } catch (IllegalStateException e) { // expected } } @Test public void testMultiSearchResponse() { - String json = - "{\n" + - " \"took\" : 1,\n" + - " \"responses\" : [\n" + - " {\n" + - " \"error\" : {\n" + - " \"root_cause\" : [\n" + - " {\n" + - " \"type\" : \"index_not_found_exception\",\n" + - " \"reason\" : \"no such index [foo_bar]\",\n" + - " \"resource.type\" : \"index_or_alias\",\n" + - " \"resource.id\" : \"foo_bar\",\n" + - " \"index_uuid\" : \"_na_\",\n" + - " \"index\" : \"foo_bar\"\n" + - " }\n" + - " ],\n" + - " \"type\" : \"index_not_found_exception\",\n" + - " \"reason\" : \"no such index [foo_bar]\",\n" + - " \"resource.type\" : \"index_or_alias\",\n" + - " \"resource.id\" : \"foo_bar\",\n" + - " \"index_uuid\" : \"_na_\",\n" + - " \"index\" : \"foo_bar\"\n" + - " },\n" + - " \"status\" : 404\n" + - " },\n" + - " {\n" + - " \"took\" : 1,\n" + - " \"timed_out\" : false,\n" + - " \"_shards\" : {\n" + - " \"total\" : 1,\n" + - " \"successful\" : 1,\n" + - " \"skipped\" : 0,\n" + - " \"failed\" : 0\n" + - " },\n" + - " \"hits\" : {\n" + - " \"total\" : {\n" + - " \"value\" : 5,\n" + - " \"relation\" : \"eq\"\n" + - " },\n" + - " \"max_score\" : 1.0,\n" + - " \"hits\" : [\n" + - " {\n" + - " \"_index\" : \"foo\",\n" + - " \"_id\" : \"Wr0ApoEBa_iiaABtVM57\",\n" + - " \"_score\" : 1.0,\n" + - " \"_source\" : {\n" + - " \"x\" : 1,\n" + - " \"y\" : true\n" + - " }\n" + - " }\n" + - " ]\n" + - " },\n" + - " \"status\" : 200\n" + - " }\n" + - " ]\n" + - "}\n"; - - JsonpMapper mapper = new AttributedJacksonJsonpMapper() - .withAttribute("org.opensearch.client:Deserializer:_global.msearch.TDocument", JsonpDeserializer.of(Foo.class)); + String json = "{\n" + + " \"took\" : 1,\n" + + " \"responses\" : [\n" + + " {\n" + + " \"error\" : {\n" + + " \"root_cause\" : [\n" + + " {\n" + + " \"type\" : \"index_not_found_exception\",\n" + + " \"reason\" : \"no such index [foo_bar]\",\n" + + " \"resource.type\" : \"index_or_alias\",\n" + + " \"resource.id\" : \"foo_bar\",\n" + + " \"index_uuid\" : \"_na_\",\n" + + " \"index\" : \"foo_bar\"\n" + + " }\n" + + " ],\n" + + " \"type\" : \"index_not_found_exception\",\n" + + " \"reason\" : \"no such index [foo_bar]\",\n" + + " \"resource.type\" : \"index_or_alias\",\n" + + " \"resource.id\" : \"foo_bar\",\n" + + " \"index_uuid\" : \"_na_\",\n" + + " \"index\" : \"foo_bar\"\n" + + " },\n" + + " \"status\" : 404\n" + + " },\n" + + " {\n" + + " \"took\" : 1,\n" + + " \"timed_out\" : false,\n" + + " \"_shards\" : {\n" + + " \"total\" : 1,\n" + + " \"successful\" : 1,\n" + + " \"skipped\" : 0,\n" + + " \"failed\" : 0\n" + + " },\n" + + " \"hits\" : {\n" + + " \"total\" : {\n" + + " \"value\" : 5,\n" + + " \"relation\" : \"eq\"\n" + + " },\n" + + " \"max_score\" : 1.0,\n" + + " \"hits\" : [\n" + + " {\n" + + " \"_index\" : \"foo\",\n" + + " \"_id\" : \"Wr0ApoEBa_iiaABtVM57\",\n" + + " \"_score\" : 1.0,\n" + + " \"_source\" : {\n" + + " \"x\" : 1,\n" + + " \"y\" : true\n" + + " }\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"status\" : 200\n" + + " }\n" + + " ]\n" + + "}\n"; + + JsonpMapper mapper = new AttributedJacksonJsonpMapper().withAttribute( + "org.opensearch.client:Deserializer:_global.msearch.TDocument", + JsonpDeserializer.of(Foo.class) + ); @SuppressWarnings("unchecked") MsearchResponse response = fromJson(json, MsearchResponse.class, mapper); @@ -204,7 +204,7 @@ public AttributedJacksonJsonpMapper(ObjectMapper objectMapper) { @Override @SuppressWarnings("unchecked") public T attribute(String name) { - return attributes == null ? null : (T)attributes.get(name); + return attributes == null ? null : (T) attributes.get(name); } /** diff --git a/java-client/src/test/java/org/opensearch/client/transport/RequestOptionsTest.java b/java-client/src/test/java/org/opensearch/client/transport/RequestOptionsTest.java index 5d48da205d..c1f2879751 100644 --- a/java-client/src/test/java/org/opensearch/client/transport/RequestOptionsTest.java +++ b/java-client/src/test/java/org/opensearch/client/transport/RequestOptionsTest.java @@ -32,20 +32,7 @@ package org.opensearch.client.transport; -import org.opensearch.client.opensearch.OpenSearchClient; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; -import org.opensearch.client.transport.rest_client.RestClientTransport; import com.sun.net.httpserver.HttpServer; -import org.opensearch.client.ResponseException; -import org.opensearch.client.RestClient; -import org.apache.hc.core5.http.HttpHost; -import org.apache.hc.core5.http.NameValuePair; -import org.apache.hc.core5.net.URLEncodedUtils; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - import java.io.IOException; import java.io.OutputStreamWriter; import java.net.InetAddress; @@ -55,13 +42,24 @@ import java.util.Locale; import java.util.Map; import java.util.Properties; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.NameValuePair; +import org.apache.hc.core5.net.URLEncodedUtils; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opensearch.client.ResponseException; +import org.opensearch.client.RestClient; +import org.opensearch.client.json.jsonb.JsonbJsonpMapper; +import org.opensearch.client.opensearch.OpenSearchClient; +import org.opensearch.client.transport.rest_client.RestClientTransport; public class RequestOptionsTest extends Assert { private static HttpServer httpServer; private static RestClient restClient; - @Before public void classSetup() throws IOException { @@ -77,7 +75,7 @@ public void classSetup() throws IOException { ex.getResponseHeaders().putAll(ex.getRequestHeaders()); ex.sendResponseHeaders(418, 0); OutputStreamWriter out = new OutputStreamWriter(ex.getResponseBody(), StandardCharsets.UTF_8); - for (Map.Entry> header: ex.getRequestHeaders().entrySet()) { + for (Map.Entry> header : ex.getRequestHeaders().entrySet()) { out.write("header-"); out.write(header.getKey().toLowerCase(Locale.ROOT)); out.write("="); @@ -85,7 +83,7 @@ public void classSetup() throws IOException { out.write("\n"); } final List params = URLEncodedUtils.parse(ex.getRequestURI(), StandardCharsets.UTF_8); - for (NameValuePair param: params) { + for (NameValuePair param : params) { out.write("param-"); out.write(param.getName()); out.write("="); @@ -96,8 +94,7 @@ public void classSetup() throws IOException { httpServer.start(); InetSocketAddress address = httpServer.getAddress(); - restClient = RestClient.builder(new HttpHost("http", address.getHostString(), address.getPort())) - .build(); + restClient = RestClient.builder(new HttpHost("http", address.getHostString(), address.getPort())).build(); } @After @@ -117,12 +114,9 @@ private Properties getProps(OpenSearchClient client) throws IOException { @Test public void testClientHeader() throws IOException { final RestClientTransport trsp = new RestClientTransport(restClient, new JsonbJsonpMapper()); - final OpenSearchClient client = new OpenSearchClient(trsp) - .withTransportOptions(trsp.options().with( - b -> b.addHeader("X-Foo", "Bar") - .addHeader("uSer-agEnt", "MegaClient/1.2.3") - ) - ); + final OpenSearchClient client = new OpenSearchClient(trsp).withTransportOptions( + trsp.options().with(b -> b.addHeader("X-Foo", "Bar").addHeader("uSer-agEnt", "MegaClient/1.2.3")) + ); Properties props = getProps(client); assertEquals("Bar", props.getProperty("header-x-foo")); diff --git a/java-client/src/test/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncEntityConsumerTest.java b/java-client/src/test/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncEntityConsumerTest.java index 9da747659a..283a7acd8b 100644 --- a/java-client/src/test/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncEntityConsumerTest.java +++ b/java-client/src/test/java/org/opensearch/client/transport/httpclient5/internal/HeapBufferedAsyncEntityConsumerTest.java @@ -8,21 +8,18 @@ package org.opensearch.client.transport.httpclient5.internal; -import org.apache.hc.core5.http.ContentTooLongException; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThrows; import com.carrotsearch.randomizedtesting.RandomizedTest; - +import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.hc.core5.http.ContentTooLongException; import org.junit.After; import org.junit.Before; import org.junit.Test; -import java.io.IOException; -import java.nio.ByteBuffer; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertThrows; - public class HeapBufferedAsyncEntityConsumerTest extends RandomizedTest { private static final int BUFFER_LIMIT = 100 * 1024 * 1024 /* 100Mb */; private HeapBufferedAsyncEntityConsumer consumer; diff --git a/samples/build.gradle.kts b/samples/build.gradle.kts index b8e7339202..f181bc6bc7 100644 --- a/samples/build.gradle.kts +++ b/samples/build.gradle.kts @@ -9,11 +9,7 @@ plugins { java application - checkstyle -} - -checkstyle { - toolVersion = "10.0" + id("com.diffplug.spotless") version "6.22.0" } java { @@ -30,6 +26,22 @@ dependencies { implementation("com.fasterxml.jackson.core", "jackson-databind", "2.15.2") } +spotless { + java { + + target("**/*.java") + + // Use the default importOrder configuration + importOrder() + removeUnusedImports() + + eclipse().configFile("../buildSrc/formatterConfig.xml") + + trimTrailingWhitespace() + endWithNewline() + } +} + application { mainClass.set("org.opensearch.client.samples.Main") } diff --git a/samples/src/main/java/org/opensearch/client/samples/Bulk.java b/samples/src/main/java/org/opensearch/client/samples/Bulk.java index b3f84f04e6..1613dc9311 100644 --- a/samples/src/main/java/org/opensearch/client/samples/Bulk.java +++ b/samples/src/main/java/org/opensearch/client/samples/Bulk.java @@ -10,7 +10,6 @@ import java.util.ArrayList; import java.util.List; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.client.opensearch._types.Refresh; @@ -46,51 +45,38 @@ public static void main(String[] args) { if (!client.indices().exists(r -> r.index(indexName)).value()) { LOGGER.info("Creating index {}", indexName); - IndexSettings settings = new IndexSettings.Builder() - .numberOfShards("2") - .numberOfReplicas("1") - .build(); - TypeMapping mapping = new TypeMapping.Builder() - .properties("age", new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build()) - .build(); - CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder() - .index(indexName) - .settings(settings) - .mappings(mapping) - .build(); + IndexSettings settings = new IndexSettings.Builder().numberOfShards("2").numberOfReplicas("1").build(); + TypeMapping mapping = new TypeMapping.Builder().properties( + "age", + new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build() + ).build(); + CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(indexName) + .settings(settings) + .mappings(mapping) + .build(); client.indices().create(createIndexRequest); } LOGGER.info("Bulk indexing documents"); ArrayList ops = new ArrayList<>(); IndexData doc1 = new IndexData("Document 1", "The text of document 1"); - ops.add(new BulkOperation.Builder().index( - IndexOperation.of(io -> io.index(indexName).id("id1").document(doc1)) - ).build()); + ops.add(new BulkOperation.Builder().index(IndexOperation.of(io -> io.index(indexName).id("id1").document(doc1))).build()); IndexData doc2 = new IndexData("Document 2", "The text of document 2"); - ops.add(new BulkOperation.Builder().index( - IndexOperation.of(io -> io.index(indexName).id("id2").document(doc2)) - ).build()); + ops.add(new BulkOperation.Builder().index(IndexOperation.of(io -> io.index(indexName).id("id2").document(doc2))).build()); IndexData doc3 = new IndexData("Document 3", "The text of document 3"); - ops.add(new BulkOperation.Builder().index( - IndexOperation.of(io -> io.index(indexName).id("id3").document(doc3)) - ).build()); + ops.add(new BulkOperation.Builder().index(IndexOperation.of(io -> io.index(indexName).id("id3").document(doc3))).build()); - BulkRequest.Builder bulkReq = new BulkRequest.Builder() - .index(indexName) - .operations(ops) - .refresh(Refresh.WaitFor); + BulkRequest.Builder bulkReq = new BulkRequest.Builder().index(indexName).operations(ops).refresh(Refresh.WaitFor); BulkResponse bulkResponse = client.bulk(bulkReq.build()); LOGGER.info("Bulk response items: {}", bulkResponse.items().size()); Query query = Query.of(qb -> qb.match(mb -> mb.field("title").query(fv -> fv.stringValue("Document")))); - final SearchRequest.Builder searchReq = new SearchRequest.Builder() - .allowPartialSearchResults(false) - .index(List.of(indexName)) - .size(10) - .source(sc -> sc.fetch(false)) - .ignoreThrottled(false) - .query(query); + final SearchRequest.Builder searchReq = new SearchRequest.Builder().allowPartialSearchResults(false) + .index(List.of(indexName)) + .size(10) + .source(sc -> sc.fetch(false)) + .ignoreThrottled(false) + .query(query); SearchResponse searchResponse = client.search(searchReq.build(), IndexData.class); LOGGER.info("Found {} documents", searchResponse.hits().hits().size()); diff --git a/samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java b/samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java index e6d8bf5875..762e94c6bf 100644 --- a/samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java +++ b/samples/src/main/java/org/opensearch/client/samples/DataStreamBasics.java @@ -38,15 +38,12 @@ public static void main(String[] args) { String dataStreamName = "sample-data-stream-1"; // Create an index template which configures data stream - PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest.Builder() - .name(dataStreamIndexTemplateName) - .indexPatterns(namePattern) - .dataStream(new DataStream.Builder() - .timestampField(t -> t.name(timestampFieldName)) - .build()) - .build(); + PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest.Builder().name(dataStreamIndexTemplateName) + .indexPatterns(namePattern) + .dataStream(new DataStream.Builder().timestampField(t -> t.name(timestampFieldName)).build()) + .build(); client.indices().putIndexTemplate(putIndexTemplateRequest); - + // Create a data stream CreateDataStreamRequest createDataStreamRequest = new CreateDataStreamRequest.Builder().name(dataStreamName).build(); client.indices().createDataStream(createDataStreamRequest); diff --git a/samples/src/main/java/org/opensearch/client/samples/IndexingBasics.java b/samples/src/main/java/org/opensearch/client/samples/IndexingBasics.java index bba03de80b..db7bfdf3f6 100644 --- a/samples/src/main/java/org/opensearch/client/samples/IndexingBasics.java +++ b/samples/src/main/java/org/opensearch/client/samples/IndexingBasics.java @@ -9,7 +9,6 @@ package org.opensearch.client.samples; import java.util.ArrayList; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.client.json.JsonData; @@ -47,27 +46,24 @@ public static void main(String[] args) { if (!client.indices().exists(r -> r.index(indexName)).value()) { LOGGER.info("Creating index {}", indexName); - IndexSettings settings = new IndexSettings.Builder() - .numberOfShards("2") - .numberOfReplicas("1") - .build(); - TypeMapping mapping = new TypeMapping.Builder() - .properties("age", new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build()) - .build(); - CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder() - .index(indexName) - .settings(settings) - .mappings(mapping) - .build(); + IndexSettings settings = new IndexSettings.Builder().numberOfShards("2").numberOfReplicas("1").build(); + TypeMapping mapping = new TypeMapping.Builder().properties( + "age", + new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build() + ).build(); + CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(indexName) + .settings(settings) + .mappings(mapping) + .build(); client.indices().create(createIndexRequest); } LOGGER.info("Indexing documents"); IndexData indexData = new IndexData("Document 1", "Text for document 1"); IndexRequest indexRequest = new IndexRequest.Builder().index(indexName) - .id("1") - .document(indexData) - .build(); + .id("1") + .document(indexData) + .build(); client.index(indexRequest); indexData = new IndexData("Document 2", "Text for document 2"); @@ -84,14 +80,8 @@ public static void main(String[] args) { LOGGER.info("Adding a new mapping to index {}", indexName); PutMappingRequest.Builder mappingsRequestBuilder = new PutMappingRequest.Builder().index(indexName) - .source( - new SourceField.Builder() - .enabled(true) - .build()) - .routing( - new RoutingField.Builder() - .required(false) - .build()) + .source(new SourceField.Builder().enabled(true).build()) + .routing(new RoutingField.Builder().required(false).build()) .dynamic(DynamicMapping.Strict) .meta("key", JsonData.of("key value")) .fieldNames(new FieldNamesField.Builder().enabled(false).build()) @@ -100,7 +90,7 @@ public static void main(String[] args) { .dynamicTemplates(new ArrayList<>()) .numericDetection(false); client.indices().putMapping(mappingsRequestBuilder.build()); - + GetMappingRequest mappingsRequest = new GetMappingRequest.Builder().index(indexName).build(); GetMappingResponse getMappingResponse = client.indices().getMapping(mappingsRequest); LOGGER.info("Mappings {} found for index {}", getMappingResponse.result().get(indexName).mappings(), indexName); diff --git a/samples/src/main/java/org/opensearch/client/samples/PointInTime.java b/samples/src/main/java/org/opensearch/client/samples/PointInTime.java index 55a348ef8c..53e29743c4 100644 --- a/samples/src/main/java/org/opensearch/client/samples/PointInTime.java +++ b/samples/src/main/java/org/opensearch/client/samples/PointInTime.java @@ -41,24 +41,21 @@ public static void main(String[] args) { if (!client.indices().exists(r -> r.index(indexName)).value()) { LOGGER.info("Creating index {}", indexName); - IndexSettings settings = new IndexSettings.Builder() - .numberOfShards("2") - .numberOfReplicas("1") - .build(); - TypeMapping mapping = new TypeMapping.Builder() - .properties("age", new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build()) - .build(); - CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder() - .index(indexName) - .settings(settings) - .mappings(mapping) - .build(); + IndexSettings settings = new IndexSettings.Builder().numberOfShards("2").numberOfReplicas("1").build(); + TypeMapping mapping = new TypeMapping.Builder().properties( + "age", + new Property.Builder().integer(new IntegerNumberProperty.Builder().build()).build() + ).build(); + CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(indexName) + .settings(settings) + .mappings(mapping) + .build(); client.indices().create(createIndexRequest); } - CreatePitRequest createPitRequest = new CreatePitRequest.Builder() - .targetIndexes(Collections.singletonList(indexName)) - .keepAlive(new Time.Builder().time("100m").build()).build(); + CreatePitRequest createPitRequest = new CreatePitRequest.Builder().targetIndexes(Collections.singletonList(indexName)) + .keepAlive(new Time.Builder().time("100m").build()) + .build(); CreatePitResponse createPitResponse = client.createPit(createPitRequest); LOGGER.info("PIT created with id: {}", createPitResponse.pitId()); @@ -66,11 +63,11 @@ public static void main(String[] args) { ListAllPitResponse listAllPitResponse = client.listAllPit(); LOGGER.info("Found {} PITs", listAllPitResponse.pits().size()); - DeletePitRequest deletePitRequest = new DeletePitRequest.Builder() - .pitId(Collections.singletonList(createPitResponse.pitId())).build(); + DeletePitRequest deletePitRequest = new DeletePitRequest.Builder().pitId(Collections.singletonList(createPitResponse.pitId())) + .build(); DeletePitResponse deletePitResponse = client.deletePit(deletePitRequest); LOGGER.info("Deleting PIT: {}", deletePitResponse.pits()); - + LOGGER.info("Deleting index {}", indexName); DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(indexName).build(); client.indices().delete(deleteIndexRequest); diff --git a/samples/src/main/java/org/opensearch/client/samples/SampleClient.java b/samples/src/main/java/org/opensearch/client/samples/SampleClient.java index 9b73d9504f..b7b735037e 100644 --- a/samples/src/main/java/org/opensearch/client/samples/SampleClient.java +++ b/samples/src/main/java/org/opensearch/client/samples/SampleClient.java @@ -32,40 +32,29 @@ public static OpenSearchClient create() throws NoSuchAlgorithmException, KeyStor var user = env.getOrDefault("USERNAME", "admin"); var pass = env.getOrDefault("PASSWORD", "admin"); - final var hosts = new HttpHost[]{ - new HttpHost(https ? "https" : "http", hostname, port) - }; + final var hosts = new HttpHost[] { new HttpHost(https ? "https" : "http", hostname, port) }; - final var sslContext = SSLContextBuilder.create() - .loadTrustMaterial(null, (chains, authType) -> true) - .build(); + final var sslContext = SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build(); - final var transport = ApacheHttpClient5TransportBuilder - .builder(hosts) - .setMapper(new JacksonJsonpMapper()) - .setHttpClientConfigCallback(httpClientBuilder -> { - final var credentialsProvider = new BasicCredentialsProvider(); - for (final var host : hosts) { - credentialsProvider.setCredentials( - new AuthScope(host), - new UsernamePasswordCredentials(user, pass.toCharArray())); - } + final var transport = ApacheHttpClient5TransportBuilder.builder(hosts) + .setMapper(new JacksonJsonpMapper()) + .setHttpClientConfigCallback(httpClientBuilder -> { + final var credentialsProvider = new BasicCredentialsProvider(); + for (final var host : hosts) { + credentialsProvider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(user, pass.toCharArray())); + } - // Disable SSL/TLS verification as our local testing clusters use self-signed certificates - final var tlsStrategy = ClientTlsStrategyBuilder.create() - .setSslContext(sslContext) - .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .build(); + // Disable SSL/TLS verification as our local testing clusters use self-signed certificates + final var tlsStrategy = ClientTlsStrategyBuilder.create() + .setSslContext(sslContext) + .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .build(); - final var connectionManager = PoolingAsyncClientConnectionManagerBuilder.create() - .setTlsStrategy(tlsStrategy) - .build(); + final var connectionManager = PoolingAsyncClientConnectionManagerBuilder.create().setTlsStrategy(tlsStrategy).build(); - return httpClientBuilder - .setDefaultCredentialsProvider(credentialsProvider) - .setConnectionManager(connectionManager); - }) - .build(); + return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(connectionManager); + }) + .build(); return new OpenSearchClient(transport); } } diff --git a/samples/src/main/java/org/opensearch/client/samples/Search.java b/samples/src/main/java/org/opensearch/client/samples/Search.java index ea41bfc48c..ce357d2942 100644 --- a/samples/src/main/java/org/opensearch/client/samples/Search.java +++ b/samples/src/main/java/org/opensearch/client/samples/Search.java @@ -11,7 +11,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.Map; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.client.opensearch.OpenSearchClient; @@ -69,9 +68,9 @@ public static void main(String[] args) { LOGGER.info("Indexing documents"); IndexData indexData = new IndexData("Document 1", "Text for document 1"); IndexRequest indexRequest = new IndexRequest.Builder().index(indexName) - .id("1") - .document(indexData) - .build(); + .id("1") + .document(indexData) + .build(); client.index(indexRequest); indexData = new IndexData("Document 2", "Text for document 2"); @@ -86,21 +85,18 @@ public static void main(String[] args) { LOGGER.info("Found {} with score {}", hit.source(), hit.score()); } - SearchRequest searchRequest = new SearchRequest.Builder().query(q -> q.match(m -> m.field("text") - .query(FieldValue.of("Text for document 2")))) - .build(); + SearchRequest searchRequest = new SearchRequest.Builder().query( + q -> q.match(m -> m.field("text").query(FieldValue.of("Text for document 2"))) + ).build(); searchResponse = client.search(searchRequest, IndexData.class); for (var hit : searchResponse.hits().hits()) { LOGGER.info("Found {} with score {}", hit.source(), hit.score()); } - searchRequest = new SearchRequest.Builder().query(q -> q.match(m -> m.field("title") - .query(FieldValue.of("Document 1")))) - .aggregations("titles", - new Aggregation.Builder().terms(t -> t.field("title.keyword")) - .build()) - .build(); + searchRequest = new SearchRequest.Builder().query(q -> q.match(m -> m.field("title").query(FieldValue.of("Document 1")))) + .aggregations("titles", new Aggregation.Builder().terms(t -> t.field("title.keyword")).build()) + .build(); searchResponse = client.search(searchRequest, IndexData.class); for (Map.Entry entry : searchResponse.aggregations().entrySet()) { @@ -123,53 +119,28 @@ public static void main(String[] args) { public static void searchWithCompletionSuggester() { try { var index = "completion-suggester"; - Property intValueProp = new Property.Builder() - .long_(v -> v) - .build(); - Property msgCompletionProp = new Property.Builder() - .completion(c -> c) - .build(); - client.indices().create(c -> c - .index(index) - .mappings(m -> m - .properties("intValue", intValueProp) - .properties("msg", msgCompletionProp))); + Property intValueProp = new Property.Builder().long_(v -> v).build(); + Property msgCompletionProp = new Property.Builder().completion(c -> c).build(); + client.indices() + .create(c -> c.index(index).mappings(m -> m.properties("intValue", intValueProp).properties("msg", msgCompletionProp))); AppData appData = new AppData(); appData.setIntValue(1337); appData.setMsg("foo"); - client.index(b -> b - .index(index) - .id("1") - .document(appData) - .refresh(Refresh.True)); + client.index(b -> b.index(index).id("1").document(appData).refresh(Refresh.True)); appData.setIntValue(1338); appData.setMsg("foobar"); - client.index(b -> b - .index(index) - .id("2") - .document(appData) - .refresh(Refresh.True)); + client.index(b -> b.index(index).id("2").document(appData).refresh(Refresh.True)); String suggesterName = "msgSuggester"; - CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion() - .field("msg") - .size(1) - .build(); - FieldSuggester fieldSuggester = new FieldSuggester.Builder().prefix("foo") - .completion(completionSuggester) - .build(); - Suggester suggester = new Suggester.Builder() - .suggesters(Collections.singletonMap(suggesterName, fieldSuggester)) - .build(); - SearchRequest searchRequest = new SearchRequest.Builder() - .index(index) - .suggest(suggester) - .build(); + CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion().field("msg").size(1).build(); + FieldSuggester fieldSuggester = new FieldSuggester.Builder().prefix("foo").completion(completionSuggester).build(); + Suggester suggester = new Suggester.Builder().suggesters(Collections.singletonMap(suggesterName, fieldSuggester)).build(); + SearchRequest searchRequest = new SearchRequest.Builder().index(index).suggest(suggester).build(); SearchResponse searchResponse = client.search(searchRequest, AppData.class); LOGGER.info("Suggester response size {}", searchResponse.suggest().get(suggesterName).size()); @@ -183,46 +154,27 @@ public static void searchWithCompletionSuggester() { public static void searchWithTermSuggester() { try { String index = "term-suggester"; - + // term suggester does not require a special mapping - client.indices().create(c -> c - .index(index)); + client.indices().create(c -> c.index(index)); AppData appData = new AppData(); appData.setIntValue(1337); appData.setMsg("foo"); - client.index(b -> b - .index(index) - .id("1") - .document(appData) - .refresh(Refresh.True)); + client.index(b -> b.index(index).id("1").document(appData).refresh(Refresh.True)); appData.setIntValue(1338); appData.setMsg("foobar"); - client.index(b -> b - .index(index) - .id("2") - .document(appData) - .refresh(Refresh.True)); + client.index(b -> b.index(index).id("2").document(appData).refresh(Refresh.True)); String suggesterName = "msgSuggester"; - TermSuggester termSuggester = FieldSuggesterBuilders.term() - .field("msg") - .size(1) - .build(); - FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("fool") - .term(termSuggester) - .build(); - Suggester suggester = new Suggester.Builder() - .suggesters(Collections.singletonMap(suggesterName, fieldSuggester)) - .build(); - SearchRequest searchRequest = new SearchRequest.Builder() - .index(index) - .suggest(suggester) - .build(); + TermSuggester termSuggester = FieldSuggesterBuilders.term().field("msg").size(1).build(); + FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("fool").term(termSuggester).build(); + Suggester suggester = new Suggester.Builder().suggesters(Collections.singletonMap(suggesterName, fieldSuggester)).build(); + SearchRequest searchRequest = new SearchRequest.Builder().index(index).suggest(suggester).build(); SearchResponse searchResponse = client.search(searchRequest, AppData.class); LOGGER.info("Suggester response size {}", searchResponse.suggest().get(suggesterName).size()); @@ -237,72 +189,51 @@ public static void searchWithPhraseSuggester() { try { String index = "test-phrase-suggester"; - ShingleTokenFilter shingleTokenFilter = new ShingleTokenFilter.Builder().minShingleSize("2") - .maxShingleSize("3") - .build(); + ShingleTokenFilter shingleTokenFilter = new ShingleTokenFilter.Builder().minShingleSize("2").maxShingleSize("3").build(); - Analyzer analyzer = new Analyzer.Builder() - .custom(new CustomAnalyzer.Builder().tokenizer("standard") - .filter(Arrays.asList("lowercase", "shingle")).build()) - .build(); + Analyzer analyzer = new Analyzer.Builder().custom( + new CustomAnalyzer.Builder().tokenizer("standard").filter(Arrays.asList("lowercase", "shingle")).build() + ).build(); - TokenFilter tokenFilter = new TokenFilter.Builder() - .definition(new TokenFilterDefinition.Builder() - .shingle(shingleTokenFilter).build()) - .build(); + TokenFilter tokenFilter = new TokenFilter.Builder().definition( + new TokenFilterDefinition.Builder().shingle(shingleTokenFilter).build() + ).build(); - IndexSettingsAnalysis indexSettingsAnalysis = new IndexSettingsAnalysis.Builder() - .analyzer("trigram", analyzer) - .filter("shingle", tokenFilter) - .build(); + IndexSettingsAnalysis indexSettingsAnalysis = new IndexSettingsAnalysis.Builder().analyzer("trigram", analyzer) + .filter("shingle", tokenFilter) + .build(); IndexSettings settings = new IndexSettings.Builder().analysis(indexSettingsAnalysis).build(); - TypeMapping mapping = new TypeMapping.Builder().properties("msg", new Property.Builder() - .text(new TextProperty.Builder().fields("trigram", new Property.Builder() - .text(new TextProperty.Builder().analyzer("trigram").build()) - .build()).build()) - .build()).build(); + TypeMapping mapping = new TypeMapping.Builder().properties( + "msg", + new Property.Builder().text( + new TextProperty.Builder().fields( + "trigram", + new Property.Builder().text(new TextProperty.Builder().analyzer("trigram").build()).build() + ).build() + ).build() + ).build(); - client.indices().create(c -> c - .index(index) - .settings(settings) - .mappings(mapping)); + client.indices().create(c -> c.index(index).settings(settings).mappings(mapping)); AppData appData = new AppData(); appData.setIntValue(1337); appData.setMsg("Design Patterns"); - client.index(b -> b - .index(index) - .id("1") - .document(appData) - .refresh(Refresh.True)); + client.index(b -> b.index(index).id("1").document(appData).refresh(Refresh.True)); appData.setIntValue(1338); appData.setMsg("Software Architecture Patterns Explained"); - client.index(b -> b - .index(index) - .id("2") - .document(appData) - .refresh(Refresh.True)); + client.index(b -> b.index(index).id("2").document(appData).refresh(Refresh.True)); String suggesterName = "msgSuggester"; - PhraseSuggester phraseSuggester = FieldSuggesterBuilders.phrase() - .field("msg.trigram") - .build(); - FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("design paterns") - .phrase(phraseSuggester) - .build(); - Suggester suggester = new Suggester.Builder() - .suggesters(Collections.singletonMap(suggesterName, fieldSuggester)) - .build(); - SearchRequest searchRequest = new SearchRequest.Builder() - .index(index) - .suggest(suggester) - .build(); + PhraseSuggester phraseSuggester = FieldSuggesterBuilders.phrase().field("msg.trigram").build(); + FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("design paterns").phrase(phraseSuggester).build(); + Suggester suggester = new Suggester.Builder().suggesters(Collections.singletonMap(suggesterName, fieldSuggester)).build(); + SearchRequest searchRequest = new SearchRequest.Builder().index(index).suggest(suggester).build(); SearchResponse searchResponse = client.search(searchRequest, AppData.class); LOGGER.info("Suggester response size {}", searchResponse.suggest().get(suggesterName).size()); diff --git a/samples/src/main/java/org/opensearch/client/samples/knn/KnnBasics.java b/samples/src/main/java/org/opensearch/client/samples/knn/KnnBasics.java index 1ac93a2723..c4d89fee1c 100644 --- a/samples/src/main/java/org/opensearch/client/samples/knn/KnnBasics.java +++ b/samples/src/main/java/org/opensearch/client/samples/knn/KnnBasics.java @@ -33,12 +33,12 @@ public static void main(String[] args) { if (!client.indices().exists(r -> r.index(indexName)).value()) { LOGGER.info("Creating index {}", indexName); - client.indices().create(r -> r - .index(indexName) - .settings(s -> s.knn(true)) - .mappings(m -> m - .properties("values", p -> p - .knnVector(k -> k.dimension(dimensions))))); + client.indices() + .create( + r -> r.index(indexName) + .settings(s -> s.knn(true)) + .mappings(m -> m.properties("values", p -> p.knnVector(k -> k.dimension(dimensions)))) + ); } final var nVectors = 10; @@ -46,11 +46,7 @@ public static void main(String[] args) { for (var i = 0; i < nVectors; ++i) { var id = Integer.toString(i); var doc = Doc.rand(dimensions); - bulkRequest.operations(b -> b - .index(o -> o - .index(indexName) - .id(id) - .document(doc))); + bulkRequest.operations(b -> b.index(o -> o.index(indexName).id(id).document(doc))); } LOGGER.info("Indexing {} vectors", nVectors); @@ -62,14 +58,10 @@ public static void main(String[] args) { final var searchVector = RandUtil.rand2SfArray(dimensions); LOGGER.info("Searching for vector {}", searchVector); - var searchResponse = client.search(s -> s - .index(indexName) - .query(q -> q - .knn(k -> k - .field("values") - .vector(searchVector) - .k(3))), - Doc.class); + var searchResponse = client.search( + s -> s.index(indexName).query(q -> q.knn(k -> k.field("values").vector(searchVector).k(3))), + Doc.class + ); for (var hit : searchResponse.hits().hits()) { LOGGER.info("Found {} with score {}", hit.source(), hit.score()); @@ -105,9 +97,7 @@ public void setValues(float[] values) { @Override public String toString() { - return "{" + - "values=" + Arrays.toString(values) + - '}'; + return "{" + "values=" + Arrays.toString(values) + '}'; } } } diff --git a/samples/src/main/java/org/opensearch/client/samples/knn/KnnBooleanFilter.java b/samples/src/main/java/org/opensearch/client/samples/knn/KnnBooleanFilter.java index 47f637a8d0..b0cff0cc6a 100644 --- a/samples/src/main/java/org/opensearch/client/samples/knn/KnnBooleanFilter.java +++ b/samples/src/main/java/org/opensearch/client/samples/knn/KnnBooleanFilter.java @@ -33,25 +33,21 @@ public static void main(String[] args) { if (!client.indices().exists(r -> r.index(indexName)).value()) { LOGGER.info("Creating index {}", indexName); - client.indices().create(r -> r - .index(indexName) - .settings(s -> s.knn(true)) - .mappings(m -> m - .properties("values", p -> p - .knnVector(k -> k.dimension(dimensions))))); + client.indices() + .create( + r -> r.index(indexName) + .settings(s -> s.knn(true)) + .mappings(m -> m.properties("values", p -> p.knnVector(k -> k.dimension(dimensions)))) + ); } final var nVectors = 3000; - final var genres = new String[] {"fiction", "drama", "romance"}; + final var genres = new String[] { "fiction", "drama", "romance" }; var bulkRequest = new BulkRequest.Builder(); for (var i = 0; i < nVectors; ++i) { var id = Integer.toString(i); var doc = Doc.rand(dimensions, genres); - bulkRequest.operations(b -> b - .index(o -> o - .index(indexName) - .id(id) - .document(doc))); + bulkRequest.operations(b -> b.index(o -> o.index(indexName).id(id).document(doc))); } LOGGER.info("Indexing {} vectors", nVectors); @@ -64,22 +60,19 @@ public static void main(String[] args) { final var searchVector = RandUtil.rand2SfArray(dimensions); LOGGER.info("Searching for vector {} with the '{}' genre", searchVector, searchGenre); - var searchResponse = client.search(s -> s - .index(indexName) - .query(q -> q - .bool(b -> b - .filter(f -> f - .bool(b2 -> b2 - .must(m -> m - .term(t -> t - .field("metadata.genre") - .value(v -> v.stringValue(searchGenre)))))) - .must(m -> m - .knn(k -> k - .field("values") - .vector(searchVector) - .k(5))))), - Doc.class); + var searchResponse = client.search( + s -> s.index(indexName) + .query( + q -> q.bool( + b -> b.filter( + f -> f.bool( + b2 -> b2.must(m -> m.term(t -> t.field("metadata.genre").value(v -> v.stringValue(searchGenre)))) + ) + ).must(m -> m.knn(k -> k.field("values").vector(searchVector).k(5))) + ) + ), + Doc.class + ); for (var hit : searchResponse.hits().hits()) { LOGGER.info("Found {} with score {}", hit.source(), hit.score()); @@ -125,10 +118,7 @@ public void setMetadata(Metadata metadata) { @Override public String toString() { - return "{" + - "values=" + Arrays.toString(values) + - ", metadata=" + metadata + - '}'; + return "{" + "values=" + Arrays.toString(values) + ", metadata=" + metadata + '}'; } } @@ -151,9 +141,7 @@ public void setGenre(String genre) { @Override public String toString() { - return "{" + - "genre=" + genre + - '}'; + return "{" + "genre=" + genre + '}'; } } } diff --git a/samples/src/main/java/org/opensearch/client/samples/knn/KnnEfficientFilter.java b/samples/src/main/java/org/opensearch/client/samples/knn/KnnEfficientFilter.java index 6f512a0f1d..e24de64826 100644 --- a/samples/src/main/java/org/opensearch/client/samples/knn/KnnEfficientFilter.java +++ b/samples/src/main/java/org/opensearch/client/samples/knn/KnnEfficientFilter.java @@ -34,49 +34,47 @@ public static void main(String[] args) { if (!client.indices().exists(r -> r.index(indexName)).value()) { LOGGER.info("Creating index {}", indexName); - client.indices().create(r -> r - .index(indexName) - .settings(s -> s - .knn(true) - .knnAlgoParamEfSearch(100) - .numberOfShards("1") - .numberOfReplicas("0")) - .mappings(m -> m - .properties("location", p -> p - .knnVector(k -> k - .dimension(2) - .method(v -> v - .name("hnsw") - .spaceType("l2") - .engine("lucene") - .parameters("ef_construction", JsonData.of(100)) - .parameters("m", JsonData.of(16))))))); + client.indices() + .create( + r -> r.index(indexName) + .settings(s -> s.knn(true).knnAlgoParamEfSearch(100).numberOfShards("1").numberOfReplicas("0")) + .mappings( + m -> m.properties( + "location", + p -> p.knnVector( + k -> k.dimension(2) + .method( + v -> v.name("hnsw") + .spaceType("l2") + .engine("lucene") + .parameters("ef_construction", JsonData.of(100)) + .parameters("m", JsonData.of(16)) + ) + ) + ) + ) + ); } final var hotels = new Hotel[] { - new Hotel(5.2f, 4.f, true, 5), - new Hotel(5.2f, 3.9f, false, 4), - new Hotel(4.9f, 3.4f, true, 9), - new Hotel(4.2f, 4.6f, false, 6), - new Hotel(3.3f, 4.5f, true, 8), - new Hotel(6.4f, 3.4f, true, 9), - new Hotel(4.2f, 6.2f, true, 5), - new Hotel(2.4f, 4.0f, true, 8), - new Hotel(1.4f, 3.2f, false, 5), - new Hotel(7.0f, 9.9f, true, 9), - new Hotel(3.0f, 2.3f, false, 6), - new Hotel(5.0f, 1.0f, true, 3), - }; + new Hotel(5.2f, 4.f, true, 5), + new Hotel(5.2f, 3.9f, false, 4), + new Hotel(4.9f, 3.4f, true, 9), + new Hotel(4.2f, 4.6f, false, 6), + new Hotel(3.3f, 4.5f, true, 8), + new Hotel(6.4f, 3.4f, true, 9), + new Hotel(4.2f, 6.2f, true, 5), + new Hotel(2.4f, 4.0f, true, 8), + new Hotel(1.4f, 3.2f, false, 5), + new Hotel(7.0f, 9.9f, true, 9), + new Hotel(3.0f, 2.3f, false, 6), + new Hotel(5.0f, 1.0f, true, 3), }; var bulkRequest = new BulkRequest.Builder(); for (var i = 0; i < hotels.length; ++i) { final var id = Integer.toString(i + 1); final var hotel = hotels[i]; LOGGER.info("Indexing hotel {} with id {}", hotel, id); - bulkRequest.operations(b -> b - .index(o -> o - .index(indexName) - .id(id) - .document(hotel))); + bulkRequest.operations(b -> b.index(o -> o.index(indexName).id(id).document(hotel))); } LOGGER.info("Indexing {} documents", hotels.length); @@ -85,34 +83,43 @@ public static void main(String[] args) { LOGGER.info("Waiting for indexing to finish"); client.indices().refresh(i -> i.index(indexName)); - final var searchLocation = new float[]{ 5.0f, 4.0f }; + final var searchLocation = new float[] { 5.0f, 4.0f }; final var searchRatingMin = 8; final var searchRatingMax = 10; final var searchParking = true; LOGGER.info( - "Searching for hotel near {} with rating >={},<={} and parking={}", - searchLocation, searchRatingMin, searchRatingMax, searchParking); - - var searchResponse = client.search(s -> s - .index(indexName) + "Searching for hotel near {} with rating >={},<={} and parking={}", + searchLocation, + searchRatingMin, + searchRatingMax, + searchParking + ); + + var searchResponse = client.search( + s -> s.index(indexName) .size(3) - .query(q -> q - .knn(k -> k - .field("location") - .vector(searchLocation) - .k(3) - .filter(Query.of(f -> f - .bool(b -> b - .must(m -> m - .range(r -> r - .field("rating") - .gte(JsonData.of(searchRatingMin)) - .lte(JsonData.of(searchRatingMax)))) - .must(m -> m - .term(t -> t - .field("parking") - .value(FieldValue.of(searchParking))))))))), - Hotel.class); + .query( + q -> q.knn( + k -> k.field("location") + .vector(searchLocation) + .k(3) + .filter( + Query.of( + f -> f.bool( + b -> b.must( + m -> m.range( + r -> r.field("rating") + .gte(JsonData.of(searchRatingMin)) + .lte(JsonData.of(searchRatingMax)) + ) + ).must(m -> m.term(t -> t.field("parking").value(FieldValue.of(searchParking)))) + ) + ) + ) + ) + ), + Hotel.class + ); for (var hit : searchResponse.hits().hits()) { LOGGER.info("Found {} with score {}", hit.source(), hit.score()); @@ -133,7 +140,7 @@ public static class Hotel { public Hotel() {} public Hotel(float locX, float locY, boolean parking, int rating) { - this.location = new float[] {locX, locY}; + this.location = new float[] { locX, locY }; this.parking = parking; this.rating = rating; } @@ -164,11 +171,7 @@ public void setRating(int rating) { @Override public String toString() { - return "{" + - "location=" + Arrays.toString(location) + - ", parking=" + parking + - ", rating=" + rating + - '}'; + return "{" + "location=" + Arrays.toString(location) + ", parking=" + parking + ", rating=" + rating + '}'; } } } diff --git a/samples/src/main/java/org/opensearch/client/samples/knn/KnnPainlessScript.java b/samples/src/main/java/org/opensearch/client/samples/knn/KnnPainlessScript.java index 3ff803138a..2a60355c7b 100644 --- a/samples/src/main/java/org/opensearch/client/samples/knn/KnnPainlessScript.java +++ b/samples/src/main/java/org/opensearch/client/samples/knn/KnnPainlessScript.java @@ -34,12 +34,12 @@ public static void main(String[] args) { if (!client.indices().exists(r -> r.index(indexName)).value()) { LOGGER.info("Creating index {}", indexName); - client.indices().create(r -> r - .index(indexName) - .settings(s -> s.knn(true)) - .mappings(m -> m - .properties("values", p -> p - .knnVector(k -> k.dimension(dimensions))))); + client.indices() + .create( + r -> r.index(indexName) + .settings(s -> s.knn(true)) + .mappings(m -> m.properties("values", p -> p.knnVector(k -> k.dimension(dimensions)))) + ); } final var nVectors = 10; @@ -47,11 +47,7 @@ public static void main(String[] args) { for (var i = 0; i < nVectors; ++i) { var id = Integer.toString(i); var doc = Doc.rand(dimensions); - bulkRequest.operations(b -> b - .index(o -> o - .index(indexName) - .id(id) - .document(doc))); + bulkRequest.operations(b -> b.index(o -> o.index(indexName).id(id).document(doc))); } LOGGER.info("Indexing {} vectors", nVectors); @@ -63,17 +59,22 @@ public static void main(String[] args) { final var searchVector = RandUtil.rand2SfArray(dimensions); LOGGER.info("Searching for vector {}", searchVector); - var searchResponse = client.search(s -> s - .index(indexName) - .query(q -> q - .scriptScore(ss -> ss - .query(qq -> qq.matchAll(m -> m)) - .script(sss -> sss - .inline(i -> i - .source("1.0 + cosineSimilarity(params.query_value, doc[params.field])") - .params("field", JsonData.of("values")) - .params("query_value", JsonData.of(searchVector)))))), - Doc.class); + var searchResponse = client.search( + s -> s.index(indexName) + .query( + q -> q.scriptScore( + ss -> ss.query(qq -> qq.matchAll(m -> m)) + .script( + sss -> sss.inline( + i -> i.source("1.0 + cosineSimilarity(params.query_value, doc[params.field])") + .params("field", JsonData.of("values")) + .params("query_value", JsonData.of(searchVector)) + ) + ) + ) + ), + Doc.class + ); for (var hit : searchResponse.hits().hits()) { LOGGER.info("Found {} with score {}", hit.source(), hit.score()); @@ -109,9 +110,7 @@ public void setValues(float[] values) { @Override public String toString() { - return "{" + - "values=" + Arrays.toString(values) + - '}'; + return "{" + "values=" + Arrays.toString(values) + '}'; } } } diff --git a/samples/src/main/java/org/opensearch/client/samples/knn/KnnScriptScore.java b/samples/src/main/java/org/opensearch/client/samples/knn/KnnScriptScore.java index 0ee46e4fc0..eb0ccec60f 100644 --- a/samples/src/main/java/org/opensearch/client/samples/knn/KnnScriptScore.java +++ b/samples/src/main/java/org/opensearch/client/samples/knn/KnnScriptScore.java @@ -34,12 +34,12 @@ public static void main(String[] args) { if (!client.indices().exists(r -> r.index(indexName)).value()) { LOGGER.info("Creating index {}", indexName); - client.indices().create(r -> r - .index(indexName) - .settings(s -> s.knn(true)) - .mappings(m -> m - .properties("values", p -> p - .knnVector(k -> k.dimension(dimensions))))); + client.indices() + .create( + r -> r.index(indexName) + .settings(s -> s.knn(true)) + .mappings(m -> m.properties("values", p -> p.knnVector(k -> k.dimension(dimensions)))) + ); } final var nVectors = 10; @@ -47,11 +47,7 @@ public static void main(String[] args) { for (var i = 0; i < nVectors; ++i) { var id = Integer.toString(i); var doc = Doc.rand(dimensions); - bulkRequest.operations(b -> b - .index(o -> o - .index(indexName) - .id(id) - .document(doc))); + bulkRequest.operations(b -> b.index(o -> o.index(indexName).id(id).document(doc))); } LOGGER.info("Indexing {} vectors", nVectors); @@ -63,19 +59,24 @@ public static void main(String[] args) { final var searchVector = RandUtil.rand2SfArray(dimensions); LOGGER.info("Searching for vector {}", searchVector); - var searchResponse = client.search(s -> s - .index(indexName) - .query(q -> q - .scriptScore(ss -> ss - .query(qq -> qq.matchAll(m -> m)) - .script(sss -> sss - .inline(i -> i - .source("knn_score") - .lang("knn") - .params("field", JsonData.of("values")) - .params("query_value", JsonData.of(searchVector)) - .params("space_type", JsonData.of("cosinesimil")))))), - Doc.class); + var searchResponse = client.search( + s -> s.index(indexName) + .query( + q -> q.scriptScore( + ss -> ss.query(qq -> qq.matchAll(m -> m)) + .script( + sss -> sss.inline( + i -> i.source("knn_score") + .lang("knn") + .params("field", JsonData.of("values")) + .params("query_value", JsonData.of(searchVector)) + .params("space_type", JsonData.of("cosinesimil")) + ) + ) + ) + ), + Doc.class + ); for (var hit : searchResponse.hits().hits()) { LOGGER.info("Found {} with score {}", hit.source(), hit.score()); @@ -111,9 +112,7 @@ public void setValues(float[] values) { @Override public String toString() { - return "{" + - "values=" + Arrays.toString(values) + - '}'; + return "{" + "values=" + Arrays.toString(values) + '}'; } } } diff --git a/samples/src/main/java/org/opensearch/client/samples/util/IndexData.java b/samples/src/main/java/org/opensearch/client/samples/util/IndexData.java index 759f03be8f..211e483f23 100644 --- a/samples/src/main/java/org/opensearch/client/samples/util/IndexData.java +++ b/samples/src/main/java/org/opensearch/client/samples/util/IndexData.java @@ -12,8 +12,7 @@ public class IndexData { private String title; private String text; - public IndexData() { - } + public IndexData() {} public IndexData(String title, String text) { this.title = title; diff --git a/samples/src/main/java/org/opensearch/client/samples/util/RandUtil.java b/samples/src/main/java/org/opensearch/client/samples/util/RandUtil.java index fbc2249621..b27731da84 100644 --- a/samples/src/main/java/org/opensearch/client/samples/util/RandUtil.java +++ b/samples/src/main/java/org/opensearch/client/samples/util/RandUtil.java @@ -18,6 +18,6 @@ public static float[] rand2SfArray(int n) { } public static T choice(T[] arr) { - return arr[(int)Math.floor(Math.random() * arr.length)]; + return arr[(int) Math.floor(Math.random() * arr.length)]; } }