Java Code Examples for org.eclipse.jetty.servlet.FilterHolder#setFilter()

The following examples show how to use org.eclipse.jetty.servlet.FilterHolder#setFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UrlRewriteServletContextListenerTest.java    From knox with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  String descriptorUrl = getTestResource( "rewrite.xml" ).toExternalForm();

  server = new ServletTester();
  server.setContextPath( "/" );
  server.getContext().addEventListener( new UrlRewriteServletContextListener() );
  server.getContext().setInitParameter(
      UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl );

  FilterHolder filter = server.addFilter( UrlRewriteServletFilter.class, "/*", EnumSet.of( DispatcherType.REQUEST ) );
  filter.setFilter( new UrlRewriteServletFilter() );

  interactions = new ArrayDeque<>();

  ServletHolder servlet = server.addServlet( MockServlet.class, "/" );
  servlet.setServlet( new MockServlet( "mock-servlet", interactions ) );
  servlet.setInitParameter(
      UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME,
      getTestResource( "rewrite.xml" ).toExternalForm() );

  server.start();
}
 
Example 2
Source File: UsernameFunctionProcessorTest.java    From knox with Apache License 2.0 5 votes vote down vote up
private void testSetup(String username, Map<String,String> initParams ) throws Exception {
  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 );

  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 ArrayDeque<>();

  ServletHolder servlet = server.addServlet( MockServlet.class, "/" );
  servlet.setServlet( new MockServlet( "mock-servlet", interactions ) );

  server.start();

  interaction = new MockInteraction();
  request = HttpTester.newRequest();
  response = null;
}
 
Example 3
Source File: UrlRewriteServletFilterTest.java    From knox with Apache License 2.0 5 votes vote down vote up
private void testSetUp(Map<String,String> initParams ) throws Exception {
  String descriptorUrl = getTestResource( "rewrite.xml" ).toExternalForm();

  server = new ServletTester();
  server.setContextPath( "/" );
  server.getContext().addEventListener( new UrlRewriteServletContextListener() );
  server.getContext().setInitParameter(
      UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl );

  FilterHolder setupFilter = server.addFilter( SetupFilter.class, "/*", EnumSet.of( DispatcherType.REQUEST ) );
  setupFilter.setFilter( new SetupFilter() );
  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 ArrayDeque<>();

  ServletHolder servlet = server.addServlet( MockServlet.class, "/" );
  servlet.setServlet( new MockServlet( "mock-servlet", interactions ) );

  server.start();

  interaction = new MockInteraction();
  request = HttpTester.newRequest();
  response = null;
}
 
Example 4
Source File: ServiceRegistryFunctionsTest.java    From knox with Apache License 2.0 4 votes vote down vote up
private void testSetup(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(ServiceType.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 ArrayDeque<>();

  ServletHolder servlet = server.addServlet( MockServlet.class, "/" );
  servlet.setServlet( new MockServlet( "mock-servlet", interactions ) );

  server.start();

  interaction = new MockInteraction();
  request = HttpTester.newRequest();
  response = null;
}
 
Example 5
Source File: FrontendFunctionProcessorTest.java    From knox with Apache License 2.0 4 votes vote down vote up
private void testSetup(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(ServiceType.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 ArrayDeque<>();

  ServletHolder servlet = server.addServlet( MockServlet.class, "/" );
  servlet.setServlet( new MockServlet( "mock-servlet", interactions ) );

  server.start();

  interaction = new MockInteraction();
  request = HttpTester.newRequest();
  response = null;
}
 
Example 6
Source File: WebServer.java    From AisAbnormal with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void start() throws Exception {
    ((ServerConnector) server.getConnectors()[0]).setReuseAddress(true);

    // Root context
    context.setContextPath("/abnormal");

    // Setup static content
    context.setResourceBase("src/main/webapp/");
    context.addServlet(DefaultServlet.class, "/");

    // Enable Jersey debug output
    context.setInitParameter("com.sun.jersey.config.statistic.Trace", "true");

    // Enable CORS - cross origin resource sharing
    FilterHolder cors = new FilterHolder();
    cors.setInitParameter("allowedOrigins", "https?://localhost:*, https?://*.e-navigation.net:*");
    cors.setInitParameter("allowedHeaders", "*");
    cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
    cors.setFilter(new CrossOriginFilter());
    context.addFilter(cors, "*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.INCLUDE));

    // Little hack to satisfy OpenLayers URLs in DMA context
    RewritePatternRule openlayersRewriteRule = new RewritePatternRule();
    openlayersRewriteRule.setPattern("/abnormal/theme/*");
    openlayersRewriteRule.setReplacement("/abnormal/js/theme/");

    RewriteHandler rewrite = new RewriteHandler();
    rewrite.setRewriteRequestURI(true);
    rewrite.setRewritePathInfo(false);
    rewrite.setOriginalPathAttribute("requestedPath");
    rewrite.addRule(openlayersRewriteRule);
    rewrite.setHandler(context);
    server.setHandler(rewrite);

    // Setup Guice-Jersey integration
    context.addEventListener(new GuiceServletContextListener() {
        @Override
        protected Injector getInjector() {
            return Guice.createInjector(new RestModule(
                    repositoryName,
                    pathToEventDatabase,
                    eventRepositoryType,
                    eventDataDbHost,
                    eventDataDbPort,
                    eventDataDbName,
                    eventDataDbUsername,
                    eventDataDbPassword
            ));
        }
    });
    context.addFilter(com.google.inject.servlet.GuiceFilter.class, "/rest/*", EnumSet.allOf(DispatcherType.class));

    // Start the server
    server.start();
}