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);
}
}
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();
}
}
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")));
}
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"));
}
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"));
}
Aggregations