Skip to content

Commit

Permalink
Merge pull request #1282 from johnoliver/fix-etag
Browse files Browse the repository at this point in the history
Remove unused hexChecksum
  • Loading branch information
karianna authored Nov 27, 2024
2 parents bcf5ba5 + 355e6a9 commit 191f765
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package net.adoptium.api.v3.dataSources.persitence.mongo

import java.math.BigInteger
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.*

@JsonIgnoreProperties(ignoreUnknown = true)
data class UpdatedInfo(
val time: ZonedDateTime,
val checksum: String,
val hashCode: Int,
val hexChecksum: String? = BigInteger(1, Base64.getDecoder().decode(checksum)).toString(16),
val lastModified: Date? = Date.from(time.toInstant()),
val lastModifiedFormatted: String? = lastModified
?.toInstant()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ class CacheControlService @Inject constructor(private var apiDataStore: APIDataS

private fun calculateEtag(requestContext: ContainerRequestContext): String {
val md = MessageDigest.getInstance("SHA1")
if (apiDataStore.getUpdateInfo().hexChecksum != null) {
md.update(HexFormat.of().parseHex(apiDataStore.getUpdateInfo().hexChecksum))
try {
md.update(Base64.getDecoder().decode(apiDataStore.getUpdateInfo().checksum))
} catch (e: Exception) {
// Should not happen as the hex checksum should always be a valid Base64 string
md.update(apiDataStore.getUpdateInfo().checksum.toByteArray())
}
md.update(requestContext.uriInfo.requestUri.toString().toByteArray())
return BigInteger(1, md.digest()).toString(16)
Expand All @@ -46,12 +49,12 @@ class CacheControlService @Inject constructor(private var apiDataStore: APIDataS

val lastModified = apiDataStore.getUpdateInfo().lastModified

if (lastModified == null || etag == null) {
if (lastModified == null) {
return
}

val builder =
requestContext!!
requestContext
.request
.evaluatePreconditions(lastModified, EntityTag(etag))

Expand All @@ -70,8 +73,7 @@ class CacheControlService @Inject constructor(private var apiDataStore: APIDataS
ecc.maxAge = MAX_CACHE_AGE_IN_SEC
ecc.sMaxAge = MAX_CACHE_AGE_IN_SEC

if (apiDataStore.getUpdateInfo().hexChecksum == null ||
apiDataStore.getUpdateInfo().lastModifiedFormatted == null) {
if (apiDataStore.getUpdateInfo().lastModifiedFormatted == null) {
return
}

Expand Down

0 comments on commit 191f765

Please sign in to comment.