return new Event(this, data);
public byte[] format(final Sse.Event event) throws Exception { // comment? data = event.comment() .map(comment -> ByteSource.concat(COMMENT, bytes(comment), NL)) .orElse(ByteSource.empty()); // id? data = event.id() .map(id -> ByteSource.concat(data, ID, bytes(id.toString()), NL)) .orElse(data); // event? data = event.name() .map(name -> ByteSource.concat(data, EVENT, bytes(name), NL)) .orElse(data); // retry? data = event.retry() .map(retry -> ByteSource.concat(data, RETRY, bytes(Long.toString(retry)), NL)) .orElse(data); Optional<Object> value = event.data(); if (value.isPresent()) { render(value.get()); } data = ByteSource.concat(data, NL); byte[] bytes = data.read(); data = null; return bytes; }
/** * Send an event. * * <pre>{@code * sse.send(new MyObject()); * }</pre> * * <pre>{@code * sse.send(new MyObject(), "json").whenComplete((id, x) -> { * if (x == null) { * handleSuccess(); * } else { * handleError(x); * } * }); * }</pre> * * The <code>id</code> of the success callback correspond to the {@link Event#id()}. * * @param data Event data. * @return A future. The success callback contains the {@link Event#id()}. */ @Nonnull public CompletableFuture<Optional<Object>> send(final Object data) { return event(data).send(); }
return new Event(this, data);
/** * Send an event and set media type. * * <pre>{@code * sse.send(new MyObject(), "json"); * }</pre> * * <pre>{@code * sse.send(new MyObject(), "json").whenComplete((id, x) -> { * if (x == null) { * handleSuccess(); * } else { * handleError(x); * } * }); * }</pre> * * The <code>id</code> of the success callback correspond to the {@link Event#id()}. * * @param data Event data. * @param type Media type, like: json, xml. * @return A future. The success callback contains the {@link Event#id()}. */ @Nonnull public CompletableFuture<Optional<Object>> send(final Object data, final MediaType type) { return event(data).type(type).send(); }
/** * Send an event. * * <pre>{@code * sse.send(new MyObject()); * }</pre> * * <pre>{@code * sse.send(new MyObject(), "json").whenComplete((id, x) -> { * if (x == null) { * handleSuccess(); * } else { * handleError(x); * } * }); * }</pre> * * The <code>id</code> of the success callback correspond to the {@link Event#id()}. * * @param data Event data. * @return A future. The success callback contains the {@link Event#id()}. */ @Nonnull public CompletableFuture<Optional<Object>> send(final Object data) { return event(data).send(); }
public byte[] format(final Sse.Event event) throws Exception { // comment? data = event.comment() .map(comment -> ByteSource.concat(COMMENT, bytes(comment), NL)) .orElse(ByteSource.empty()); // id? data = event.id() .map(id -> ByteSource.concat(data, ID, bytes(id.toString()), NL)) .orElse(data); // event? data = event.name() .map(name -> ByteSource.concat(data, EVENT, bytes(name), NL)) .orElse(data); // retry? data = event.retry() .map(retry -> ByteSource.concat(data, RETRY, bytes(Long.toString(retry)), NL)) .orElse(data); Optional<Object> value = event.data(); if (value.isPresent()) { render(value.get()); } data = ByteSource.concat(data, NL); byte[] bytes = data.read(); data = null; return bytes; }
private CompletableFuture<Optional<Object>> send(final Event event) { List<MediaType> produces = event.type().<List<MediaType>>map(ImmutableList::of) .orElse(this.produces); SseRenderer ctx = new SseRenderer(renderers, produces, StandardCharsets.UTF_8, locale, locals); return Try.apply(() -> { byte[] bytes = ctx.format(event); return send(event.id(), bytes); }).recover(x -> { CompletableFuture<Optional<Object>> future = new CompletableFuture<>(); future.completeExceptionally(x); return future; }) .get(); }
/** * Set event media type. Useful for sengin json, xml, etc.. * * @param type Media Type. * @return This event. */ public Event type(final String type) { return type(MediaType.valueOf(type)); }
/** * Send an event and set media type. * * <pre>{@code * sse.send(new MyObject(), "json"); * }</pre> * * <pre>{@code * sse.send(new MyObject(), "json").whenComplete((id, x) -> { * if (x == null) { * handleSuccess(); * } else { * handleError(x); * } * }); * }</pre> * * The <code>id</code> of the success callback correspond to the {@link Event#id()}. * * @param data Event data. * @param type Media type, like: json, xml. * @return A future. The success callback contains the {@link Event#id()}. */ @Nonnull public CompletableFuture<Optional<Object>> send(final Object data, final MediaType type) { return event(data).type(type).send(); }
private CompletableFuture<Optional<Object>> send(final Event event) { List<MediaType> produces = event.type().<List<MediaType>>map(ImmutableList::of) .orElse(this.produces); SseRenderer ctx = new SseRenderer(renderers, produces, StandardCharsets.UTF_8, locale, locals); return Try.apply(() -> { byte[] bytes = ctx.format(event); return send(event.id(), bytes); }).recover(x -> { CompletableFuture<Optional<Object>> future = new CompletableFuture<>(); future.completeExceptionally(x); return future; }) .get(); }
/** * Set event media type. Useful for sengin json, xml, etc.. * * @param type Media Type. * @return This event. */ public Event type(final String type) { return type(MediaType.valueOf(type)); }