org.graylog2.indexer.cluster.Node Maven / Gradle / Ivy
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see .
*/
package org.graylog2.indexer.cluster;
import com.github.zafarkhaja.semver.Version;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Ping;
import org.graylog2.indexer.ElasticsearchException;
import org.graylog2.indexer.cluster.jest.JestUtils;
import javax.inject.Inject;
import java.util.Optional;
public class Node {
private final JestClient jestClient;
@Inject
public Node(JestClient jestClient) {
this.jestClient = jestClient;
}
public Optional getVersion() {
final Ping request = new Ping.Builder().build();
final JestResult jestResult = JestUtils.execute(jestClient, request, () -> "Unable to retrieve Elasticsearch version");
return Optional.ofNullable(jestResult.getJsonObject().path("version").path("number").asText(null))
.map(this::parseVersion);
}
private Version parseVersion(String version) {
try {
return Version.valueOf(version);
} catch (Exception e) {
throw new ElasticsearchException("Unable to parse Elasticsearch version: " + version, e);
}
}
}