Skip to content

Commit

Permalink
Restore support for Java 8
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
  • Loading branch information
reta committed Dec 12, 2023
1 parent d4aade4 commit cde940f
Show file tree
Hide file tree
Showing 62 changed files with 175 additions and 101 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-integration-unreleased.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- { opensearch_ref: '1.x', java: 11 }
- { opensearch_ref: '2.x', java: 11 }
- { opensearch_ref: '2.x', java: 17 }
- { opensearch_ref: '2.x', java: 21 }
- { opensearch_ref: 'main', java: 11 }
- { opensearch_ref: 'main', java: 17 }
- { opensearch_ref: 'main', java: 21 }
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Dependencies

### Changed
- Restore support for Java 8 ([#767](https://github.com/opensearch-project/opensearch-java/pull/767))

### Deprecated
- Deprecated "_toQuery()" in Query and QueryVariant ([#760](https://github.com/opensearch-project/opensearch-java/pull/760)
Expand Down
48 changes: 44 additions & 4 deletions java-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ configurations {
}

java {
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8

withJavadocJar()
withSourcesJar()
Expand Down Expand Up @@ -149,15 +149,20 @@ val integrationTest = task<Test>("integrationTest") {
System.getProperty("tests.awsSdk2support.domainRegion", "us-east-1"))
}

val opensearchVersion = "3.0.0-SNAPSHOT"

dependencies {

val opensearchVersion = "3.0.0-SNAPSHOT"
val jacksonVersion = "2.15.2"
val jacksonDatabindVersion = "2.15.2"

// Apache 2.0
compileOnly("org.opensearch.client", "opensearch-rest-client", opensearchVersion)
testImplementation("org.opensearch.test", "framework", opensearchVersion)
testImplementation("org.hamcrest:hamcrest:2.1")
testImplementation("com.carrotsearch.randomizedtesting:randomizedtesting-runner:2.7.1") {
exclude(group = "junit")
}
testImplementation("org.opensearch.client", "opensearch-rest-client", opensearchVersion)

api("org.apache.httpcomponents.client5:httpclient5:5.2.3") {
exclude(group = "org.apache.httpcomponents.core5")
Expand Down Expand Up @@ -331,3 +336,38 @@ publishing {
}
}
}

if (JavaVersion.current() >= JavaVersion.VERSION_11) {
val java11: SourceSet = sourceSets.create("java11") {
java {
compileClasspath += sourceSets.main.get().output + sourceSets.test.get().output
runtimeClasspath += sourceSets.main.get().output + sourceSets.test.get().output
srcDir("src/test/java11")
}
}

configurations[java11.implementationConfigurationName].extendsFrom(configurations.testImplementation.get())
configurations[java11.runtimeOnlyConfigurationName].extendsFrom(configurations.testRuntimeOnly.get())

dependencies {
testImplementation("org.opensearch.test", "framework", opensearchVersion) {
exclude(group = "org.hamcrest")
}
}

tasks.named<JavaCompile>("compileJava11Java") {
targetCompatibility = JavaVersion.VERSION_11.toString()
sourceCompatibility = JavaVersion.VERSION_11.toString()
}

tasks.named<JavaCompile>("compileTestJava") {
targetCompatibility = JavaVersion.VERSION_11.toString()
sourceCompatibility = JavaVersion.VERSION_11.toString()
}

tasks.test {
testClassesDirs += java11.output.classesDirs
classpath = sourceSets["java11"].runtimeClasspath
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public CatRequestBase(CatRequestBaseBuilder<?> builder) {

protected final Map<String, String> queryParameters() {
Map<String, String> params = new HashMap<>();
if (headers != null && !headers.isBlank()) {
if (headers != null && !headers.trim().isEmpty()) {
params.put("h", headers);
}
if (sort != null && !sort.isBlank()) {
if (sort != null && !sort.trim().isEmpty()) {
params.put("s", sort);
}
params.put("format", "json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.client.transport;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -98,7 +99,7 @@ public Builder addHeader(String name, String value) {
if (headers.isEmpty()) {
headers = new ArrayList<>();
}
headers.add(Map.entry(name, value));
headers.add(new AbstractMap.SimpleEntry<>(name, value));
return this;
}

Expand Down Expand Up @@ -135,8 +136,8 @@ class DefaultImpl implements TransportOptions {
private final Function<List<String>, Boolean> onWarnings;

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.headers = builder.headers.isEmpty() ? Collections.emptyList() : new ArrayList<>(builder.headers);
this.params = builder.queryParameters.isEmpty() ? Collections.emptyMap() : new HashMap<>(builder.queryParameters);
this.onWarnings = builder.onWarnings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
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.function.Supplier;
import java.util.zip.GZIPInputStream;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -261,11 +262,12 @@ private <RequestT> OpenSearchRequestBodyBuffer prepareRequestBody(
.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);
final int maxUncompressedSize = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::requestCompressionSize),
() -> Optional.ofNullable(transportOptions.requestCompressionSize())
).orElse(DEFAULT_REQUEST_COMPRESSION_SIZE);

OpenSearchRequestBodyBuffer buffer = new OpenSearchRequestBodyBuffer(mapper, maxUncompressedSize);
buffer.addContent(request);
Expand All @@ -280,7 +282,7 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
Endpoint<RequestT, ?, ?> endpoint,
@CheckForNull TransportOptions options,
@CheckForNull OpenSearchRequestBodyBuffer body
) {
) throws UnsupportedEncodingException {
SdkHttpFullRequest.Builder req = SdkHttpFullRequest.builder().method(SdkHttpMethod.fromValue(endpoint.method(request)));

StringBuilder url = new StringBuilder();
Expand All @@ -293,9 +295,9 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
Map<String, String> params = endpoint.queryParameters(request);
if (params != null && !params.isEmpty()) {
char sep = '?';
for (var ent : params.entrySet()) {
for (Map.Entry<String, String> ent : params.entrySet()) {
url.append(sep).append(ent.getKey()).append('=');
url.append(URLEncoder.encode(ent.getValue(), StandardCharsets.UTF_8));
url.append(URLEncoder.encode(ent.getValue(), "UTF-8"));
sep = '&';
}
}
Expand All @@ -321,22 +323,24 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
req.putHeader("x-amz-content-sha256", "required");
}

boolean responseCompression = Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::responseCompression)
.or(() -> Optional.ofNullable(transportOptions.responseCompression()))
.orElse(Boolean.TRUE);
boolean responseCompression = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::responseCompression),
() -> Optional.ofNullable(transportOptions.responseCompression())
).orElse(Boolean.TRUE);
if (responseCompression) {
req.putHeader("Accept-Encoding", "gzip");
} else {
req.removeHeader("Accept-Encoding");
}

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());
final AwsCredentialsProvider credentials = or(
Optional.ofNullable(options)
.map(o -> o instanceof AwsSdk2TransportOptions ? ((AwsSdk2TransportOptions) o) : null)
.map(AwsSdk2TransportOptions::credentials),
() -> Optional.ofNullable(transportOptions.credentials())
).orElse(DefaultCredentialsProvider.create());

Aws4SignerParams signerParams = Aws4SignerParams.builder()
.awsCredentials(credentials.resolveCredentials())
Expand All @@ -346,7 +350,7 @@ private <RequestT> SdkHttpFullRequest prepareRequest(
return Aws4Signer.create().sign(req.build(), signerParams);
}

private void applyOptionsParams(StringBuilder url, TransportOptions options) {
private void applyOptionsParams(StringBuilder url, TransportOptions options) throws UnsupportedEncodingException {
if (options == null) {
return;
}
Expand All @@ -355,7 +359,7 @@ private void applyOptionsParams(StringBuilder url, TransportOptions options) {
char sep = url.indexOf("?") < 0 ? '?' : '&';
for (Map.Entry<String, String> param : params.entrySet()) {
url.append(sep).append(param.getKey()).append('=');
url.append(URLEncoder.encode(param.getValue(), StandardCharsets.UTF_8));
url.append(URLEncoder.encode(param.getValue(), "UTF-8"));
sep = '?';
}
}
Expand Down Expand Up @@ -524,4 +528,16 @@ private <ResponseT, ErrorT> ResponseT parseResponse(
}
}
}

private static <T> Optional<T> or(Optional<T> opt, Supplier<? extends Optional<? extends T>> supplier) {
Objects.requireNonNull(opt);
Objects.requireNonNull(supplier);
if (opt.isPresent()) {
return opt;
} else {
@SuppressWarnings("unchecked")
Optional<T> r = (Optional<T>) supplier.get();
return Objects.requireNonNull(r);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -932,9 +932,15 @@ public long getContentLength() {
if (chunkedEnabled.get()) {
return -1L;
} else {
long size;
long size = 0;
final byte[] buf = new byte[8192];
int nread = 0;

try (InputStream is = getContent()) {
size = is.readAllBytes().length;
// read to EOF which may read more or less than buffer size
while ((nread = is.read(buf)) > 0) {
size += nread;
}
} catch (IOException ex) {
size = -1L;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* The {@link RestClientTransport} is deprecated and is scheduled for removal in later versions. Please
* use {@link org.opensearch.client.transport.httpclient5.ApacheHttpClient5Transport} instead.
*/
@Deprecated(since = "3.0.0", forRemoval = true)
@Deprecated
public class RestClientOptions implements TransportOptions {

private final RequestOptions options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
* The {@link RestClientTransport} is deprecated and is scheduled for removal in later versions. Please
* use {@link org.opensearch.client.transport.httpclient5.ApacheHttpClient5Transport} instead.
*/
@Deprecated(since = "3.0.0", forRemoval = true)
@Deprecated
public class RestClientTransport implements OpenSearchTransport {

static final ContentType JsonContentType = ContentType.APPLICATION_JSON;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.util.Map;
import java.util.Collections;
import junit.framework.TestCase;
import org.junit.Test;
import org.opensearch.client.json.JsonData;
Expand All @@ -30,7 +30,7 @@ public void testCreateKnnVectorMethodWithAll() {
KnnVectorMethod knnVectorMethod = new KnnVectorMethod.Builder().name("hnsw")
.spaceType("l2")
.engine("nmslib")
.parameters(Map.of("ef_construction", JsonData.of(128)))
.parameters(Collections.singletonMap("ef_construction", JsonData.of(128)))
.build();
assertEquals("hnsw", knnVectorMethod.name());
assertEquals("l2", knnVectorMethod.spaceType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -12,7 +13,9 @@
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import org.opensearch.client.opensearch._types.mapping.IcuCollationKeywordProperty;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch._types.mapping.TypeMapping;
import org.opensearch.client.opensearch.indices.GetTemplateResponse;
import org.opensearch.client.opensearch.indices.TemplateMapping;

public class GetMappingsResponseTest extends Assert {

Expand All @@ -36,32 +39,27 @@ public void deserialize_IcuCollationKeywordExists_propertyDeserializes() throws
icuCollationConfig.put("strength", "quaternary");
icuCollationConfig.put("variable_top", "$");

mappingTemplate.put(
"test-index",
Map.of(
"aliases",
Collections.emptyMap(),
"index_patterns",
Collections.singletonList("test-pattern*"),
"mappings",
Map.of("properties", Map.of("icu_test_field", icuCollationConfig)),
"order",
0,
"settings",
Collections.emptyMap(),
"version",
1
)
final Map<String, Object> mappingTemplateMap = new HashMap<>();
mappingTemplateMap.put("aliases", Collections.emptyMap());
mappingTemplateMap.put("index_patterns", Collections.singletonList("test-pattern*"));
mappingTemplateMap.put(
"mappings",
Collections.singletonMap("properties", Collections.singletonMap("icu_test_field", icuCollationConfig))
);
mappingTemplateMap.put("order", 0);
mappingTemplateMap.put("settings", Collections.emptyMap());
mappingTemplateMap.put("version", 1);
mappingTemplate.put("test-index", mappingTemplateMap);

final JsonpMapper mapper = new JsonbJsonpMapper();
final String indexTemplate = new ObjectMapper().writeValueAsString(mappingTemplate);
final var parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));
final JsonParser parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));

final GetTemplateResponse response = GetTemplateResponse._DESERIALIZER.deserialize(parser, mapper);
final var template = response.get("test-index");
final var mappings = template.mappings();
final var properties = mappings.properties();
final var property = properties.get("icu_test_field");
final TemplateMapping template = response.get("test-index");
final TypeMapping mappings = template.mappings();
final Map<String, Property> properties = mappings.properties();
final Property property = properties.get("icu_test_field");
final IcuCollationKeywordProperty icu = property.icuCollationKeyword();

assertEquals(property._kind(), Property.Kind.IcuCollationKeyword);
Expand Down
Loading

0 comments on commit cde940f

Please sign in to comment.