Skip to content

Commit 833bba0

Browse files
authored
Add ser/deser to get AD transport action request (#1150) (#1151)
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
1 parent b26c6c8 commit 833bba0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/main/java/org/opensearch/ad/transport/GetAnomalyDetectorRequest.java

+19
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
package org.opensearch.ad.transport;
1313

14+
import java.io.ByteArrayInputStream;
15+
import java.io.ByteArrayOutputStream;
1416
import java.io.IOException;
1517

1618
import org.opensearch.action.ActionRequest;
1719
import org.opensearch.action.ActionRequestValidationException;
1820
import org.opensearch.ad.model.Entity;
21+
import org.opensearch.core.common.io.stream.InputStreamStreamInput;
22+
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput;
1923
import org.opensearch.core.common.io.stream.StreamInput;
2024
import org.opensearch.core.common.io.stream.StreamOutput;
2125

@@ -119,4 +123,19 @@ public void writeTo(StreamOutput out) throws IOException {
119123
public ActionRequestValidationException validate() {
120124
return null;
121125
}
126+
127+
public static GetAnomalyDetectorRequest fromActionRequest(final ActionRequest actionRequest) {
128+
if (actionRequest instanceof GetAnomalyDetectorRequest) {
129+
return (GetAnomalyDetectorRequest) actionRequest;
130+
}
131+
132+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
133+
actionRequest.writeTo(osso);
134+
try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
135+
return new GetAnomalyDetectorRequest(input);
136+
}
137+
} catch (IOException e) {
138+
throw new IllegalArgumentException("failed to parse ActionRequest into GetAnomalyDetectorRequest", e);
139+
}
140+
}
122141
}

src/main/java/org/opensearch/ad/transport/GetAnomalyDetectorTransportAction.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.logging.log4j.LogManager;
3838
import org.apache.logging.log4j.Logger;
3939
import org.opensearch.OpenSearchStatusException;
40+
import org.opensearch.action.ActionRequest;
4041
import org.opensearch.action.get.MultiGetItemResponse;
4142
import org.opensearch.action.get.MultiGetRequest;
4243
import org.opensearch.action.get.MultiGetResponse;
@@ -75,7 +76,7 @@
7576

7677
import com.google.common.collect.Sets;
7778

78-
public class GetAnomalyDetectorTransportAction extends HandledTransportAction<GetAnomalyDetectorRequest, GetAnomalyDetectorResponse> {
79+
public class GetAnomalyDetectorTransportAction extends HandledTransportAction<ActionRequest, GetAnomalyDetectorResponse> {
7980

8081
private static final Logger LOG = LogManager.getLogger(GetAnomalyDetectorTransportAction.class);
8182

@@ -131,7 +132,9 @@ public GetAnomalyDetectorTransportAction(
131132
}
132133

133134
@Override
134-
protected void doExecute(Task task, GetAnomalyDetectorRequest request, ActionListener<GetAnomalyDetectorResponse> actionListener) {
135+
protected void doExecute(Task task, ActionRequest actionRequest, ActionListener<GetAnomalyDetectorResponse> actionListener) {
136+
GetAnomalyDetectorRequest request = GetAnomalyDetectorRequest.fromActionRequest(actionRequest);
137+
135138
String detectorID = request.getDetectorID();
136139
User user = getUserContext(client);
137140
ActionListener<GetAnomalyDetectorResponse> listener = wrapRestActionListener(actionListener, FAIL_TO_GET_DETECTOR);

0 commit comments

Comments
 (0)