コード例 #1
1
  @Test
  public void testUserAgentExclusionByExcludedAgentPatterns() throws Exception {
    GzipTester tester = new GzipTester(testingdir, compressionType);

    FilterHolder holder = tester.setContentServlet(DefaultServlet.class);
    holder.setInitParameter("excludedAgents", "bar");
    holder.setInitParameter("excludeAgentPatterns", "fo.*");
    tester.setUserAgent("foo");

    int filesize = CompressedResponseWrapper.DEFAULT_BUFFER_SIZE * 4;
    tester.prepareServerFile("file.txt", filesize);

    try {
      tester.start();
      tester.assertIsResponseNotGzipCompressed("file.txt", filesize, HttpStatus.OK_200);
    } finally {
      tester.stop();
    }
  }
コード例 #2
1
  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();
  }
コード例 #3
0
  @Test
  public void testIsNotGzipCompressedHttpBadRequestStatus() throws Exception {
    GzipTester tester = new GzipTester(testingdir, compressionType);

    // Test error code 400
    FilterHolder holder = tester.setContentServlet(HttpErrorServlet.class);
    holder.setInitParameter("mimeTypes", "text/plain");

    try {
      tester.start();
      tester.assertIsResponseNotGzipCompressedAndEqualToExpectedString("error message", -1, 400);
    } finally {
      tester.stop();
    }
  }
コード例 #4
0
  @Test
  public void testIsNotGzipCompressedHttpStatus() throws Exception {
    GzipTester tester = new GzipTester(testingdir, compressionType);

    // Test error code 204
    FilterHolder holder = tester.setContentServlet(HttpStatusServlet.class);
    holder.setInitParameter("mimeTypes", "text/plain");

    try {
      tester.start();
      tester.assertIsResponseNotGzipCompressed(-1, 204);
    } finally {
      tester.stop();
    }
  }
コード例 #5
0
  private void assertIsGzipCompressed(String filename, int filesize) throws Exception {
    GzipTester tester = new GzipTester(testingdir, compressionType);

    File testfile =
        tester.prepareServerFile(testServlet.getSimpleName() + "-" + filename, filesize);

    FilterHolder holder = tester.setContentServlet(testServlet);
    holder.setInitParameter("mimeTypes", "text/plain");

    try {
      tester.start();
      tester.assertIsResponseGzipCompressed("GET", testfile.getName());
    } finally {
      tester.stop();
    }
  }
コード例 #6
0
  @Test
  public void testIsGzipCompressedTiny() throws Exception {
    GzipTester tester = new GzipTester(testingdir, compressionType);

    // Test content that is smaller than the buffer.
    int filesize = CompressedResponseWrapper.DEFAULT_BUFFER_SIZE / 4;
    tester.prepareServerFile("file.txt", filesize);

    FilterHolder holder = tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class);
    holder.setInitParameter("mimeTypes", "text/plain");

    try {
      tester.start();
      HttpTester.Response http = tester.assertIsResponseGzipCompressed("file.txt");
      Assert.assertEquals("Accept-Encoding", http.get("Vary"));
    } finally {
      tester.stop();
    }
  }
コード例 #7
0
  @Test
  public void testIsNotGzipCompressed() throws Exception {
    GzipTester tester = new GzipTester(testingdir, compressionType);

    int filesize = CompressedResponseWrapper.DEFAULT_BUFFER_SIZE * 4;
    tester.prepareServerFile("file.mp3", filesize);

    FilterHolder holder = tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class);
    holder.setInitParameter("mimeTypes", "text/plain");

    try {
      tester.start();
      HttpTester.Response http =
          tester.assertIsResponseNotGzipCompressed("file.mp3", filesize, HttpStatus.OK_200);
      Assert.assertNull(http.get("Vary"));
    } finally {
      tester.stop();
    }
  }
