Permalink
Browse files

Enabled accessing SkinnyHttpSession everywhere

  • Loading branch information...
1 parent a6303a4 commit 37713c421cc05d9e50e0e45964b54db16dfec469 @seratch seratch committed Mar 6, 2014
@@ -8,6 +8,7 @@ import java.util.Locale
import org.joda.time._
import skinny.I18n
import grizzled.slf4j.Logging
+import javax.servlet.http.HttpServletRequest
object RequestScopeFeature {
@@ -35,6 +36,20 @@ object RequestScopeFeature {
// Used in the SkinnyResource & TemplateEngineFeature
val ATTR_RESOURCE_NAME = "resourceName"
val ATTR_RESOURCES_NAME = "resourcesName"
+
+ def requestScope(request: HttpServletRequest): scala.collection.concurrent.Map[String, Any] = {
+ request.getAttribute(REQUEST_SCOPE_KEY) match {
+ case null =>
+ val values = scala.collection.concurrent.TrieMap[String, Any]()
+ request.setAttribute(REQUEST_SCOPE_KEY, values)
+ values
+ case values: scala.collection.concurrent.Map[_, _] =>
+ values.asInstanceOf[scala.collection.concurrent.Map[String, Any]]
+ case _ => throw new RequestScopeConflictException(
+ s"Don't use '${REQUEST_SCOPE_KEY}' for request attribute key name.")
+ }
+ }
+
}
/**
@@ -48,7 +63,7 @@ trait RequestScopeFeature extends ScalatraBase with SnakeCasedParamKeysFeature w
* Registers default attributes in the request scope.
*/
before() {
- if (requestScope().isEmpty) {
+ if (requestScope().get(ATTR_SKINNY).isEmpty) {
set(ATTR_SKINNY, skinny.Skinny(requestScope()))
// requestPath/contextPath
val requestPathWithContext = contextPath + requestPath
@@ -71,19 +86,7 @@ trait RequestScopeFeature extends ScalatraBase with SnakeCasedParamKeysFeature w
*
* @return whole attributes
*/
- def requestScope(): scala.collection.concurrent.Map[String, Any] = {
- request.getAttribute(REQUEST_SCOPE_KEY) match {
- case null =>
- val values = scala.collection.concurrent.TrieMap[String, Any]()
- request.setAttribute(REQUEST_SCOPE_KEY, values)
- values
- case values: scala.collection.concurrent.Map[_, _] =>
- values.asInstanceOf[scala.collection.concurrent.Map[String, Any]]
- case _ => throw new RequestScopeConflictException(
- s"Don't use '${REQUEST_SCOPE_KEY}' for request attribute key name.")
- }
- }
-
+ def requestScope(): scala.collection.concurrent.Map[String, Any] = RequestScopeFeature.requestScope(request)
/**
* Set attribute to request scope.
*
@@ -28,24 +28,7 @@ object SkinnySessionFilter {
trait SkinnySessionFilter extends SkinnyFilter { self: FlashFeature with CSRFProtectionFeature with LocaleFeature =>
import SkinnySessionFilter._
- def initializeSkinnySession: SkinnyHttpSession = {
- val jsessionIdCookieName = servletContext.getSessionCookieConfig.getName
- val jsessionIdInCookie = request.getCookies.find(_.getName == jsessionIdCookieName).map(_.getValue)
- val jsessionIdInSession = request.getSession.getId
- logger.debug(s"[Skinny Session] session id (cookie: ${jsessionIdInCookie}, local session: ${jsessionIdInSession})")
-
- val expireAt = SkinnySession.getExpireAtFromMaxInactiveInterval(session.getMaxInactiveInterval)
- val skinnySession = if (jsessionIdInCookie.isDefined && jsessionIdInCookie.get != jsessionIdInSession) {
- SkinnySession.findOrCreate(jsessionIdInCookie.get, Option(jsessionIdInSession), expireAt)
- } else {
- SkinnySession.findOrCreate(jsessionIdInSession, None, expireAt)
- }
- val skinnySessionWrapper = new SkinnyHttpSessionJDBCImpl(request.getSession, skinnySession)
- logger.debug("[Skinny Session] " +
- s"initial attributes: ${skinnySession.attributeNames.map(name => s"$name -> ${skinnySession.getAttribute(name)}")}")
- set(ATTR_SKINNY_SESSION_IN_REQUEST_SCOPE, skinnySessionWrapper)
- skinnySessionWrapper
- }
+ def initializeSkinnySession: SkinnyHttpSession = SkinnyHttpSession.getOrCreate(request)
beforeAction()(initializeSkinnySession)
@@ -1,5 +1,11 @@
package skinny.session
+import skinny.session.jdbc.SkinnySession
+import javax.servlet.http.HttpServletRequest
+import skinny.filter.SkinnySessionFilter
+import grizzled.slf4j.Logging
+import skinny.controller.feature.RequestScopeFeature
+
/**
* SkinnySession works as a shared session for multiple servers.
*/
@@ -18,3 +24,28 @@ trait SkinnyHttpSession {
def invalidate(): Unit
}
+
+object SkinnyHttpSession extends Logging {
+
+ def getOrCreate(request: HttpServletRequest): SkinnyHttpSession = {
+ val jsessionIdCookieName = request.getServletContext.getSessionCookieConfig.getName
+ val jsessionIdInCookie = request.getCookies.find(_.getName == jsessionIdCookieName).map(_.getValue)
+ val jsessionIdInSession = request.getSession.getId
+ logger.debug(s"[Skinny Session] session id (cookie: ${jsessionIdInCookie}, local session: ${jsessionIdInSession})")
+
+ val expireAt = SkinnySession.getExpireAtFromMaxInactiveInterval(request.getSession.getMaxInactiveInterval)
+ val skinnySession = if (jsessionIdInCookie.isDefined && jsessionIdInCookie.get != jsessionIdInSession) {
+ SkinnySession.findOrCreate(jsessionIdInCookie.get, Option(jsessionIdInSession), expireAt)
+ } else {
+ SkinnySession.findOrCreate(jsessionIdInSession, None, expireAt)
+ }
+
+ val skinnySessionWrapper = new SkinnyHttpSessionJDBCImpl(request.getSession, skinnySession)
+ logger.debug("[Skinny Session] " +
+ s"initial attributes: ${skinnySession.attributeNames.map(name => s"$name -> ${skinnySession.getAttribute(name)}")}")
+ val requestScope = RequestScopeFeature.requestScope(request)
+ requestScope += (SkinnySessionFilter.ATTR_SKINNY_SESSION_IN_REQUEST_SCOPE -> skinnySessionWrapper)
+ skinnySessionWrapper
+ }
+
+}

0 comments on commit 37713c4

Please sign in to comment.