Search in sources :

Example 6 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class DeploymentFactory method contributeServices.

private static void contributeServices(DeploymentContext context, Topology topology, Map<String, List<ServiceDeploymentContributor>> services) {
    if (services != null) {
        for (Service service : topology.getServices()) {
            ServiceDeploymentContributor contributor = getServiceContributor(service.getRole(), service.getName(), service.getVersion());
            if (contributor != null) {
                try {
                    log.contributeService(service.getName(), service.getRole());
                    contributor.contributeService(context, service);
                    if (gatewayServices != null) {
                        ServiceRegistry sr = gatewayServices.getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
                        if (sr != null) {
                            String regCode = sr.getRegistrationCode(topology.getName());
                            sr.registerService(regCode, topology.getName(), service.getRole(), service.getUrls());
                        }
                    }
                } catch (Exception e) {
                    // Maybe it makes sense to throw exception
                    log.failedToContributeService(service.getName(), service.getRole(), e);
                    throw new DeploymentException("Failed to contribute service.", e);
                }
            }
        }
    }
}
Also used : Service(org.apache.knox.gateway.topology.Service) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 7 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class HaServletContextListener method setupHaProvider.

private void setupHaProvider(HaDescriptor descriptor, ServletContext servletContext) {
    GatewayServices services = (GatewayServices) servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    String clusterName = (String) servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE);
    ServiceRegistry serviceRegistry = services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
    HaProvider provider = new DefaultHaProvider(descriptor);
    List<String> serviceNames = descriptor.getEnabledServiceNames();
    for (String serviceName : serviceNames) {
        provider.addHaService(serviceName, serviceRegistry.lookupServiceURLs(clusterName, serviceName));
    }
    servletContext.setAttribute(PROVIDER_ATTRIBUTE_NAME, provider);
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) DefaultHaProvider(org.apache.knox.gateway.ha.provider.impl.DefaultHaProvider) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) DefaultHaProvider(org.apache.knox.gateway.ha.provider.impl.DefaultHaProvider)

Example 8 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class FrontendFunctionProcessorTest method setUp.

public void setUp(String username, Map<String, String> initParams, Attributes attributes) throws Exception {
    ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-nn-scheme://test-nn-host:411").anyTimes();
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "JOBTRACKER")).andReturn("test-jt-scheme://test-jt-host:511").anyTimes();
    GatewayServices mockGatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(mockGatewayServices.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(mockServiceRegistry).anyTimes();
    EasyMock.replay(mockServiceRegistry, mockGatewayServices);
    String descriptorUrl = TestUtils.getResourceUrl(FrontendFunctionProcessorTest.class, "rewrite.xml").toExternalForm();
    Log.setLog(new NoOpLogger());
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    if (attributes != null) {
        server.getContext().setAttributes(attributes);
    }
    server.getContext().setAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster");
    server.getContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices);
    FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter(username));
    FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
        for (Map.Entry<String, String> entry : initParams.entrySet()) {
            rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
        }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayQueue<MockInteraction>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) UrlRewriteServletContextListener(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener) MockServlet(org.apache.knox.test.mock.MockServlet) UrlRewriteServletFilter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NoOpLogger(org.apache.knox.test.log.NoOpLogger) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Map(java.util.Map) AttributesMap(org.eclipse.jetty.util.AttributesMap) HashMap(java.util.HashMap)

Example 9 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class OozieServiceDefinitionTest method testOozieRewriteRulesForLiteralTemplateValuesBugKnox394.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForLiteralTemplateValuesBugKnox394() throws Exception {
    LOG_ENTER();
    // This is a unique part of this test.
    String testResource = "oozie-request-with-var.xml";
    // Mock out the service url registry which is required for several url rewrite functions to work.
    ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
    // Mock out the gateway services registry which is required for several url rewrite functions to work.
    GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
    UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
    EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
    EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
    UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
    Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", "UTF-8");
    UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
    rulesReader.close();
    rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
    UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
    InputStream stream = rewriteRequest.getInputStream();
    Document document = XmlUtils.readXml(stream);
    assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("${appPath}/workflow.xml")));
    LOG_EXIT();
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteRequest(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteServletEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Test(org.junit.Test)

Aggregations

ServiceRegistry (org.apache.knox.gateway.services.registry.ServiceRegistry)9 GatewayServices (org.apache.knox.gateway.services.GatewayServices)6 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 FilterConfig (javax.servlet.FilterConfig)3 ServletContext (javax.servlet.ServletContext)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)3 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)3 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)3 UrlRewriteServletEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment)3 UrlRewriteRequest (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest)3 MockServletInputStream (org.apache.knox.test.mock.MockServletInputStream)3 Test (org.junit.Test)3 Document (org.w3c.dom.Document)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UrlRewriteServletContextListener (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener)2 UrlRewriteServletFilter (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter)2