Search in sources :

Example 1 with GatewayDescriptor

use of org.apache.knox.gateway.descriptor.GatewayDescriptor in project knox by apache.

the class GatewayServlet method createFilter.

private static GatewayFilter createFilter(InputStream stream, ServletContext servletContext) throws ServletException {
    try {
        GatewayFilter filter = null;
        if (stream != null) {
            try {
                GatewayDescriptor descriptor = GatewayDescriptorFactory.load("xml", new InputStreamReader(stream));
                filter = GatewayFactory.create(descriptor);
            } finally {
                stream.close();
            }
        }
        GatewayConfig gatewayConfig = (GatewayConfig) servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
        if (gatewayConfig.isMetricsEnabled()) {
            GatewayServices gatewayServices = (GatewayServices) servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
            MetricsService metricsService = gatewayServices.getService(GatewayServices.METRICS_SERVICE);
            if (metricsService != null) {
                GatewayFilter instrumentedFilter = metricsService.getInstrumented(filter);
                if (instrumentedFilter != null) {
                    filter = instrumentedFilter;
                }
            }
        }
        return filter;
    } catch (IOException e) {
        throw new ServletException(e);
    } catch (URISyntaxException e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) GatewayServices(org.apache.knox.gateway.services.GatewayServices) InputStreamReader(java.io.InputStreamReader) MetricsService(org.apache.knox.gateway.services.metrics.MetricsService) GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) AbstractGatewayFilter(org.apache.knox.gateway.filter.AbstractGatewayFilter) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

Example 2 with GatewayDescriptor

use of org.apache.knox.gateway.descriptor.GatewayDescriptor in project knox by apache.

the class XmlGatewayDescriptorExporterTest method testXmlGatewayDescriptorStoreFailure.

@Test
public void testXmlGatewayDescriptorStoreFailure() throws IOException {
    GatewayDescriptor descriptor = GatewayDescriptorFactory.create().addResource().addFilter().param().up().up().up();
    Console console = new Console();
    try {
        console.capture();
        GatewayDescriptorFactory.store(descriptor, "xml", new BrokenWriter());
        fail("Expected IOException");
    } catch (IOException e) {
        assertThat(e.getMessage(), containsString("BROKEN"));
    } finally {
        console.release();
    }
}
Also used : GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) Console(org.apache.knox.test.Console) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with GatewayDescriptor

use of org.apache.knox.gateway.descriptor.GatewayDescriptor in project knox by apache.

the class XmlGatewayDescriptorExporterTest method testXmlGatewayDescriptorStore.

@Test
public void testXmlGatewayDescriptorStore() throws IOException, SAXException, ParserConfigurationException {
    GatewayDescriptor descriptor = GatewayDescriptorFactory.create().addResource().pattern("resource1-source").addFilter().role("resource1-filter1-role").impl("resource1-filter1-impl").param().name("resource1-filter1-param1-name").value("resource1-filter1-param1-value").up().param().name("resource1-filter1-param2-name").value("resource1-filter1-param2-value").up().up().addFilter().name("resource1-filter2-name").role("resource1-filter2-role").impl("resource1-filter2-impl").up().up();
    CharArrayWriter writer = new CharArrayWriter();
    GatewayDescriptorFactory.store(descriptor, "xml", writer);
    String xml = writer.toString();
    InputSource source = new InputSource(new StringReader(xml));
    Document doc = XmlUtils.readXml(source);
    assertThat(doc, hasXPath("/gateway/resource[1]/pattern", is("resource1-source")));
    // assertThat( doc, hasXPath( "/gateway/resource[1]/target", is( "resource1-target" ) ) );
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[1]/name", is("")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[1]/role", is("resource1-filter1-role")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[1]/class", is("resource1-filter1-impl")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[1]/param[1]/name", is("resource1-filter1-param1-name")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[1]/param[1]/value", is("resource1-filter1-param1-value")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[1]/param[2]/name", is("resource1-filter1-param2-name")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[1]/param[2]/value", is("resource1-filter1-param2-value")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[2]/name", is("resource1-filter2-name")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[2]/role", is("resource1-filter2-role")));
    assertThat(doc, hasXPath("/gateway/resource[1]/filter[2]/class", is("resource1-filter2-impl")));
}
Also used : InputSource(org.xml.sax.InputSource) GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) StringReader(java.io.StringReader) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.w3c.dom.Document) CharArrayWriter(java.io.CharArrayWriter) Test(org.junit.Test)

Example 4 with GatewayDescriptor

use of org.apache.knox.gateway.descriptor.GatewayDescriptor in project knox by apache.

the class XmlGatewayDescriptorExporterTest method testXmlGatewayDescriptorStoreMissingValue.

@Test
public void testXmlGatewayDescriptorStoreMissingValue() throws IOException, SAXException, ParserConfigurationException {
    GatewayDescriptor descriptor = GatewayDescriptorFactory.create().addResource().addFilter().param().up().up().up();
    CharArrayWriter writer = new CharArrayWriter();
    GatewayDescriptorFactory.store(descriptor, "xml", writer);
    String xml = writer.toString();
    // System.out.println( xml );
    InputSource source = new InputSource(new StringReader(xml));
    Document doc = XmlUtils.readXml(source);
    assertThat(doc, hasXPath("/gateway/resource/filter/param"));
}
Also used : InputSource(org.xml.sax.InputSource) GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) StringReader(java.io.StringReader) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.w3c.dom.Document) CharArrayWriter(java.io.CharArrayWriter) Test(org.junit.Test)

Example 5 with GatewayDescriptor

use of org.apache.knox.gateway.descriptor.GatewayDescriptor in project knox by apache.

