@Override
  public Server apply(@Nullable String baseResource) {
    if (!jetty.getState().equals(Server.STARTED)
        // TODO code smell = hard coding addresses or ports!!
        && !new InetSocketAddressConnect().apply(new IPSocket("localhost", port))) {
      ResourceHandler resource_handler = new ResourceHandler();
      resource_handler.setDirectoriesListed(true);
      resource_handler.setWelcomeFiles(new String[] {"index.html"});

      resource_handler.setResourceBase(baseResource);
      logger.info("serving " + resource_handler.getBaseResource());

      HandlerList handlers = new HandlerList();
      handlers.setHandlers(new Handler[] {resource_handler, new DefaultHandler()});
      jetty.setHandler(handlers);

      try {
        jetty.start();
      } catch (Exception e) {
        logger.error(e, "Server jetty could not be started at this %s", baseResource);
      }
      return jetty;
    } else {
      logger.debug("Server jetty serving %s already running. Skipping start", baseResource);
      return jetty;
    }
  }
Esempio n. 2
0
  public static void main(String[] args) throws Exception {
    Server server = new Server();

    ServerConnector connector = new ServerConnector(server);
    connector.setPort(7080);
    server.setConnectors(new Connector[] {connector});

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

    webApiContext.addServlet(new ServletHolder(AppTokenServer.class), "/sts/*");
    webApiContext.addServlet(new ServletHolder(CallbackServer.class), "/callback/*");
    webApiContext.addServlet(new ServletHolder(PostObjectPolicy.class), "/get-post-policy/*");

    //    webApiContext.addServlet(new ServletHolder(STSToken.class), "/2/*");
    //    webApiContext.addServlet(new ServletHolder(CallbackServer.class), "/3/*");
    //    webApiContext.addServlet(new ServletHolder(DownloadApi.class), "/download/*");
    webApiContext.setSessionHandler(new SessionHandler());

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {webApiContext, new DefaultHandler()});

    server.setHandler(handlers);
    server.start();
    server.join();
  }
Esempio n. 3
0
  public static void main(String[] args) throws Exception {
    if (args.length < 1) {
      throw new RuntimeException("Expected >=1 command line arguments");
    }
    String sourceDir = args[0];
    logger.log(Level.INFO, "Source directory: " + sourceDir);

    int port = args.length > 1 ? Integer.parseInt(args[1]) : 8080;
    Server server = new Server(port);

    ServletContextHandler servletHandler =
        new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletHandler.setContextPath("/");
    servletHandler.addServlet(ProxyingServlet.class, "/mirror");
    servletHandler.addServlet(AwtThumbnailServlet.class, "/thumb");
    servletHandler.addServlet(OAuthServlet.class, "/oauth");

    final ResourceHandler fileServlet = new ResourceHandler();
    fileServlet.setResourceBase(sourceDir);
    fileServlet.setDirectoriesListed(true);
    // Don't let the corp proxy cache content served out of the filesystem,
    // since we expect this may be used by a developer who's modifying it.
    fileServlet.setCacheControl("no-cache");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(
        new Handler[] {
          fileServlet, servletHandler, new DartHandler(sourceDir), new DefaultHandler()
        });
    server.setHandler(handlers);

    System.out.println("Sample dart apps served at:\n" + "http://localhost:" + port + "/samples/");
    server.start();
    server.join();
  }
  public static void main(String[] args) throws Exception {
    int port = 8080;
    if (args.length == 1) {
      String portString = args[0];
      port = Integer.valueOf(portString);
    }

    System.out.append("Starting at port: ").append(String.valueOf(port)).append('\n');

    AccountService accountService = new AccountService();

    Servlet frontend = new Frontend();
    Servlet auth = new AuthServlet(accountService);
    Server server = new Server(port);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(frontend), "/api/v1/auth/signin");
    context.addServlet(new ServletHolder(auth), "/auth/*");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase("public_html");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {resource_handler, context});
    server.setHandler(handlers);

    server.start();
    server.join();
  }
Esempio n. 5
0
  public void start(Injector injector) throws Exception {
    ResourceHandler resHandler = new ResourceHandler();
    resHandler.setDirectoriesListed(false);
    resHandler.setWelcomeFiles(new String[] {"index.html"});
    resHandler.setResourceBase(args.get("jetty.resourcebase", "./src/main/webapp"));

    server = new Server();
    // getSessionHandler and getSecurityHandler should always return null
    ServletContextHandler servHandler =
        new ServletContextHandler(
            ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
    servHandler.setContextPath("/");

    servHandler.addServlet(new ServletHolder(new InvalidRequestServlet()), "/*");

    FilterHolder guiceFilter = new FilterHolder(injector.getInstance(GuiceFilter.class));
    servHandler.addFilter(guiceFilter, "/*", EnumSet.allOf(DispatcherType.class));

    SelectChannelConnector connector0 = new SelectChannelConnector();
    int httpPort = args.getInt("jetty.port", 8989);
    String host = args.get("jetty.host", "");
    connector0.setPort(httpPort);
    if (!host.isEmpty()) connector0.setHost(host);

    server.addConnector(connector0);

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {resHandler, servHandler});
    server.setHandler(handlers);
    server.start();
    logger.info("Started server at HTTP " + host + ":" + httpPort);
  }
