Repository: nifi-minifi Updated Branches: refs/heads/master ec6227246 -> 7bac01ee1
MINIFI-450 Handling closing of HTTP response in PullHttpChangeIngestor. This closes #129 Signed-off-by: Jeremy Dyer <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi-minifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi-minifi/commit/7bac01ee Tree: http://git-wip-us.apache.org/repos/asf/nifi-minifi/tree/7bac01ee Diff: http://git-wip-us.apache.org/repos/asf/nifi-minifi/diff/7bac01ee Branch: refs/heads/master Commit: 7bac01ee1758da618974851af99c6dd72a5c4689 Parents: ec62272 Author: Aldrin Piri <[email protected]> Authored: Mon Jun 4 14:18:15 2018 -0400 Committer: Jeremy Dyer <[email protected]> Committed: Tue Jun 26 12:39:46 2018 -0400 ---------------------------------------------------------------------- .../ingestors/PullHttpChangeIngestor.java | 60 +++++++++----------- 1 file changed, 27 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi-minifi/blob/7bac01ee/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestor.java ---------------------------------------------------------------------- diff --git a/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestor.java b/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestor.java index f7add36..f363dcd 100644 --- a/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestor.java +++ b/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/configuration/ingestors/PullHttpChangeIngestor.java @@ -17,7 +17,6 @@ package org.apache.nifi.minifi.bootstrap.configuration.ingestors; -import okhttp3.Call; import okhttp3.Credentials; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; @@ -44,7 +43,6 @@ import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; - import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -148,7 +146,7 @@ public class PullHttpChangeIngestor extends AbstractPullChangeIngestor { queryReference.set(query); final String useEtagString = (String) properties.getOrDefault(USE_ETAG_KEY, "false"); - if ("true".equalsIgnoreCase(useEtagString) || "false".equalsIgnoreCase(useEtagString)){ + if ("true".equalsIgnoreCase(useEtagString) || "false".equalsIgnoreCase(useEtagString)) { useEtag = Boolean.parseBoolean(useEtagString); } else { throw new IllegalArgumentException("Property, " + USE_ETAG_KEY + ", to specify whether to use the ETag header, must either be a value boolean value (\"true\" or \"false\") or left to " + @@ -156,7 +154,7 @@ public class PullHttpChangeIngestor extends AbstractPullChangeIngestor { } final String overrideSecurityProperties = (String) properties.getOrDefault(OVERRIDE_SECURITY, "false"); - if ("true".equalsIgnoreCase(overrideSecurityProperties) || "false".equalsIgnoreCase(overrideSecurityProperties)){ + if ("true".equalsIgnoreCase(overrideSecurityProperties) || "false".equalsIgnoreCase(overrideSecurityProperties)) { overrideSecurity = Boolean.parseBoolean(overrideSecurityProperties); } else { throw new IllegalArgumentException("Property, " + OVERRIDE_SECURITY + ", to specify whether to override security properties must either be a value boolean value (\"true\" or \"false\")" + @@ -222,36 +220,31 @@ public class PullHttpChangeIngestor extends AbstractPullChangeIngestor { @Override public void run() { - try { - logger.debug("Attempting to pull new config"); - HttpUrl.Builder builder = new HttpUrl.Builder() - .host(hostReference.get()) - .port(portReference.get()) - .encodedPath(pathReference.get()); - String query = queryReference.get(); - if (!StringUtil.isNullOrEmpty(query)) { - builder = builder.encodedQuery(query); - } - final HttpUrl url = builder - .scheme(connectionScheme) - .build(); - - - final Request.Builder requestBuilder = new Request.Builder() - .get() - .url(url); - - if (useEtag) { - requestBuilder.addHeader("If-None-Match", lastEtag); - } + logger.debug("Attempting to pull new config"); + HttpUrl.Builder builder = new HttpUrl.Builder() + .host(hostReference.get()) + .port(portReference.get()) + .encodedPath(pathReference.get()); + final String query = queryReference.get(); + if (!StringUtil.isNullOrEmpty(query)) { + builder = builder.encodedQuery(query); + } + final HttpUrl url = builder + .scheme(connectionScheme) + .build(); - final Request request = requestBuilder.build(); + final Request.Builder requestBuilder = new Request.Builder() + .get() + .url(url); - final OkHttpClient httpClient = httpClientReference.get(); + if (useEtag) { + requestBuilder.addHeader("If-None-Match", lastEtag); + } - final Call call = httpClient.newCall(request); - final Response response = call.execute(); + final Request request = requestBuilder.build(); + ResponseBody body = null; + try (Response response = httpClientReference.get().newCall(request).execute()) { logger.debug("Response received: {}", response.toString()); int code = response.code(); @@ -264,17 +257,18 @@ public class PullHttpChangeIngestor extends AbstractPullChangeIngestor { throw new IOException("Got response code " + code + " while trying to pull configuration: " + response.body().string()); } - ResponseBody body = response.body(); + body = response.body(); + if (body == null) { logger.warn("No body returned when pulling a new configuration"); return; } - ByteBuffer bodyByteBuffer = ByteBuffer.wrap(body.bytes()); + final ByteBuffer bodyByteBuffer = ByteBuffer.wrap(body.bytes()); ByteBuffer readOnlyNewConfig = null; // checking if some parts of the configuration must be preserved - if(overrideSecurity) { + if (overrideSecurity) { readOnlyNewConfig = bodyByteBuffer.asReadOnlyBuffer(); } else { logger.debug("Preserving previous security properties...");
