/**
 * 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;

@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;
                if (filter.equals("#")) {
                    return new AlwaysTrueTopicFilter();
                } else {
                    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;
    }
}

@@ -1527,106 +1527,8 @@
 %3E %7B%0A
-            if (filter.contains(%22+%22) %7C%7C filter.contains(%22#%22)) %7B%0A                String regex;%0A    
     
@@ -1557,28 +1557,24 @@
 als(%22#%22)) %7B%0A
-    
             
@@ -1625,32 +1625,75 @@
         
-    
 %7D else 
-%7B%0A   
+if (filter.contains(%22+%22) %7C%7C filter.contains(%22#%22)) %7B%0A
         
@@ -1700,16 +1700,22 @@
         
+String
  regex =
@@ -1722,20 +1722,16 @@
  filter%0A
-    
         
@@ -1785,36 +1785,32 @@
                 
-  
-  
 .replace(%22+%22, %22%5B
@@ -1816,20 +1816,16 @@
 %5B%5E/%5D+%22)%0A
-    
         
@@ -1871,26 +1871,8 @@
 %22);%0A
-                %7D%0A
     

fix MqttTopicFilterFactory toFilter