Search in sources :

Example 1 with RestRequest

use of org.opensearch.rest.RestRequest in project OpenSearch by opensearch-project.

the class RestSearchTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    // Creates the search request with all required params
    SearchRequest searchRequest = new SearchRequest();
    RestSearchAction.parseSearchRequest(searchRequest, request, null, client.getNamedWriteableRegistry(), size -> searchRequest.source().size(size));
    // Creates the search template request
    SearchTemplateRequest searchTemplateRequest;
    try (XContentParser parser = request.contentOrSourceParamParser()) {
        searchTemplateRequest = SearchTemplateRequest.fromXContent(parser);
    }
    searchTemplateRequest.setRequest(searchRequest);
    return channel -> client.execute(SearchTemplateAction.INSTANCE, searchTemplateRequest, new RestStatusToXContentListener<>(channel));
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) Arrays(java.util.Arrays) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) IOException(java.io.IOException) XContentParser(org.opensearch.common.xcontent.XContentParser) RestSearchAction(org.opensearch.rest.action.search.RestSearchAction) HashSet(java.util.HashSet) List(java.util.List) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) SearchRequest(org.opensearch.action.search.SearchRequest) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Collections(java.util.Collections) SearchRequest(org.opensearch.action.search.SearchRequest) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with RestRequest

use of org.opensearch.rest.RestRequest in project OpenSearch by opensearch-project.

the class RestRenderSearchTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    // Creates the render template request
    SearchTemplateRequest renderRequest;
    try (XContentParser parser = request.contentOrSourceParamParser()) {
        renderRequest = SearchTemplateRequest.fromXContent(parser);
    }
    renderRequest.setSimulate(true);
    String id = request.param("id");
    if (id != null) {
        renderRequest.setScriptType(ScriptType.STORED);
        renderRequest.setScript(id);
    }
    return channel -> client.execute(SearchTemplateAction.INSTANCE, renderRequest, new RestToXContentListener<>(channel));
}
Also used : XContentParser(org.opensearch.common.xcontent.XContentParser) ScriptType(org.opensearch.script.ScriptType) POST(org.opensearch.rest.RestRequest.Method.POST) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 3 with RestRequest

use of org.opensearch.rest.RestRequest in project OpenSearch by opensearch-project.

the class MultiSearchTemplateRequestTests method testParseWithCarriageReturn.

public void testParseWithCarriageReturn() throws Exception {
    final String content = "{\"index\":[\"test0\", \"test1\"], \"request_cache\": true}\r\n" + "{\"source\": {\"query\" : {\"match_{{template}}\" :{}}}, \"params\": {\"template\": \"all\" } }\r\n";
    RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(content), XContentType.JSON).build();
    MultiSearchTemplateRequest request = RestMultiSearchTemplateAction.parseRequest(restRequest, true);
    assertThat(request.requests().size(), equalTo(1));
    assertThat(request.requests().get(0).getRequest().indices()[0], equalTo("test0"));
    assertThat(request.requests().get(0).getRequest().indices()[1], equalTo("test1"));
    assertThat(request.requests().get(0).getRequest().indices(), arrayContaining("test0", "test1"));
    assertThat(request.requests().get(0).getRequest().requestCache(), equalTo(true));
    assertThat(request.requests().get(0).getRequest().preference(), nullValue());
    assertNotNull(request.requests().get(0).getScript());
    assertEquals(ScriptType.INLINE, request.requests().get(0).getScriptType());
    assertEquals("{\"query\":{\"match_{{template}}\":{}}}", request.requests().get(0).getScript());
    assertEquals(1, request.requests().get(0).getScriptParams().size());
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 4 with RestRequest

use of org.opensearch.rest.RestRequest in project OpenSearch by opensearch-project.

the class MultiSearchTemplateRequestTests method testParseRequest.

