Browse Source

Refactoring, deleting redundant classes

pull/3925/head
Viacheslav Kukhtyn 5 years ago
parent
commit
9481654f5b
  1. 12
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/AnonymousCredentials.java
  2. 13
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/BasicCredentials.java
  3. 7
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/CertPemCredentials.java
  4. 21
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/ClientCredentials.java
  5. 11
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNode.java
  6. 8
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNodeConfiguration.java
  7. 4
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/azure/AzureIotHubSasCredentials.java
  8. 40
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/azure/TbAzureIotHubNode.java
  9. 26
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttAnonymousCredentials.java
  10. 33
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttBasicCredentials.java
  11. 26
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttCertPemCredentials.java
  12. 24
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java
  13. 8
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeConfiguration.java
  14. 26
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpAnonymousCredentials.java
  15. 35
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpBasicCredentials.java
  16. 26
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpCertPemCredentials.java
  17. 32
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpClientCredentials.java
  18. 10
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/credentials/BasicCredentialsTest.java

12
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/AnonymousCredentials.java

@ -16,7 +16,17 @@
package org.thingsboard.rule.engine.credentials;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.netty.handler.ssl.SslContext;
@JsonIgnoreProperties(ignoreUnknown = true)
public class AnonymousCredentials {
public class AnonymousCredentials implements ClientCredentials {
@Override
public CredentialsType getType() {
return CredentialsType.ANONYMOUS;
}
@Override
public SslContext initSslContext() {
return null;
}
}

13
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/BasicCredentials.java

@ -16,11 +16,22 @@
package org.thingsboard.rule.engine.credentials;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.netty.handler.ssl.SslContext;
import lombok.Data;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class BasicCredentials {
public class BasicCredentials implements ClientCredentials {
private String username;
private String password;
@Override
public CredentialsType getType() {
return CredentialsType.BASIC;
}
@Override
public SslContext initSslContext() {
return null;
}
}

7
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/CertPemCredentials.java

@ -53,7 +53,7 @@ import java.security.spec.PKCS8EncodedKeySpec;
@Data
@Slf4j
@JsonIgnoreProperties(ignoreUnknown = true)
public class CertPemCredentials {
public class CertPemCredentials implements ClientCredentials {
private static final String TLS_VERSION = "TLSv1.2";
private String caCert;
@ -61,6 +61,11 @@ public class CertPemCredentials {
private String privateKey;
private String password;
@Override
public CredentialsType getType() {
return CredentialsType.CERT_PEM;
}
public SslContext initSslContext() {
try {
Security.addProvider(new BouncyCastleProvider());

21
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttClientCredentials.java → rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/credentials/ClientCredentials.java

@ -13,31 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.rule.engine.mqtt.credentials;
package org.thingsboard.rule.engine.credentials;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.netty.handler.ssl.SslContext;
import org.thingsboard.mqtt.MqttClientConfig;
import org.thingsboard.rule.engine.credentials.CredentialsType;
import org.thingsboard.rule.engine.mqtt.azure.AzureIotHubSasCredentials;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = MqttAnonymousCredentials.class, name = "anonymous"),
@JsonSubTypes.Type(value = MqttBasicCredentials.class, name = "basic"),
@JsonSubTypes.Type(value = AnonymousCredentials.class, name = "anonymous"),
@JsonSubTypes.Type(value = BasicCredentials.class, name = "basic"),
@JsonSubTypes.Type(value = AzureIotHubSasCredentials.class, name = "sas"),
@JsonSubTypes.Type(value = MqttCertPemCredentials.class, name = "cert.PEM")})
public interface MqttClientCredentials {
@JsonSubTypes.Type(value = CertPemCredentials.class, name = "cert.PEM")})
public interface ClientCredentials {
@JsonIgnore
CredentialsType getType();
default SslContext initSslContext() {
return null;
}
default void configure(MqttClientConfig config) {
}
@JsonIgnore
SslContext initSslContext();
}

11
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNode.java

@ -31,6 +31,9 @@ import org.thingsboard.rule.engine.api.TbNode;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.rule.engine.credentials.BasicCredentials;
import org.thingsboard.rule.engine.credentials.ClientCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData;
@ -105,7 +108,13 @@ public class TbMqttNode implements TbNode {
config.setClientId(this.mqttNodeConfiguration.getClientId());
}
config.setCleanSession(this.mqttNodeConfiguration.isCleanSession());
this.mqttNodeConfiguration.getCredentials().configure(config);
ClientCredentials credentials = this.mqttNodeConfiguration.getCredentials();
if (credentials.getType() == CredentialsType.BASIC) {
config.setUsername(((BasicCredentials) credentials).getUsername());
config.setPassword(((BasicCredentials) credentials).getPassword());
}
MqttClient client = MqttClient.create(config, null);
client.setEventLoop(ctx.getSharedEventLoop());
Future<MqttConnectResult> connectFuture = client.connect(this.mqttNodeConfiguration.getHost(), this.mqttNodeConfiguration.getPort());

8
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/TbMqttNodeConfiguration.java

@ -17,8 +17,8 @@ package org.thingsboard.rule.engine.mqtt;
import lombok.Data;
import org.thingsboard.rule.engine.api.NodeConfiguration;
import org.thingsboard.rule.engine.mqtt.credentials.MqttAnonymousCredentials;
import org.thingsboard.rule.engine.mqtt.credentials.MqttClientCredentials;
import org.thingsboard.rule.engine.credentials.AnonymousCredentials;
import org.thingsboard.rule.engine.credentials.ClientCredentials;
@Data
public class TbMqttNodeConfiguration implements NodeConfiguration<TbMqttNodeConfiguration> {
@ -31,7 +31,7 @@ public class TbMqttNodeConfiguration implements NodeConfiguration<TbMqttNodeConf
private boolean cleanSession;
private boolean ssl;
private MqttClientCredentials credentials;
private ClientCredentials credentials;
@Override
public TbMqttNodeConfiguration defaultConfiguration() {
@ -42,7 +42,7 @@ public class TbMqttNodeConfiguration implements NodeConfiguration<TbMqttNodeConf
configuration.setConnectTimeoutSec(10);
configuration.setCleanSession(true);
configuration.setSsl(false);
configuration.setCredentials(new MqttAnonymousCredentials());
configuration.setCredentials(new AnonymousCredentials());
return configuration;
}

4
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/azure/AzureIotHubSasCredentials.java

@ -24,8 +24,8 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.thingsboard.common.util.AzureIotHubUtil;
import org.thingsboard.rule.engine.credentials.CertPemCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
import org.thingsboard.rule.engine.mqtt.credentials.MqttClientCredentials;
import javax.net.ssl.TrustManagerFactory;
import java.io.ByteArrayInputStream;
@ -37,7 +37,7 @@ import java.security.cert.X509Certificate;
@Data
@Slf4j
@JsonIgnoreProperties(ignoreUnknown = true)
public class AzureIotHubSasCredentials implements MqttClientCredentials {
public class AzureIotHubSasCredentials extends CertPemCredentials {
private String sasKey;
private String caCert;

40
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/azure/TbAzureIotHubNode.java

@ -15,21 +15,19 @@
*/
package org.thingsboard.rule.engine.mqtt.azure;
import io.netty.handler.codec.mqtt.MqttVersion;
import io.netty.handler.ssl.SslContext;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.AzureIotHubUtil;
import org.thingsboard.mqtt.MqttClientConfig;
import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.rule.engine.credentials.CertPemCredentials;
import org.thingsboard.rule.engine.credentials.ClientCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
import org.thingsboard.rule.engine.mqtt.TbMqttNode;
import org.thingsboard.rule.engine.mqtt.TbMqttNodeConfiguration;
import org.thingsboard.rule.engine.mqtt.credentials.MqttCertPemCredentials;
import org.thingsboard.rule.engine.mqtt.credentials.MqttClientCredentials;
import org.thingsboard.server.common.data.plugin.ComponentType;
@Slf4j
@ -49,17 +47,11 @@ public class TbAzureIotHubNode extends TbMqttNode {
this.mqttNodeConfiguration = TbNodeUtils.convert(configuration, TbMqttNodeConfiguration.class);
mqttNodeConfiguration.setPort(8883);
mqttNodeConfiguration.setCleanSession(true);
MqttClientCredentials credentials = mqttNodeConfiguration.getCredentials();
mqttNodeConfiguration.setCredentials(new MqttClientCredentials() {
ClientCredentials credentials = mqttNodeConfiguration.getCredentials();
mqttNodeConfiguration.setCredentials(new ClientCredentials() {
@Override
public CredentialsType getType() {
if (credentials instanceof AzureIotHubSasCredentials) {
return CredentialsType.SAS;
} else if (credentials instanceof MqttCertPemCredentials) {
return CredentialsType.CERT_PEM;
} else {
throw new IllegalArgumentException("[" + credentials.getType() + "] is not supported!");
}
return credentials.getType();
}
@Override
@ -69,8 +61,8 @@ public class TbAzureIotHubNode extends TbMqttNode {
if (sasCredentials.getCaCert() == null || sasCredentials.getCaCert().isEmpty()) {
sasCredentials.setCaCert(AzureIotHubUtil.getDefaultCaCert());
}
} else if (credentials instanceof MqttCertPemCredentials) {
MqttCertPemCredentials pemCredentials = (MqttCertPemCredentials) credentials;
} else if (credentials instanceof CertPemCredentials) {
CertPemCredentials pemCredentials = (CertPemCredentials) credentials;
if (pemCredentials.getCaCert() == null || pemCredentials.getCaCert().isEmpty()) {
pemCredentials.setCaCert(AzureIotHubUtil.getDefaultCaCert());
}
@ -78,15 +70,15 @@ public class TbAzureIotHubNode extends TbMqttNode {
return credentials.initSslContext();
}
@Override
public void configure(MqttClientConfig config) {
config.setProtocolVersion(MqttVersion.MQTT_3_1_1);
config.setUsername(AzureIotHubUtil.buildUsername(mqttNodeConfiguration.getHost(), config.getClientId()));
if (credentials instanceof AzureIotHubSasCredentials) {
AzureIotHubSasCredentials sasCredentials = (AzureIotHubSasCredentials) credentials;
config.setPassword(AzureIotHubUtil.buildSasToken(mqttNodeConfiguration.getHost(), sasCredentials.getSasKey()));
}
}
// @Override
// public void configure(MqttClientConfig config) {
// config.setProtocolVersion(MqttVersion.MQTT_3_1_1);
// config.setUsername(AzureIotHubUtil.buildUsername(mqttNodeConfiguration.getHost(), config.getClientId()));
// if (credentials instanceof AzureIotHubSasCredentials) {
// AzureIotHubSasCredentials sasCredentials = (AzureIotHubSasCredentials) credentials;
// config.setPassword(AzureIotHubUtil.buildSasToken(mqttNodeConfiguration.getHost(), sasCredentials.getSasKey()));
// }
// }
});
this.mqttClient = initClient(ctx);

26
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttAnonymousCredentials.java

@ -1,26 +0,0 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.rule.engine.mqtt.credentials;
import org.thingsboard.rule.engine.credentials.AnonymousCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
public class MqttAnonymousCredentials extends AnonymousCredentials implements MqttClientCredentials {
@Override
public CredentialsType getType() {
return CredentialsType.ANONYMOUS;
}
}

33
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttBasicCredentials.java

@ -1,33 +0,0 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.rule.engine.mqtt.credentials;
import org.thingsboard.mqtt.MqttClientConfig;
import org.thingsboard.rule.engine.credentials.BasicCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
public class MqttBasicCredentials extends BasicCredentials implements MqttClientCredentials {
@Override
public CredentialsType getType() {
return CredentialsType.BASIC;
}
@Override
public void configure(MqttClientConfig config) {
config.setUsername(getUsername());
config.setPassword(getPassword());
}
}

26
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/mqtt/credentials/MqttCertPemCredentials.java

@ -1,26 +0,0 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.rule.engine.mqtt.credentials;
import org.thingsboard.rule.engine.credentials.CertPemCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
public class MqttCertPemCredentials extends CertPemCredentials implements MqttClientCredentials {
@Override
public CredentialsType getType() {
return CredentialsType.CERT_PEM;
}
}

24
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java

@ -21,6 +21,7 @@ import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
@ -45,10 +46,10 @@ import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.api.TbRelationTypes;
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.rule.engine.credentials.BasicCredentials;
import org.thingsboard.rule.engine.credentials.CertPemCredentials;
import org.thingsboard.rule.engine.credentials.ClientCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
import org.thingsboard.rule.engine.rest.credentials.HttpBasicCredentials;
import org.thingsboard.rule.engine.rest.credentials.HttpCertPemCredentials;
import org.thingsboard.rule.engine.rest.credentials.HttpClientCredentials;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData;
@ -56,8 +57,10 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.util.Deque;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit;
@ -147,13 +150,13 @@ public class TbHttpClient {
}
}
private SslContext getSslContext(HttpClientCredentials credentials) throws SSLException {
private SslContext getSslContext(ClientCredentials credentials) throws SSLException {
switch (credentials.getType()) {
case ANONYMOUS:
case BASIC:
return SslContextBuilder.forClient().build();
case CERT_PEM:
return ((HttpCertPemCredentials) credentials).initSslContext();
return credentials.initSslContext();
default:
throw new IllegalArgumentException("[" + credentials.getType() + "] is not supported!");
}
@ -243,7 +246,7 @@ public class TbHttpClient {
private HttpHeaders prepareHeaders(TbMsgMetaData metaData) {
HttpHeaders headers = new HttpHeaders();
config.getHeaders().forEach((k, v) -> headers.add(TbNodeUtils.processPattern(k, metaData), TbNodeUtils.processPattern(v, metaData)));
addAuthorizationHeader(headers);
getBasicAuthHeaderValue(config.getCredentials()).ifPresent(authString -> headers.add("Authorization", authString));
return headers;
}
@ -278,10 +281,13 @@ public class TbHttpClient {
}
}
private void addAuthorizationHeader(HttpHeaders headers) {
HttpClientCredentials credentials = config.getCredentials();
public static Optional<String> getBasicAuthHeaderValue(ClientCredentials credentials) {
if (CredentialsType.BASIC == credentials.getType()) {
headers.add("Authorization", ((HttpBasicCredentials) credentials).getBasicAuthHeaderValue());
BasicCredentials basicCredentials = (BasicCredentials) credentials;
String authString = basicCredentials.getUsername() + ":" + basicCredentials.getPassword();
String encodedAuthString = new String(Base64.encodeBase64(authString.getBytes(StandardCharsets.UTF_8)));
return Optional.of("Basic " + encodedAuthString);
}
return Optional.empty();
}
}

8
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeConfiguration.java

@ -18,8 +18,8 @@ package org.thingsboard.rule.engine.rest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.thingsboard.rule.engine.api.NodeConfiguration;
import org.thingsboard.rule.engine.rest.credentials.HttpAnonymousCredentials;
import org.thingsboard.rule.engine.rest.credentials.HttpClientCredentials;
import org.thingsboard.rule.engine.credentials.AnonymousCredentials;
import org.thingsboard.rule.engine.credentials.ClientCredentials;
import java.util.Collections;
import java.util.Map;
@ -44,7 +44,7 @@ public class TbRestApiCallNodeConfiguration implements NodeConfiguration<TbRestA
private String proxyUser;
private String proxyPassword;
private String proxyScheme;
private HttpClientCredentials credentials;
private ClientCredentials credentials;
@Override
public TbRestApiCallNodeConfiguration defaultConfiguration() {
@ -58,7 +58,7 @@ public class TbRestApiCallNodeConfiguration implements NodeConfiguration<TbRestA
configuration.setUseRedisQueueForMsgPersistence(false);
configuration.setTrimQueue(false);
configuration.setEnableProxy(false);
configuration.setCredentials(new HttpAnonymousCredentials());
configuration.setCredentials(new AnonymousCredentials());
return configuration;
}
}

26
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpAnonymousCredentials.java

@ -1,26 +0,0 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.rule.engine.rest.credentials;
import org.thingsboard.rule.engine.credentials.AnonymousCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
public class HttpAnonymousCredentials extends AnonymousCredentials implements HttpClientCredentials {
@Override
public CredentialsType getType() {
return CredentialsType.ANONYMOUS;
}
}

35
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpBasicCredentials.java

@ -1,35 +0,0 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.rule.engine.rest.credentials;
import org.apache.commons.codec.binary.Base64;
import org.thingsboard.rule.engine.credentials.BasicCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
import java.nio.charset.StandardCharsets;
public class HttpBasicCredentials extends BasicCredentials implements HttpClientCredentials {
@Override
public CredentialsType getType() {
return CredentialsType.BASIC;
}
public String getBasicAuthHeaderValue() {
String authString = getUsername() + ":" + getPassword();
String encodedAuthString = new String(Base64.encodeBase64(authString.getBytes(StandardCharsets.UTF_8)));
return "Basic " + encodedAuthString;
}
}

26
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpCertPemCredentials.java

@ -1,26 +0,0 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.rule.engine.rest.credentials;
import org.thingsboard.rule.engine.credentials.CertPemCredentials;
import org.thingsboard.rule.engine.credentials.CredentialsType;
public class HttpCertPemCredentials extends CertPemCredentials implements HttpClientCredentials {
@Override
public CredentialsType getType() {
return CredentialsType.CERT_PEM;
}
}

32
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/credentials/HttpClientCredentials.java

@ -1,32 +0,0 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.rule.engine.rest.credentials;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.thingsboard.rule.engine.credentials.CredentialsType;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = HttpAnonymousCredentials.class, name = "anonymous"),
@JsonSubTypes.Type(value = HttpBasicCredentials.class, name = "basic"),
@JsonSubTypes.Type(value = HttpCertPemCredentials.class, name = "cert.PEM")})
public interface HttpClientCredentials {
@JsonIgnore
CredentialsType getType();
}

10
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/credentials/HttpBasicCredentialsTest.java → rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/credentials/BasicCredentialsTest.java

@ -17,16 +17,16 @@ package org.thingsboard.rule.engine.rest.credentials;
import org.junit.Assert;
import org.junit.Test;
import org.thingsboard.rule.engine.credentials.BasicCredentials;
import org.thingsboard.rule.engine.rest.TbHttpClient;
import java.util.Optional;
public class HttpBasicCredentialsTest {
public class BasicCredentialsTest {
@Test
public void getBasicAuthHeaderValueTest() {
HttpBasicCredentials credentials = new HttpBasicCredentials();
BasicCredentials credentials = new BasicCredentials();
credentials.setUsername("testUser");
credentials.setPassword("testPwd");
String actualHeaderValue = credentials.getBasicAuthHeaderValue();
String actualHeaderValue = TbHttpClient.getBasicAuthHeaderValue(credentials).get();
String expectedHeaderValue = "Basic dGVzdFVzZXI6dGVzdFB3ZA==";
Assert.assertEquals(expectedHeaderValue, actualHeaderValue);
Loading…
Cancel
Save