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

org.scalatra.SslRequirement.scala Maven / Gradle / Ivy

The newest version!
package org.scalatra

import java.net.URI

/**
 * Redirects unsecured requests to the corresponding secure URL.
 */
trait SslRequirement extends Handler { 
  abstract override def handle(req: RequestT, res: ResponseT) {
    if (!req.isSecure) {
      val oldUri = req.uri
      val port = securePortMap.lift(oldUri.getPort) getOrElse 443
      val uri = new URI(
	"https", oldUri.getRawUserInfo, oldUri.getHost, port,
	oldUri.getPath, oldUri.getQuery, oldUri.getFragment).toString
      res.redirect(uri)
    } else {
      super.handle(req, res)
    }
  }
  
  /**
   * Maps unsecured ports to secure ports.  By default, 80 redirects to
   * 443, and 8080 to 8443.
   */
  protected def securePortMap: PartialFunction[Int, Int] = 
    Map(80 -> 443, 8080 -> 8443)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy