<commit_before>/*
 * Gretty
 *
 * Copyright (C) 2013-2014 <NAME> and contributors.
 *
 * See the file "LICENSE" for copying and usage permission.
 * See the file "CONTRIBUTORS" for complete list of contributors.
 */
package org.akhikhl.gretty

import java.util.jar.JarEntry
import java.util.jar.JarFile
import org.apache.commons.io.FilenameUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 *
 * @author akhikhl
 */
class JettyServerConfigurer {

  protected final Logger log
  protected final JettyConfigurer configurer
  protected final Map params

  JettyServerConfigurer(JettyConfigurer configurer, Map params) {
    log = LoggerFactory.getLogger(this.getClass())
    this.configurer = configurer
    this.params = params
  }

  protected void configureWithBaseResource(Map webapp, context) {

    URL contextConfigFile = getContextConfigFile(context.baseResource, params.servletContainerId)
    if(!contextConfigFile && webapp.contextConfigFile)
      contextConfigFile = new File(webapp.contextConfigFile).toURI().toURL()
    configurer.applyContextConfigFile(context, contextConfigFile)

    String realm = webapp.realm ?: params.realm
    URL realmConfigFile = getRealmFile(context.baseResource, params.servletContainerId)
    if(!realmConfigFile) {
      if(webapp.realmConfigFile) {
        realmConfigFile = new File(webapp.realmConfigFile).canonicalFile.toURI().toURL()
      }
      else if(params.realmConfigFile) {
        realmConfigFile = new File(params.realmConfigFile).canonicalFile.toURI().toURL()
      }
    }
    if(realm && realmConfigFile) {
      if(context.securityHandler.loginService == null) {
        log.info 'Configuring {} with realm \'{}\', {}', context.contextPath, realm, realmConfigFile
        configurer.configureSecurity(context, realm, realmConfigFile.toString(), params.singleSignOn ?: false)
      } else
        log.warn 'loginService is already configured, ignoring realm \'{}\', {}', realm, realmConfigFile
    }
  }

  def createAndConfigureServer(Closure configureContext = null) {

    def server = configurer.createServer()

    File baseDir = new File(params.baseDir)
    new File(baseDir, 'webapps').mkdirs()

    configurer.applyJettyXml(server, params.serverConfigFile)
    configurer.configureConnectors(server, params)

    List handlers = []
    //params.useFileMappedBuffer = true // DBG

    for(Map webapp in params.webApps) {
      def context = configurer.createWebAppContext(params, webapp)
      context.displayName = webapp.contextPath
      context.contextPath = webapp.contextPath

      if(!params.supressSetConfigurations) {
        List configurations = configurer.getConfigurations(webapp)
        BaseResourceConfiguration baseRes = configurations.find { it instanceof BaseResourceConfiguration }
        if(baseRes) {
          baseRes.setExtraResourceBases(webapp.extraResourceBases)
          baseRes.addBaseResourceListener this.&configureWithBaseResource.curry(webapp)
        }
        configurer.setConfigurationsToWebAppContext(context, configurations)
      }

      File tempDir = new File(baseDir, 'webapps-exploded' + context.contextPath)
      tempDir.mkdirs()
      log.debug 'jetty context temp directory: {}', tempDir
      context.setTempDirectory(tempDir)
      if(context.respondsTo('setPersistTempDirectory')) // not supported on older jetty versions
        context.setPersistTempDirectory(true)

      webapp.initParams?.each { key, value ->
        context.setInitParameter(key, value)
      }
<commit_msg>fixed issue #101: 'Jetty.xml Rewrite Handler doesnt seem to take effect.'<commit_after>/*
 * Gretty
 *
 * Copyright (C) 2013-2014 <NAME> and contributors.
 *
 * See the file "LICENSE" for copying and usage permission.
 * See the file "CONTRIBUTORS" for complete list of contributors.
 */
package org.akhikhl.gretty

import java.util.jar.JarEntry
import java.util.jar.JarFile
import org.apache.commons.io.FilenameUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 *
 * @author akhikhl
 */
class JettyServerConfigurer {

  protected final Logger log
  protected final JettyConfigurer configurer
  protected final Map params

  JettyServerConfigurer(JettyConfigurer configurer, Map params) {
    log = LoggerFactory.getLogger(this.getClass())
    this.configurer = configurer
    this.params = params
  }

  protected void configureWithBaseResource(Map webapp, context) {

    URL contextConfigFile = getContextConfigFile(context.baseResource, params.servletContainerId)
    if(!contextConfigFile && webapp.contextConfigFile)
      contextConfigFile = new File(webapp.contextConfigFile).toURI().toURL()
    configurer.applyContextConfigFile(context, contextConfigFile)

    String realm = webapp.realm ?: params.realm
    URL realmConfigFile = getRealmFile(context.baseResource, params.servletContainerId)
    if(!realmConfigFile) {
      if(webapp.realmConfigFile) {
        realmConfigFile = new File(webapp.realmConfigFile).canonicalFile.toURI().toURL()
      }
      else if(params.realmConfigFile) {
        realmConfigFile = new File(params.realmConfigFile).canonicalFile.toURI().toURL()
      }
    }
    if(realm && realmConfigFile) {
      if(context.securityHandler.loginService == null) {
        log.info 'Configuring {} with realm \'{}\', {}', context.contextPath, realm, realmConfigFile
        configurer.configureSecurity(context, realm, realmConfigFile.toString(), params.singleSignOn ?: false)
      } else
        log.warn 'loginService is already configured, ignoring realm \'{}\', {}', realm, realmConfigFile
    }
  }

  def createAndConfigureServer(Closure configureContext = null) {

    def server = configurer.createServer()

    File baseDir = new File(params.baseDir)
    new File(baseDir, 'webapps').mkdirs()

    configurer.configureConnectors(server, params)
    configurer.applyJettyXml(server, params.serverConfigFile)

    List handlers = []
    //params.useFileMappedBuffer = true // DBG

    for(Map webapp in params.webApps) {
      def context = configurer.createWebAppContext(params, webapp)
      context.displayName = webapp.contextPath
      context.contextPath = webapp.contextPath

      if(!params.supressSetConfigurations) {
        List configurations = configurer.getConfigurations(webapp)
        BaseResourceConfiguration baseRes = configurations.find { it instanceof BaseResourceConfiguration }
        if(baseRes) {
          baseRes.setExtraResourceBases(webapp.extraResourceBases)
          baseRes.addBaseResourceListener this.&configureWithBaseResource.curry(webapp)
        }
        configurer.setConfigurationsToWebAppContext(context, configurations)
      }

      File tempDir = new File(baseDir, 'webapps-exploded' + context.contextPath)
      tempDir.mkdirs()
      log.debug 'jetty context temp directory: {}', tempDir
      context.setTempDirectory(tempDir)
      if(context.respondsTo('setPersistTempDirectory')) // not supported on older jetty versions
        context.setPersistTempDirectory(true)

      webapp.initParams?.each { key, value ->
        context.setInitParameter(key, value)
      }

{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.003240349394195548, 'mean_word_length': 3.8457337883959046, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '1224047', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1984, 'n_words': 414}