Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.handler.config;
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.*;
/**
* @author Nicholas Azar
* @author Dan Dobrin
*/
public class HandlerConfig {
public static final String CONFIG_NAME = "handler";
private static final Logger logger = LoggerFactory.getLogger(HandlerConfig.class);
private static final String ENABLED = "enabled";
private static final String HANDLERS = "handlers";
private static final String ADDITIONAL_HANDLERS = "additionalHandlers";
private static final String CHAINS = "chains";
private static final String ADDITIONAL_CHAINS = "additionalChains";
private static final String PATHS = "paths";
private static final String ADDITIONAL_PATHS = "additionalPaths";
private static final String DEFAULT_HANDLERS = "defaultHandlers";
private static final String AUDIT_ON_ERROR = "auditOnError";
private static final String AUDIT_STACK_TRACE = "auditStackTrace";
private static final String BASE_PATH = "basePath";
private static final String PATH = "path";
private static final String SOURCE = "source";
private static final String EXEC = "exec";
private static final String METHOD = "method";
private boolean enabled;
private List handlers;
private Map> chains;
private List paths;
private List defaultHandlers;
private boolean auditOnError;
private boolean auditStackTrace;
private String basePath;
private Map mappedConfig;
private final Config config;
private HandlerConfig() {
this(CONFIG_NAME);
}
private HandlerConfig(String configName) {
config = Config.getInstance();
mappedConfig = config.getJsonMapConfigNoCache(configName);
setConfigData();
setConfigList();
setConfigMap();
}
public static HandlerConfig load() {
return new HandlerConfig();
}
public static HandlerConfig load(String configName) {
return new HandlerConfig(configName);
}
public void reload() {
mappedConfig = config.getJsonMapConfigNoCache(CONFIG_NAME);
setConfigData();
setConfigList();
setConfigMap();
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public List getHandlers() {
return handlers;
}
public void setHandlers(List handlers) {
this.handlers = handlers;
}
public Map> getChains() {
return chains;
}
public void setChains(Map> chains) {
this.chains = chains;
}
public List getPaths() {
return paths;
}
public void setPaths(List paths) {
this.paths = paths;
}
public List getDefaultHandlers() {
return defaultHandlers;
}
public void setDefaultHandlers(List defaultHandlers) {
this.defaultHandlers = defaultHandlers;
}
public boolean getAuditOnError() {
return auditOnError;
}
public void setAuditOnError(boolean auditOnError) {
this.auditOnError = auditOnError;
}
public boolean getAuditStackTrace() {
return auditStackTrace;
}
public void setAuditStackTrace(boolean auditStackTrace) {
this.auditStackTrace = auditStackTrace;
}
public String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
public boolean isAuditOnError() {
return auditOnError;
}
public boolean isAuditStackTrace() {
return auditStackTrace;
}
public Map getMappedConfig() {
return mappedConfig;
}
private void setConfigData() {
if(mappedConfig != null) {
Object object = mappedConfig.get(ENABLED);
if(object != null) enabled = Config.loadBooleanValue(ENABLED, object);
object = mappedConfig.get(AUDIT_ON_ERROR);
if(object != null) auditOnError = Config.loadBooleanValue(AUDIT_ON_ERROR, object);
object = mappedConfig.get(AUDIT_STACK_TRACE);
if(object != null) auditStackTrace = Config.loadBooleanValue(AUDIT_STACK_TRACE, object);
object = mappedConfig.get(BASE_PATH);
if(object != null) basePath = (String)object;
}
}
private void setConfigList() {
if (mappedConfig != null && mappedConfig.get(HANDLERS) != null) {
Object object = mappedConfig.get(HANDLERS);
handlers = 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 {
handlers = Config.getInstance().getMapper().readValue(s, new TypeReference<>() {});
} catch (Exception e) {
throw new ConfigException("could not parse the handlers json with a list of strings.");
}
} else {
// comma separated
handlers = Arrays.asList(s.split("\\s*,\\s*"));
}
} else if (object instanceof List) {
List list = (List)object;
handlers.addAll(list);
} else {
throw new ConfigException("handlers must be a string or a list of strings.");
}
// add additional handlers to the handlers if exist
if (mappedConfig.get(ADDITIONAL_HANDLERS) != null) {
object = mappedConfig.get(ADDITIONAL_HANDLERS);
if(object instanceof String) {
String s = (String)object;
s = s.trim();
if(logger.isTraceEnabled()) logger.trace("s = " + s);
if(s.startsWith("[")) {
// json format
try {
handlers.addAll(Config.getInstance().getMapper().readValue(s, new TypeReference<>() {}));
} catch (Exception e) {
throw new ConfigException("could not parse the additionalHandlers json with a list of strings.");
}
} else {
// comma separated
handlers.addAll(Arrays.asList(s.split("\\s*,\\s*")));
}
} else if (object instanceof List) {
List list = (List)object;
handlers.addAll(list);
} else {
throw new ConfigException("additionalHandlers must be a string or a list of strings.");
}
}
}
if (mappedConfig != null && mappedConfig.get(DEFAULT_HANDLERS) != null) {
Object object = mappedConfig.get(DEFAULT_HANDLERS);
defaultHandlers = 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 {
defaultHandlers = Config.getInstance().getMapper().readValue(s, new TypeReference<>() {});
} catch (Exception e) {
throw new ConfigException("could not parse the defaultHandlers json with a list of strings.");
}
} else {
// comma separated
defaultHandlers = Arrays.asList(s.split("\\s*,\\s*"));
}
} else if (object instanceof List) {
List list = (List)object;
defaultHandlers.addAll(list);
} else {
throw new ConfigException("defaultHandlers must be a string or a list of strings.");
}
}
if (mappedConfig != null && mappedConfig.get(PATHS) != null) {
Object object = mappedConfig.get(PATHS);
paths = new ArrayList<>();
if(object instanceof String) {
String s = (String)object;
s = s.trim();
if(logger.isTraceEnabled()) logger.trace("paths s = " + s);
if(s.startsWith("[")) {
// json format
try {
paths = Config.getInstance().getMapper().readValue(s, new TypeReference>() {});
} catch (Exception e) {
throw new ConfigException("could not parse the paths json with a list of string and object.");
}
} else {
throw new ConfigException("paths must be a list of string object map.");
}
} else if (object instanceof List) {
// the object is a list of map, we need convert it to PathPrefixAuth object.
List