ContentNegotiationStrategy class abstract interface

Abstract strategy for determining the appropriate MediaType to use when writing HTTP response bodies.

Overview

The ContentNegotiationStrategy defines the contract for content negotiation algorithms in Jetleaf. Different implementations can employ various strategies:

  • Client-driven: Based on Accept headers
  • Server-driven: Based on server configuration
  • Dynamic: Based on request parameters or resource state
  • Hybrid: Combining multiple factors

Responsibilities

  • Analyze request headers and context to determine response media type
  • Consider request parameters, resource properties, and server configuration
  • Return the most appropriate MediaType for the response
  • Provide fallback behavior when no suitable type is found

Usage Pattern

final strategy = ClientAcceptHeaderNegotiationStrategy();
final mediaType = strategy.negotiate(returnValue, method, request);
// mediaType is now the negotiated content type

Implementation Examples

  • ClientAcceptHeaderNegotiationStrategy: Uses Accept header from request
  • FixedMediaTypeStrategy: Always returns a specific media type
  • ConfigBasedStrategy: Uses application configuration
  • HybridStrategy: Combines multiple strategies with fallback

Design Notes

  • Implementations should be stateless and thread-safe
  • Should handle missing/malformed headers gracefully
  • Must return a valid MediaType or null to indicate no match
  • Can throw exceptions for invalid states
Implementers

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

negotiate(Method? method, ServerHttpRequest request, List<MediaType> supportedMediaTypes) → Future<MediaType?>
Negotiates the appropriate MediaType for the response.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited