Search in sources :

Example 26 with UrlRewriteRulesDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.

the class InboundUrlFunctionProcessorTest method testQueryParam.

@Test
public void testQueryParam() throws Exception {
    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(environment.resolve("cluster.name")).andReturn(Collections.singletonList("test-cluster-name")).anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("query.param.host")).andReturn(Lists.newArrayList("http://foo:50075")).anyTimes();
    EasyMock.replay(gatewayServices, environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-location");
    rule.pattern("{*}://{*}:{*}/{**}/?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{$inboundurl[host]}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("https://localhost:8443/gateway/default/datanode/?host=http://foo:50075");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-location");
    assertThat(output.toString(), is("http://foo:50075"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 27 with UrlRewriteRulesDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.

the class UrlRewriteRequest method getRewriteFilterConfig.

private UrlRewriteFilterContentDescriptor getRewriteFilterConfig(String filterName, MimeType mimeType) {
    UrlRewriteFilterContentDescriptor filterContentConfig = null;
    UrlRewriteRulesDescriptor rewriteConfig = rewriter.getConfig();
    if (rewriteConfig != null) {
        UrlRewriteFilterDescriptor filterConfig = rewriteConfig.getFilter(filterName);
        if (filterConfig != null) {
            filterContentConfig = filterConfig.getContent(mimeType);
        }
    }
    return filterContentConfig;
}
Also used : UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)

Example 28 with UrlRewriteRulesDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.

the class UrlRewriteDeploymentContributor method finalizeContribution.

public void finalizeContribution(DeploymentContext context) {
    // Write the descriptor into the archive.
    UrlRewriteRulesDescriptor descriptor = context.getDescriptor(getRole());
    StringWriter writer = new StringWriter();
    try {
        UrlRewriteRulesDescriptorFactory.store(descriptor, "xml", writer);
    } catch (IOException e) {
        LOG.failedToWriteRulesDescriptor(e);
    }
    String asset = writer.toString();
    context.getWebArchive().addAsWebInfResource(new StringAsset(asset), UrlRewriteServletContextListener.DESCRIPTOR_DEFAULT_FILE_NAME);
    // Tell the provider the location of the descriptor.
    context.getWebAppDescriptor().createListener().listenerClass(UrlRewriteServletContextListener.class.getName());
    context.getWebAppDescriptor().createContextParam().paramName(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME).paramValue(UrlRewriteServletContextListener.DESCRIPTOR_DEFAULT_LOCATION);
// ServletType<WebAppDescriptor> servlet = findServlet( context, context.getTopology().getName() );
// servlet.createInitParam()
// .paramName( UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME )
// .paramValue( DESCRIPTOR_FILE_NAME );
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) StringWriter(java.io.StringWriter) UrlRewriteServletContextListener(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) IOException(java.io.IOException)

Example 29 with UrlRewriteRulesDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.

the class ApplicationDeploymentContributor method loadRewriteRules.

private static UrlRewriteRulesDescriptor loadRewriteRules(Application application, File file) throws IOException {
    UrlRewriteRulesDescriptor rules;
    if (!file.exists()) {
        rules = UrlRewriteRulesDescriptorFactory.load("xml", new StringReader("<rules/>"));
    } else {
        FileReader reader = new FileReader(file);
        rules = UrlRewriteRulesDescriptorFactory.load("xml", reader);
        reader.close();
    }
    return rules;
}
Also used : StringReader(java.io.StringReader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) FileReader(java.io.FileReader)

Example 30 with UrlRewriteRulesDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.

the class ServiceDefinitionDeploymentContributor method contributeRewriteRules.

private void contributeRewriteRules(DeploymentContext context, Service service) {
    if (serviceRules != null) {
        UrlRewriteRulesDescriptor clusterRules = context.getDescriptor("rewrite");
        clusterRules.addRules(serviceRules);
    }
}
Also used : UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)

Aggregations

UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)46 Test (org.junit.Test)38 StringReader (java.io.StringReader)20 UrlRewriteFilterContentDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor)20 UrlRewriteFilterDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)20 Matchers.containsString (org.hamcrest.Matchers.containsString)19 InputStream (java.io.InputStream)16 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)13 UrlRewriteFilterApplyDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor)12 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)11 UrlRewriteFilterBufferDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor)11 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)11 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)9 StringWriter (java.io.StringWriter)8 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)8 Template (org.apache.knox.gateway.util.urltemplate.Template)8 URL (java.net.URL)7 Source (javax.xml.transform.Source)6 IOException (java.io.IOException)5 GatewayServices (org.apache.knox.gateway.services.GatewayServices)5