Last active
July 27, 2018 11:27
-
-
Save mlesikov/753d4439944c7a908c8779b8d66fb975 to your computer and use it in GitHub Desktop.
Chain of responsibility example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
core.security | |
interface SessionHandler { | |
fun getSessionById(sessionId: String): Session | |
fun saveSessionInCache(session: Session) | |
} | |
data class Session(val sid: String = "") | |
adapter.postrgree | |
adapter.mysql | |
class PersistentSQLSessionHandler(ConnectionPool, PosgreeeAdpater) : SessionHandler { | |
override fun getSessionById(sessionId: String): Session { | |
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
override fun saveSessionInCache(session: Session) { | |
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
} | |
adapter.gcp.datastore | |
class PersistentSessionHandler : SessionHandler { | |
override fun getSessionById(sessionId: String): Session { | |
val dss = DatastoreServiceFactory.getDatastoreService() | |
val entity = dss.get(KeyFactory.createKey("SID", sessionId)) ?: return Optional.empty<Session>() | |
val typedEntity = TypedEntity(entity) | |
return Session(typedEntity.stringOr("value", "")) | |
} | |
override fun saveSessionInCache(session: Session) { | |
val dss = DatastoreServiceFactory.getDatastoreService() | |
val entity = Entity("SID", session.sid) | |
val typedEntity = TypedEntity(entity) | |
typedEntity.setUnindexedProperty("value", session.sid) | |
dss.put(typedEntity.raw()) | |
} | |
} | |
class CachedSessionHandler(private val orgin: SessionHandler) : SessionHandler { | |
val service: MemcacheService = MemcacheServiceFactory.getMemcacheService() | |
override fun getSessionById(sessionId: String): Session { | |
val cashedSession = service.get("sid$sessionId") | |
return if(cashedSession == null){ | |
val session = orgin.getSessionById(sessionId) | |
service.put("SIDsid$sessionId", session) | |
session | |
} else { | |
cashedSession as Session | |
} | |
} | |
} | |
class SecuredSessionHandler(private val origin: SessionHandler) : SessionHandler { | |
override fun getSessionById(sessionId: String): Session { | |
return origin.getSessionById(sessionId) | |
} | |
override fun saveSessionInCache(session: Session) { | |
val activeSession = origin.getSessionById(session.sid) | |
if(activeSession == null) origin.saveSessionInCache(session) | |
throw UserAlreadyHasSessionException() | |
} | |
} | |
class SeesionLoader(val sessionHandler: SessionHandler) { | |
fun doSomething(sid: String) { | |
//logic | |
val session = sessionHandler.getSessionById(sid) | |
sessionHandler.saveSessionInCache(Session("2342234")) | |
} | |
} | |
class AppBootstrap { | |
fun inint() { | |
val sessions = PersistentSession() | |
before(SecusitryFilter(sessions)) | |
LoaginHandler(sessions) | |
val loader = SeesionLoader(SecuredSessionHandler(CachedSessionHandler(PersistentSessionHandler()))) | |
val loaderSql = SeesionLoader(PersistentSQLSessionHandler()) | |
} | |
} | |
interface Sessions { | |
fun issueSession(expDate: LocalDate, userId: String): Session | |
fun get(sessionId: String): Session | |
fun find(userId: String) | |
interface Provider<T> { | |
fun get(): T | |
} | |
class MemcacheServiceProvider : Provider<MemcacheService> { | |
override fun get(): MemcacheService = MemcacheServiceFactory.getMemcacheService() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Controller{ | |
fun handle(request: Request, response: Response, instant:Instant, currentUser:User): Any? { | |
} | |
class AppController(controller: Controller, users: Users, session: Sessions) : Route { | |
override fun handle(request: Request?, response: Response?): Any { | |
val session = sessions.get(request.headers("SID")) | |
val user = userRepo.get(session.userId) | |
return controller.handle(request, response, Instant.now(), user) | |
} | |
} | |
post( AppController(RegisterUserController(users) ,users,sessions)) | |
post( AppController(CreateTransactionController(users) ,users,sessions)) | |
delete( AppController(CancelTransactionController(users) ,users,sessions)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment