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 = new HttpTester();
    response = new HttpTester();
  }
Example #2
0
 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
   GatewayServices services =
       (GatewayServices)
           filterConfig
               .getServletContext()
               .getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
   authority = (JWTokenAuthority) services.getService(GatewayServices.TOKEN_SERVICE);
 }
  @Test
  public void testBasicUseCase() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hostmap.txt");

    HostMapper hm = EasyMock.createNiceMock(HostMapper.class);
    EasyMock.expect(hm.resolveInboundHostName("test-inbound-host"))
        .andReturn("test-inbound-rewritten-host")
        .anyTimes();

    HostMapperService hms = EasyMock.createNiceMock(HostMapperService.class);

    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.HOST_MAPPING_SERVICE))
        .andReturn(hms)
        .anyTimes();

    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE))
        .andReturn(gatewayServices)
        .anyTimes();
    EasyMock.expect(environment.resolve("cluster.name"))
        .andReturn(Arrays.asList("test-cluster-name"))
        .anyTimes();
    EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt"))
        .andReturn(configUrl)
        .anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("host"))
        .andReturn(Arrays.asList("test-inbound-host"))
        .anyTimes();
    EasyMock.replay(gatewayServices, hm, hms, environment, resolver);

    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://{$hostmap(host)}:{*}/{**}?{**}");

    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);

    Template input =
        Parser.parseLiteral(
            "test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("test-inbound-rewritten-host"));
  }