private void setupRequestLimits(WebAppContext root, Integer limit, int maxOps) {
   if (limit == null) return;
   GuardingRequestFilter requestTimeLimitFilter = new GuardingRequestFilter(limit, maxOps);
   root.addFilter(new FilterHolder(requestTimeLimitFilter), "/backend/*", FilterMapping.REQUEST);
 }
  public static void main(final String[] args) throws Exception {
    final int timeout = (int) Duration.ONE_HOUR.getMilliseconds();

    int port = 8080;
    final CommandLineParser parser = new BasicParser();
    final Options options = new Options();
    options.addOption("h", "help", false, "Prints usage information");
    options.addOption("p", "port", true, "Server port");
    options.addOption("v", "version", false, "Version");
    options.addOption("f", "file", true, "kMyMoney file path");
    // Parse the program arguments
    final CommandLine commandLine = parser.parse(options, args);
    boolean isBadArgs = false;
    String file = null;

    if (commandLine.hasOption('h')) {
      isBadArgs = true;
    }
    if (commandLine.hasOption('v')) {
      System.out.println("kMyMoneyScripter version 0.01");
      System.exit(0);
    }
    if (!commandLine.hasOption('f')) {
      isBadArgs = true;
    }
    if (commandLine.hasOption('p')) {
      try {
        System.out.println(commandLine.getOptionValue('p'));
        port = Integer.parseInt(commandLine.getOptionValue('p'));
      } catch (final Exception e) {
        isBadArgs = true;
        System.err.println("Not a proper port number:" + commandLine.getOptionValue('p'));
      }
    }
    if (commandLine.hasOption('f')) {
      file = commandLine.getOptionValue('f');
      if (file == null) {
        isBadArgs = true;
      } else {
        final File f = new File(file);
        if (f.exists() == false) {
          System.err.println("File " + file + " does not exist.");
          isBadArgs = true;
        }
      }
    }

    if (isBadArgs) {
      final HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("kMymoneyScripter", options);
      System.exit(1);
    }
    final Server server = new Server();
    final SocketConnector connector = new SocketConnector();

    // Set some timeout options to make debugging easier.
    connector.setMaxIdleTime(timeout);
    connector.setSoLingerTime(-1);
    connector.setPort(port);
    server.addConnector(connector);

    // check if a keystore for a SSL certificate is available, and
    // if so, start a SSL connector on port 8443. By default, the
    // quickstart comes with a Apache Wicket Quickstart Certificate
    // that expires about half way september 2021. Do not use this
    // certificate anywhere important as the passwords are available
    // in the source.

    final Resource keystore = Resource.newClassPathResource("/keystore");
    if (keystore != null && keystore.exists()) {
      connector.setConfidentialPort(8443);

      final SslContextFactory factory = new SslContextFactory();
      factory.setKeyStoreResource(keystore);
      factory.setKeyStorePassword("wicket");
      factory.setTrustStore(keystore);
      factory.setKeyManagerPassword("wicket");
      final SslSocketConnector sslConnector = new SslSocketConnector(factory);
      sslConnector.setMaxIdleTime(timeout);
      sslConnector.setPort(8443);
      sslConnector.setAcceptors(4);
      server.addConnector(sslConnector);

      System.out.println("SSL access to the quickstart has been enabled on port 8443");
      System.out.println("You can access the application using SSL on https://localhost:8443");
      System.out.println();
    }

    final WebAppContext bb = new WebAppContext();
    bb.setServer(server);
    bb.setContextPath("/");

    new WicketFilter();
    final FilterHolder filterHolder = new FilterHolder(WicketFilter.class);
    filterHolder.setInitParameter(
        ContextParamWebApplicationFactory.APP_CLASS_PARAM, WICKET_WEBAPP_CLASS_NAME);
    filterHolder.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*");
    // filterHolder.setInitParameter(param, value)
    bb.addFilter(filterHolder, "/*", 1);

    bb.setWar(Start.class.getClassLoader().getResource("webapp").toExternalForm());
    // START JMX SERVER
    // MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    // MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
    // server.getContainer().addEventListener(mBeanContainer);
    // mBeanContainer.start();

    server.setHandler(bb);
    createDB(file);

    try {
      System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
      server.start();
      System.in.read();
      System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
      server.stop();
      server.join();
      WicketApplication.db.close();
    } catch (final Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
Beispiel #3
0
  public void startWebSocketServer(final Injector injector) {
    httpServer = new Server();

    List<Connector> connectors = getSelectChannelConnectors(httpAddresses);
    if (connectors.isEmpty()) {
      LOG.severe("No valid http end point address provided!");
    }
    for (Connector connector : connectors) {
      httpServer.addConnector(connector);
    }
    final WebAppContext context = new WebAppContext();

    context.setParentLoaderPriority(true);

    if (jettySessionManager != null) {
      // This disables JSessionIDs in URLs redirects
      // see:
      // http://stackoverflow.com/questions/7727534/how-do-you-disable-jsessionid-for-jetty-running-with-the-eclipse-jetty-maven-plu
      // and:
      // http://jira.codehaus.org/browse/JETTY-467?focusedCommentId=114884&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-114884
      jettySessionManager.setSessionIdPathParameterName(null);

      context.getSessionHandler().setSessionManager(jettySessionManager);
    }
    final ResourceCollection resources = new ResourceCollection(resourceBases);
    context.setBaseResource(resources);

    addWebSocketServlets();

    try {
      final Injector parentInjector = injector;

      final ServletModule servletModule = getServletModule(parentInjector);

      ServletContextListener contextListener =
          new GuiceServletContextListener() {

            private final Injector childInjector =
                parentInjector.createChildInjector(servletModule);

            @Override
            protected Injector getInjector() {
              return childInjector;
            }
          };

      context.addEventListener(contextListener);
      context.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
      context.addFilter(GzipFilter.class, "/webclient/*", EnumSet.allOf(DispatcherType.class));
      String[] hosts = new String[httpAddresses.length];
      for (int i = 0; i < httpAddresses.length; i++) {
        hosts[i] = httpAddresses[i].getHostName();
      }
      context.addVirtualHosts(hosts);
      httpServer.setHandler(context);

      httpServer.start();
      restoreSessions();

    } catch (Exception e) { // yes, .start() throws "Exception"
      LOG.severe("Fatal error starting http server.", e);
      return;
    }
    LOG.fine("WebSocket server running.");
  }