コード例 #8
0
  private void assertIsNotGzipCompressed(String filename, int filesize) throws Exception {
    GzipTester tester = new GzipTester(testingdir, compressionType);

    File testfile =
        tester.prepareServerFile(testServlet.getSimpleName() + "-" + filename, filesize);

    FilterHolder holder = tester.setContentServlet(testServlet);
    holder.setInitParameter("mimeTypes", "text/plain");

    try {
      tester.start();
      HttpTester.Response response =
          tester.assertIsResponseNotGzipCompressed(
              "GET", testfile.getName(), filesize, HttpStatus.OK_200);
      Assert.assertThat(response.get("ETAG"), Matchers.startsWith("W/etag-"));
    } finally {
      tester.stop();
    }
  }
コード例 #9
0
  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);
    }
  }
コード例 #10
0
  public static void startServer(Class<? extends Filter> filter) throws Exception {
    _tester = new ServletTester("/ctx");
    HttpURI uri = new HttpURI(_tester.createConnector(true));
    _host = uri.getHost();
    _port = uri.getPort();

    _tester.getContext().addServlet(TestServlet.class, "/*");

    _dosFilter =
        _tester.getContext().addFilter(filter, "/dos/*", EnumSet.allOf(DispatcherType.class));
    _dosFilter.setInitParameter("maxRequestsPerSec", "4");
    _dosFilter.setInitParameter("delayMs", "200");
    _dosFilter.setInitParameter("throttledRequests", "1");
    _dosFilter.setInitParameter("waitMs", "10");
    _dosFilter.setInitParameter("throttleMs", "4000");
    _dosFilter.setInitParameter("remotePort", "false");
    _dosFilter.setInitParameter("insertHeaders", "true");

    _timeoutFilter =
        _tester.getContext().addFilter(filter, "/timeout/*", EnumSet.allOf(DispatcherType.class));
    _timeoutFilter.setInitParameter("maxRequestsPerSec", "4");
    _timeoutFilter.setInitParameter("delayMs", "200");
    _timeoutFilter.setInitParameter("throttledRequests", "1");
    _timeoutFilter.setInitParameter("waitMs", "10");
    _timeoutFilter.setInitParameter("throttleMs", "4000");
    _timeoutFilter.setInitParameter("remotePort", "false");
    _timeoutFilter.setInitParameter("insertHeaders", "true");
    _timeoutFilter.setInitParameter("maxRequestMs", _requestMaxTime + "");

    _tester.start();
  }
