Skip to content

Commit

Permalink
cleanup a bit full source with put index template
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Apr 15, 2012
1 parent f1a8e7e commit 79309ae
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 40 deletions.
Expand Up @@ -243,8 +243,8 @@ Map<String, String> mappings() {
*/
public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
try {
return source(templateBuilder.string());
} catch (IOException e) {
return source(templateBuilder.underlyingBytes(), 0, templateBuilder.underlyingBytesLength());
} catch (Exception e) {
throw new ElasticSearchIllegalArgumentException("Failed to build json for template request", e);
}
}
Expand All @@ -253,49 +253,58 @@ public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
* The template source definition.
*/
public PutIndexTemplateRequest source(Map templateSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(templateSource);
return source(builder.string());
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + templateSource + "]", e);
Map<String, Object> source = templateSource;
if (source.containsKey("template")) {
template(source.get("template").toString());
}
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.
*/
public PutIndexTemplateRequest source(String templateSource) {
// parse source
Map<String, Object> source = null;
try {
source = XContentFactory.xContent(templateSource)
.createParser(templateSource).mapOrderedAndClose();
if (source.containsKey("template")) {
template(source.get("template").toString());
}
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 source(XContentFactory.xContent(templateSource).createParser(templateSource).mapOrderedAndClose());
} catch (Exception e) {
throw new ElasticSearchIllegalArgumentException("failed to parse template source [" + templateSource + "]", e);
}
}

/**
* The template source definition.
*/
public PutIndexTemplateRequest source(byte[] source) {
return source(source, 0, source.length);
}

/**
* The template source definition.
*/
public PutIndexTemplateRequest source(byte[] source, int offset, int length) {
try {
return source(XContentFactory.xContent(source, offset, length).createParser(source, offset, length).mapOrderedAndClose());
} catch (IOException e) {
throw new ElasticSearchIllegalArgumentException("Malformed template source", e);
throw new ElasticSearchIllegalArgumentException("failed to parse template source", e);
}
return this;
}


Expand Down
Expand Up @@ -163,6 +163,22 @@ public PutIndexTemplateRequestBuilder setSource(String templateSource) {
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
* to <tt>10s</tt>.
Expand Down
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.rest.action.admin.indices.template.put;

import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
Expand All @@ -28,13 +27,10 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
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.action.support.RestXContentBuilder;

import java.io.IOException;
import java.util.Map;

import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
import static org.elasticsearch.rest.RestStatus.OK;
Expand Down Expand Up @@ -68,7 +64,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel)
putRequest.create(request.paramAsBoolean("create", false));
putRequest.cause(request.param("cause", ""));
putRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10)));
putRequest.source(request.contentAsString());
putRequest.source(request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} catch (Exception e) {
try {
channel.sendResponse(new XContentThrowableRestResponse(request, e));
Expand Down

0 comments on commit 79309ae

Please sign in to comment.