The aether-core module provides the fundamental building blocks for Aether applications, including the Exchange interface for handling HTTP interactions and the Pipeline for middleware orchestration.
The Exchange interface represents a single HTTP request-response cycle. It provides access to the request data, response manipulation, and helper methods for sending various types of responses.
| Property | Type | Description |
|---|---|---|
request | Request | The incoming HTTP request. |
response | Response | The outgoing HTTP response. |
attributes | Attributes | A map for storing request-scoped data (e.g., extracted path parameters, user sessions). |
respond(statusCode: Int = 200, body: String)Sends a plain text response.
Content-Type to text/plain; charset=utf-8.respondHtml(statusCode: Int = 200, html: String)Sends an HTML response.
Content-Type to text/html; charset=utf-8.respondJson<T>(statusCode: Int = 200, data: T, json: Json = Json, serializer: KSerializer<T>)Sends a JSON response.
Content-Type to application/json; charset=utf-8.kotlinx.serialization to encode the data.respondCbor(statusCode: Int = 200, data: ByteArray)Sends a CBOR (Concise Binary Object Representation) response.
Content-Type to application/cbor.respondBytes(statusCode: Int = 200, contentType: String, bytes: ByteArray)Sends raw binary data with a specified content type.
redirect(url: String, permanent: Boolean = false)Sends a redirect response.
302 Found (default) or 301 Moved Permanently.Location header.Set streamRequestBodies = true on VertxServerConfig or AetherServerConfig to opt into the single-consumer Request.openBodyStream() capability. The default remains the compatible bounded bodyBytes() path. Server limits always take precedence over route limits:
val stream = exchange.request.openBodyStream(
RequestBodyStreamLimits(
maximumTotalBytes = 64L * 1024 * 1024,
maximumChunkBytes = 64 * 1024,
deadline = 30.seconds
)
) ?: error("Enable streamRequestBodies")
stream.consume { ciphertextChunk ->
objectStoreMultipartPart(ciphertextChunk) // consume before returning
}
exchange.response.write("accepted")
Each body can be opened once. Oversize chunks or totals, deadlines, disconnects, and repeated consumption produce typed RequestBodyStreamException failures. The adapter pauses the Vert.x source until downstream demand is available; highWaterBytes reports the largest retained body window. Cancellation closes only that request connection. Calling bodyBytes() on a streaming request uses the same bounded source and should remain limited to ordinary JSON/form payloads.
Response.write(ByteArray) is incremental and awaits every Vert.x write. The adapter also waits for a full responseWriteQueueBytes queue to drain before accepting another chunk, so a slow reader applies backpressure. A response must still end exactly once.
For large multipart/form-data, use Exchange.streamMultipart(...) with a StreamingMultipartSink. It recognizes boundaries split across transport chunks, bounds headers, file count, file size, total bytes, chunk size, and deadline, and reports transport high-water in StreamingMultipartResult. Sink calls are sequential. onPartData reuses its transfer buffer, so upload or encrypt each range before returning; copy only when the destination API requires retention. This supports direct ciphertext object-store multipart writes without materializing the plaintext file in framework memory.
StreamingProxyClient similarly bounds request chunks and response demand with ProxyConfig.streamBufferSize and streamBufferChunks. ProxyResult.metrics.highWaterBytes reports retained response bytes. Stopping collection closes the upstream connection; closing the client is idempotent.
Both JVM servers reject new work during stop(), drain owned request and WebSocket children for shutdownGraceMillis, then cancel remaining children and close transport resources once. Use the bounded grace period for rolling replacement; existing WebSockets remain active during the drain, while the replacement listener receives new connections.
notFound(message: String = "Not Found"): Sends a 404 response.internalError(message: String = "Internal Server Error"): Sends a 500 response.badRequest(message: String = "Bad Request"): Sends a 400 response.unauthorized(message: String = "Unauthorized"): Sends a 401 response.The Pipeline class implements the middleware chain pattern (often called the "Russian Doll" model). Middleware functions are executed sequentially, and each middleware can decide whether to pass control to the next one or short-circuit the request.
Middleware is defined as a suspend function:
typealias Middleware = suspend (exchange: Exchange, next: suspend () -> Unit) -> Unit
val pipeline = Pipeline()
pipeline.use { exchange, next ->
println("Before request")
next() // Pass control to the next middleware
println("After request")
}
use(middleware: Middleware): Adds a middleware to the end of the pipeline.execute(exchange: Exchange, handler: suspend (Exchange) -> Unit): Executes the pipeline. If all middleware call next(), the final handler is executed.copy(): Creates a shallow copy of the pipeline.clear(): Removes all middleware.Both JVM server adapters establish a bounded X-Request-Id before pipeline execution. A supplied ID is retained only when it is 8–64 ASCII letters, digits, ., _, or -; malformed values are replaced. installRecovery() emits the canonical JSON envelope {"error":{"code":"…","message":"…","retryable":false},"request_id":"…"}. Throw ApiException(ApiErrorKind.…) for a deliberate public classification. Arbitrary exception types, messages, causes, SQL, and stack traces always map to opaque INTERNAL; CancellationException and fatal Error values are rethrown.
ApiErrorKind | HTTP |
|---|---|
BAD_REQUEST | 400 |
UNAUTHENTICATED | 401 |
PERMISSION_DENIED | 403 |
NOT_FOUND | 404 |
REQUEST_TIMEOUT | 408 |
CONFLICT | 409 |
GONE | 410 |
PAYLOAD_TOO_LARGE | 413 |
VALIDATION_FAILED, UNSUPPORTED_FORMAT | 422 |
RATE_LIMITED | 429 |
DEPENDENCY_UNAVAILABLE | 503 |
INTERNAL | 500 |
installCallLogging() records only request ID, method, matched route template, status, bounded duration, and typed error category. It never formats raw paths, path parameters, query strings, headers, cookies, bodies, SQL, URLs, or exception causes. Set RequestDiagnosticsPolicy(includeRouteTemplate = false) when route templates themselves are sensitive. DebugToolbarConfig(profile = DiagnosticsProfile.PRIVATE_PRODUCTION) disables toolbar injection and SQL capture. Do not use development diagnostics in a private production process.
You can use the pipeline builder function:
val appPipeline = pipeline {
use(loggingMiddleware)
use(authMiddleware)
}
The rate limit middleware provides quota-based request limiting to protect your API from abuse.
pipeline.installRateLimit {
quotaProvider = InMemoryQuotaProvider(
limit = 100, // 100 requests
windowMillis = 60_000 // per minute
)
keyExtractor = { exchange ->
exchange.request.headers.get("X-API-Key") ?: exchange.request.remoteAddress
}
}
pipeline.installRateLimit {
// Required: Quota provider
quotaProvider = InMemoryQuotaProvider(limit = 1000, windowMillis = 3600_000)
// Extract key to identify the client (default: IP address)
keyExtractor = { exchange -> exchange.request.remoteAddress }
// Cost per request (default: 1)
costFunction = { exchange ->
if (exchange.request.path.startsWith("/api/heavy/")) 10L else 1L
}
// Paths to exclude from rate limiting
excludedPaths = setOf("/health", "/metrics")
excludedPathPrefixes = setOf("/public/", "/static/")
// Bypass condition (e.g., for admin users)
bypassCondition = { exchange ->
exchange.attributes[UserKey]?.isAdmin == true
}
// Response customization
statusCode = 429 // Too Many Requests (default)
errorMessage = "Rate limit exceeded"
includeHeaders = true // X-RateLimit-* headers
// Custom response headers
responseHeaders = { usage ->
mapOf("X-Credits-Remaining" to usage.remaining.toString())
}
}
For persistent quota tracking:
pipeline.installRateLimitWithCredits(
getCredits = { userId -> userService.getCredits(userId) },
deductCredits = { userId, amount -> userService.deductCredits(userId, amount) }
) {
keyExtractor = { exchange -> exchange.attributes[UserKey]?.id }
}
When includeHeaders = true, these headers are added:
X-RateLimit-Limit: Total quotaX-RateLimit-Remaining: Remaining quotaX-RateLimit-Reset: Unix timestamp when quota resetsThe proxy middleware enables forwarding requests to upstream services with full streaming support.
// One-liner: forward entire request and response
exchange.proxyTo("https://api.example.com/v1${exchange.request.path}")
// Inspect response before forwarding
val response = exchange.proxyRequest("https://api.example.com/v1/data")
if (response.statusCode == 200) {
exchange.respondBytes(response.statusCode, response.contentType, response.body)
} else {
exchange.internalError("Upstream error")
}
pipeline.installProxy {
// Route by path prefix
route("/api/users") {
target = "https://users-service.internal:8080"
stripPrefix = true // /api/users/123 -> /123
}
route("/api/orders") {
target = "https://orders-service.internal:8080"
// Header management
addHeaders = mapOf("X-Internal-Token" to "secret")
removeHeaders = setOf("Cookie", "Authorization")
// Path rewriting
pathRewriter = { path -> "/v2$path" }
}
// Global settings
timeout = 30_000 // 30 seconds
includeForwardedHeaders = true // X-Forwarded-For, X-Forwarded-Proto
// Circuit breaker
circuitBreaker {
failureThreshold = 5
successThreshold = 2
timeout = 60_000 // Open state duration
}
}
The proxy supports true streaming for SSE and chunked responses:
route("/api/stream") {
target = "https://streaming-service.internal:8080"
streaming = true // Enable Flow<ByteArray> streaming
}
pipeline.apply {
handleProxyExceptions() // Automatic 502/503/504 responses
installProxy { ... }
}
Exception types:
ProxyConnectionException → 502 Bad GatewayProxyTimeoutException → 504 Gateway TimeoutProxyCircuitOpenException → 503 Service Unavailable