Search in sources :

Example 11 with UrlRewriteRulesDescriptor

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

the class XmlUrlRewriteRulesExporterTest method testControlStep.

@Test
public void testControlStep() throws Exception {
    UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = rules.addRule("test-rule");
    UrlRewriteControlDescriptor control = rule.addStep("control");
    control.flow("or");
    StringWriter writer = new StringWriter();
    UrlRewriteRulesDescriptorFactory.store(rules, "xml", writer);
    String str = writer.toString();
    // System.out.println( str );
    Source xml = XmlConverters.the(str);
    assertThat(xml, XmlMatchers.hasXPath("/rules"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule"));
    assertThat(xml, XmlMatchers.hasXPath("count(/rules/rule)", is("1")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/@name", is("test-rule")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/control"));
    assertThat(xml, XmlMatchers.hasXPath("count(/rules/rule/control)", is("1")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/control/@flow", is("OR")));
}
Also used : UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) StringWriter(java.io.StringWriter) UrlRewriteControlDescriptor(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteControlDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 12 with UrlRewriteRulesDescriptor

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

the class XmlUrlRewriteRulesExporterTest method testEmptyRules.

@Test
public void testEmptyRules() throws IOException {
    UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
    StringWriter writer = new StringWriter();
    UrlRewriteRulesDescriptorFactory.store(rules, "xml", writer);
    String xml = writer.toString();
    assertThat(XmlConverters.the(xml), XmlMatchers.hasXPath("/rules"));
}
Also used : StringWriter(java.io.StringWriter) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Test(org.junit.Test)

Example 13 with UrlRewriteRulesDescriptor

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

the class ServiceDefinitionDeploymentContributorTest method testServiceAttributeUseTwoWaySSLParamOverride.

/**
 * Test that service param useTwoWaySsl in topologies overrides the corresponding custom dispatch property.
 */
@Test
public void testServiceAttributeUseTwoWaySSLParamOverride() throws Exception {
    final String TEST_SERVICE_ROLE = "Test";
    final String USE_TWO_WAY_SSL_PARAM = "useTwoWaySsl";
    UrlRewriteRulesDescriptor clusterRules = EasyMock.createNiceMock(UrlRewriteRulesDescriptor.class);
    EasyMock.replay(clusterRules);
    UrlRewriteRulesDescriptor svcRules = EasyMock.createNiceMock(UrlRewriteRulesDescriptor.class);
    EasyMock.replay(svcRules);
    ServiceDefinition svcDef = EasyMock.createNiceMock(ServiceDefinition.class);
    EasyMock.expect(svcDef.getRole()).andReturn(TEST_SERVICE_ROLE).anyTimes();
    List<Route> svcRoutes = new ArrayList<>();
    Route route = EasyMock.createNiceMock(Route.class);
    List<Rewrite> filters = new ArrayList<>();
    EasyMock.expect(route.getRewrites()).andReturn(filters).anyTimes();
    svcRoutes.add(route);
    EasyMock.replay(route);
    EasyMock.expect(svcDef.getRoutes()).andReturn(svcRoutes).anyTimes();
    CustomDispatch cd = EasyMock.createNiceMock(CustomDispatch.class);
    EasyMock.expect(cd.getClassName()).andReturn("TestDispatch").anyTimes();
    EasyMock.expect(cd.getHaClassName()).andReturn("TestHADispatch").anyTimes();
    EasyMock.expect(cd.getHaContributorName()).andReturn(null).anyTimes();
    // Let useTwoWaySsl be FALSE by default
    EasyMock.expect(cd.getUseTwoWaySsl()).andReturn(false).anyTimes();
    EasyMock.replay(cd);
    EasyMock.expect(svcDef.getDispatch()).andReturn(cd).anyTimes();
    EasyMock.replay(svcDef);
    ServiceDefinitionDeploymentContributor sddc = new ServiceDefinitionDeploymentContributor(svcDef, svcRules);
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getDescriptor("rewrite")).andReturn(clusterRules).anyTimes();
    GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(gc.isXForwardedEnabled()).andReturn(false).anyTimes();
    EasyMock.expect(gc.isCookieScopingToPathEnabled()).andReturn(false).anyTimes();
    EasyMock.replay(gc);
    EasyMock.expect(context.getGatewayConfig()).andReturn(gc).anyTimes();
    // Configure the HaProvider
    Topology topology = EasyMock.createNiceMock(Topology.class);
    List<Provider> providers = new ArrayList<>();
    Provider haProvider = EasyMock.createNiceMock(Provider.class);
    EasyMock.expect(haProvider.getRole()).andReturn("ha").anyTimes();
    EasyMock.expect(haProvider.isEnabled()).andReturn(true).anyTimes();
    Map<String, String> providerParams = new HashMap<>();
    providerParams.put(TEST_SERVICE_ROLE, "whatever");
    EasyMock.expect(haProvider.getParams()).andReturn(providerParams).anyTimes();
    EasyMock.replay(haProvider);
    providers.add(haProvider);
    EasyMock.expect(topology.getProviders()).andReturn(providers).anyTimes();
    EasyMock.replay(topology);
    EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes();
    TestGatewayDescriptor gd = new TestGatewayDescriptor();
    EasyMock.expect(context.getGatewayDescriptor()).andReturn(gd).anyTimes();
    EasyMock.replay(context);
    // Configure the service with the useTwoWaySsl param to OVERRIDE the value in the service definition
    Service service = EasyMock.createNiceMock(Service.class);
    Map<String, String> svcParams = new HashMap<>();
    svcParams.put(USE_TWO_WAY_SSL_PARAM, "true");
    EasyMock.expect(service.getParams()).andReturn(svcParams).anyTimes();
    EasyMock.replay(service);
    sddc.contributeService(context, service);
    List<ResourceDescriptor> resources = gd.resources();
    assertEquals(1, gd.resources().size());
    ResourceDescriptor res = gd.resources().get(0);
    assertNotNull(res);
    List<FilterDescriptor> filterList = res.filters();
    assertEquals(1, filterList.size());
    FilterDescriptor f = filterList.get(0);
    assertNotNull(f);
    assertEquals("dispatch", f.role());
    List<FilterParamDescriptor> fParams = f.params();
    assertNotNull(fParams);
    // Collect the values of filter params named useTwoWaySsl
    List<String> useTwoWaySslFilterParamValues = new ArrayList<>();
    for (FilterParamDescriptor param : fParams) {
        if (param.name().equals(USE_TWO_WAY_SSL_PARAM)) {
            useTwoWaySslFilterParamValues.add(param.value());
        }
    }
    assertEquals("Expected only a single filter param named " + USE_TWO_WAY_SSL_PARAM, 1, useTwoWaySslFilterParamValues.size());
    assertEquals("Expected the service param to override the service definition value for " + USE_TWO_WAY_SSL_PARAM, "true", useTwoWaySslFilterParamValues.get(0));
}
Also used : CustomDispatch(org.apache.knox.gateway.service.definition.CustomDispatch) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) FilterDescriptor(org.apache.knox.gateway.descriptor.FilterDescriptor) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) Route(org.apache.knox.gateway.service.definition.Route) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) FilterParamDescriptor(org.apache.knox.gateway.descriptor.FilterParamDescriptor) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Provider(org.apache.knox.gateway.topology.Provider) DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) Rewrite(org.apache.knox.gateway.service.definition.Rewrite) ResourceDescriptor(org.apache.knox.gateway.descriptor.ResourceDescriptor) Test(org.junit.Test)

