Java Code Examples for org.eclipse.jetty.servlet.ServletContextHandler#setInitParameter()

The following examples show how to use org.eclipse.jetty.servlet.ServletContextHandler#setInitParameter() . 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: StatsServer.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
    final Server server = new Server(8686);

    final ServletHolder staticHolder = new ServletHolder(new DefaultServlet());
    final ServletContextHandler staticContext = new ServletContextHandler();
    staticContext.setContextPath("/static");
    staticContext.addServlet(staticHolder, "/*");
    staticContext.setResourceBase(StatsServer.class.getResource("/web-ui").toURI().toString());

     // Register and map the dispatcher servlet
    final ServletHolder cxfServletHolder = new ServletHolder(new CXFServlet());
    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.addEventListener(new ContextLoaderListener());
    context.addServlet(cxfServletHolder, "/rest/*");
    context.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
    context.setInitParameter("contextConfigLocation", StatsConfig.class.getName());

    HandlerList handlers = new HandlerList();
    handlers.addHandler(staticContext);
    handlers.addHandler(context);

    server.setHandler(handlers);
    server.start();
    server.join();
}
 
Example 2
Source File: Main.java    From Embedded-Jetty-RESTEasy-Guice-Example with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Injector injector = Guice.createInjector(new HelloModule(args));

    injector.getAllBindings();

    injector.createChildInjector().getAllBindings();

    Server server = new Server(8080);
    ServletContextHandler servletHandler = new ServletContextHandler();
    servletHandler.addEventListener(injector.getInstance(GuiceResteasyBootstrapServletContextListener.class));

    ServletHolder sh = new ServletHolder(HttpServletDispatcher.class);
    servletHandler.setInitParameter("resteasy.role.based.security", "true");
    servletHandler.addFilter(new FilterHolder(injector.getInstance(HelloFilter.class)), "/*", null);
    //servletHandler.addServlet(DefaultServlet.class, "/*");
    servletHandler.addServlet(sh, "/*");

    server.setHandler(servletHandler);
    server.start();
    server.join();
}
 
Example 3
Source File: ServerStarter.java    From service-autodiscovery with Apache License 2.0 6 votes vote down vote up
public static void main( final String[] args ) throws Exception {
	if( args.length != 1 ) {
		System.out.println( "Please provide port number" );
		return;
	}
	
	final int port = Integer.valueOf( args[ 0 ] );
	final Server server = new Server( port );
	 
	System.setProperty( AppConfig.SERVER_PORT, Integer.toString( port ) );
	System.setProperty( AppConfig.SERVER_HOST, "localhost" );
	
		// Register and map the dispatcher servlet
		final ServletHolder servletHolder = new ServletHolder( new CXFServlet() );
		final ServletContextHandler context = new ServletContextHandler(); 		
		context.setContextPath( "/" );
		context.addServlet( servletHolder, "/" + AppConfig.CONTEXT_PATH + "/*" ); 	
		context.addEventListener( new ContextLoaderListener() );
		
		context.setInitParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() );
		context.setInitParameter( "contextConfigLocation", AppConfig.class.getName() );
		 		
       server.setHandler( context );
       server.start();
       server.join();	
}
 
Example 4
Source File: ServerStarter.java    From jetty-web-sockets-jsr356 with Apache License 2.0 6 votes vote down vote up
public static void main( String[] args ) throws Exception {
    Server server = new Server(8080);

    // Create the 'root' Spring application context
    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.addEventListener(new ContextLoaderListener());
    context.setInitParameter("contextClass",AnnotationConfigWebApplicationContext.class.getName());
    context.setInitParameter("contextConfigLocation",AppConfig.class.getName());

    // Create default servlet (servlet api required)
    // The name of DefaultServlet should be set to 'defualt'.
    final ServletHolder defaultHolder = new ServletHolder( "default", DefaultServlet.class );
    defaultHolder.setInitParameter( "resourceBase", System.getProperty("user.dir") );
    context.addServlet( defaultHolder, "/" );

    server.setHandler(context);
    WebSocketServerContainerInitializer.configureContext(context);

    server.start();
    server.join();	
}
 
Example 5
Source File: WebappBoot.java    From awacs with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler handler = new ServletContextHandler();
    handler.setContextPath("/");
    handler.setInitParameter("contextConfigLocation", "classpath*:spring-mvc.xml");
    ServletHolder servletHolder = new ServletHolder();
    servletHolder.setInitOrder(1);
    servletHolder.setHeldClass(DispatcherServlet.class);
    servletHolder.setInitParameter("contextConfigLocation", "classpath*:spring-mvc.xml");
    handler.addServlet(servletHolder, "/");
    server.setHandler(handler);
    server.start();
    server.join();
}
 
