AmFilterInitializer extends FilterInitializer { @Override public void initFilter(FilterContainer container, Configuration conf) { Map<String, String> params = new HashMap<>(); List<String> proxies = WebAppUtils.getProxyHostsAndPortsForAmFilter(conf); StringBuilder sb = new StringBuilder(); for (String proxy : proxies) { sb.append(proxy.split(":")[0]).append(AmIpFilter.PROXY_HOSTS_DELIMITER); } sb.setLength(sb.length() - 1); params.put(AmIpFilter.PROXY_HOSTS, sb.toString()); String prefix = WebAppUtils.getHttpSchemePrefix(conf); String proxyBase = getApplicationWebProxyBase(); sb = new StringBuilder(); for (String proxy : proxies) { sb.append(prefix).append(proxy).append(proxyBase) .append(AmIpFilter.PROXY_HOSTS_DELIMITER); } sb.setLength(sb.length() - 1); params.put(AmIpFilter.PROXY_URI_BASES, sb.toString()); container.addFilter(FILTER_NAME, FILTER_CLASS, params); }  @Override void initFilter(FilterContainer container, Configuration conf);  }
@Test public void testInitFilter() { MockFilterContainer con = new MockFilterContainer(); Configuration conf = new Configuration(false); conf.set(YarnConfiguration.PROXY_ADDRESS, "host1:1000"); AmFilterInitializer afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); assertEquals(2, con.givenParameters.size()); assertEquals("host1", con.givenParameters.get(AmIpFilter.PROXY_HOSTS)); assertEquals("http: con.givenParameters.get(AmIpFilter.PROXY_URI_BASES)); con = new MockFilterContainer(); conf = new Configuration(false); conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS, "host2:2000"); afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); assertEquals(2, con.givenParameters.size()); assertEquals("host2", con.givenParameters.get(AmIpFilter.PROXY_HOSTS)); assertEquals("http: con.givenParameters.get(AmIpFilter.PROXY_URI_BASES)); con = new MockFilterContainer(); conf = new Configuration(false); conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true); conf.set(YarnConfiguration.RM_HA_IDS, "rm1,rm2,rm3"); conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm1", "host2:2000"); conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm2", "host3:3000"); conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm3", "host4:4000"); afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); assertEquals(2, con.givenParameters.size()); String[] proxyHosts = con.givenParameters.get(AmIpFilter.PROXY_HOSTS) .split(AmIpFilter.PROXY_HOSTS_DELIMITER); assertEquals(3, proxyHosts.length); Arrays.sort(proxyHosts); assertEquals("host2", proxyHosts[0]); assertEquals("host3", proxyHosts[1]); assertEquals("host4", proxyHosts[2]); String[] proxyBases = con.givenParameters.get(AmIpFilter.PROXY_URI_BASES) .split(AmIpFilter.PROXY_URI_BASES_DELIMITER); assertEquals(3, proxyBases.length); Arrays.sort(proxyBases); assertEquals("http: assertEquals("http: assertEquals("http: con = new MockFilterContainer(); conf = new Configuration(false); conf.set(YarnConfiguration.YARN_HTTP_POLICY_KEY, HttpConfig.Policy.HTTPS_ONLY.toString()); conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true); conf.set(YarnConfiguration.RM_HA_IDS, "rm1,rm2"); conf.set(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS + ".rm1", "host5:5000"); conf.set(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS + ".rm2", "host6:6000"); afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); assertEquals(2, con.givenParameters.size()); proxyHosts = con.givenParameters.get(AmIpFilter.PROXY_HOSTS) .split(AmIpFilter.PROXY_HOSTS_DELIMITER); assertEquals(2, proxyHosts.length); Arrays.sort(proxyHosts); assertEquals("host5", proxyHosts[0]); assertEquals("host6", proxyHosts[1]); proxyBases = con.givenParameters.get(AmIpFilter.PROXY_URI_BASES) .split(AmIpFilter.PROXY_URI_BASES_DELIMITER); assertEquals(2, proxyBases.length); Arrays.sort(proxyBases); assertEquals("https: assertEquals("https: }