Package one.nio.http
Class EventSourceResponse
- java.lang.Object
-
- one.nio.http.Response
-
- one.nio.http.EventSourceResponse
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,EventSource<java.lang.String>
public class EventSourceResponse extends Response implements EventSource<java.lang.String>
A Response, which can poll for server emitted events. Unlike regularResponsethis object must be close'd to prevent resource leak.The usage flow is as follows:
- Call
HttpClient.openEvents(Request, int) - Inspect the result code, if it is not OK process the error
- Inspect the content-type, it must be text/event-stream; if it is not - process the response body - there will be no events
- while ( ( event = poll() ) != null ) process( event )
- call
close() - call
HttpClient.reopenEvents(Request, String, int)with last processedEventSource.Event.id()and go to p.2
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface one.nio.http.EventSource
EventSource.Event<D>
-
-
Field Summary
Fields Modifier and Type Field Description private EventSource<java.lang.String>eventSource-
Fields inherited from class one.nio.http.Response
ACCEPTED, BAD_GATEWAY, BAD_REQUEST, CONFLICT, CONTINUE, CREATED, EMPTY, EXPECTATION_FAILED, FORBIDDEN, FOUND, GATEWAY_TIMEOUT, GONE, HTTP_VERSION_NOT_SUPPORTED, INTERNAL_ERROR, LENGTH_REQUIRED, METHOD_NOT_ALLOWED, MOVED_PERMANENTLY, MULTIPLE_CHOICES, NO_CONTENT, NON_AUTHORITATIVE_INFORMATION, NOT_ACCEPTABLE, NOT_FOUND, NOT_IMPLEMENTED, NOT_MODIFIED, OK, PARTIAL_CONTENT, PAYMENT_REQUIRED, PRECONDITION_FAILED, PROXY_AUTHENTICATION_REQUIRED, REQUEST_ENTITY_TOO_LARGE, REQUEST_TIMEOUT, REQUEST_URI_TOO_LONG, REQUESTED_RANGE_NOT_SATISFIABLE, RESET_CONTENT, SEE_OTHER, SERVICE_UNAVAILABLE, SWITCHING_PROTOCOLS, TEMPORARY_REDIRECT, UNAUTHORIZED, UNSUPPORTED_MEDIA_TYPE, USE_PROXY
-
-
Constructor Summary
Constructors Constructor Description EventSourceResponse(java.lang.String resultCode)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()EventSource.Event<java.lang.String>poll()Waits for the next SSE and returns an event.(package private) voidsetEventSource(EventSource<java.lang.String> es)-
Methods inherited from class one.nio.http.Response
addHeader, getBody, getBodyUtf8, getHeader, getHeaderCount, getHeaders, getStatus, json, ok, ok, redirect, setBody, toBytes, toString
-
-
-
-
Field Detail
-
eventSource
private EventSource<java.lang.String> eventSource
-
-
Method Detail
-
poll
public EventSource.Event<java.lang.String> poll() throws java.io.IOException, HttpException
Description copied from interface:EventSourceWaits for the next SSE and returns an event. The method can block for a long time ( determined by server ). It is essential to check for null andEventSource.Event.isEmpty()before processing- Specified by:
pollin interfaceEventSource<java.lang.String>- Returns:
- the next event from the stream or null, if stream was closed by either party
- Throws:
java.io.IOException- an I/O exception occurredHttpException- an incorrect HTTP request received
-
setEventSource
void setEventSource(EventSource<java.lang.String> es)
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
-