Search in sources :

Example 1 with HostmapFunctionDescriptor

use of org.apache.knox.gateway.hostmap.api.HostmapFunctionDescriptor in project knox by apache.

the class HostmapDeploymentContributor method contributeProvider.

// Write the provider init params to the hostmap.txt file.
// Add the function to the rewrite descriptor providing the location of the hostmap.txt file.
@Override
public void contributeProvider(DeploymentContext context, Provider provider) {
    if (provider.isEnabled()) {
        UrlRewriteRulesDescriptor rules = context.getDescriptor(REWRITE_ROLE_NAME);
        if (rules != null) {
            HostmapFunctionDescriptor func = rules.addFunction(HostmapFunctionDescriptor.FUNCTION_NAME);
            if (func != null) {
                Asset asset = createAsset(provider);
                context.getWebArchive().addAsWebInfResource(asset, HostmapFunctionProcessor.DESCRIPTOR_DEFAULT_FILE_NAME);
                func.config(HostmapFunctionProcessor.DESCRIPTOR_DEFAULT_LOCATION);
            }
        }
    }
}
Also used : HostmapFunctionDescriptor(org.apache.knox.gateway.hostmap.api.HostmapFunctionDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) Asset(org.jboss.shrinkwrap.api.asset.Asset)

Example 2 with HostmapFunctionDescriptor

use of org.apache.knox.gateway.hostmap.api.HostmapFunctionDescriptor in project knox by apache.

the class HostmapDeploymentContributorTest method testDeployment.

@Test
public void testDeployment() throws IOException {
    WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-acrhive");
    UrlRewriteRulesDescriptorImpl rewriteRules = new UrlRewriteRulesDescriptorImpl();
    Map<String, String> providerParams = new HashMap<>();
    providerParams.put("test-host-external", "test-host-internal");
    Provider provider = new Provider();
    provider.setEnabled(true);
    provider.setName("hostmap");
    provider.setParams(providerParams);
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getDescriptor("rewrite")).andReturn(rewriteRules).anyTimes();
    EasyMock.expect(context.getWebArchive()).andReturn(webArchive).anyTimes();
    EasyMock.replay(context);
    HostmapDeploymentContributor contributor = new HostmapDeploymentContributor();
    assertThat(contributor.getRole(), is("hostmap"));
    assertThat(contributor.getName(), is("static"));
    // Just make sure it doesn't blow up.
    contributor.contributeFilter(null, null, null, null, null);
    // Just make sure it doesn't blow up.
    contributor.initializeContribution(context);
    contributor.contributeProvider(context, provider);
    HostmapFunctionDescriptor funcDesc = rewriteRules.getFunction("hostmap");
    assertThat(funcDesc.config(), is("/WEB-INF/hostmap.txt"));
    Node node = webArchive.get("/WEB-INF/hostmap.txt");
    String asset = IOUtils.toString(node.getAsset().openStream());
    assertThat(asset, containsString("test-host-external=test-host-internal"));
    // Just make sure it doesn't blow up.
    contributor.finalizeContribution(context);
}
Also used : DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) UrlRewriteRulesDescriptorImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRulesDescriptorImpl) HashMap(java.util.HashMap) HostmapFunctionDescriptor(org.apache.knox.gateway.hostmap.api.HostmapFunctionDescriptor) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) Node(org.jboss.shrinkwrap.api.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Provider(org.apache.knox.gateway.topology.Provider) Test(org.junit.Test)

Aggregations

HostmapFunctionDescriptor (org.apache.knox.gateway.hostmap.api.HostmapFunctionDescriptor)2 HashMap (java.util.HashMap)1 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)1 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)1 UrlRewriteRulesDescriptorImpl (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRulesDescriptorImpl)1 Provider (org.apache.knox.gateway.topology.Provider)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Node (org.jboss.shrinkwrap.api.Node)1 Asset (org.jboss.shrinkwrap.api.asset.Asset)1 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)1 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)1 Test (org.junit.Test)1