Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A2aproject A2a java SSE Parsing Stack

From Leeroopedia
Knowledge Sources
Domains SSE, Parsing, Security, DoS_Protection
Last Updated 2026-10-30 00:00 GMT

Overview

Concrete Server-Sent Events (SSE) parsing stack with bounded line accumulation for DoS protection, configurable size limits, and a template-method base listener for transport-specific SSE handling.

Description

The SSE parsing stack converts raw HTTP byte streams into parsed ServerSentEvent records:

  • ServerSentEventParser: Streaming parser implementing the W3C SSE spec — handles event dispatch (multiple `data:` lines concatenated), comment lines, and BOM stripping
  • ServerSentEvent: Immutable record with `event`, `data`, `id`, `retry` fields
  • SSEParserConfig: Configurable limits (max event size, max line length) — defaults: 1MB event, 64KB line
  • BoundedLineAccumulator: Bounded byte buffer that throws when exceeding max line length — DoS protection against unlimited line streaming
  • AbstractSSEEventListener: Template Method base class that SSEEventListener subclasses (in jsonrpc/rest transport modules) extend to bridge parsed events into streaming event consumers with error/complete handling

Usage

  • Streaming task updates: Used by all transports for `sendMessageStreaming` and `subscribeToTask` SSE handling
  • DoS protection: SSEParserConfig limits prevent memory exhaustion from malicious event streams
  • Transport-specific listeners: Extend AbstractSSEEventListener to implement custom SSE event handling

Code Reference

Source Location

  • Repository: a2a-java
  • Files:
    • `http-client/src/main/java/org/a2aproject/sdk/client/http/ServerSentEventParser.java` (L1-221)
    • `http-client/src/main/java/org/a2aproject/sdk/client/http/ServerSentEvent.java` (L1-32)
    • `http-client/src/main/java/org/a2aproject/sdk/client/http/SSEParserConfig.java` (L1-96)
    • `http-client/src/main/java/org/a2aproject/sdk/client/http/BoundedLineAccumulator.java` (L1-108)
    • `client/transport/spi/src/main/java/org/a2aproject/sdk/client/transport/spi/sse/AbstractSSEEventListener.java` (L1-143)

Signature

// Parsed event record
public record ServerSentEvent(
        @Nullable String event,   // event: field
        String data,             // concatenated data: lines
        @Nullable String id,     // id: field
        @Nullable Long retry     // retry: field
) {}

// Parser limits
public class SSEParserConfig {
    public static final SSEParserConfig DEFAULT;
    public static class Builder {
        public Builder setMaxEventSize(int bytes);
        public Builder setMaxLineLength(int bytes);
        public SSEParserConfig build();
    }
}

// Streaming parser
public class ServerSentEventParser {
    public void feed(byte[] buffer, int length,
            Consumer<ServerSentEvent> eventConsumer,
            Consumer<Throwable> errorConsumer);
}

// Bounded accumulator with DoS protection
public class BoundedLineAccumulator {
    public BoundedLineAccumulator(int maxLineLength);
    public void accumulate(byte[] data, int length,
            Consumer<String> lineConsumer,
            Consumer<Throwable> errorConsumer);
}

// Template Method listener base
public abstract class AbstractSSEEventListener {
    // Subclasses: SSEEventListener (jsonrpc), SSEEventListener (rest)
    protected abstract void onEvent(ServerSentEvent event);
    protected void onError(Throwable error);
    protected void onComplete();
}

Import

import org.a2aproject.sdk.client.http.ServerSentEvent;
import org.a2aproject.sdk.client.http.ServerSentEventParser;
import org.a2aproject.sdk.client.http.SSEParserConfig;
import org.a2aproject.sdk.client.http.BoundedLineAccumulator;
import org.a2aproject.sdk.client.transport.spi.sse.AbstractSSEEventListener;

I/O Contract

Inputs

Name Type Required Description
buffer byte[] Yes Raw HTTP stream bytes
length int Yes Bytes to process
maxEventSize int No (1MB) Max bytes per SSE event
maxLineLength int No (64KB) Max bytes per line

Outputs

Name Type Description
ServerSentEvent record Parsed event with data, event, id, retry
Errors Throwable via consumer Parsing failures, limit exceeded

Usage Examples

Parsing SSE with Limits

import org.a2aproject.sdk.client.http.SSEParserConfig;
import org.a2aproject.sdk.client.http.ServerSentEventParser;
import org.a2aproject.sdk.client.http.ServerSentEvent;

SSEParserConfig config = new SSEParserConfig.Builder()
    .setMaxEventSize(512 * 1024)   // 512KB per event
    .setMaxLineLength(8 * 1024)    // 8KB per line
    .build();

ServerSentEventParser parser = new ServerSentEventParser(config);

// Feed raw bytes from HTTP stream
parser.feed(responseBytes, responseBytes.length,
    event -> System.out.println("Data: " + event.data()),
    error -> System.err.println("Parse error: " + error));

Custom SSE Listener

import org.a2aproject.sdk.client.transport.spi.sse.AbstractSSEEventListener;
import org.a2aproject.sdk.client.http.ServerSentEvent;

public class MyEventListener extends AbstractSSEEventListener {
    private final Consumer<StreamingEventKind> eventConsumer;

    public MyEventListener(Consumer<StreamingEventKind> eventConsumer) {
        this.eventConsumer = eventConsumer;
    }

    @Override
    protected void onEvent(ServerSentEvent event) {
        // Parse JSON payload and convert to StreamingEventKind
        StreamingEventKind kind = parseEvent(event.data());
        eventConsumer.accept(kind);
    }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment