AbstractElasticIndex: Factor out more variants of {post,perform}Request
Factor out more variants of these methods that eventually end up in the one that actually makes the call to the RestClient instance. This will allow to make any necessary adjustments to the URI or request parameters in a single place. At the same time, reorder the arguments to make the order consistent across methods. Change-Id: I3ced32e8df6cbd5a5f05f4d3241f84a7e7bebb10
This commit is contained in:
@@ -7,6 +7,7 @@ java_library(
|
|||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
"//gerrit-antlr:query_exception",
|
"//gerrit-antlr:query_exception",
|
||||||
|
"//gerrit-common:annotations",
|
||||||
"//gerrit-extension-api:api",
|
"//gerrit-extension-api:api",
|
||||||
"//gerrit-reviewdb:server",
|
"//gerrit-reviewdb:server",
|
||||||
"//gerrit-server:server",
|
"//gerrit-server:server",
|
||||||
|
@@ -21,6 +21,7 @@ import static org.apache.commons.codec.binary.Base64.decodeBase64;
|
|||||||
import com.google.common.collect.FluentIterable;
|
import com.google.common.collect.FluentIterable;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.io.CharStreams;
|
import com.google.common.io.CharStreams;
|
||||||
|
import com.google.gerrit.common.Nullable;
|
||||||
import com.google.gerrit.elasticsearch.ElasticMapping.MappingProperties;
|
import com.google.gerrit.elasticsearch.ElasticMapping.MappingProperties;
|
||||||
import com.google.gerrit.elasticsearch.builders.SearchSourceBuilder;
|
import com.google.gerrit.elasticsearch.builders.SearchSourceBuilder;
|
||||||
import com.google.gerrit.elasticsearch.bulk.DeleteRequest;
|
import com.google.gerrit.elasticsearch.bulk.DeleteRequest;
|
||||||
@@ -135,7 +136,7 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
|
|||||||
@Override
|
@Override
|
||||||
public void delete(K id) throws IOException {
|
public void delete(K id) throws IOException {
|
||||||
String uri = getURI(type, BULK);
|
String uri = getURI(type, BULK);
|
||||||
Response response = postRequest(getDeleteActions(id), uri, getRefreshParam());
|
Response response = postRequest(uri, getDeleteActions(id), getRefreshParam());
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != HttpStatus.SC_OK) {
|
if (statusCode != HttpStatus.SC_OK) {
|
||||||
throw new IOException(
|
throw new IOException(
|
||||||
@@ -147,10 +148,10 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
|
|||||||
public void deleteAll() throws IOException {
|
public void deleteAll() throws IOException {
|
||||||
// Delete the index, if it exists.
|
// Delete the index, if it exists.
|
||||||
String endpoint = indexName + client.adapter().indicesExistParam();
|
String endpoint = indexName + client.adapter().indicesExistParam();
|
||||||
Response response = client.get().performRequest(new Request("HEAD", endpoint));
|
Response response = performRequest("HEAD", endpoint);
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode == HttpStatus.SC_OK) {
|
if (statusCode == HttpStatus.SC_OK) {
|
||||||
response = client.get().performRequest(new Request("DELETE", indexName));
|
response = performRequest("DELETE", indexName);
|
||||||
statusCode = response.getStatusLine().getStatusCode();
|
statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != HttpStatus.SC_OK) {
|
if (statusCode != HttpStatus.SC_OK) {
|
||||||
throw new IOException(
|
throw new IOException(
|
||||||
@@ -160,7 +161,7 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
|
|||||||
|
|
||||||
// Recreate the index.
|
// Recreate the index.
|
||||||
String indexCreationFields = concatJsonString(getSettings(), getMappings());
|
String indexCreationFields = concatJsonString(getSettings(), getMappings());
|
||||||
response = performRequest("PUT", indexCreationFields, indexName, Collections.emptyMap());
|
response = performRequest("PUT", indexName, indexCreationFields);
|
||||||
statusCode = response.getStatusLine().getStatusCode();
|
statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != HttpStatus.SC_OK) {
|
if (statusCode != HttpStatus.SC_OK) {
|
||||||
String error = String.format("Failed to create index %s: %s", indexName, statusCode);
|
String error = String.format("Failed to create index %s: %s", indexName, statusCode);
|
||||||
@@ -228,20 +229,36 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
|
|||||||
return encodedIndexName + "/" + encodedType + "/" + request;
|
return encodedIndexName + "/" + encodedType + "/" + request;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Response postRequest(Object payload, String uri, Map<String, String> params)
|
protected Response postRequest(String uri, Object payload) throws IOException {
|
||||||
|
return performRequest("POST", uri, payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Response postRequest(String uri, Object payload, Map<String, String> params)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
return performRequest("POST", payload, uri, params);
|
return performRequest("POST", uri, payload, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String concatJsonString(String target, String addition) {
|
private String concatJsonString(String target, String addition) {
|
||||||
return target.substring(0, target.length() - 1) + "," + addition.substring(1);
|
return target.substring(0, target.length() - 1) + "," + addition.substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Response performRequest(String method, String uri) throws IOException {
|
||||||
|
return performRequest(method, uri, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Response performRequest(String method, String uri, @Nullable Object payload)
|
||||||
|
throws IOException {
|
||||||
|
return performRequest(method, uri, payload, Collections.emptyMap());
|
||||||
|
}
|
||||||
|
|
||||||
private Response performRequest(
|
private Response performRequest(
|
||||||
String method, Object payload, String uri, Map<String, String> params) throws IOException {
|
String method, String uri, @Nullable Object payload, Map<String, String> params)
|
||||||
|
throws IOException {
|
||||||
Request request = new Request(method, uri);
|
Request request = new Request(method, uri);
|
||||||
String payloadStr = payload instanceof String ? (String) payload : payload.toString();
|
if (payload != null) {
|
||||||
request.setEntity(new NStringEntity(payloadStr, ContentType.APPLICATION_JSON));
|
String payloadStr = payload instanceof String ? (String) payload : payload.toString();
|
||||||
|
request.setEntity(new NStringEntity(payloadStr, ContentType.APPLICATION_JSON));
|
||||||
|
}
|
||||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||||
request.addParameter(entry.getKey(), entry.getValue());
|
request.addParameter(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
|
@@ -93,7 +93,7 @@ public class ElasticAccountIndex extends AbstractElasticIndex<Account.Id, Accoun
|
|||||||
.add(new UpdateRequest<>(schema, as));
|
.add(new UpdateRequest<>(schema, as));
|
||||||
|
|
||||||
String uri = getURI(type, BULK);
|
String uri = getURI(type, BULK);
|
||||||
Response response = postRequest(bulk, uri, getRefreshParam());
|
Response response = postRequest(uri, bulk, getRefreshParam());
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != HttpStatus.SC_OK) {
|
if (statusCode != HttpStatus.SC_OK) {
|
||||||
throw new IOException(
|
throw new IOException(
|
||||||
@@ -152,7 +152,7 @@ public class ElasticAccountIndex extends AbstractElasticIndex<Account.Id, Accoun
|
|||||||
try {
|
try {
|
||||||
List<AccountState> results = Collections.emptyList();
|
List<AccountState> results = Collections.emptyList();
|
||||||
String uri = getURI(type, SEARCH);
|
String uri = getURI(type, SEARCH);
|
||||||
Response response = postRequest(search, uri, Collections.emptyMap());
|
Response response = postRequest(uri, search);
|
||||||
StatusLine statusLine = response.getStatusLine();
|
StatusLine statusLine = response.getStatusLine();
|
||||||
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
|
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
|
||||||
String content = getContent(response);
|
String content = getContent(response);
|
||||||
|
@@ -148,7 +148,7 @@ public class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeDa
|
|||||||
}
|
}
|
||||||
|
|
||||||
String uri = getURI(type, BULK);
|
String uri = getURI(type, BULK);
|
||||||
Response response = postRequest(bulk, uri, getRefreshParam());
|
Response response = postRequest(uri, bulk, getRefreshParam());
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != HttpStatus.SC_OK) {
|
if (statusCode != HttpStatus.SC_OK) {
|
||||||
throw new IOException(
|
throw new IOException(
|
||||||
@@ -229,7 +229,7 @@ public class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeDa
|
|||||||
try {
|
try {
|
||||||
List<ChangeData> results = Collections.emptyList();
|
List<ChangeData> results = Collections.emptyList();
|
||||||
String uri = getURI(types);
|
String uri = getURI(types);
|
||||||
Response response = postRequest(search, uri, Collections.emptyMap());
|
Response response = postRequest(uri, search);
|
||||||
StatusLine statusLine = response.getStatusLine();
|
StatusLine statusLine = response.getStatusLine();
|
||||||
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
|
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
|
||||||
String content = getContent(response);
|
String content = getContent(response);
|
||||||
|
@@ -90,7 +90,7 @@ public class ElasticGroupIndex extends AbstractElasticIndex<AccountGroup.UUID, A
|
|||||||
.add(new UpdateRequest<>(schema, group));
|
.add(new UpdateRequest<>(schema, group));
|
||||||
|
|
||||||
String uri = getURI(type, BULK);
|
String uri = getURI(type, BULK);
|
||||||
Response response = postRequest(bulk, uri, getRefreshParam());
|
Response response = postRequest(uri, bulk, getRefreshParam());
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != HttpStatus.SC_OK) {
|
if (statusCode != HttpStatus.SC_OK) {
|
||||||
throw new IOException(
|
throw new IOException(
|
||||||
@@ -149,7 +149,7 @@ public class ElasticGroupIndex extends AbstractElasticIndex<AccountGroup.UUID, A
|
|||||||
try {
|
try {
|
||||||
List<AccountGroup> results = Collections.emptyList();
|
List<AccountGroup> results = Collections.emptyList();
|
||||||
String uri = getURI(type, SEARCH);
|
String uri = getURI(type, SEARCH);
|
||||||
Response response = postRequest(search, uri, Collections.emptyMap());
|
Response response = postRequest(uri, search);
|
||||||
StatusLine statusLine = response.getStatusLine();
|
StatusLine statusLine = response.getStatusLine();
|
||||||
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
|
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
|
||||||
String content = getContent(response);
|
String content = getContent(response);
|
||||||
|
Reference in New Issue
Block a user