Skip to content

Commit

Permalink
Merge pull request #197 from ba-st/etag_calculation_when_content_lang…
Browse files Browse the repository at this point in the history
…uage_present

Fix etag calculation for updates when the content language in the req…
  • Loading branch information
gcotelli committed Aug 23, 2024
2 parents da18739 + 1af252e commit 7ada0ab
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,24 @@ RESTfulRequestHandlerBehavior >> applyCachingDirectivesFor: aResource to: respon
]

{ #category : 'private' }
RESTfulRequestHandlerBehavior >> assert: etag matchesEntityTagOf: entity encodedAs: mediaType within: requestContext [
RESTfulRequestHandlerBehavior >> assert: etag matchesEntityTagOf: entity encodedAs: httpRequest within: requestContext [

etag = ( self entityTagOf: entity encodedAs: mediaType within: requestContext )
ifFalse: [ HTTPClientError preconditionFailed signal ]
| calculatedEtag entityTagCalculation |
entityTagCalculation := [
self
entityTagOf: entity
encodedAs: httpRequest contentType
within: requestContext ].

"The request content can be in another language that the one requested
in the Accept header, so in that case we need to calculated the etag
of the entity with this language as the current one"
calculatedEtag := httpRequest contentLanguageTags
ifEmpty: entityTagCalculation
ifNotEmpty: [ :languages |
CurrentLocale use: languages first during: entityTagCalculation ].

etag = calculatedEtag ifFalse: [ HTTPClientError preconditionFailed signal ]
]

{ #category : 'decoding/encoding' }
Expand Down Expand Up @@ -188,29 +202,26 @@ RESTfulRequestHandlerBehavior >> from: httpRequest within: requestContext get: a
{ #category : 'API' }
RESTfulRequestHandlerBehavior >> from: httpRequest within: requestContext get: findBlock thenUpdateWith: updateBlock [

| etag |
| etag |

etag := self entityTagToMatchBasedOn: httpRequest.
etag := self entityTagToMatchBasedOn: httpRequest.

^ self
from: httpRequest
within: requestContext
get: [ :id |
| resourceToUpdate updatedResource |

resourceToUpdate := findBlock cull: id.
self
assert: etag
matchesEntityTagOf: resourceToUpdate
encodedAs: httpRequest contentType
within: requestContext.

updatedResource := self decode: httpRequest within: requestContext.
self exceptionHandler handleConflictsDuring: [
self exceptionHandler handleDecodingFailedDuring: [
updateBlock value: resourceToUpdate value: updatedResource ]
]
]
^ self from: httpRequest within: requestContext get: [ :id |
| resourceToUpdate updatedResource |

resourceToUpdate := findBlock cull: id.
self
assert: etag
matchesEntityTagOf: resourceToUpdate
encodedAs: httpRequest
within: requestContext.

updatedResource := self decode: httpRequest within: requestContext.
self exceptionHandler handleConflictsDuring: [
self exceptionHandler handleDecodingFailedDuring: [
updateBlock value: resourceToUpdate value: updatedResource ]
]
]
]

{ #category : 'API' }
Expand Down

0 comments on commit 7ada0ab

Please sign in to comment.