Some ETag refactoring
This commit is contained in:
@@ -1,17 +1,15 @@
|
||||
package controllers
|
||||
|
||||
import javax.inject.*
|
||||
import play.api.*
|
||||
import play.api.mvc.*
|
||||
import authentication.{AuthenticatedAction, AuthenticatedRequest}
|
||||
import caching.{Etagable, CheckFresh}
|
||||
import _root_.controllers.authentication.{AuthenticatedAction, AuthenticatedRequest}
|
||||
import _root_.controllers.caching.{CheckFresh, DigestEtagable}
|
||||
import models.User
|
||||
import play.api.libs.json.Json
|
||||
import play.api.mvc.*
|
||||
|
||||
import java.nio.ByteBuffer
|
||||
import java.io.DataOutputStream
|
||||
import java.nio.charset.StandardCharsets.UTF_8
|
||||
import java.security.MessageDigest
|
||||
import java.util.Base64
|
||||
import javax.inject.*
|
||||
|
||||
/**
|
||||
* This controller creates an `Action` to handle HTTP requests to the
|
||||
@@ -37,21 +35,15 @@ class HomeController @Inject()(val controllerComponents: ControllerComponents,
|
||||
val user = request.user
|
||||
checkFresh(user) {
|
||||
// TODO: put the max-age somewhere into config
|
||||
Ok(Json.toJson(user)).withHeaders("Etag" -> user.etag, "Cache-Control" -> "private, max-age=15, must-revalidate")
|
||||
Ok(Json.toJson(user)).withHeaders("Cache-Control" -> "private, max-age=15, must-revalidate")
|
||||
}
|
||||
}
|
||||
|
||||
private given Etagable[models.User] with {
|
||||
private val base64 = Base64.getUrlEncoder
|
||||
|
||||
extension (a: User)
|
||||
override def etag: String = {
|
||||
val md = MessageDigest.getInstance("SHA-1")
|
||||
md.update(ByteBuffer.allocate(8).putLong(a.id.getOrElse(0)))
|
||||
md.update(a.gid.getBytes(UTF_8))
|
||||
md.update(a.username.getBytes(UTF_8))
|
||||
|
||||
base64.encodeToString(md.digest())
|
||||
}
|
||||
private given DigestEtagable[models.User] with {
|
||||
override protected def writeDigestData(a: User, s: DataOutputStream): Unit = {
|
||||
s.writeLong(a.id.getOrElse(0))
|
||||
s.write(a.gid.getBytes(UTF_8))
|
||||
s.write(a.username.getBytes(UTF_8))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user