Esempio n. 6
0
  @Test
  public void test() throws Exception {
    // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this
    // to port 0
    // then a randomly available port will be assigned that you can either look in the logs for the
    // port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);

    // Create the ResourceHandler. It is the object that will actually handle the request for a
    // given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in
    // other examples.
    ResourceHandler resource_handler = new ResourceHandler();
    // Configure the ResourceHandler. Setting the resource base indicates where the files should be
    // served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm
    // has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[] {"index.html"});
    resource_handler.setResourceBase("C:\\work\\wget_files");

    // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {resource_handler, new DefaultHandler()});
    server.setHandler(handlers);

    // Start things up! By using the server.join() the server thread will join with the current
    // thread.
    // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more
    // details.
    server.start();
    server.join();
  }
Esempio n. 7
0
  public void run() {

    Properties jettyProperties = new Properties();
    String pFileName = PropertiesUtil.getJettyPropertiesFile();
    try {
      jettyProperties.load(new FileInputStream(pFileName));
    } catch (IOException e) {
      log.error("load properties from {} error.", pFileName);
      return;
    }

    // crate server
    server = new Server(Integer.parseInt(jettyProperties.getProperty(PropList.HTTP_PORT)));

    // Create the ResourceHandler. It is the object that will actually handle the request for a
    // given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in
    // other examples.
    ResourceHandler resource_handler = new ResourceHandler();
    // Configure the ResourceHandler. Setting the resource base indicates where the files should be
    // served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm
    // has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[] {"index.html"});
    resource_handler.setResourceBase(PropertiesUtil.getWebBaseDir());

    log.debug(PropertiesUtil.getWebBaseDir());

    // Add a single handler on context "/hello"
    ContextHandler context = new ContextHandler();
    context.setContextPath("/LoadT");
    context.setHandler(new LoadTHandler());

    // Add the ResourceHandler to the server.
    GzipHandler gzip = new GzipHandler();
    server.setHandler(gzip);

    // make handler chain
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {resource_handler, context, new DefaultHandler()});
    gzip.setHandler(handlers);

    try {
      server.start();
    } catch (Exception e) {
      log.error("embedded jetty server start error.", e);
    }

    server.dumpStdErr();

    /*
    try {
        server.join();
    } catch (InterruptedException e) {
        log.info("jetty server interrupted", e);
    }*/
  }
Esempio n. 8
0
  private Handler handlers() {
    HandlerList handlerList = new HandlerList();
    handlerList.setHandlers(new Handler[] {webAppHandler()});

    HandlerCollection handlerCollection = new HandlerCollection();
    handlerCollection.addHandler(handlerList);

    return handlerCollection;
  }
  /** Constructor. */
  public TestWebServer() {

    // initRuntimeConfiguration();

    WebAppContext webappContext = buildWebappContext();
    ResourceHandler staticHandler = buildStaticResourcesHandler();

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {staticHandler, webappContext});
    server.setHandler(handlers);

    initHttpConnector();
  }
Esempio n. 10
0
 public void start(int port) throws Exception {
   LOG.warn("Port used: " + port + " location " + WEBAPP_LOCATION + " " + databaseInfo.toString());
   server = new Server(port);
   WebAppContext root = new WebAppContext();
   root.setContextPath("/");
   root.setDescriptor(WEBAPP_LOCATION + "/WEB-INF/web.xml");
   root.setResourceBase(WEBAPP_LOCATION);
   root.setParentLoaderPriority(true);
   root.setAttribute(BackendFilter.DATABASE_ATTRIBUTE, databaseInfo);
   //        setupRequestLimits(root, REQUEST_TIME_LIMIT, MAX_OPS_LIMIT);
   final HandlerList handlers = new HandlerList();
   final Handler resourceHandler = createResourceHandler("/console_assets", WEBAPP_LOCATION);
   handlers.setHandlers(new Handler[] {resourceHandler, root});
   server.setHandler(handlers);
   server.start();
 }
  public static void createServlets(DBService dbService) throws Exception {

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new SignUpServlet(dbService)), "/signup");
    context.addServlet(new ServletHolder(new SignInServlet(dbService)), "/signin");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {context});

    Server server = new Server(8090);
    server.setHandler(handlers);

    server.start();
    System.out.println("Server started");
    server.join();
  }
Esempio n. 12
0
  /**
   * Populate the server with handlers and start it.
   *
   * @throws Exception if unable to set up and start Jetty server.
   */
  public void start() throws Exception {
    final SocketConnector connector = new SocketConnector();
    connector.setPort(port);
    server.setConnectors(new Connector[] {connector});

    // Configure a basic resource handler.
    ResourceHandler rh = new ResourceHandler();
    rh.setDirectoriesListed(true);
    rh.setWelcomeFiles(new String[] {"index.html"});
    rh.setResourceBase(staticResourcePath);

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {rh, new DefaultHandler()});
    server.setHandler(handlers);

    server.start();
  }
  @Before
  public void startHttp() throws Exception {
    server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(8778);
    server.addConnector(connector);

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(false);
    resource_handler.setWelcomeFiles(new String[] {});

    resource_handler.setResourceBase("target/test-classes/repo2");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {resource_handler, new DefaultHandler()});
    server.setHandler(handlers);

    server.start();
  }
