Search in sources :

Example 31 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class ServiceHostFunctionProcessor 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) {
                    parameter = host.getFirstValue().getPattern();
                }
            }
            results.add(parameter);
        }
    }
    return results;
}
Also used : Host(org.apache.knox.gateway.util.urltemplate.Host) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 32 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class ServiceMappedAddressFunctionProcessor method resolve.

public String resolve(UrlRewriter.Direction direction, String parameter) throws Exception {
    String addr = parameter;
    String url = lookupServiceUrl(parameter);
    if (url != null) {
        Template template = Parser.parseLiteral(url);
        Host host = template.getHost();
        String hostStr = null;
        if (host != null) {
            hostStr = host.getFirstValue().getPattern();
            if (hostmap != null) {
                switch(direction) {
                    case IN:
                        hostStr = hostmap.resolveInboundHostName(hostStr);
                        break;
                    case OUT:
                        hostStr = hostmap.resolveOutboundHostName(hostStr);
                        break;
                }
            }
        }
        Port port = template.getPort();
        String portStr = null;
        if (port != null) {
            portStr = port.getFirstValue().getPattern();
        }
        if (hostStr != null && portStr != null) {
            addr = hostStr + ":" + portStr;
        } else if (host != null && port == null) {
            addr = hostStr;
        } else if (host == null && port != null) {
            addr = ":" + portStr;
        }
    }
    return addr;
}
Also used : Port(org.apache.knox.gateway.util.urltemplate.Port) Host(org.apache.knox.gateway.util.urltemplate.Host) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 33 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class ServicePortFunctionProcessor 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);
                Port port = template.getPort();
                if (port != null) {
                    parameter = port.getFirstValue().getPattern();
                }
            }
            results.add(parameter);
        }
    }
    return results;
}
Also used : Port(org.apache.knox.gateway.util.urltemplate.Port) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 34 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class ServiceAddressFunctionProcessor method resolve.

public String resolve(String parameter) throws Exception {
    String addr = parameter;
    String url = lookupServiceUrl(parameter);
    if (url != null) {
        Template template = Parser.parseLiteral(url);
        Host host = template.getHost();
        String hostStr = null;
        if (host != null) {
            hostStr = host.getFirstValue().getPattern();
        }
        Port port = template.getPort();
        String portStr = null;
        if (port != null) {
            portStr = port.getFirstValue().getPattern();
        }
        if (hostStr != null && portStr != null) {
            addr = hostStr + ":" + portStr;
        } else if (host != null && port == null) {
            addr = hostStr;
        } else if (host == null && port != null) {
            addr = ":" + portStr;
        }
    }
    return addr;
}
Also used : Port(org.apache.knox.gateway.util.urltemplate.Port) Host(org.apache.knox.gateway.util.urltemplate.Host) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 35 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class UrlRewriteRequest method rewriteValue.

private String rewriteValue(UrlRewriter rewriter, String value, String rule) {
    try {
        Template input = Parser.parseLiteral(value);
        Template output = rewriter.rewrite(this, input, UrlRewriter.Direction.IN, rule);
        value = output.getPattern();
    } catch (URISyntaxException e) {
        LOG.failedToParseValueForUrlRewrite(value);
    }
    return value;
}
Also used : URISyntaxException(java.net.URISyntaxException) Template(org.apache.knox.gateway.util.urltemplate.Template)

Aggregations

Template (org.apache.knox.gateway.util.urltemplate.Template)50 Test (org.junit.Test)23 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)15 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)10 URISyntaxException (java.net.URISyntaxException)9 URL (java.net.URL)8 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)8 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)8 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)8 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)6 GatewayServices (org.apache.knox.gateway.services.GatewayServices)5 Capture (org.easymock.Capture)5 Host (org.apache.knox.gateway.util.urltemplate.Host)4 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)4 Query (org.apache.knox.gateway.util.urltemplate.Query)4 URI (java.net.URI)3 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)3