12 changed files with 229 additions and 72 deletions
@ -0,0 +1,29 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.server.transport.mqtt.util; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class EqualsTopicFilter implements MqttTopicFilter { |
|||
|
|||
private final String filter; |
|||
|
|||
@Override |
|||
public boolean filter(String topic) { |
|||
return filter.equals(topic); |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.server.transport.mqtt.util; |
|||
|
|||
public interface MqttTopicFilter { |
|||
|
|||
boolean filter(String topic); |
|||
|
|||
} |
|||
@ -0,0 +1,57 @@ |
|||
/** |
|||
* Copyright © 2016-2020 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.server.transport.mqtt.util; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.device.profile.MqttTopics; |
|||
|
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.ConcurrentMap; |
|||
import java.util.regex.Pattern; |
|||
|
|||
@Slf4j |
|||
public class MqttTopicFilterFactory { |
|||
|
|||
private static final ConcurrentMap<String, MqttTopicFilter> filters = new ConcurrentHashMap<>(); |
|||
private static final MqttTopicFilter DEFAULT_TELEMETRY_TOPIC_FILTER = toFilter(MqttTopics.DEVICE_TELEMETRY_TOPIC); |
|||
private static final MqttTopicFilter DEFAULT_ATTRIBUTES_TOPIC_FILTER = toFilter(MqttTopics.DEVICE_ATTRIBUTES_TOPIC); |
|||
|
|||
public static MqttTopicFilter toFilter(String topicFilter) { |
|||
if (topicFilter == null || topicFilter.isEmpty()) { |
|||
throw new IllegalArgumentException("Topic filter can't be empty!"); |
|||
} |
|||
return filters.computeIfAbsent(topicFilter, filter -> { |
|||
if (filter.contains("+") || filter.contains("#")) { |
|||
String regex = filter |
|||
.replace("\\", "\\\\") |
|||
.replace("+", "[^/]+") |
|||
.replace("/#", "($|/.*)"); |
|||
log.debug("Converting [{}] to [{}]", filter, regex); |
|||
return new RegexTopicFilter(regex); |
|||
} else { |
|||
return new EqualsTopicFilter(filter); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public static MqttTopicFilter getDefaultTelemetryFilter() { |
|||
return DEFAULT_TELEMETRY_TOPIC_FILTER; |
|||
} |
|||
|
|||
public static MqttTopicFilter getDefaultAttributesFilter() { |
|||
return DEFAULT_ATTRIBUTES_TOPIC_FILTER; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue