com.networknt.decode.RequestDecodeConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of encode-decode Show documentation
Show all versions of encode-decode Show documentation
Request decode and response encode for data compression.
/*
* Copyright (c) 2016 Network New Technologies Inc.
*
* 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 com.networknt.decode;
import com.fasterxml.jackson.core.type.TypeReference;
import com.networknt.config.Config;
import com.networknt.config.ConfigException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class RequestDecodeConfig {
private static final Logger logger = LoggerFactory.getLogger(RequestDecodeConfig.class);
public static final String CONFIG_NAME = "request-decode";
public static final String ENABLED = "enabled";
public static final String DECODERS = "decoders";
private Map mappedConfig;
private final Config config;
boolean enabled;
List decoders;
private RequestDecodeConfig(String configName) {
config = Config.getInstance();
mappedConfig = config.getJsonMapConfigNoCache(configName);
setConfigData();
setConfigList();
}
private RequestDecodeConfig() {
this(CONFIG_NAME);
}
public static RequestDecodeConfig load(String configName) {
return new RequestDecodeConfig(configName);
}
public static RequestDecodeConfig load() {
return new RequestDecodeConfig();
}
public void reload() {
mappedConfig = config.getJsonMapConfigNoCache(CONFIG_NAME);
setConfigData();
setConfigList();
}
public void reload(String configName) {
mappedConfig = config.getJsonMapConfigNoCache(configName);
setConfigData();
setConfigList();
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public List getDecoders() {
return decoders;
}
public void setDecoders(List decoders) {
this.decoders = decoders;
}
public Map getMappedConfig() {
return mappedConfig;
}
Config getConfig() {
return config;
}
private void setConfigData() {
if (getMappedConfig() != null) {
Object object = getMappedConfig().get(ENABLED);
if(object != null) enabled = Config.loadBooleanValue(ENABLED, object);
}
}
private void setConfigList() {
if (mappedConfig != null && mappedConfig.get(DECODERS) != null) {
Object object = mappedConfig.get(DECODERS);
decoders = new ArrayList<>();
if(object instanceof String) {
String s = (String)object;
s = s.trim();
if(logger.isTraceEnabled()) logger.trace("s = " + s);
if(s.startsWith("[")) {
// json format
try {
decoders = Config.getInstance().getMapper().readValue(s, new TypeReference>() {});
} catch (Exception e) {
throw new ConfigException("could not parse the decoders json with a list of strings.");
}
} else {
// comma separated
decoders = Arrays.asList(s.split("\\s*,\\s*"));
}
} else if (object instanceof List) {
List prefixes = (List)object;
prefixes.forEach(item -> {
decoders.add((String)item);
});
} else {
throw new ConfigException("decoders must be a string or a list of strings.");
}
}
}
}