forked from opensearch-project/opensearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Enable Generic HTTP Actions in Java Client
Signed-off-by: Andriy Redko <[email protected]> Signed-off-by: Andriy Redko <[email protected]> Signed-off-by: Andriy Redko <[email protected]>
- Loading branch information
Showing
14 changed files
with
682 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
java-client/src/main/java/org/opensearch/client/opensearch/generic/GenericBodies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.generic; | ||
|
||
import jakarta.json.stream.JsonGenerator; | ||
import jakarta.json.stream.JsonParser; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import org.opensearch.client.ApiClient; | ||
import org.opensearch.client.json.JsonpMapper; | ||
|
||
public final class GenericBodies { | ||
private static final String APPLICATION_JSON = "application/json; charset=UTF-8"; | ||
|
||
private GenericBodies() {} | ||
|
||
public static <C> C json(GenericBody body, Class<C> clazz, ApiClient<?, ?> client) { | ||
return json(body, clazz, client._transport().jsonpMapper()); | ||
} | ||
|
||
public static <C> GenericBody json(C value, ApiClient<?, ?> client) throws IOException { | ||
return json(value, client._transport().jsonpMapper()); | ||
} | ||
|
||
public static <C> C json(GenericBody body, Class<C> clazz, JsonpMapper jsonpMapper) { | ||
try (JsonParser parser = jsonpMapper.jsonProvider().createParser(body.body())) { | ||
return jsonpMapper.deserialize(parser, clazz); | ||
} | ||
} | ||
|
||
public static <C> GenericBody json(C value, JsonpMapper jsonpMapper) throws IOException { | ||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | ||
try (JsonGenerator generator = jsonpMapper.jsonProvider().createGenerator(baos)) { | ||
jsonpMapper.serialize(value, generator); | ||
return GenericBody.from(baos.toByteArray(), APPLICATION_JSON); | ||
} | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
java-client/src/main/java/org/opensearch/client/opensearch/generic/GenericBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.generic; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Generic HTTP request / response body. It is responsibility of the caller to close the body instance | ||
* explicitly (or through {@link GenericResponse} instance) to release all associated streams. | ||
*/ | ||
public interface GenericBody extends AutoCloseable { | ||
/** | ||
* Constructs the generic response body out of {@link InputStream} with assumed content type | ||
* @param body response body stream | ||
* @param contentType content type | ||
* @return generic response body instance | ||
*/ | ||
static @Nullable GenericBody from(@Nullable final InputStream body, @Nullable final String contentType) { | ||
if (body == null) { | ||
return null; | ||
} else { | ||
return new GenericInputStreamBody(body, contentType); | ||
} | ||
} | ||
|
||
/** | ||
* Constructs the generic response body out of {@link InputStream} with assumed content type | ||
* @param body response body stream | ||
* @param contentType content type | ||
* @return generic response body instance | ||
*/ | ||
static @Nullable GenericBody from(@Nullable final byte[] body, @Nullable final String contentType) { | ||
if (body == null) { | ||
return null; | ||
} else { | ||
return new GenericByteArrayBody(body, contentType); | ||
} | ||
} | ||
|
||
/** | ||
* Content type of this body | ||
* @return content type | ||
*/ | ||
String contentType(); | ||
|
||
/** | ||
* Gets the body as {@link InputStream} | ||
* @return | ||
*/ | ||
InputStream body(); | ||
|
||
/** | ||
* Releases all resources associated with this body stream. | ||
*/ | ||
void close() throws IOException; | ||
} |
42 changes: 42 additions & 0 deletions
42
java-client/src/main/java/org/opensearch/client/opensearch/generic/GenericByteArrayBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.generic; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* The HTTP request / response body that uses {@link byte[]} | ||
*/ | ||
class GenericByteArrayBody implements GenericBody { | ||
private final InputStream in; | ||
private final String contentType; | ||
|
||
GenericByteArrayBody(final byte[] bytes, @Nullable final String contentType) { | ||
this.in = new ByteArrayInputStream(bytes); | ||
this.contentType = contentType; | ||
} | ||
|
||
@Override | ||
public String contentType() { | ||
return contentType; | ||
} | ||
|
||
@Override | ||
public InputStream body() { | ||
return in; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
in.close(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
java-client/src/main/java/org/opensearch/client/opensearch/generic/GenericEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.generic; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
import org.opensearch.client.transport.RawEndpoint; | ||
|
||
/** | ||
* Generic endpoint instance | ||
*/ | ||
class GenericEndpoint implements RawEndpoint<GenericRequest, GenericResponse> { | ||
private final GenericRequest request; | ||
|
||
public GenericEndpoint(GenericRequest request) { | ||
this.request = request; | ||
} | ||
|
||
@Override | ||
public String method(GenericRequest request) { | ||
return request.getMethod(); | ||
} | ||
|
||
@Override | ||
public String requestUrl(GenericRequest request) { | ||
return request.getEndpoint(); | ||
} | ||
|
||
@Override | ||
public boolean hasRequestBody() { | ||
return request.getBody().isPresent(); | ||
} | ||
|
||
@Override | ||
public GenericResponse responseDeserializer( | ||
String uri, | ||
String method, | ||
String protocol, | ||
int status, | ||
String reason, | ||
List<Entry<String, String>> headers, | ||
String contentType, | ||
InputStream body | ||
) { | ||
return new GenericResponse(uri, protocol, method, status, reason, headers, GenericBody.from(body, contentType)); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...client/src/main/java/org/opensearch/client/opensearch/generic/GenericInputStreamBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.generic; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* The HTTP request / response body that uses {@link InputStream} | ||
*/ | ||
class GenericInputStreamBody implements GenericBody { | ||
private final InputStream in; | ||
private final String contentType; | ||
|
||
GenericInputStreamBody(final InputStream in, @Nullable final String contentType) { | ||
this.in = in; | ||
this.contentType = contentType; | ||
} | ||
|
||
@Override | ||
public String contentType() { | ||
return contentType; | ||
} | ||
|
||
@Override | ||
public InputStream body() { | ||
return in; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
in.close(); | ||
} | ||
} |
Oops, something went wrong.