use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStep method parseStringCommand.
protected ElasticsearchCommand parseStringCommand(String command) {
ElasticsearchCommand esCommand = new ElasticsearchCommand();
String formattedCommand = command.trim();
// skip empty lines or lines starting with '#'
if (formattedCommand.isEmpty() || formattedCommand.charAt(0) == '#') {
esCommand.setSkip(true);
} else {
int firstSeparatorIndex = formattedCommand.indexOf(':');
int secondSeparatorIndex = formattedCommand.indexOf(':', firstSeparatorIndex + 1);
if (firstSeparatorIndex == -1 || secondSeparatorIndex == -1) {
throw new ElasticsearchSetupException("Command '" + command + "' in the script file is not properly formatted." + " The format is: REQUEST_METHOD:path:json_script." + " Ex: PUT:indexName/typeName/id:{\"shoe_size\":39, \"shoe_color\":\"orange\"}");
}
String methodName = formattedCommand.substring(0, firstSeparatorIndex).trim();
ElasticsearchCommand.RequestMethod method = ElasticsearchCommand.RequestMethod.fromName(methodName);
esCommand.setRequestMethod(method);
String relativeUrl = formattedCommand.substring(firstSeparatorIndex + 1, secondSeparatorIndex).trim();
esCommand.setRelativeUrl(relativeUrl);
String json = formattedCommand.substring(secondSeparatorIndex + 1).trim();
esCommand.setJson(json);
}
return esCommand;
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStep method executeInitCommand.
protected void executeInitCommand(ElasticsearchClient client, Log log, ElasticsearchCommand command) {
String url = "/" + command.getRelativeUrl();
String content = command.getJson();
try {
switch(command.getRequestMethod()) {
case PUT:
client.put(url, content);
break;
case POST:
client.post(url, content, String.class);
break;
case DELETE:
client.delete(url);
break;
default:
throw new IllegalStateException(String.format("Unsupported request method: %s", command.getRequestMethod()));
}
} catch (ElasticsearchClientException e) {
throw new ElasticsearchSetupException(String.format("Cannot execute command %s", command), e);
}
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStep method parseMapCommand.
protected ElasticsearchCommand parseMapCommand(Map<String, Object> command) {
ElasticsearchCommand esCommand = new ElasticsearchCommand();
String methodName = (String) command.get("method");
esCommand.setRequestMethod(ElasticsearchCommand.RequestMethod.fromName(methodName));
String path = (String) command.get("path");
esCommand.setRelativeUrl(path);
Object payload = command.get("payload");
if (ElasticsearchCommand.RequestMethod.DELETE == esCommand.getRequestMethod()) {
Validate.isTrue(payload == null, "For DELETE commands the payload should be undefined");
} else {
try {
esCommand.setJson(new ObjectMapper().writeValueAsString(payload));
} catch (JsonProcessingException e) {
throw new ElasticsearchSetupException("Cannot serialize the JSON payload for command '" + command + "'", e);
}
}
return esCommand;
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStepTest method testExecuteInitCommandPost.
@Test
public void testExecuteInitCommandPost() throws ElasticsearchClientException {
ElasticsearchCommand command = mock(ElasticsearchCommand.class);
when(command.getRequestMethod()).thenReturn(RequestMethod.POST);
when(command.getJson()).thenReturn("json");
when(command.getRelativeUrl()).thenReturn("index/type/id");
step.executeInitCommand(client, log, command);
verify(client).post("/index/type/id", "json", String.class);
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand in project elasticsearch-maven-plugin by alexcojocaru.
the class BootstrapClusterStepTest method parseStringCommand.
@Test
public void parseStringCommand() {
String command = "PUT:index/type/id:{}";
ElasticsearchCommand esCommand = step.parseStringCommand(command);
assertFalse(esCommand.isSkip());
assertEquals(RequestMethod.PUT, esCommand.getRequestMethod());
assertEquals("index/type/id", esCommand.getRelativeUrl());
assertEquals("{}", esCommand.getJson());
}
Aggregations