Example 14 with UrlRewriteRulesDescriptor

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

the class ServiceDefinitionsLoader method loadServiceDefinitions.

public static Set<ServiceDeploymentContributor> loadServiceDefinitions(File servicesDir) {
    Set<ServiceDeploymentContributor> contributors = new HashSet<>();
    if (servicesDir.exists() && servicesDir.isDirectory()) {
        try {
            JAXBContext context = JAXBContext.newInstance(ServiceDefinition.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            for (File file : getFileList(servicesDir)) {
                try {
                    FileInputStream inputStream = new FileInputStream(file);
                    ServiceDefinition definition = (ServiceDefinition) unmarshaller.unmarshal(inputStream);
                    // look for rewrite rules as a sibling (for now)
                    UrlRewriteRulesDescriptor rewriteRulesDescriptor = loadRewriteRules(file.getParentFile());
                    contributors.add(new ServiceDefinitionDeploymentContributor(definition, rewriteRulesDescriptor));
                    log.addedServiceDefinition(definition.getName(), definition.getRole(), definition.getVersion());
                } catch (FileNotFoundException e) {
                    log.failedToFindServiceDefinitionFile(file.getAbsolutePath(), e);
                }
            }
        } catch (JAXBException e) {
            log.failedToLoadServiceDefinition(SERVICE_FILE_NAME, e);
        }
    }
    return contributors;
}
Also used : ServiceDefinitionDeploymentContributor(org.apache.knox.gateway.deploy.impl.ServiceDefinitionDeploymentContributor) ServiceDeploymentContributor(org.apache.knox.gateway.deploy.ServiceDeploymentContributor) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet)

Example 15 with UrlRewriteRulesDescriptor

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

the class ServiceDefinitionsLoader method loadRewriteRules.

public static UrlRewriteRulesDescriptor loadRewriteRules(File servicesDir) {
    File rewriteFile = new File(servicesDir, REWRITE_FILE);
    if (rewriteFile.exists()) {
        InputStream stream = null;
        try {
            stream = new FileInputStream(rewriteFile);
            Reader reader = new InputStreamReader(stream);
            UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.load("xml", reader);
            reader.close();
            stream.close();
            return rules;
        } catch (FileNotFoundException e) {
            log.failedToFindRewriteFile(rewriteFile.getAbsolutePath(), e);
        } catch (IOException e) {
            log.failedToLoadRewriteFile(rewriteFile.getAbsolutePath(), e);
        }
    }
    log.noRewriteFileFound(servicesDir.getAbsolutePath());
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

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