Skip to content

Commit 2dc556a

Browse files
committed
Fix get mappings HEAD requests
Get mappings HEAD requests incorrectly return a content-length header of 0. This commit addresses this by removing the special handling for get mappings HEAD requests, and just relying on the general mechanism that exists for handling HEAD requests in the REST layer.
1 parent 0a5917d commit 2dc556a

File tree

4 files changed

+27
-94
lines changed

4 files changed

+27
-94
lines changed

core/src/main/java/org/elasticsearch/action/ActionModule.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@
262262
import org.elasticsearch.rest.action.admin.indices.RestRolloverIndexAction;
263263
import org.elasticsearch.rest.action.admin.indices.RestShrinkIndexAction;
264264
import org.elasticsearch.rest.action.admin.indices.RestSyncedFlushAction;
265-
import org.elasticsearch.rest.action.admin.indices.RestTypesExistsAction;
266265
import org.elasticsearch.rest.action.admin.indices.RestUpdateSettingsAction;
267266
import org.elasticsearch.rest.action.admin.indices.RestUpgradeAction;
268267
import org.elasticsearch.rest.action.admin.indices.RestValidateQueryAction;
@@ -524,7 +523,6 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
524523
registerHandler.accept(new RestDeleteSnapshotAction(settings, restController));
525524
registerHandler.accept(new RestSnapshotsStatusAction(settings, restController));
526525

527-
registerHandler.accept(new RestTypesExistsAction(settings, restController));
528526
registerHandler.accept(new RestGetIndicesAction(settings, restController, indexScopedSettings, settingsFilter));
529527
registerHandler.accept(new RestIndicesStatsAction(settings, restController));
530528
registerHandler.accept(new RestIndicesSegmentsAction(settings, restController));

core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetMappingAction.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,32 @@
4242
import java.io.IOException;
4343

4444
import static org.elasticsearch.rest.RestRequest.Method.GET;
45+
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
4546
import static org.elasticsearch.rest.RestStatus.OK;
4647

4748
public class RestGetMappingAction extends BaseRestHandler {
48-
public RestGetMappingAction(Settings settings, RestController controller) {
49+
50+
public RestGetMappingAction(final Settings settings, final RestController controller) {
4951
super(settings);
5052
controller.registerHandler(GET, "/{index}/{type}/_mapping", this);
5153
controller.registerHandler(GET, "/{index}/_mappings/{type}", this);
5254
controller.registerHandler(GET, "/{index}/_mapping/{type}", this);
55+
controller.registerWithDeprecatedHandler(HEAD, "/{index}/_mapping/{type}", this, HEAD, "/{index}/{type}", deprecationLogger);
5356
controller.registerHandler(GET, "/_mapping/{type}", this);
5457
}
5558

5659
@Override
5760
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
5861
final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
5962
final String[] types = request.paramAsStringArrayOrEmptyIfAll("type");
60-
GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
63+
final GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
6164
getMappingsRequest.indices(indices).types(types);
6265
getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
6366
getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
6467
return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) {
6568
@Override
66-
public RestResponse buildResponse(GetMappingsResponse response, XContentBuilder builder) throws Exception {
67-
68-
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
69+
public RestResponse buildResponse(final GetMappingsResponse response, final XContentBuilder builder) throws Exception {
70+
final ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
6971
if (mappingsByIndex.isEmpty()) {
7072
if (indices.length != 0 && types.length != 0) {
7173
return new BytesRestResponse(OK, builder.startObject().endObject());
@@ -81,27 +83,28 @@ public RestResponse buildResponse(GetMappingsResponse response, XContentBuilder
8183
}
8284

8385
builder.startObject();
84-
for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexEntry : mappingsByIndex) {
85-
if (indexEntry.value.isEmpty()) {
86-
continue;
87-
}
88-
builder.startObject(indexEntry.key);
89-
builder.startObject(Fields.MAPPINGS);
90-
for (ObjectObjectCursor<String, MappingMetaData> typeEntry : indexEntry.value) {
91-
builder.field(typeEntry.key);
92-
builder.map(typeEntry.value.sourceAsMap());
86+
{
87+
for (final ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexEntry : mappingsByIndex) {
88+
if (indexEntry.value.isEmpty()) {
89+
continue;
90+
}
91+
builder.startObject(indexEntry.key);
92+
{
93+
builder.startObject("mappings");
94+
{
95+
for (final ObjectObjectCursor<String, MappingMetaData> typeEntry : indexEntry.value) {
96+
builder.field(typeEntry.key, typeEntry.value.sourceAsMap());
97+
}
98+
}
99+
builder.endObject();
100+
}
101+
builder.endObject();
93102
}
94-
builder.endObject();
95-
builder.endObject();
96103
}
97-
98104
builder.endObject();
99105
return new BytesRestResponse(OK, builder);
100106
}
101107
});
102108
}
103109

104-
static class Fields {
105-
static final String MAPPINGS = "mappings";
106-
}
107110
}

core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestTypesExistsAction.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

modules/transport-netty4/src/test/java/org/elasticsearch/rest/Netty4HeadBodyIsEmptyIT.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ public void testIndexExists() throws IOException {
7474

7575
public void testTypeExists() throws IOException {
7676
createTestDoc();
77-
headTestCase("/test/test", emptyMap(), equalTo(0));
78-
headTestCase("/test/test", singletonMap("pretty", "true"), equalTo(0));
77+
headTestCase("/test/_mapping/test", emptyMap(), greaterThan(0));
78+
headTestCase("/test/_mapping/test", singletonMap("pretty", "true"), greaterThan(0));
79+
headTestCase("/test/test", emptyMap(), greaterThan(0));
80+
headTestCase("/test/test", singletonMap("pretty", "true"), greaterThan(0));
7981
}
8082

8183
public void testAliasExists() throws IOException {

0 commit comments

Comments
 (0)