-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1207 from johnoliver/cache-validation
Add cache validation headers. Fix date time serialization
- Loading branch information
Showing
20 changed files
with
320 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
16 changes: 15 additions & 1 deletion
16
...rsistence/src/main/kotlin/net/adoptium/api/v3/dataSources/persitence/mongo/UpdatedInfo.kt
This file contains 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
53 changes: 53 additions & 0 deletions
53
...nce/src/main/kotlin/net/adoptium/api/v3/dataSources/persitence/mongo/codecs/DateCodecs.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package net.adoptium.api.v3.dataSources.persitence.mongo.codecs | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.JsonDeserializer | ||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.JsonSerializer | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import com.fasterxml.jackson.databind.node.LongNode | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.databind.node.TextNode | ||
import net.adoptium.api.v3.TimeSource | ||
import java.time.Instant | ||
import java.time.format.DateTimeFormatter | ||
import java.util.* | ||
|
||
object DateCodecs { | ||
class DateSerializer : JsonSerializer<Date>() { | ||
override fun serialize(p0: Date?, p1: JsonGenerator?, p2: SerializerProvider?) { | ||
p1?.writeStartObject(); | ||
p1?.writeStringField( | ||
"\$date", | ||
p0?.toInstant()?.atZone(TimeSource.ZONE)?.format(DateTimeFormatter.ISO_INSTANT) | ||
); | ||
p1?.writeEndObject(); | ||
} | ||
} | ||
|
||
class DateDeserializer : JsonDeserializer<Date>() { | ||
override fun deserialize(jsonParser: JsonParser?, context: DeserializationContext?): Date { | ||
when (val value = jsonParser?.readValueAsTree<JsonNode>()) { | ||
is ObjectNode -> { | ||
val datetime = DateTimeFormatter.ISO_INSTANT.parse(value.get("\$date").asText()) | ||
return Date(Instant.from(datetime).toEpochMilli()) | ||
} | ||
|
||
is TextNode -> { | ||
val datetime = DateTimeFormatter.ISO_INSTANT.parse(value.asText()) | ||
return Date(Instant.from(datetime).toEpochMilli()) | ||
} | ||
|
||
is LongNode -> { | ||
return Date(value.asLong()) | ||
} | ||
} | ||
|
||
throw IllegalArgumentException("Could not parse ZonedDateTime") | ||
} | ||
|
||
} | ||
|
||
} |
This file contains 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
50 changes: 50 additions & 0 deletions
50
...ain/kotlin/net/adoptium/api/v3/dataSources/persitence/mongo/codecs/ZonedDateTimeCodecs.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package net.adoptium.api.v3.dataSources.persitence.mongo.codecs | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.JsonDeserializer | ||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.JsonSerializer | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import com.fasterxml.jackson.databind.node.LongNode | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.databind.node.TextNode | ||
import net.adoptium.api.v3.TimeSource | ||
import java.time.Instant | ||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
object ZonedDateTimeCodecs { | ||
class ZonedDateTimeDeserializer : JsonDeserializer<ZonedDateTime>() { | ||
override fun deserialize(jsonParser: JsonParser?, context: DeserializationContext?): ZonedDateTime { | ||
when (val value = jsonParser?.readValueAsTree<JsonNode>()) { | ||
is ObjectNode -> { | ||
val datetime = DateTimeFormatter.ISO_INSTANT.parse(value.get("\$date").asText()) | ||
return Instant.from(datetime).atZone(TimeSource.ZONE) | ||
} | ||
|
||
is TextNode -> { | ||
val datetime = DateTimeFormatter.ISO_INSTANT.parse(value.asText()) | ||
return Instant.from(datetime).atZone(TimeSource.ZONE) | ||
} | ||
|
||
is LongNode -> { | ||
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(value.asLong()), TimeSource.ZONE) | ||
} | ||
} | ||
|
||
throw IllegalArgumentException("Could not parse ZonedDateTime") | ||
} | ||
|
||
} | ||
|
||
class ZonedDateTimeSerializer : JsonSerializer<ZonedDateTime>() { | ||
override fun serialize(p0: ZonedDateTime?, p1: JsonGenerator?, p2: SerializerProvider?) { | ||
p1?.writeStartObject(); | ||
p1?.writeStringField("\$date", p0?.format(DateTimeFormatter.ISO_INSTANT)); | ||
p1?.writeEndObject(); | ||
} | ||
|
||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
...tlin/net/adoptium/api/v3/dataSources/persitence/mongo/codecs/ZonedDateTimeDeserializer.kt
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
...arent/adoptium-api-v3-frontend/src/main/kotlin/net/adoptium/api/v3/CacheControlService.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package net.adoptium.api.v3 | ||
|
||
import jakarta.enterprise.context.ApplicationScoped | ||
import jakarta.inject.Inject | ||
import jakarta.ws.rs.container.ContainerRequestContext | ||
import jakarta.ws.rs.container.ContainerRequestFilter | ||
import jakarta.ws.rs.container.ContainerResponseContext | ||
import jakarta.ws.rs.core.EntityTag | ||
import jakarta.ws.rs.ext.Provider | ||
import net.adoptium.api.v3.dataSources.APIDataStore | ||
import org.jboss.resteasy.reactive.common.headers.CacheControlDelegate | ||
import org.jboss.resteasy.reactive.common.util.ExtendedCacheControl | ||
import org.jboss.resteasy.reactive.server.ServerResponseFilter | ||
|
||
@Provider | ||
@ApplicationScoped | ||
class CacheControlService @Inject constructor(private var apiDataStore: APIDataStore) : ContainerRequestFilter { | ||
|
||
private val CACHE_CONTROLLED_PATHS = listOf("/v3/info", "/v3/assets") | ||
|
||
override fun filter(requestContext: ContainerRequestContext?) { | ||
if (requestContext == null) return | ||
|
||
requestContext.uriInfo?.path?.let { path -> | ||
if (CACHE_CONTROLLED_PATHS.any { path.startsWith(it) }) { | ||
val etag = apiDataStore.getUpdateInfo().hexChecksum | ||
val lastModified = apiDataStore.getUpdateInfo().lastModified | ||
|
||
if (lastModified == null || etag == null) { | ||
return | ||
} | ||
|
||
val builder = | ||
requestContext | ||
.request | ||
.evaluatePreconditions(lastModified, EntityTag(etag)) | ||
|
||
if (builder != null) { | ||
requestContext.abortWith(builder.build()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ServerResponseFilter | ||
fun responseFilter(responseContext: ContainerResponseContext?) { | ||
val ecc = ExtendedCacheControl(); | ||
ecc.isPublic = true | ||
|
||
if (apiDataStore.getUpdateInfo().hexChecksum == null || | ||
apiDataStore.getUpdateInfo().lastModifiedFormatted == null) { | ||
return | ||
} | ||
|
||
responseContext?.headers?.add("ETag", apiDataStore.getUpdateInfo().hexChecksum) | ||
responseContext?.headers?.add("Last-Modified", apiDataStore.getUpdateInfo().lastModifiedFormatted) | ||
responseContext?.headers?.add("Cache-Control", CacheControlDelegate.INSTANCE.toString(ecc)) | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.