Example 6
Source File: JettyResourceServer.java    From BeyondUPnP with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.setInitParameter("org.eclipse.jetty.servlet.Default.gzip", "false");
    mServer.setHandler(context);

    context.addServlet(AudioResourceServlet.class, "/audio/*");
    context.addServlet(VideoResourceServlet.class, "/video/*");

    startIfNotRunning();
}
 
Example 7
Source File: JettyConfigurationHelper.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
static void addContextListeners( ServletContextHandler root, Iterable<ServiceReference<ServletContextListener>> contextListeners )
{
    // Iterate the available context listeners and add them to the server
    for( ServiceReference<ServletContextListener> contextListener : contextListeners )
    {
        ContextListenerInfo contextListenerInfo = contextListener.metaInfo( ContextListenerInfo.class );
        Map<String, String> initParams = contextListenerInfo.initParams();
        for( Map.Entry<String, String> entry : initParams.entrySet() )
        {
            root.setInitParameter( entry.getKey(), entry.getValue() );
        }
        root.addEventListener( contextListener.get() );
    }
}
 
Example 8
Source File: WebServletShiroTest.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test()
        throws Exception
{
    int port = FreePortFinder.findFreePortOnLoopback();
    Server server = new Server( port );
    try {

        ServletContextHandler context = new ServletContextHandler();
        context.setContextPath( "/" );

        context.setInitParameter( "shiroConfigLocations", "classpath:web-shiro.ini" );
        context.addEventListener( new EnvironmentLoaderListener() );

        context.addFilter( ShiroFilter.class, "/*", EnumSet.of( REQUEST, FORWARD, INCLUDE, ERROR ) );

        server.setHandler( context );
        server.start();

        // HttpClient client = new DefaultHttpClient();
        // String result = client.execute( new HttpGet( "http://127.0.0.1:" + port + "/" ), new BasicResponseHandler() );

    } finally {
        server.stop();
    }

}
 
Example 9
Source File: AbstractDownloadTest.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Before
public void startServer()
    throws Exception
{

    System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );

    server = new Server();
    serverConnector = new ServerConnector( server, new HttpConnectionFactory() );
    server.addConnector( serverConnector );

    ServletHolder servletHolder = new ServletHolder( new CXFServlet() );
    ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS );
    context.setResourceBase( SystemUtils.JAVA_IO_TMPDIR );
    context.setSessionHandler( new SessionHandler() );
    context.addServlet( servletHolder, "/" + getRestServicesPath() + "/*" );
    context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
    context.addEventListener( new ContextLoaderListener() );

    ServletHolder servletHolderRepo = new ServletHolder( new RepositoryServlet() );
    context.addServlet( servletHolderRepo, "/repository/*" );

    server.setHandler( context );
    server.start();
    port = serverConnector.getLocalPort();
    log.info( "start server on port {}", this.port );

    User user = new User();
    user.setEmail( "[email protected]" );
    user.setFullName( "the root user" );
    user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
    user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );

    getUserService( null ).createAdminUser( user );


}
 
Example 10
Source File: GatewayMicroService.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * Configure the web application(s).
 * @param handlers
 * @throws Exception
 */