public void testParseRequest() throws Exception {
    byte[] data = StreamsUtils.copyToBytesFromClasspath("/org/opensearch/script/mustache/simple-msearch-template.json");
    RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(data), XContentType.JSON).build();
    MultiSearchTemplateRequest request = RestMultiSearchTemplateAction.parseRequest(restRequest, true);
    assertThat(request.requests().size(), equalTo(3));
    assertThat(request.requests().get(0).getRequest().indices()[0], equalTo("test0"));
    assertThat(request.requests().get(0).getRequest().indices()[1], equalTo("test1"));
    assertThat(request.requests().get(0).getRequest().indices(), arrayContaining("test0", "test1"));
    assertThat(request.requests().get(0).getRequest().requestCache(), equalTo(true));
    assertThat(request.requests().get(0).getRequest().preference(), nullValue());
    assertThat(request.requests().get(1).getRequest().indices()[0], equalTo("test2"));
    assertThat(request.requests().get(1).getRequest().indices()[1], equalTo("test3"));
    assertThat(request.requests().get(1).getRequest().requestCache(), nullValue());
    assertThat(request.requests().get(1).getRequest().preference(), equalTo("_local"));
    assertThat(request.requests().get(2).getRequest().indices()[0], equalTo("test4"));
    assertThat(request.requests().get(2).getRequest().indices()[1], equalTo("test1"));
    assertThat(request.requests().get(2).getRequest().routing(), equalTo("123"));
    assertNotNull(request.requests().get(0).getScript());
    assertNotNull(request.requests().get(1).getScript());
    assertNotNull(request.requests().get(2).getScript());
    assertEquals(ScriptType.INLINE, request.requests().get(0).getScriptType());
    assertEquals(ScriptType.INLINE, request.requests().get(1).getScriptType());
    assertEquals(ScriptType.INLINE, request.requests().get(2).getScriptType());
    assertEquals("{\"query\":{\"match_{{template}}\":{}}}", request.requests().get(0).getScript());
    assertEquals("{\"query\":{\"match_{{template}}\":{}}}", request.requests().get(1).getScript());
    assertEquals("{\"query\":{\"match_{{template}}\":{}}}", request.requests().get(2).getScript());
    assertEquals(1, request.requests().get(0).getScriptParams().size());
    assertEquals(1, request.requests().get(1).getScriptParams().size());
    assertEquals(1, request.requests().get(2).getScriptParams().size());
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 5 with RestRequest

use of org.opensearch.rest.RestRequest in project OpenSearch by opensearch-project.

the class Netty4HttpServerTransportTests method testBadRequest.

public void testBadRequest() throws InterruptedException {
    final AtomicReference<Throwable> causeReference = new AtomicReference<>();
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
            logger.error("--> Unexpected successful request [{}]", FakeRestRequest.requestToString(request));
            throw new AssertionError();
        }

        @Override
        public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
            causeReference.set(cause);
            try {
                final OpenSearchException e = new OpenSearchException("you sent a bad request and you should feel bad");
                channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
            } catch (final IOException e) {
                throw new AssertionError(e);
            }
        }
    };
    final Settings settings;
    final int maxInitialLineLength;
    final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
    if (randomBoolean()) {
        maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
        settings = createSettings();
    } else {
        maxInitialLineLength = randomIntBetween(1, 8192);
        settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
    }
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, clusterSettings, new SharedGroupFactory(settings))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (Netty4HttpClient client = new Netty4HttpClient()) {
            final String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
                assertThat(new String(response.content().array(), Charset.forName("UTF-8")), containsString("you sent a bad request and you should feel bad"));
            } finally {
                response.release();
            }
        }
    }
    assertNotNull(causeReference.get());
    assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) TransportAddress(org.opensearch.common.transport.TransportAddress) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) BytesRestResponse(org.opensearch.rest.BytesRestResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.opensearch.common.settings.Settings) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) RestChannel(org.opensearch.rest.RestChannel) SharedGroupFactory(org.opensearch.transport.SharedGroupFactory) IOException(java.io.IOException) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) OpenSearchException(org.opensearch.OpenSearchException)

Aggregations

RestRequest (org.opensearch.rest.RestRequest)183 NodeClient (org.opensearch.client.node.NodeClient)123 List (java.util.List)115 IOException (java.io.IOException)102 BaseRestHandler (org.opensearch.rest.BaseRestHandler)96 Arrays.asList (java.util.Arrays.asList)77 Collections.unmodifiableList (java.util.Collections.unmodifiableList)77 GET (org.opensearch.rest.RestRequest.Method.GET)65 Strings (org.opensearch.common.Strings)63 RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)61 FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)54 POST (org.opensearch.rest.RestRequest.Method.POST)45 RestResponse (org.opensearch.rest.RestResponse)38 IndicesOptions (org.opensearch.action.support.IndicesOptions)33 BytesRestResponse (org.opensearch.rest.BytesRestResponse)33 HashMap (java.util.HashMap)29 Collections.singletonList (java.util.Collections.singletonList)28 Settings (org.opensearch.common.settings.Settings)24 RestChannel (org.opensearch.rest.RestChannel)23 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)22