From 68715a0dd43133e821c87010bcf98532a6ba0853 Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Mon, 23 Oct 2017 11:44:34 +0200 Subject: [PATCH] NIFI-4513 - add EL support for proxy settings in InvokeHttp Signed-off-by: Matthew Burgess This closes #2223 --- .../nifi/processors/standard/InvokeHTTP.java | 14 +++++++++----- .../nifi/processors/standard/TestInvokeHTTP.java | 13 +++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java index 6a9f994eec..65a28d502e 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/InvokeHTTP.java @@ -222,6 +222,7 @@ public final class InvokeHTTP extends AbstractProcessor { .description("The fully qualified hostname or IP address of the proxy server") .required(false) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .expressionLanguageSupported(true) .build(); public static final PropertyDescriptor PROP_PROXY_PORT = new PropertyDescriptor.Builder() @@ -229,6 +230,7 @@ public final class InvokeHTTP extends AbstractProcessor { .description("The port of the proxy server") .required(false) .addValidator(StandardValidators.PORT_VALIDATOR) + .expressionLanguageSupported(true) .build(); public static final PropertyDescriptor PROP_PROXY_USER = new PropertyDescriptor.Builder() @@ -237,6 +239,7 @@ public final class InvokeHTTP extends AbstractProcessor { .description("Username to set when authenticating against proxy") .required(false) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .expressionLanguageSupported(true) .build(); public static final PropertyDescriptor PROP_PROXY_PASSWORD = new PropertyDescriptor.Builder() @@ -246,6 +249,7 @@ public final class InvokeHTTP extends AbstractProcessor { .required(false) .sensitive(true) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .expressionLanguageSupported(true) .build(); public static final PropertyDescriptor PROP_CONTENT_TYPE = new PropertyDescriptor.Builder() @@ -518,8 +522,8 @@ public final class InvokeHTTP extends AbstractProcessor { OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder(); // Add a proxy if set - final String proxyHost = context.getProperty(PROP_PROXY_HOST).getValue(); - final Integer proxyPort = context.getProperty(PROP_PROXY_PORT).asInteger(); + final String proxyHost = context.getProperty(PROP_PROXY_HOST).evaluateAttributeExpressions().getValue(); + final Integer proxyPort = context.getProperty(PROP_PROXY_PORT).evaluateAttributeExpressions().asInteger(); if (proxyHost != null && proxyPort != null) { final Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); okHttpClientBuilder.proxy(proxy); @@ -609,7 +613,7 @@ public final class InvokeHTTP extends AbstractProcessor { private void setAuthenticator(OkHttpClient.Builder okHttpClientBuilder, ProcessContext context) { final String authUser = trimToEmpty(context.getProperty(PROP_BASIC_AUTH_USERNAME).getValue()); - final String proxyUsername = trimToEmpty(context.getProperty(PROP_PROXY_USER).getValue()); + final String proxyUsername = trimToEmpty(context.getProperty(PROP_PROXY_USER).evaluateAttributeExpressions().getValue()); // If the username/password properties are set then check if digest auth is being used if (!authUser.isEmpty() && "true".equalsIgnoreCase(context.getProperty(PROP_DIGEST_AUTH).getValue())) { @@ -625,7 +629,7 @@ public final class InvokeHTTP extends AbstractProcessor { final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials); if(!proxyUsername.isEmpty()) { - final String proxyPassword = context.getProperty(PROP_PROXY_PASSWORD).getValue(); + final String proxyPassword = context.getProperty(PROP_PROXY_PASSWORD).evaluateAttributeExpressions().getValue(); ProxyAuthenticator proxyAuthenticator = new ProxyAuthenticator(proxyUsername, proxyPassword); okHttpClientBuilder.proxyAuthenticator(proxyAuthenticator); @@ -636,7 +640,7 @@ public final class InvokeHTTP extends AbstractProcessor { } else { // Add proxy authentication only if(!proxyUsername.isEmpty()) { - final String proxyPassword = context.getProperty(PROP_PROXY_PASSWORD).getValue(); + final String proxyPassword = context.getProperty(PROP_PROXY_PASSWORD).evaluateAttributeExpressions().getValue(); ProxyAuthenticator proxyAuthenticator = new ProxyAuthenticator(proxyUsername, proxyPassword); okHttpClientBuilder.proxyAuthenticator(proxyAuthenticator); diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java index a6b798b5df..bb4f7ee00b 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestInvokeHTTP.java @@ -137,8 +137,13 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon { addHandler(new MyProxyHandler()); URL proxyURL = new URL(url); + runner.setVariable("proxy.host", proxyURL.getHost()); + runner.setVariable("proxy.port", String.valueOf(proxyURL.getPort())); + runner.setVariable("proxy.username", "username"); + runner.setVariable("proxy.password", "password"); + runner.setProperty(InvokeHTTP.PROP_URL, "http://nifi.apache.org/"); // just a dummy URL no connection goes out - runner.setProperty(InvokeHTTP.PROP_PROXY_HOST, proxyURL.getHost()); + runner.setProperty(InvokeHTTP.PROP_PROXY_HOST, "${proxy.host}"); try{ runner.run(); @@ -146,9 +151,9 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon { } catch (AssertionError e){ // Expect assertion error when proxy port isn't set but host is. } - runner.setProperty(InvokeHTTP.PROP_PROXY_PORT, String.valueOf(proxyURL.getPort())); + runner.setProperty(InvokeHTTP.PROP_PROXY_PORT, "${proxy.port}"); - runner.setProperty(InvokeHTTP.PROP_PROXY_USER, "username"); + runner.setProperty(InvokeHTTP.PROP_PROXY_USER, "${proxy.username}"); try{ runner.run(); @@ -156,7 +161,7 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon { } catch (AssertionError e){ // Expect assertion error when proxy password isn't set but host is. } - runner.setProperty(InvokeHTTP.PROP_PROXY_PASSWORD, "password"); + runner.setProperty(InvokeHTTP.PROP_PROXY_PASSWORD, "${proxy.password}"); createFlowFiles(runner);