You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's add a mechanism to return "304 Not Modified" versions of DataResponse<T>. While it is always possible to control the response status via JAX-RS Response object, there's a benefit to preserving DataResponse<T> in the API method signature, as it allows documentation tools like Swagger to guess the response type.
So while we are not (yet) building full cache control support in Agrest, let's at least allow the users to implement conditional requests without changing the endpoint method signatures.
Changes required:
DataResponseBodyWriter and SimpleResponseBodyWriter must ensure an empty body if the status is 304
AgResponse should support setting cache control (or arbitrary) headers
ResponseStatusFilter should append those headers to response
Headers must be settable from stage callbacks (i.e. should be set at the Context level)
Here is a simplied example of a custom endpoint that recognizes ETags. Real-life example would need to provide a smarter etag calculation function, and also set a reasonable Cache-Control header.
publicstaticclassResource {
@ContextprivateConfigurationconfig;
@GETpublicDataResponse<P1> conditional(@HeaderParam("If-None-Match") StringeTag) {
returnAgJaxrs
.select(P1.class, config)
.routingStage(SelectStage.APPLY_SERVER_PARAMS, c -> stopIfMatch(c, eTag))
.stage(SelectStage.ENCODE, this::cacheControl)
.get();
}
ProcessorOutcomestopIfMatch(SelectContext<?> context, StringeTag) {
if (!"my-tag".equals(eTag)) {
returnProcessorOutcome.CONTINUE;
}
context.setResponseStatus(HttpStatus.NOT_MODIFIED);
// even though we are not modified, per HTTP standards, we must return the original ETagcontext.addResponseHeader("etag", eTag);
returnProcessorOutcome.STOP;
}
voidcacheControl(SelectContext<?> context) {
// calculate response etag context.addResponseHeader("etag", "my-tag");
}
}
The text was updated successfully, but these errors were encountered:
Let's add a mechanism to return "304 Not Modified" versions of
DataResponse<T>
. While it is always possible to control the response status via JAX-RSResponse
object, there's a benefit to preservingDataResponse<T>
in the API method signature, as it allows documentation tools like Swagger to guess the response type.So while we are not (yet) building full cache control support in Agrest, let's at least allow the users to implement conditional requests without changing the endpoint method signatures.
Changes required:
Here is a simplied example of a custom endpoint that recognizes ETags. Real-life example would need to provide a smarter etag calculation function, and also set a reasonable
Cache-Control
header.The text was updated successfully, but these errors were encountered: