<commit_before>package com.jtbdevelopment.games.dev.utilities.jetty

import groovy.transform.CompileStatic
import org.atmosphere.cpr.AtmosphereServlet
import org.eclipse.jetty.http.HttpVersion
import org.eclipse.jetty.server.*
import org.eclipse.jetty.servlet.FilterHolder
import org.eclipse.jetty.servlet.ServletHolder
import org.eclipse.jetty.util.log.Slf4jLog
import org.eclipse.jetty.util.ssl.SslContextFactory
import org.eclipse.jetty.webapp.WebAppContext
import org.glassfish.jersey.servlet.ServletContainer
import org.springframework.web.context.ContextLoaderListener
import org.springframework.web.context.request.RequestContextListener
import org.springframework.web.filter.DelegatingFilterProxy

import javax.servlet.DispatcherType
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption

/**
 * Date: 12/20/2014
 * Time: 3:24 PM
 *
 * Run with VM params like
 * -Dfacebook.clientID=1234 -Dfacebook.clientSecret=5678 -Dmongo.dbName=adb -Dmongo.userName=auser -Dmongo.userPassword=<PASSWORD>
 */
@CompileStatic
class JettyServer {
    static Server makeServer(int port, final String springContext) {
        Server server = new Server(port)

        configureHttps(port, server)

        WebAppContext webAppContext = new WebAppContext()
        webAppContext.setInitParameter("contextConfigLocation", "classpath:" + springContext);
        webAppContext.setResourceBase(".")
        webAppContext.setContextPath("/")
        webAppContext.setParentLoaderPriority(false)
        webAppContext.addEventListener(new ContextLoaderListener())
        webAppContext.addEventListener(new RequestContextListener())
        webAppContext.logger = new Slf4jLog()

        configureAtmosphere(webAppContext)

        configureJersey(webAppContext)

        webAppContext.addFilter(new FilterHolder(new DelegatingFilterProxy("springSecurityFilterChain")), "/*", EnumSet.allOf(DispatcherType.class))

        server.setHandler(webAppContext)

        // Add Spring Security Filter by the name
        server
    }