the class JerseyDeploymentContributorTest method testDeploymentContributors.

@Test
public void testDeploymentContributors() throws Exception {
    JerseyDispatchDeploymentContributor providerContributor = new JerseyDispatchDeploymentContributor();
    assertThat(providerContributor.getRole(), is("pivot"));
    assertThat(providerContributor.getName(), is("jersey"));
    MockJerseyService serviceContributor = new MockJerseyService();
    WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-archive");
    Topology topology = new Topology();
    topology.setName("test-topology");
    Provider provider = new Provider();
    provider.setRole("pivot");
    provider.setName("jersey");
    provider.setEnabled(true);
    topology.addProvider(provider);
    GatewayDescriptor descriptor = GatewayDescriptorFactory.create();
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getWebArchive()).andReturn(webArchive).anyTimes();
    EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes();
    EasyMock.expect(context.getGatewayDescriptor()).andReturn(descriptor).anyTimes();
    context.contributeFilter(EasyMock.<Service>isA(Service.class), EasyMock.<ResourceDescriptor>isA(ResourceDescriptor.class), EasyMock.<String>isA(String.class), EasyMock.<String>isA(String.class), EasyMock.<List>isA(List.class));
    EasyMock.expectLastCall().andDelegateTo(new MockDeploymentContext(context, providerContributor, provider)).anyTimes();
    EasyMock.replay(context);
    // Just make sure they don't blow up.
    providerContributor.initializeContribution(context);
    serviceContributor.initializeContribution(context);
    Service service = new Service();
    service.setRole("test-service-role");
    service.setName("test-service-name");
    service.addUrl("http://test-service-host:777/test-service-path");
    // This should end up calling providerContributor.contributeFilter
    serviceContributor.contributeService(context, service);
    ResourceDescriptor resource = context.getGatewayDescriptor().resources().get(0);
    // Just make sure they don't blow up.
    serviceContributor.finalizeContribution(context);
    providerContributor.finalizeContribution(context);
    /*
    GatewayDescriptorFactory.store( descriptor, "xml", new PrintWriter( System.out ) );
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <gateway>
      <resource>
        <role>test-service-role</role>
        <pattern>test-service/?**</pattern>
        <filter>
          <role>dispatch</role>
          <name>jersey</name>
          <class>org.glassfish.jersey.servlet.ServletContainer</class>
          <param>
            <name>jersey.config.server.provider.packages</name>
            <value>test-package-1;test-package-2</value>
          </param>
        </filter>
      </resource>
      <resource>
        <role>test-service-role</role>
        <pattern>test-service/**?**</pattern>
        <filter>
          <role>dispatch</role>
          <name>jersey</name>
          <class>org.glassfish.jersey.servlet.ServletContainer</class>
          <param>
            <name>jersey.config.server.provider.packages</name>
            <value>test-package-1;test-package-2</value>
          </param>
        </filter>
      </resource>
    </gateway>
    */
    List<ResourceDescriptor> resources = context.getGatewayDescriptor().resources();
    assertThat(resources.size(), is(2));
    resource = resources.get(0);
    assertThat(resource.role(), is("test-service-role"));
    assertThat(resource.pattern(), is("test-service/?**"));
    List<FilterDescriptor> filters = resource.filters();
    assertThat(filters.size(), is(1));
    FilterDescriptor filter = filters.get(0);
    assertThat(filter.role(), is("pivot"));
    assertThat(filter.name(), is("jersey"));
    assertThat(filter.impl(), is("org.glassfish.jersey.servlet.ServletContainer"));
    List<FilterParamDescriptor> params = filter.params();
    assertThat(params.size(), is(1));
    FilterParamDescriptor param = params.get(0);
    assertThat(param.name(), is("jersey.config.server.provider.packages"));
    assertThat(param.value(), is("test-package-1;test-package-2"));
    resource = resources.get(1);
    assertThat(resource.role(), is("test-service-role"));
    assertThat(resource.pattern(), is("test-service/**?**"));
    filters = resource.filters();
    assertThat(filters.size(), is(1));
    filter = filters.get(0);
    assertThat(filter.role(), is("pivot"));
    assertThat(filter.name(), is("jersey"));
    assertThat(filter.impl(), is("org.glassfish.jersey.servlet.ServletContainer"));
    params = filter.params();
    assertThat(params.size(), is(1));
    param = params.get(0);
    assertThat(param.name(), is("jersey.config.server.provider.packages"));
    assertThat(param.value(), is("test-package-1;test-package-2"));
}
Also used : GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) 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) FilterDescriptor(org.apache.knox.gateway.descriptor.FilterDescriptor) List(java.util.List) ResourceDescriptor(org.apache.knox.gateway.descriptor.ResourceDescriptor) Test(org.junit.Test)

Aggregations

GatewayDescriptor (org.apache.knox.gateway.descriptor.GatewayDescriptor)10 Test (org.junit.Test)6 StringReader (java.io.StringReader)4 FilterParamDescriptor (org.apache.knox.gateway.descriptor.FilterParamDescriptor)4 ResourceDescriptor (org.apache.knox.gateway.descriptor.ResourceDescriptor)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 IOException (java.io.IOException)3 FilterDescriptor (org.apache.knox.gateway.descriptor.FilterDescriptor)3 CharArrayWriter (java.io.CharArrayWriter)2 Reader (java.io.Reader)2 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)2 Document (org.w3c.dom.Document)2 InputSource (org.xml.sax.InputSource)2 InputStreamReader (java.io.InputStreamReader)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ServletException (javax.servlet.ServletException)1 Digester (org.apache.commons.digester3.Digester)1 ExtendedBaseRules (org.apache.commons.digester3.ExtendedBaseRules)1