use of org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteStepProcessorHolder in project knox by apache.
the class UrlRewriteProcessor method rewrite.
@Override
public Template rewrite(Resolver resolver, Template inputUri, Direction direction, String ruleName) {
Template outputUri = inputUri;
String serviceRole = null;
if (resolver != null) {
List<String> serviceRoles = resolver.resolve("service.role");
if (serviceRoles != null && !serviceRoles.isEmpty()) {
serviceRole = serviceRoles.get(0);
}
}
UrlRewriteStepProcessorHolder stepHolder = null;
String effectiveRuleName = null;
if (ruleName == null || "*".equals(ruleName)) {
// Used for logging later.
ruleName = null;
Matcher<UrlRewriteRuleProcessorHolder>.Match match = null;
switch(direction) {
case IN:
match = inbound.match(outputUri, serviceRole);
break;
case OUT:
match = outbound.match(outputUri, serviceRole);
break;
}
if (match != null) {
stepHolder = match.getValue();
effectiveRuleName = match.getValue().getRuleName();
}
} else if (!ruleName.isEmpty()) {
stepHolder = rules.get(ruleName);
effectiveRuleName = ruleName;
}
if (stepHolder != null) {
UrlRewriteContext context = new UrlRewriteContextImpl(environment, resolver, functions, direction, inputUri);
try {
UrlRewriteStepStatus stepStatus = stepHolder.process(context);
if (UrlRewriteStepStatus.SUCCESS == stepStatus) {
outputUri = context.getCurrentUrl();
if (ruleName == null) {
LOG.rewroteUrlViaImplicitRule(inputUri, direction, effectiveRuleName, outputUri);
} else {
LOG.rewroteUrlViaExplicitRule(inputUri, direction, effectiveRuleName, outputUri);
}
} else {
LOG.failedToRewriteUrl(inputUri, direction, effectiveRuleName, stepStatus);
outputUri = null;
}
} catch (Exception e) {
LOG.failedToRewriteUrlDueToException(inputUri, direction, effectiveRuleName, e);
outputUri = null;
}
} else {
LOG.noRuleMatchingUrl(inputUri, direction);
}
return outputUri;
}
Aggregations