    private static void configureJersey(WebAppContext webAppContext) {
        ServletHolder jerseyServlet = webAppContext.addServlet(ServletContainer.class, "/api/*")
        jerseyServlet.setInitOrder(2);
        jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "com.jtbdevelopment");
        jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "org.glassfish.jersey.filter.LoggingFilter");
        jerseyServlet.setInitParameter("jersey.config.server.tracing", "ALL");
    }

    private static void configureAtmosphere(WebAppContext webAppContext) {
        ServletHolder atmosphereServletHolder = webAppContext.addServlet(AtmosphereServlet.class, "/livefeed/*")
        atmosphereServletHolder.setInitOrder(1)
        atmosphereServletHolder.setInitParameter("org.atmosphere.cpr.packages", "com.jtbdevelopment")
        atmosphereServletHolder.setInitParameter("org.atmosphere.websocket.messageContentType", "application/json")
        atmosphereServletHolder.setInitParameter("org.atmosphere.cpr.broadcasterLifeCyclePolicy", "EMPTY_DESTROY")
        atmosphereServletHolder.setInitParameter("org.atmosphere.cpr.sessionSupport", "true")
        atmosphereServletHolder.setInitParameter("org.atmosphere.websocket.suppressJSR356", "true")
        atmosphereServletHolder.setInitParameter("org.atmosphere.interceptor.HeartbeatInterceptor.heartbeatFrequencyInSeconds", "20")
        atmosphereServletHolder.asyncSupported = true
    }

    //  NOTE - this keystore and password is just from jetty example pack and is not secure
    private static void configureHttps(int port, Server server) {
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setSecureScheme("https");
        http_config.setSecurePort(port + 1);
        http_config.setOutputBufferSize(32768);
        http_config.setRequestHeaderSize(8192);
        http_config.setResponseHeaderSize(8192);
        http_config.setSendServerVersion(true);
        http_config.setSendDateHeader(false);

        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.trustAll = true

        def resource = JettyServer.classLoader.getResourceAsStream("keystore")
        Path path = Files.createTempFile('keystore', 'tmp')
        Files.copy(resource, path, StandardCopyOption.REPLACE_EXISTING)
        def fullPath = path.toAbsolutePath().toString()<commit_msg>turn off atmospher cors interceptor since we already have one<commit_after>package com.jtbdevelopment.games.dev.utilities.jetty

import groovy.transform.CompileStatic
import org.atmosphere.cpr.AtmosphereServlet
import org.eclipse.jetty.http.HttpVersion
import org.eclipse.jetty.server.*
import org.eclipse.jetty.servlet.FilterHolder
import org.eclipse.jetty.servlet.ServletHolder
import org.eclipse.jetty.util.log.Slf4jLog
import org.eclipse.jetty.util.ssl.SslContextFactory
import org.eclipse.jetty.webapp.WebAppContext
import org.glassfish.jersey.servlet.ServletContainer
import org.springframework.web.context.ContextLoaderListener
import org.springframework.web.context.request.RequestContextListener
import org.springframework.web.filter.DelegatingFilterProxy

import javax.servlet.DispatcherType
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption

/**
 * Date: 12/20/2014
 * Time: 3:24 PM
 *
 * Run with VM params like
 * -Dfacebook.clientID=1234 -Dfacebook.clientSecret=5678 -Dmongo.dbName=adb -Dmongo.userName=auser -Dmongo.userPassword=<PASSWORD>
 */
@CompileStatic
class JettyServer {
    static Server makeServer(int port, final String springContext) {
        Server server = new Server(port)

        configureHttps(port, server)

        WebAppContext webAppContext = new WebAppContext()
        webAppContext.setInitParameter("contextConfigLocation", "classpath:" + springContext);
        webAppContext.setResourceBase(".")
        webAppContext.setContextPath("/")
        webAppContext.setParentLoaderPriority(false)
        webAppContext.addEventListener(new ContextLoaderListener())
        webAppContext.addEventListener(new RequestContextListener())
        webAppContext.logger = new Slf4jLog()

        configureAtmosphere(webAppContext)

        configureJersey(webAppContext)

        webAppContext.addFilter(new FilterHolder(new DelegatingFilterProxy("springSecurityFilterChain")), "/*", EnumSet.allOf(DispatcherType.class))

        server.setHandler(webAppContext)

        // Add Spring Security Filter by the name
        server
    }

    private static void configureJersey(WebAppContext webAppContext) {
        ServletHolder jerseyServlet = webAppContext.addServlet(ServletContainer.class, "/api/*")
        jerseyServlet.setInitOrder(2);
        jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "com.jtbdevelopment");
        jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "org.glassfish.jersey.filter.LoggingFilter");
        jerseyServlet.setInitParameter("jersey.config.server.tracing", "ALL");
    }

    private static void configureAtmosphere(WebAppContext webAppContext) {
        ServletHolder atmosphereServletHolder = webAppContext.addServlet(AtmosphereServlet.class, "/livefeed/*")
        atmosphereServletHolder.setInitOrder(1)
        atmosphereServletHolder.setInitParameter("org.atmosphere.cpr.packages", "com.jtbdevelopment")
        atmosphereServletHolder.setInitParameter("org.atmosphere.websocket.messageContentType", "application/json")
        atmosphereServletHolder.setInitParameter("org.atmosphere.cpr.broadcasterLifeCyclePolicy", "EMPTY_DESTROY")
        atmosphereServletHolder.setInitParameter("org.atmosphere.cpr.sessionSupport", "true")
        atmosphereServletHolder.setInitParameter("org.atmosphere.websocket.suppressJSR356", "true")
        atmosphereServletHolder.setInitParameter("org.atmosphere.interceptor.HeartbeatInterceptor.heartbeatFrequencyInSeconds", "20")
        atmosphereServletHolder.setInitParameter("org.atmosphere.cpr.dropAccessControlAllowOriginHeader", "true")
        atmosphereServletHolder.asyncSupported = true
    }

    //  NOTE - this keystore and password is just from jetty example pack and is not secure
    private static void configureHttps(int port, Server server) {
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setSecureScheme("https");
        http_config.setSecurePort(port + 1);
        http_config.setOutputBufferSize(32768);
        http_config.setRequestHeaderSize(8192);
        http_config.setResponseHeaderSize(8192);
        http_config.setSendServerVersion(true);
        http_config.setSendDateHeader(false);

        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.trustAll = true

        def resource = JettyServer.classLoader.getResourceAsStream("keystore")
        Path path = Files.createTempFile('keystore', 'tmp')
        Files.copy(resource, path, StandardCopyOption.REPLACE_EXISTING)
        def fullPath = path.toAbsolutePath().toString()
{'fraction_non_alphanumeric': 0.09243421052631579, 'fraction_numerical': 0.009210526315789473, 'mean_word_length': 4.595705521472393, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7326271', 'n_tokens_mistral': 2580, 'n_tokens_neox': 2493, 'n_words': 394}