protected void addModulesToJetty(ContextHandlerCollection handlers) throws Exception {
	/* *************
     * Gateway API
     * ************* */
    ServletContextHandler gatewayApiServer = new ServletContextHandler(ServletContextHandler.SESSIONS);
    addSecurityHandler(gatewayApiServer);
    gatewayApiServer.setContextPath("/api");
    gatewayApiServer.addEventListener(new ResteasyBootstrap());
    gatewayApiServer.addEventListener(new WarGatewayBootstrapper());
    gatewayApiServer.addFilter(HttpRequestThreadLocalFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    gatewayApiServer.addFilter(LocaleFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    gatewayApiServer.addFilter(ApimanCorsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    gatewayApiServer.addFilter(DisableCachingFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    addApiAuthFilter(gatewayApiServer);
    gatewayApiServer.addFilter(RootResourceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    ServletHolder resteasyServlet = new ServletHolder(new HttpServletDispatcher());
    resteasyServlet.setInitParameter("javax.ws.rs.Application", GatewayMicroServiceApplication.class.getName());
    gatewayApiServer.addServlet(resteasyServlet, "/*");
    gatewayApiServer.setInitParameter("resteasy.servlet.mapping.prefix", "");

    handlers.addHandler(gatewayApiServer);


    /* *************
     * Gateway
     * ************* */
    ServletContextHandler gatewayServer = new ServletContextHandler(ServletContextHandler.SESSIONS);
    addSecurityHandler(gatewayServer);
    gatewayServer.setContextPath("/gateway");
    ServletHolder servlet = new ServletHolder(new WarGatewayServlet());
    gatewayServer.addServlet(servlet, "/*");

    handlers.addHandler(gatewayServer);
}
 
Example 11
Source File: JettyAppServer.java    From selenium with Apache License 2.0 4 votes vote down vote up
public JettyAppServer(String hostName, int httpPort, int httpsPort) {
  this.hostName = hostName;
  this.port = httpPort;
  this.securePort = httpsPort;

  // Be quiet. Unless we want things to be chatty
  if (!Boolean.getBoolean("webdriver.debug")) {
    new NullLogger().disableLogging();
  }

  server = new Server();

  handlers = new ContextHandlerCollection();

  Path webSrc = locate("common/src/web");
  ServletContextHandler defaultContext = addResourceHandler(
      DEFAULT_CONTEXT_PATH, webSrc);

  // Only non-null when running with bazel test.
  Path runfiles = InProject.findRunfilesRoot();
  if (runfiles != null) {
    addResourceHandler(FILEZ_CONTEXT_PATH, runfiles);
  }

  addJsResourceHandler(JS_SRC_CONTEXT_PATH, "javascript");
  addJsResourceHandler(CLOSURE_CONTEXT_PATH, "third_party/closure/goog");
  addJsResourceHandler(THIRD_PARTY_JS_CONTEXT_PATH, "third_party/js");

  TemporaryFilesystem tempFs = TemporaryFilesystem.getDefaultTmpFS();
  tempPageDir = tempFs.createTempDir("pages", "test");
  addResourceHandler(TEMP_SRC_CONTEXT_PATH, tempPageDir.toPath());
  defaultContext.setInitParameter("tempPageDir", tempPageDir.getAbsolutePath());
  defaultContext.setInitParameter("hostname", hostName);
  defaultContext.setInitParameter("port", ""+port);
  defaultContext.setInitParameter("path", TEMP_SRC_CONTEXT_PATH);
  defaultContext.setInitParameter("webSrc", webSrc.toAbsolutePath().toString());

  server.setHandler(handlers);

  addServlet(defaultContext, "/redirect", RedirectServlet.class);
  addServlet(defaultContext, "/page/*", PageServlet.class);

  addServlet(defaultContext, "/manifest/*", ManifestServlet.class);
  // Serves every file under DEFAULT_CONTEXT_PATH/utf8 as UTF-8 to the browser
  addServlet(defaultContext, "/utf8/*", Utf8Servlet.class);

  addServlet(defaultContext, "/upload", UploadServlet.class);
  addServlet(defaultContext, "/encoding", EncodingServlet.class);
  addServlet(defaultContext, "/sleep", SleepingServlet.class);
  addServlet(defaultContext, "/cookie", CookieServlet.class);
  addServlet(defaultContext, "/quitquitquit", KillSwitchServlet.class);
  addServlet(defaultContext, "/basicAuth", BasicAuth.class);
  addServlet(defaultContext, "/generated/*", GeneratedJsTestServlet.class);
  addServlet(defaultContext, "/createPage", CreatePageServlet.class);
}
 
Example 12
Source File: ManagerApiMicroService.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * Configure the web application(s).
 * @param handlers
 * @throws Exception
 */
protected void addModulesToJetty(HandlerCollection handlers) throws Exception {
	/* *************
     * Manager API
     * ************* */
    ServletContextHandler apiManServer = new ServletContextHandler(ServletContextHandler.SESSIONS);
    addSecurityHandler(apiManServer);
    apiManServer.setContextPath("/apiman");
    apiManServer.addEventListener(new Listener());
    apiManServer.addEventListener(new BeanManagerResourceBindingListener());
    apiManServer.addEventListener(new ResteasyBootstrap());
    apiManServer.addFilter(LocaleFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    apiManServer.addFilter(ApimanCorsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    apiManServer.addFilter(DisableCachingFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    addAuthFilter(apiManServer);
    apiManServer.addFilter(DefaultSecurityContextFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    apiManServer.addFilter(RootResourceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    apiManServer.addFilter(ManagerApiMicroServiceTxWatchdogFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    ServletHolder resteasyServlet = new ServletHolder(new HttpServletDispatcher());
    resteasyServlet.setInitParameter("javax.ws.rs.Application", ManagerApiMicroServiceApplication.class.getName());
    apiManServer.addServlet(resteasyServlet, "/*");

    apiManServer.setInitParameter("resteasy.injector.factory", "org.jboss.resteasy.cdi.CdiInjectorFactory");
    apiManServer.setInitParameter("resteasy.scan", "true");
    apiManServer.setInitParameter("resteasy.servlet.mapping.prefix", "");

    handlers.addHandler(apiManServer);

    /* ********** *
     * Manager UI *
     * ********** */
    ResourceHandler apimanUiServer = new ResourceHandler() {
        /**
         * @see org.eclipse.jetty.server.handler.ResourceHandler#getResource(java.lang.String)
         */
        @Override
        public Resource getResource(String path) {
            Resource resource = null;

            if (path == null || path.equals("/") || path.equals("/index.html")) {
                path = "/index.html";
            }
            if (path.startsWith("/apimanui/api-manager") || path.equals("/apimanui") || path.equals("/apimanui/")) {
                path = "/apimanui/index.html";
            }
            if (path.equals("/apimanui/apiman/config.js")) {
                resource = getConfigResource(path);
            }
            if (path.equals("/apimanui/apiman/translations.js")) {
                resource = getTranslationsResource(path);
            }

            if (resource == null) {
                URL url = getClass().getResource(path);
                if (url != null) {
                    resource = new ApimanResource(url);
                }
            }

            return resource;
        }
    };
    apimanUiServer.setResourceBase("/apimanui/");
    apimanUiServer.setWelcomeFiles(new String[] { "index.html" });
    handlers.addHandler(apimanUiServer);
}
 
Example 13
Source File: ManagerApiTestServer.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
     * Configure the web application(s).
     * @param handlers
     * @throws Exception
     */
    protected void addModulesToJetty(ContextHandlerCollection handlers) throws Exception {
        /* *************
         * Manager API
         * ************* */
        ServletContextHandler apiManServer = new ServletContextHandler(ServletContextHandler.SESSIONS);
        apiManServer.setSecurityHandler(createSecurityHandler());
        apiManServer.setContextPath("/apiman");
        apiManServer.addEventListener(new Listener());
        apiManServer.addEventListener(new BeanManagerResourceBindingListener());
        apiManServer.addEventListener(new ResteasyBootstrap());
        apiManServer.addFilter(DatabaseSeedFilter.class, "/db-seeder", EnumSet.of(DispatcherType.REQUEST));
//        apiManServer.addFilter(LocaleFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        apiManServer.addFilter(ApimanCorsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        apiManServer.addFilter(DisableCachingFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        configureAuthentication(apiManServer);
        apiManServer.addFilter(DefaultSecurityContextFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        apiManServer.addFilter(TransactionWatchdogFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        apiManServer.addFilter(RootResourceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        ServletHolder resteasyServlet = new ServletHolder(new HttpServletDispatcher());
        resteasyServlet.setInitParameter("javax.ws.rs.Application", TestManagerApiApplication.class.getName());
        apiManServer.addServlet(resteasyServlet, "/*");

        apiManServer.setInitParameter("resteasy.injector.factory", "org.jboss.resteasy.cdi.CdiInjectorFactory");
        apiManServer.setInitParameter("resteasy.scan", "true");
        apiManServer.setInitParameter("resteasy.servlet.mapping.prefix", "");

        // Add the web contexts to jetty
        handlers.addHandler(apiManServer);

        /* *************
         * Mock Gateway (to test publishing of APIs from dt to rt)
         * ************* */
        ServletContextHandler mockGatewayServer = new ServletContextHandler(ServletContextHandler.SESSIONS);
        mockGatewayServer.setSecurityHandler(createSecurityHandler());
        mockGatewayServer.setContextPath("/mock-gateway");
        ServletHolder mockGatewayServlet = new ServletHolder(new MockGatewayServlet());
        mockGatewayServer.addServlet(mockGatewayServlet, "/*");

        // Add the web contexts to jetty
        handlers.addHandler(mockGatewayServer);
    }