Esempio n. 14
0
  public static void main(String[] args) throws Exception {
    AccountService accountService = new AccountService();

    accountService.addNewUser(new UserProfile("admin"));
    accountService.addNewUser(new UserProfile("Test"));

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new UsersServlet(accountService)), "/users");
    context.addServlet(new ServletHolder(new SessionsServlet(accountService)), "/sessions");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setResourceBase("templates/lab2/public");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {resource_handler, context});

    Server server = new Server(8080);
    server.setHandler(handlers);

    server.start();
    server.join();
  }
Esempio n. 15
0
  public Server jettyServer() {
    if (jettyServer == null) {
      jettyServer = new Server();
      ServerConnector connector = new ServerConnector(jettyServer);
      connector.setHost(settings.serverHost());
      connector.setPort(settings.serverPort());
      connector.setIdleTimeout(settings.serverIdleTimeout());
      jettyServer.addConnector(connector);

      JettyHandler appHandler = jettyHandler();

      ResourceHandler resourceHandler = new ResourceHandler();
      resourceHandler.setResourceBase(settings.assetsPath());

      HandlerList handlers = new HandlerList();
      handlers.setHandlers(new Handler[] {appHandler, resourceHandler});

      jettyServer.setHandler(handlers);
    }

    return jettyServer;
  }
Esempio n. 16
0
  public static void main(String[] args) {
    DBService dbService = new DBService();
    dbService.printConnectInfo();
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    //        context.setContextPath("/");
    //        context.addServlet(new ServletHolder(new MainServlet()), "/");
    context.addServlet(new ServletHolder(new SignUpServlet(dbService)), "/signup");
    context.addServlet(new ServletHolder(new SignInServlet(dbService)), "/signin");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setResourceBase("./public_html");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {resource_handler, context});

    Server server = new Server(8080);
    server.setHandler(handlers);

    try {
      long userId = dbService.addUser("test1", "test1");
      System.out.println("Added user id: " + userId);

      UsersDataSet dataSet = dbService.getUser(userId);
      System.out.println("User data set: " + dataSet);

      //            dbService.cleanUp();
    } catch (DBException e) {
      e.printStackTrace();
    }
    try {
      server.start();
      System.out.println("Server started");
      server.join();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 17
0
  public void start() {
    server = new Server(port);

    // Override the context classloader, so that Jetty uses the plugin classloader not the main
    // DMDirc loader.
    final Thread currentThread = Thread.currentThread();
    final ClassLoader classLoader = currentThread.getContextClassLoader();
    currentThread.setContextClassLoader(getClass().getClassLoader());

    try {
      final ResourceHandler resourceHandler = new ResourceHandler();
      resourceHandler.setWelcomeFiles(new String[] {"index.html"});
      resourceHandler.setBaseResource(Resource.newClassPathResource("/www"));

      final ResourceHandler clientResourceHandler = new ResourceHandler();
      clientResourceHandler.setBaseResource(Resource.newClassPathResource("/com/dmdirc/res/"));
      final ContextHandler clientResourceContext = new ContextHandler("/res");
      clientResourceContext.setHandler(clientResourceHandler);

      final ServletContextHandler wsHandler = new ServletContextHandler();
      wsHandler.setContextPath("/");
      wsHandler.addServlet(WebUiWebSocketServlet.class, "/ws");

      HandlerList handlers = new HandlerList();
      handlers.setHandlers(
          new Handler[] {resourceHandler, clientResourceContext, wsHandler, new DefaultHandler()});
      server.setHandler(handlers);

      server.start();
    } catch (Exception ex) {
      LOG.error(LogUtils.USER_ERROR, "Unable to start web server", ex);
      server = null;
    } finally {
      // Restore the usual context class loader.
      currentThread.setContextClassLoader(classLoader);
    }
  }
Esempio n. 18
0
 @Override
 public void run() {
   try {
     Server server = new Server(httpPort);
     ResourceHandler rh1 = new ResourceHandler();
     rh1.setDirectoriesListed(false);
     rh1.setResourceBase(httpdir);
     rh1.setWelcomeFiles(new String[] {fileName});
     ResourceHandler rh2 = new ResourceHandler();
     rh2.setDirectoriesListed(false);
     rh2.setResourceBase(runtimePath);
     HandlerList handlers = new HandlerList();
     handlers.setHandlers(new Handler[] {rh1, rh2, new DefaultHandler()});
     server.setHandler(handlers);
     server.start();
     this.server = server;
     this.server.join();
   } catch (BindException e) {
     httpPort++;
     run();
   } catch (Exception e) {
     ex = e;
   }
 }