use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class EncryptUriProcessor method process.
@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
if (param != null && !param.isEmpty() && template != null && !template.isEmpty()) {
Template uri = Parser.parseTemplate(template);
String resolvedTemplate = Expander.expandToString(uri, context.getParameters(), context.getEvaluator());
if (resolvedTemplate != null && !resolvedTemplate.isEmpty()) {
String endcoedUrl = encode(resolvedTemplate);
EncryptStepContextParams params = new EncryptStepContextParams();
params.addParam(param, Arrays.asList(endcoedUrl));
context.addParameters(params);
return UrlRewriteStepStatus.SUCCESS;
}
}
return UrlRewriteStepStatus.FAILURE;
}
use of org.apache.knox.gateway.util.urltemplate.Template 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;
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class SecureQueryEncodeProcessor method process.
@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
// TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
Template url = context.getCurrentUrl();
String str = url.toString();
String path = str;
String query = null;
int index = str.indexOf('?');
if (index >= 0) {
path = str.substring(0, index);
if (index < str.length()) {
query = str.substring(index + 1);
}
}
if (query != null) {
query = Base64.encodeBase64String(query.getBytes("UTF-8"));
query = removeTrailingEquals(query);
url = Parser.parseLiteral(path + "?" + ENCODED_PARAMETER_NAME + "=" + query);
context.setCurrentUrl(url);
}
return UrlRewriteStepStatus.SUCCESS;
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class SecureQueryEncryptProcessor method process.
@Override
public UrlRewriteStepStatus process(UrlRewriteContext context) throws Exception {
// TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
Template url = context.getCurrentUrl();
String str = url.toString();
String path = str;
String query = null;
int index = str.indexOf('?');
if (index >= 0) {
path = str.substring(0, index);
if (index < str.length()) {
query = str.substring(index + 1);
}
}
if (query != null) {
query = encode(query);
url = Parser.parseLiteral(path + "?" + ENCRYPTED_PARAMETER_NAME + "=" + query);
context.setCurrentUrl(url);
}
return UrlRewriteStepStatus.SUCCESS;
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class ServiceMappedHostFunctionProcessor method resolve.
@Override
public List<String> resolve(UrlRewriteContext context, List<String> parameters) throws Exception {
List<String> results = null;
if (parameters != null) {
results = new ArrayList<String>(parameters.size());
for (String parameter : parameters) {
String url = lookupServiceUrl(parameter);
if (url != null) {
Template template = Parser.parseLiteral(url);
Host host = template.getHost();
if (host != null) {
String hostStr = host.getFirstValue().getPattern();
if (hostmap != null) {
switch(context.getDirection()) {
case IN:
parameter = hostmap.resolveInboundHostName(hostStr);
break;
case OUT:
parameter = hostmap.resolveOutboundHostName(hostStr);
break;
}
} else {
parameter = hostStr;
}
}
}
results.add(parameter);
}
}
return results;
}
Aggregations