Search in sources :

Example 1 with Application

use of org.apache.knox.gateway.topology.Application in project knox by apache.

the class DeploymentFactory method validateNoAppsWithDuplicateUrlsInTopology.

// Verify that there are no two apps with duplicate urls.
static void validateNoAppsWithDuplicateUrlsInTopology(Topology topology) {
    if (topology != null) {
        Collection<Application> apps = topology.getApplications();
        if (apps != null) {
            for (Application app : apps) {
                List<String> urls = app.getUrls();
                if (urls == null || urls.isEmpty()) {
                    urls = new ArrayList<String>(1);
                    urls.add(app.getName());
                }
                for (String url : urls) {
                    List<Application> dups = findApplicationsByUrl(topology, url);
                    if (dups != null) {
                        for (Application dup : dups) {
                            if (dup != app) {
                                throw new DeploymentException("Topology " + topology.getName() + " contains applications " + app.getName() + " and " + dup.getName() + " with the same url: " + url);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Application(org.apache.knox.gateway.topology.Application)

Example 2 with Application

use of org.apache.knox.gateway.topology.Application in project knox by apache.

the class DeploymentFactory method findApplicationsByUrl.

static List<Application> findApplicationsByUrl(Topology topology, String url) {
    List<Application> foundApps = new ArrayList<Application>();
    if (topology != null) {
        url = Urls.trimLeadingAndTrailingSlash(url);
        Collection<Application> searchApps = topology.getApplications();
        if (searchApps != null) {
            for (Application searchApp : searchApps) {
                List<String> searchUrls = searchApp.getUrls();
                if (searchUrls == null || searchUrls.isEmpty()) {
                    searchUrls = new ArrayList<String>(1);
                    searchUrls.add(searchApp.getName());
                }
                for (String searchUrl : searchUrls) {
                    if (url.equalsIgnoreCase(Urls.trimLeadingAndTrailingSlash(searchUrl))) {
                        foundApps.add(searchApp);
                        break;
                    }
                }
            }
        }
    }
    return foundApps;
}
Also used : ArrayList(java.util.ArrayList) Application(org.apache.knox.gateway.topology.Application)

Example 3 with Application

use of org.apache.knox.gateway.topology.Application in project knox by apache.

the class DeploymentFactory method selectContextApplications.

private static Map<String, ServiceDeploymentContributor> selectContextApplications(GatewayConfig config, Topology topology) {
    Map<String, ServiceDeploymentContributor> contributors = new HashMap<>();
    if (topology != null) {
        for (Application application : topology.getApplications()) {
            String name = application.getName();
            if (name == null || name.isEmpty()) {
                throw new DeploymentException("Topologies cannot contain an application without a name.");
            }
            ApplicationDeploymentContributor contributor = new ApplicationDeploymentContributor(config, application);
            List<String> urls = application.getUrls();
            if (urls == null || urls.isEmpty()) {
                urls = new ArrayList<String>(1);
                urls.add("/" + name);
            }
            for (String url : urls) {
                if (url == null || url.isEmpty() || url.equals("/")) {
                    if (!topology.getServices().isEmpty()) {
                        throw new DeploymentException(String.format("Topologies with services cannot contain an application (%s) with a root url.", name));
                    }
                }
                contributors.put(url, contributor);
            }
        }
    }
    return contributors;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Application(org.apache.knox.gateway.topology.Application) ApplicationDeploymentContributor(org.apache.knox.gateway.deploy.impl.ApplicationDeploymentContributor)

Example 4 with Application

use of org.apache.knox.gateway.topology.Application in project knox by apache.

the class DeploymentFactoryTest method test_validateNoAppsWithDuplicateUrlsInTopology.

@Test(timeout = TestUtils.SHORT_TIMEOUT)
public void test_validateNoAppsWithDuplicateUrlsInTopology() {
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(null);
    Topology topology = new Topology();
    topology.setName("test-topology");
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    Application application;
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-2");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("/test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-2");
    topology.addApplication(application);
    DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("/test-application-dup");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-dup");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("/");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-1");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-1");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    application = new Application();
    application.setName("test-application-1");
    application.addUrl("/test-application-1");
    application.addUrl("/test-application-3");
    topology.addApplication(application);
    application = new Application();
    application.setName("test-application-2");
    application.addUrl("/test-application-2");
    application.addUrl("/test-application-3");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithDuplicateUrlsInTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
}
Also used : Topology(org.apache.knox.gateway.topology.Topology) Application(org.apache.knox.gateway.topology.Application) Test(org.junit.Test)

Example 5 with Application

use of org.apache.knox.gateway.topology.Application in project knox by apache.

the class DeploymentFactoryTest method test_validateNoAppsWithRootUrlsInServicesTopology.

@Test(timeout = TestUtils.SHORT_TIMEOUT)
public void test_validateNoAppsWithRootUrlsInServicesTopology() {
    DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(null);
    Topology topology = new Topology();
    topology.setName("test-topology");
    DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
    Service service;
    Application application;
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    topology.addApplication(application);
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
}
Also used : Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Application(org.apache.knox.gateway.topology.Application) Test(org.junit.Test)

Aggregations

Application (org.apache.knox.gateway.topology.Application)11 Topology (org.apache.knox.gateway.topology.Topology)7 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 File (java.io.File)3 URL (java.net.URL)3 Service (org.apache.knox.gateway.topology.Service)3 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)2 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)2 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)2 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)2 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)2 Document (org.w3c.dom.Document)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1