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

no.kodeworks.kvarg.util.PageDirectives.scala Maven / Gradle / Ivy

There is a newer version: 0.7
Show newest version
package no.kodeworks.kvarg.util

import akka.event.LoggingAdapter
import akka.http.javadsl.server.AuthorizationFailedRejection
import akka.http.scaladsl.server.{Directive, Directive0, Directive1}
import akka.http.scaladsl.server.Directives._
import no.kodeworks.kvarg.model.Page
import com.softwaremill.session.SessionDirectives._
import com.softwaremill.session.SessionOptions._
import com.softwaremill.session.{SessionDirectives, SessionManager, SessionUtil}

import scala.language.postfixOps

trait PageDirectives {
  def ensurePage(session: String)(implicit pageManager: SessionManager[String]): Directive1[Page] = {
    val page = createPage

    touchRequiredPage(session) |
      mapToPage(session, setSession(oneOff[String], usingHeaders, page).tflatMap(_ => provide(page)))
  }

  def touchRequiredPage(session: String)(implicit pm: SessionManager[String]): Directive1[Page] =
    mapToPage(session, recoverFromAuthFailed & SessionDirectives.touchRequiredSession(oneOff[String], usingHeaders))

  def requiredPage(session: String)(implicit
                                    pm: SessionManager[String],
                                    log: LoggingAdapter): Directive1[Page] = {
    log.debug("Requiring page")
    mapToPage(session, SessionDirectives.requiredSession(oneOff[String], usingHeaders))
  }

  def mapToPage(session: String, d: Directive1[String]) = d.map { p =>
    Page(p, session)
  }

  def createPage: String = SessionUtil.randomString(64)

  val recoverFromAuthFailed = mapRejections(_.filterNot(_.isInstanceOf[AuthorizationFailedRejection]))
}

object PageDirectives extends PageDirectives




© 2015 - 2025 Weber Informatics LLC | Privacy Policy