Search in sources :

Example 11 with BytesRestResponse

use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.

the class RestBuilderListenerTests method testXContentBuilderClosedInBuildResponse.

public void testXContentBuilderClosedInBuildResponse() throws Exception {
    AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
    RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {

        @Override
        public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
            builderAtomicReference.set(builder);
            builder.close();
            return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
        }
    };
    builderListener.buildResponse(Empty.INSTANCE);
    assertNotNull(builderAtomicReference.get());
    assertTrue(builderAtomicReference.get().generator().isClosed());
}
Also used : Empty(org.elasticsearch.transport.TransportResponse.Empty) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 12 with BytesRestResponse

use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.

the class RestBuilderListenerTests method testXContentBuilderNotClosedInBuildResponseAssertionsEnabled.

public void testXContentBuilderNotClosedInBuildResponseAssertionsEnabled() throws Exception {
    assumeTrue("tests are not being run with assertions", RestBuilderListener.class.desiredAssertionStatus());
    RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {

        @Override
        public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
            return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
        }
    };
    AssertionError error = expectThrows(AssertionError.class, () -> builderListener.buildResponse(Empty.INSTANCE));
    assertEquals("callers should ensure the XContentBuilder is closed themselves", error.getMessage());
}
Also used : Empty(org.elasticsearch.transport.TransportResponse.Empty) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 13 with BytesRestResponse

use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.

the class RestRefreshAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    RefreshRequest refreshRequest = new RefreshRequest(Strings.splitStringByCommaToArray(request.param("index")));
    refreshRequest.indicesOptions(IndicesOptions.fromRequest(request, refreshRequest.indicesOptions()));
    return channel -> client.admin().indices().refresh(refreshRequest, new RestBuilderListener<RefreshResponse>(channel) {

        @Override
        public RestResponse buildResponse(RefreshResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            buildBroadcastShardsHeader(builder, request, response);
            builder.endObject();
            return new BytesRestResponse(response.getStatus(), builder);
        }
    });
}
Also used : RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 14 with BytesRestResponse

use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.

the class RestSyncedFlushAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, IndicesOptions.lenientExpandOpen());
    SyncedFlushRequest syncedFlushRequest = new SyncedFlushRequest(Strings.splitStringByCommaToArray(request.param("index")));
    syncedFlushRequest.indicesOptions(indicesOptions);
    return channel -> client.admin().indices().syncedFlush(syncedFlushRequest, new RestBuilderListener<SyncedFlushResponse>(channel) {

        @Override
        public RestResponse buildResponse(SyncedFlushResponse results, XContentBuilder builder) throws Exception {
            builder.startObject();
            results.toXContent(builder, request);
            builder.endObject();
            return new BytesRestResponse(results.restStatus(), builder);
        }
    });
}
Also used : SyncedFlushRequest(org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) SyncedFlushResponse(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse) NodeClient(org.elasticsearch.client.node.NodeClient) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) SyncedFlushResponse(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) SyncedFlushRequest(org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 15 with BytesRestResponse

use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.

the class RestTypesExistsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    TypesExistsRequest typesExistsRequest = new TypesExistsRequest(Strings.splitStringByCommaToArray(request.param("index")), Strings.splitStringByCommaToArray(request.param("type")));
    typesExistsRequest.local(request.paramAsBoolean("local", typesExistsRequest.local()));
    typesExistsRequest.indicesOptions(IndicesOptions.fromRequest(request, typesExistsRequest.indicesOptions()));
    return channel -> client.admin().indices().typesExists(typesExistsRequest, new RestResponseListener<TypesExistsResponse>(channel) {

        @Override
        public RestResponse buildResponse(TypesExistsResponse response) throws Exception {
            if (response.isExists()) {
                return new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
            } else {
                return new BytesRestResponse(NOT_FOUND, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
            }
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) RestResponse(org.elasticsearch.rest.RestResponse) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesArray(org.elasticsearch.common.bytes.BytesArray) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) TypesExistsRequest(org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) TypesExistsResponse(org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) TypesExistsRequest(org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) TypesExistsResponse(org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse) IOException(java.io.IOException)

Aggregations

BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)47 RestRequest (org.elasticsearch.rest.RestRequest)38 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)35 IOException (java.io.IOException)33 Settings (org.elasticsearch.common.settings.Settings)33 NodeClient (org.elasticsearch.client.node.NodeClient)32 RestController (org.elasticsearch.rest.RestController)32 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)31 RestResponse (org.elasticsearch.rest.RestResponse)31 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)25 Strings (org.elasticsearch.common.Strings)24 GET (org.elasticsearch.rest.RestRequest.Method.GET)24 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)21 OK (org.elasticsearch.rest.RestStatus.OK)20 RestStatus (org.elasticsearch.rest.RestStatus)15 POST (org.elasticsearch.rest.RestRequest.Method.POST)11 RestActions.buildBroadcastShardsHeader (org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader)11 Map (java.util.Map)7 Set (java.util.Set)5 SettingsFilter (org.elasticsearch.common.settings.SettingsFilter)5