use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.
the class ServiceRegistryFunctionsTest method setUp.
public void setUp(String username, Map<String, String> initParams) 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 = getTestResource("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);
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;
}
use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.
the class GatewayServer method internalDeactivateTopology.
private synchronized void internalDeactivateTopology(Topology topology) {
log.deactivatingTopology(topology.getName());
String topoName = topology.getName();
String topoPath = "/" + Urls.trimLeadingAndTrailingSlashJoin(config.getGatewayPath(), topoName);
String topoPathSlash = topoPath + "/";
ServiceRegistry sr = getGatewayServices().getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
if (sr != null) {
sr.removeClusterServices(topoName);
}
// Find all the deployed contexts we need to deactivate.
List<WebAppContext> deactivate = new ArrayList<WebAppContext>();
if (deployments != null) {
for (WebAppContext app : deployments.values()) {
String appPath = app.getContextPath();
if (appPath.equals(topoPath) || appPath.startsWith(topoPathSlash)) {
deactivate.add(app);
}
}
}
// Deactivate the required deployed contexts.
for (WebAppContext context : deactivate) {
String contextPath = context.getContextPath();
deployments.remove(contextPath);
contexts.removeHandler(context);
try {
context.stop();
} catch (Exception e) {
auditor.audit(Action.UNDEPLOY, topology.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
log.failedToUndeployTopology(topology.getName(), e);
}
}
deactivate.clear();
}
use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.
the class GatewayWebsocketHandler method getMatchedBackendURL.
/**
* This method looks at the context path and returns the backend websocket
* url. If websocket url is found it is used as is, or we default to
* ws://{host}:{port} which might or might not be right.
*
* @param
* @return Websocket backend url
*/
private synchronized String getMatchedBackendURL(final String path) {
final ServiceRegistry serviceRegistryService = services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
final ServiceDefinitionRegistry serviceDefinitionService = services.getService(GatewayServices.SERVICE_DEFINITION_REGISTRY);
/* Filter out the /cluster/topology to get the context we want */
String[] pathInfo = path.split(REGEX_SPLIT_CONTEXT);
final ServiceDefEntry entry = serviceDefinitionService.getMatchingService(pathInfo[1]);
if (entry == null) {
throw new RuntimeException(String.format("Cannot find service for the given path: %s", path));
}
/* Filter out /cluster/topology/service to get endpoint */
String[] pathService = path.split(REGEX_SPLIT_SERVICE_PATH);
final File servicesDir = new File(config.getGatewayServicesDir());
final Set<ServiceDefinition> serviceDefs = ServiceDefinitionsLoader.getServiceDefinitions(servicesDir);
/* URL used to connect to websocket backend */
String backendURL = urlFromServiceDefinition(serviceDefs, serviceRegistryService, entry, path);
StringBuffer backend = new StringBuffer();
try {
/* if we do not find websocket URL we default to HTTP */
if (!StringUtils.containsAny(backendURL, WEBSOCKET_PROTOCOL_STRING, SECURE_WEBSOCKET_PROTOCOL_STRING)) {
URL serviceUrl = new URL(backendURL);
/* Use http host:port if ws url not configured */
final String protocol = (serviceUrl.getProtocol() == "ws" || serviceUrl.getProtocol() == "wss") ? serviceUrl.getProtocol() : "ws";
backend.append(protocol).append("://");
backend.append(serviceUrl.getHost()).append(":");
backend.append(serviceUrl.getPort()).append("/");
backend.append(serviceUrl.getPath());
} else {
URI serviceUri = new URI(backendURL);
backend.append(serviceUri);
/* Avoid Zeppelin Regression - as this would require ambari changes and break current knox websocket use case*/
if (!StringUtils.endsWith(backend.toString(), "/ws") && pathService.length > 0 && pathService[1] != null) {
backend.append(pathService[1]);
}
}
backendURL = backend.toString();
} catch (MalformedURLException e) {
LOG.badUrlError(e);
throw new RuntimeException(e.toString());
} catch (Exception e1) {
LOG.failedCreatingWebSocket(e1);
throw new RuntimeException(e1.toString());
}
return backendURL;
}
use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.
the class OozieServiceDefinitionTest method testOozieRewriteRulesForLiteralComplexTemplateValuesBugKnox394.
@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForLiteralComplexTemplateValuesBugKnox394() throws Exception {
LOG_ENTER();
// This is a unique part of this test.
String testResource = "oozie-request-with-complex-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("${nameNode}/user/${user.name}/${examplesRoot}/apps/hive")));
LOG_EXIT();
}
use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.
the class OozieServiceDefinitionTest method testOozieRewriteRulesForValuesRelativeToServiceRegistry.
@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForValuesRelativeToServiceRegistry() throws Exception {
LOG_ENTER();
// This is a unique part of this test.
String testResource = "oozie-request-relative.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();
EasyMock.expect(servletRequest.getContentLength()).andReturn(-1).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("test-scheme://test-host:42/workflow.xml")));
LOG_EXIT();
}
Aggregations