All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalatra.servlet.HasMultipartConfig.scala Maven / Gradle / Ivy

There is a newer version: 3.0.0-M5-javax
Show newest version
package org.scalatra
package servlet

import javax.servlet.ServletContext
import scala.language.reflectiveCalls

object HasMultipartConfig {
  val DefaultMultipartConfig = MultipartConfig()
  val MultipartConfigKey = "org.scalatra.MultipartConfigKey"
}
trait HasMultipartConfig extends Initializable { self: { def servletContext: ServletContext } =>

  import org.scalatra.servlet.HasMultipartConfig._

  private[this] def multipartConfigFromContext: Option[MultipartConfig] = {
    // hack to support the tests without changes
    providedConfig orElse {
      try {
        (Option(servletContext)
          flatMap (sc => Option(sc.getAttribute(MultipartConfigKey)))
          filterNot (_ == null)
          map (_.asInstanceOf[MultipartConfig]))
      } catch {
        case _: NullPointerException => Some(DefaultMultipartConfig)
      }

    }
  }

  def multipartConfig: MultipartConfig = try {
    multipartConfigFromContext getOrElse DefaultMultipartConfig
  } catch {
    case e: Throwable =>
      System.err.println("Couldn't get the multipart config from the servlet context because: ")
      e.printStackTrace()
      DefaultMultipartConfig
  }

  private[this] var providedConfig: Option[MultipartConfig] = None

  abstract override def initialize(config: ConfigT): Unit = {
    super.initialize(config)

    providedConfig foreach { _ apply config.context }
  }

  def configureMultipartHandling(config: MultipartConfig): Unit = {
    providedConfig = Some(config)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy