Skip to content

Commit

Permalink
fix for RequestTransformer plus debug logging for ResponseTransformer (
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoFKT authored Oct 1, 2022
1 parent f7a3768 commit 0775831
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.networknt.reqtrans;

import com.networknt.utility.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

public class ConfigUtils {
private static final Logger logger = LoggerFactory.getLogger(ConfigUtils.class);
public static final String DELIMITOR = "@";
protected static final String INTERNAL_KEY_FORMAT = "%s %s";

public static String findServiceEntry(String method, String searchKey, Map<String, Object> mapping) {
if(logger.isDebugEnabled()) logger.debug("findServiceEntry for " + searchKey + " and method: " + method);
if(logger.isDebugEnabled()) logger.debug("mapping size: " + mapping.size());
String result = null;
for (Map.Entry<String, Object> entry : mapping.entrySet()) {
String[] tokens = StringUtils.trimToEmpty(entry.getKey()).split(DELIMITOR);
String ConfigPrefix = tokens[0];
String ConfigMethod = tokens[1];
if(logger.isDebugEnabled()) logger.debug("prefix: " + ConfigPrefix);
if(logger.isDebugEnabled()) logger.debug("method: " + ConfigMethod);
if(searchKey.startsWith(ConfigPrefix)) {
if((searchKey.length() == ConfigPrefix.length() || searchKey.charAt(ConfigPrefix.length()) == '/')
&& method.equals​(ConfigMethod)) {
result = entry.getKey();
break;
}
}
}
if(result == null) {
if(logger.isDebugEnabled()) logger.debug("serviceEntry not found!");
} else {
if(logger.isDebugEnabled()) logger.debug("prefix = " + result);
}
return result;
}

public static String normalisePath(String requestPath) {
if(!requestPath.startsWith("/")) {
return "/" + requestPath;
}
return requestPath;
}

public static String toInternalKey(String key) {
String[] tokens = StringUtils.trimToEmpty(key).split(DELIMITOR);

if (tokens.length ==2) {
return toInternalKey(tokens[1], tokens[0]);
}

logger.warn("Invalid key {}", key);
return key;
}

public static String toInternalKey(String method, String path) {
return String.format(INTERNAL_KEY_FORMAT, method, ConfigUtils.normalisePath(path));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,29 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
}
String method = exchange.getRequestMethod().toString();
if (!HttpContinue.requiresContinueResponse(exchange.getRequestHeaders())) {

if(logger.isDebugEnabled()) logger.debug("request can be transformed since no Expect headers found");

Map<String, Object> auditInfo = exchange.getAttachment(AttachmentConstants.AUDIT_INFO);
// need to get the rule/rules to execute from the RuleLoaderStartupHook. First, get the endpoint.
String endpoint = null;
if(auditInfo != null) {
endpoint = (String) auditInfo.get("endpoint");
} else {
endpoint = exchange.getRequestPath() + "@" + method.toString().toLowerCase();
}
// TODO any auditInfo properties to be added here?

// checked the RuleLoaderStartupHook to ensure it is loaded. If not, return an error to the caller.
if(RuleLoaderStartupHook.endpointRules == null) {
logger.error("RuleLoaderStartupHook endpointRules is null");
}
// need to get the rule/rules to execute from the RuleLoaderStartupHook. First, get the endpoint.
String endpoint, serviceEntry = null;
// Grab ServiceEntry from config
endpoint = ConfigUtils.toInternalKey(exchange.getRequestMethod().toString().toLowerCase(), exchange.getRequestURI());
if(logger.isDebugEnabled()) logger.debug("request endpoint: " + endpoint);
serviceEntry = ConfigUtils.findServiceEntry(exchange.getRequestMethod().toString().toLowerCase(), exchange.getRequestURI(), RuleLoaderStartupHook.endpointRules);
if(logger.isDebugEnabled()) logger.debug("request serviceEntry: " + serviceEntry);

// get the rules (maybe multiple) based on the endpoint.
Map<String, List> endpointRules = (Map<String, List>)RuleLoaderStartupHook.endpointRules.get(endpoint);
Map<String, List> endpointRules = (Map<String, List>)RuleLoaderStartupHook.endpointRules.get(serviceEntry);
if(endpointRules == null) {
if(logger.isDebugEnabled())
logger.debug("endpointRules iS NULL");
} else { if(logger.isDebugEnabled()) logger.debug("endpointRules: " + endpointRules.get(REQUEST_TRANSFORM).size()); }
if(endpointRules != null) {
List<Map<String, Object>> requestTransformRules = endpointRules.get(REQUEST_TRANSFORM);
if(requestTransformRules != null) {
Expand All @@ -120,10 +128,13 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
Map<String, Object> result = null;
String ruleId = null;
// iterate the rules and execute them in sequence. Break only if one rule is successful.
if(logger.isDebugEnabled()) logger.debug("requestTransformRules list count: " + requestTransformRules.size());
for(Map<String, Object> ruleMap: requestTransformRules) {
ruleId = (String)ruleMap.get(Constants.RULE_ID);
if(logger.isDebugEnabled()) logger.debug("ruleID found: " + ruleId);
result = engine.executeRule(ruleId, objMap);
boolean res = (Boolean)result.get(RuleConstants.RESULT);
if(logger.isDebugEnabled() && res) logger.debug("ruleID result is true");
if(!res) {
finalResult = false;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,23 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
// need to get the rule/rules to execute from the RuleLoaderStartupHook. First, get the endpoint.
String endpoint = null;
if(auditInfo != null) {
if(logger.isDebugEnabled()) logger.debug("auditInfo exists. Grab endpoint from it.");
endpoint = (String) auditInfo.get("endpoint");
} else {
if(logger.isDebugEnabled()) logger.debug("auditInfo is NULL. Grab endpoint from exchange.");
endpoint = exchange.getRequestPath() + "@" + method.toString().toLowerCase();
}
if(logger.isDebugEnabled()) logger.debug("request endpoint: " + endpoint);
// checked the RuleLoaderStartupHook to ensure it is loaded. If not, return an error to the caller.
if(RuleLoaderStartupHook.endpointRules == null) {
logger.error("RuleLoaderStartupHook endpointRules is null");
}
// get the rules (maybe multiple) based on the endpoint.
Map<String, List> endpointRules = (Map<String, List>)RuleLoaderStartupHook.endpointRules.get(endpoint);
if(endpointRules == null) {
if(logger.isDebugEnabled())
logger.debug("endpointRules iS NULL");
} else { if(logger.isDebugEnabled()) logger.debug("endpointRules: " + endpointRules.get(RESPONSE_TRANSFORM).size()); }
// if there is no access rule for this endpoint, check the default deny flag in the config.
boolean finalResult = true;
List<Map<String, Object>> responseTransformRules = endpointRules.get(RESPONSE_TRANSFORM);
Expand Down

0 comments on commit 0775831

Please sign in to comment.