cleanup a bit full source with put index template

This commit is contained in:
Shay Banon 2012-04-15 18:22:29 +03:00
parent f1a8e7e4c7
commit 79309ae7e3
3 changed files with 62 additions and 41 deletions

View File

@ -243,8 +243,8 @@ public class PutIndexTemplateRequest extends MasterNodeOperationRequest {
*/ */
public PutIndexTemplateRequest source(XContentBuilder templateBuilder) { public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
try { try {
return source(templateBuilder.string()); return source(templateBuilder.underlyingBytes(), 0, templateBuilder.underlyingBytesLength());
} catch (IOException e) { } catch (Exception e) {
throw new ElasticSearchIllegalArgumentException("Failed to build json for template request", e); throw new ElasticSearchIllegalArgumentException("Failed to build json for template request", e);
} }
} }
@ -253,49 +253,58 @@ public class PutIndexTemplateRequest extends MasterNodeOperationRequest {
* The template source definition. * The template source definition.
*/ */
public PutIndexTemplateRequest source(Map templateSource) { public PutIndexTemplateRequest source(Map templateSource) {
try { Map<String, Object> source = templateSource;
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); if (source.containsKey("template")) {
builder.map(templateSource); template(source.get("template").toString());
return source(builder.string());
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + templateSource + "]", e);
} }
if (source.containsKey("order")) {
order(XContentMapValues.nodeIntegerValue(source.get("order"), order()));
}
if (source.containsKey("settings")) {
if (!(source.get("settings") instanceof Map)) {
throw new ElasticSearchIllegalArgumentException("Malformed settings section, should include an inner object");
}
settings((Map<String, Object>) source.get("settings"));
}
if (source.containsKey("mappings")) {
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings");
for (Map.Entry<String, Object> entry : mappings.entrySet()) {
if (!(entry.getValue() instanceof Map)) {
throw new ElasticSearchIllegalArgumentException("Malformed mappings section for type [" + entry.getKey() + "], should include an inner object describing the mapping");
}
mapping(entry.getKey(), (Map<String, Object>) entry.getValue());
}
}
return this;
} }
/** /**
* The template source definition. * The template source definition.
*/ */
public PutIndexTemplateRequest source(String templateSource) { public PutIndexTemplateRequest source(String templateSource) {
// parse source
Map<String, Object> source = null;
try { try {
source = XContentFactory.xContent(templateSource) return source(XContentFactory.xContent(templateSource).createParser(templateSource).mapOrderedAndClose());
.createParser(templateSource).mapOrderedAndClose(); } catch (Exception e) {
if (source.containsKey("template")) { throw new ElasticSearchIllegalArgumentException("failed to parse template source [" + templateSource + "]", e);
template(source.get("template").toString()); }
} }
if (source.containsKey("order")) {
order(XContentMapValues.nodeIntegerValue(source.get("order"), order())); /**
} * The template source definition.
if (source.containsKey("settings")) { */
if (!(source.get("settings") instanceof Map)) { public PutIndexTemplateRequest source(byte[] source) {
throw new ElasticSearchIllegalArgumentException("Malformed settings section, should include an inner object"); return source(source, 0, source.length);
} }
settings((Map<String, Object>) source.get("settings"));
} /**
if (source.containsKey("mappings")) { * The template source definition.
Map<String, Object> mappings = (Map<String, Object>) source.get("mappings"); */
for (Map.Entry<String, Object> entry : mappings.entrySet()) { public PutIndexTemplateRequest source(byte[] source, int offset, int length) {
if (!(entry.getValue() instanceof Map)) { try {
throw new ElasticSearchIllegalArgumentException("Malformed mappings section for type [" + entry.getKey() + "], should include an inner object describing the mapping"); return source(XContentFactory.xContent(source, offset, length).createParser(source, offset, length).mapOrderedAndClose());
} } catch (IOException e) {
mapping(entry.getKey(), (Map<String, Object>) entry.getValue()); throw new ElasticSearchIllegalArgumentException("failed to parse template source", e);
}
}
} catch (IOException e) {
throw new ElasticSearchIllegalArgumentException("Malformed template source", e);
} }
return this;
} }

View File

@ -163,6 +163,22 @@ public class PutIndexTemplateRequestBuilder extends BaseIndicesRequestBuilder<Pu
return this; return this;
} }
/**
* The template source definition.
*/
public PutIndexTemplateRequestBuilder setSource(byte[] templateSource) {
request.source(templateSource);
return this;
}
/**
* The template source definition.
*/
public PutIndexTemplateRequestBuilder setSource(byte[] templateSource, int offset, int length) {
request.source(templateSource, offset, length);
return this;
}
/** /**
* Timeout to wait for the index creation to be acknowledged by current cluster nodes. Defaults * Timeout to wait for the index creation to be acknowledged by current cluster nodes. Defaults
* to <tt>10s</tt>. * to <tt>10s</tt>.

View File

@ -19,7 +19,6 @@
package org.elasticsearch.rest.action.admin.indices.template.put; package org.elasticsearch.rest.action.admin.indices.template.put;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
@ -28,13 +27,10 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
import static org.elasticsearch.rest.RestStatus.OK; import static org.elasticsearch.rest.RestStatus.OK;
@ -68,7 +64,7 @@ public class RestPutIndexTemplateAction extends BaseRestHandler {
putRequest.create(request.paramAsBoolean("create", false)); putRequest.create(request.paramAsBoolean("create", false));
putRequest.cause(request.param("cause", "")); putRequest.cause(request.param("cause", ""));
putRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10))); putRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10)));
putRequest.source(request.contentAsString()); putRequest.source(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} catch (Exception e) { } catch (Exception e) {
try { try {
channel.sendResponse(new XContentThrowableRestResponse(request, e)); channel.sendResponse(new XContentThrowableRestResponse(request, e));