コード例 #11
0
ファイル: Users.java プロジェクト: andriybobyr/lnk
  static {
    List<String> allowedUserHostsList = Nxt.getStringListProperty("nxt.allowedUserHosts");
    if (!allowedUserHostsList.contains("*")) {
      allowedUserHosts = Collections.unmodifiableSet(new HashSet<>(allowedUserHostsList));
    } else {
      allowedUserHosts = null;
    }

    boolean enableUIServer = Nxt.getBooleanProperty("nxt.enableUIServer");
    if (enableUIServer) {
      final int port =
          Constants.isTestnet ? TESTNET_UI_PORT : Nxt.getIntProperty("nxt.uiServerPort");
      final String host = Nxt.getStringProperty("nxt.uiServerHost");
      userServer = new Server();
      ServerConnector connector;

      boolean enableSSL = Nxt.getBooleanProperty("nxt.uiSSL");
      if (enableSSL) {
        Logger.logMessage("Using SSL (https) for the user interface server");
        HttpConfiguration https_config = new HttpConfiguration();
        https_config.setSecureScheme("https");
        https_config.setSecurePort(port);
        https_config.addCustomizer(new SecureRequestCustomizer());
        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setKeyStorePath(Nxt.getStringProperty("nxt.keyStorePath"));
        sslContextFactory.setKeyStorePassword(
            Nxt.getStringProperty("nxt.keyStorePassword", null, true));
        sslContextFactory.setExcludeCipherSuites(
            "SSL_RSA_WITH_DES_CBC_SHA",
            "SSL_DHE_RSA_WITH_DES_CBC_SHA",
            "SSL_DHE_DSS_WITH_DES_CBC_SHA",
            "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
            "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
            "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
            "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
        sslContextFactory.setExcludeProtocols("SSLv3");
        connector =
            new ServerConnector(
                userServer,
                new SslConnectionFactory(sslContextFactory, "http/1.1"),
                new HttpConnectionFactory(https_config));
      } else {
        connector = new ServerConnector(userServer);
      }

      connector.setPort(port);
      connector.setHost(host);
      connector.setIdleTimeout(Nxt.getIntProperty("nxt.uiServerIdleTimeout"));
      connector.setReuseAddress(true);
      userServer.addConnector(connector);

      HandlerList userHandlers = new HandlerList();

      ResourceHandler userFileHandler = new ResourceHandler();
      userFileHandler.setDirectoriesListed(false);
      userFileHandler.setWelcomeFiles(new String[] {"index.html"});
      userFileHandler.setResourceBase(Nxt.getStringProperty("nxt.uiResourceBase"));

      userHandlers.addHandler(userFileHandler);

      String javadocResourceBase = Nxt.getStringProperty("nxt.javadocResourceBase");
      if (javadocResourceBase != null) {
        ContextHandler contextHandler = new ContextHandler("/doc");
        ResourceHandler docFileHandler = new ResourceHandler();
        docFileHandler.setDirectoriesListed(false);
        docFileHandler.setWelcomeFiles(new String[] {"index.html"});
        docFileHandler.setResourceBase(javadocResourceBase);
        contextHandler.setHandler(docFileHandler);
        userHandlers.addHandler(contextHandler);
      }

      ServletHandler userHandler = new ServletHandler();
      ServletHolder userHolder = userHandler.addServletWithMapping(UserServlet.class, "/nxt");
      userHolder.setAsyncSupported(true);

      if (Nxt.getBooleanProperty("nxt.uiServerCORS")) {
        FilterHolder filterHolder =
            userHandler.addFilterWithMapping(CrossOriginFilter.class, "/*", FilterMapping.DEFAULT);
        filterHolder.setInitParameter("allowedHeaders", "*");
        filterHolder.setAsyncSupported(true);
      }

      userHandlers.addHandler(userHandler);

      userHandlers.addHandler(new DefaultHandler());

      userServer.setHandler(userHandlers);
      userServer.setStopAtShutdown(true);

      ThreadPool.runBeforeStart(
          () -> {
            try {
              userServer.start();
              Logger.logMessage("Started user interface server at " + host + ":" + port);
            } catch (Exception e) {
              Logger.logErrorMessage("Failed to start user interface server", e);
              throw new RuntimeException(e.toString(), e);
            }
          },
          true);

    } else {
      userServer = null;
      Logger.logMessage("User interface server not enabled");
    }
  }
コード例 #12
0
ファイル: Application.java プロジェクト: 3rdstage/exercise
  public static void main(String[] args) throws Exception {

    // load main configuration
    String configLoc = System.getProperty(CONFIG_LOCATION_SYS_VAR, CONFIG_LOCATION_DEFAULT);
    System.setProperty(CONFIG_LOCATION_SYS_VAR, configLoc);
    Properties configProps = new Properties();
    try {
      configProps.load(new FileInputStream(ResourceUtils.getFile(configLoc)));
      logger.info("Successfully loaded main configuration.");
    } catch (Exception ex) {
      logger.error("Fail to start in early part", ex);
      throw ex;
    }
    Config config = new Config(configProps);

    // load log4j configuration
    // @todo review the effect and final configuration when -Dlog4j.properties is specified with
    // command line
    String log4jConfigLoc = config.getValue(ItemMeta.BOOTSTRAP_LOG4J_CONFIG_LOCATION);
    if (log4jConfigLoc != null) {
      Properties log4jConfig = new Properties();

      try {
        log4jConfig.load(new FileInputStream(ResourceUtils.getFile(log4jConfigLoc)));
        org.apache.log4j.LogManager.resetConfiguration();
        org.apache.log4j.PropertyConfigurator.configure(log4jConfig);
        logger.info("Successfully loaded log4j configuration at {}", log4jConfigLoc);
      } catch (Exception ex) {
        logger.error("Faile to load specified log4j configuration", ex);
        throw ex;
      }
    }

    ServletContextHandler sch =
        new ServletContextHandler(ServletContextHandler.SECURITY | ServletContextHandler.SESSIONS);

    sch.setContextPath(config.getValue(ItemMeta.ROOT_SERVLET_CONTEXT_PATH));
    sch.getSessionHandler()
        .getSessionManager()
        .setSessionCookie(config.getValue(ItemMeta.ROOT_SERVLET_SESSION_ID));

    ServletHolder dsh = sch.addServlet(DefaultServlet.class, "/");
    dsh.setInitOrder(1);

    ServletHolder jsh = new ServletHolder(JerseySpringServlet.class);
    jsh.setInitParameter(
        JerseySpringServlet.INIT_PARM_SPRING_CONFIG_LOCATION,
        config.getValue(ItemMeta.BOOTSTRAP_SPRING_CONFIG_LOCATION));
    // Refer https://jersey.java.net/apidocs/1.18/jersey/index.html?constant-values.html
    jsh.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
    jsh.setInitParameter(
        ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS,
        "com.sun.jersey.api.container.filter.LoggingFilter");
    jsh.setInitParameter(
        ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS,
        "com.sun.jersey.api.container.filter.LoggingFilter");
    jsh.setInitParameter(ResourceConfig.FEATURE_TRACE, "true");
    // jsh.setInitParameter(JSONMarshaller.FORMATTED, "true");
    // jsh.setInitParameter(FeaturesAndProperties.FEATURE_FORMATTED, "true");
    // jsh.setInitParameter(FeaturesAndProperties.FEATURE_XMLROOTELEMENT_PROCESSING, "true");

    sch.addServlet(jsh, config.getValue(ItemMeta.JERSEY_SERVLET_URL_PATTEN));
    jsh.setInitOrder(config.getIntValue(ItemMeta.JERSEY_SERVLET_INIT_ORDER));

    // For more, refer
    // http://download.eclipse.org/jetty/stable-7/apidocs/index.html?org/eclipse/jetty/servlets/CrossOriginFilter.html
    FilterHolder fh = new FilterHolder(CrossOriginFilter.class);
    fh.setName("crossOriginFilter");
    fh.setInitParameter(
        CrossOriginFilter.ALLOWED_ORIGINS_PARAM,
        config.getValue(ItemMeta.CROSS_ORIGIN_FILTER_ALLOWED_ORIGINS));
    fh.setInitParameter(
        CrossOriginFilter.ALLOWED_METHODS_PARAM,
        config.getValue(ItemMeta.CROSS_ORIGIN_FILTER_ALLOWED_METHODS));
    fh.setInitParameter(
        CrossOriginFilter.ALLOWED_HEADERS_PARAM,
        config.getValue(ItemMeta.CROSS_ORIGIN_FILTER_ALLOWED_HEADERS));
    sch.addFilter(fh, "/*", FilterMapping.DEFAULT);

    Server jetty = new Server();
    HandlerList hl = new HandlerList();
    hl.addHandler(sch);
    jetty.setHandler(hl);
    jetty.setThreadPool(
        new QueuedThreadPool(config.getIntValue(ItemMeta.WEB_SERVER_THREAD_POOL_SIZE)));

    SelectChannelConnector conn = new SelectChannelConnector();
    conn.setPort(config.getIntValue(ItemMeta.WEB_SERVER_PORT));
    conn.setMaxIdleTime(config.getIntValue(ItemMeta.WEB_SERVER_MAX_IDLE_TIME));

    MBeanContainer mbc = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    mbc.setDomain(config.getValue(ItemMeta.JMX_DOMAIN) + ".jetty");
    jetty.getContainer().addEventListener(mbc);
    jetty.addBean(mbc);

    jetty.addConnector(conn);
    jetty.setStopAtShutdown(true);

    try {
      jetty.start();
      logger.info("Jetty started at port {} on {}", conn.getPort(), "127.0.0.1");
    } catch (Exception ex) {
      logger.error("Fail to start Jetty.", ex);
      System.exit(-1);
    }
  }