Added proxy configs to rest api call rule node (#2943)
* Added proxy configs to rest api call rule node * added validation proxyHost and proxyPort * refactored checkProxyPort * TbHttpClient improvements
This commit is contained in:
parent
7a555fca8d
commit
ebed307757
@ -20,11 +20,20 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.handler.ssl.SslContextBuilder;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;
|
||||
import org.springframework.http.client.Netty4ClientHttpRequestFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
import org.springframework.util.concurrent.ListenableFutureCallback;
|
||||
import org.springframework.web.client.AsyncRestTemplate;
|
||||
@ -36,7 +45,9 @@ import org.thingsboard.rule.engine.api.util.TbNodeUtils;
|
||||
import org.thingsboard.server.common.msg.TbMsg;
|
||||
import org.thingsboard.server.common.msg.TbMsgMetaData;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Deque;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -63,7 +74,30 @@ class TbHttpClient {
|
||||
if (config.getMaxParallelRequestsCount() > 0) {
|
||||
pendingFutures = new ConcurrentLinkedDeque<>();
|
||||
}
|
||||
if (config.isUseSimpleClientHttpFactory()) {
|
||||
|
||||
if (config.isEnableProxy()) {
|
||||
checkProxyHost(config.getProxyHost());
|
||||
checkProxyPort(config.getProxyPort());
|
||||
|
||||
HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClientBuilder.create()
|
||||
.setSSLHostnameVerifier(new DefaultHostnameVerifier())
|
||||
.setSSLContext(SSLContext.getDefault())
|
||||
.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort()));
|
||||
|
||||
if (!StringUtils.isEmpty(config.getProxyUser()) && !StringUtils.isEmpty(config.getProxyPassword())) {
|
||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(
|
||||
new AuthScope(config.getProxyHost(), config.getProxyPort()),
|
||||
new UsernamePasswordCredentials(config.getProxyUser(), config.getProxyPassword())
|
||||
);
|
||||
httpAsyncClientBuilder.setDefaultCredentialsProvider(credsProvider);
|
||||
}
|
||||
|
||||
HttpComponentsAsyncClientHttpRequestFactory requestFactory = new HttpComponentsAsyncClientHttpRequestFactory();
|
||||
requestFactory.setAsyncClient(httpAsyncClientBuilder.build());
|
||||
requestFactory.setReadTimeout(config.getReadTimeoutMs());
|
||||
httpClient = new AsyncRestTemplate(requestFactory);
|
||||
} else if (config.isUseSimpleClientHttpFactory()) {
|
||||
httpClient = new AsyncRestTemplate();
|
||||
} else {
|
||||
this.eventLoopGroup = new NioEventLoopGroup();
|
||||
@ -72,7 +106,7 @@ class TbHttpClient {
|
||||
nettyFactory.setReadTimeout(config.getReadTimeoutMs());
|
||||
httpClient = new AsyncRestTemplate(nettyFactory);
|
||||
}
|
||||
} catch (SSLException e) {
|
||||
} catch (SSLException | NoSuchAlgorithmException e) {
|
||||
throw new TbNodeException(e);
|
||||
}
|
||||
}
|
||||
@ -169,4 +203,15 @@ class TbHttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkProxyHost(String proxyHost) throws TbNodeException {
|
||||
if (StringUtils.isEmpty(proxyHost)) {
|
||||
throw new TbNodeException("Proxy host can't be empty");
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkProxyPort(int proxyPort) throws TbNodeException {
|
||||
if (proxyPort < 0 || proxyPort > 65535) {
|
||||
throw new TbNodeException("Proxy port out of range:" + proxyPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ package org.thingsboard.rule.engine.rest;
|
||||
import lombok.Data;
|
||||
import org.thingsboard.rule.engine.api.NodeConfiguration;
|
||||
|
||||
import java.net.Proxy;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
@ -33,6 +34,12 @@ public class TbRestApiCallNodeConfiguration implements NodeConfiguration<TbRestA
|
||||
private boolean useRedisQueueForMsgPersistence;
|
||||
private boolean trimQueue;
|
||||
private int maxQueueSize;
|
||||
private boolean enableProxy;
|
||||
private String proxyHost;
|
||||
private int proxyPort;
|
||||
private String proxyUser;
|
||||
private String proxyPassword;
|
||||
private Proxy.Type proxyType;
|
||||
|
||||
@Override
|
||||
public TbRestApiCallNodeConfiguration defaultConfiguration() {
|
||||
@ -45,6 +52,7 @@ public class TbRestApiCallNodeConfiguration implements NodeConfiguration<TbRestA
|
||||
configuration.setMaxParallelRequestsCount(0);
|
||||
configuration.setUseRedisQueueForMsgPersistence(false);
|
||||
configuration.setTrimQueue(false);
|
||||
configuration.setEnableProxy(false);
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user