NIFI-4513 - add EL support for proxy settings in InvokeHttp

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #2223
This commit is contained in:
Pierre Villard 2017-10-23 11:44:34 +02:00 committed by Matthew Burgess
parent d6744b9ee5
commit 68715a0dd4
2 changed files with 18 additions and 9 deletions

View File

@ -222,6 +222,7 @@ public final class InvokeHTTP extends AbstractProcessor {
.description("The fully qualified hostname or IP address of the proxy server") .description("The fully qualified hostname or IP address of the proxy server")
.required(false) .required(false)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.expressionLanguageSupported(true)
.build(); .build();
public static final PropertyDescriptor PROP_PROXY_PORT = new PropertyDescriptor.Builder() 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") .description("The port of the proxy server")
.required(false) .required(false)
.addValidator(StandardValidators.PORT_VALIDATOR) .addValidator(StandardValidators.PORT_VALIDATOR)
.expressionLanguageSupported(true)
.build(); .build();
public static final PropertyDescriptor PROP_PROXY_USER = new PropertyDescriptor.Builder() 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") .description("Username to set when authenticating against proxy")
.required(false) .required(false)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.expressionLanguageSupported(true)
.build(); .build();
public static final PropertyDescriptor PROP_PROXY_PASSWORD = new PropertyDescriptor.Builder() public static final PropertyDescriptor PROP_PROXY_PASSWORD = new PropertyDescriptor.Builder()
@ -246,6 +249,7 @@ public final class InvokeHTTP extends AbstractProcessor {
.required(false) .required(false)
.sensitive(true) .sensitive(true)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.expressionLanguageSupported(true)
.build(); .build();
public static final PropertyDescriptor PROP_CONTENT_TYPE = new PropertyDescriptor.Builder() 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(); OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder();
// Add a proxy if set // Add a proxy if set
final String proxyHost = context.getProperty(PROP_PROXY_HOST).getValue(); final String proxyHost = context.getProperty(PROP_PROXY_HOST).evaluateAttributeExpressions().getValue();
final Integer proxyPort = context.getProperty(PROP_PROXY_PORT).asInteger(); final Integer proxyPort = context.getProperty(PROP_PROXY_PORT).evaluateAttributeExpressions().asInteger();
if (proxyHost != null && proxyPort != null) { if (proxyHost != null && proxyPort != null) {
final Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); final Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
okHttpClientBuilder.proxy(proxy); okHttpClientBuilder.proxy(proxy);
@ -609,7 +613,7 @@ public final class InvokeHTTP extends AbstractProcessor {
private void setAuthenticator(OkHttpClient.Builder okHttpClientBuilder, ProcessContext context) { private void setAuthenticator(OkHttpClient.Builder okHttpClientBuilder, ProcessContext context) {
final String authUser = trimToEmpty(context.getProperty(PROP_BASIC_AUTH_USERNAME).getValue()); 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 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())) { 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); final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
if(!proxyUsername.isEmpty()) { 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); ProxyAuthenticator proxyAuthenticator = new ProxyAuthenticator(proxyUsername, proxyPassword);
okHttpClientBuilder.proxyAuthenticator(proxyAuthenticator); okHttpClientBuilder.proxyAuthenticator(proxyAuthenticator);
@ -636,7 +640,7 @@ public final class InvokeHTTP extends AbstractProcessor {
} else { } else {
// Add proxy authentication only // Add proxy authentication only
if(!proxyUsername.isEmpty()) { 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); ProxyAuthenticator proxyAuthenticator = new ProxyAuthenticator(proxyUsername, proxyPassword);
okHttpClientBuilder.proxyAuthenticator(proxyAuthenticator); okHttpClientBuilder.proxyAuthenticator(proxyAuthenticator);

View File

@ -137,8 +137,13 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon {
addHandler(new MyProxyHandler()); addHandler(new MyProxyHandler());
URL proxyURL = new URL(url); 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_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{ try{
runner.run(); runner.run();
@ -146,9 +151,9 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon {
} catch (AssertionError e){ } catch (AssertionError e){
// Expect assertion error when proxy port isn't set but host is. // 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{ try{
runner.run(); runner.run();
@ -156,7 +161,7 @@ public class TestInvokeHTTP extends TestInvokeHttpCommon {
} catch (AssertionError e){ } catch (AssertionError e){
// Expect assertion error when proxy password isn't set but host is. // 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); createFlowFiles(runner);