/** * Get the original event data string {@link String}. * * @return event data de-serialized into a string. * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause. * @since 2.3 */ public String readData() { return readData(STRING_AS_GENERIC_TYPE, null); }
/** * Called by the event source when an inbound event is received. * * This listener aggregator method is responsible for invoking {@link EventSource#onEvent(InboundEvent)} * method on the owning event source as well as for notifying all registered {@link EventListener event listeners}. * * @param event incoming {@link InboundEvent inbound event}. */ @Override public void onEvent(final InboundEvent event) { if (event == null) { return; } LOGGER.debugLog("New event received."); if (event.getId() != null) { lastEventId = event.getId(); } if (event.isReconnectDelaySet()) { reconnectDelay = event.getReconnectDelay(); } notify(eventListener, event); notify(unboundListeners, event); final String eventName = event.getName(); if (eventName != null) { final List<EventListener> eventListeners = boundListeners.get(eventName); if (eventListeners != null) { notify(eventListeners, event); } } }
/** * Build a new inbound event instance using the supplied data. * * @return new inbound event instance. */ public InboundEvent build() { return new InboundEvent( name, id, commentBuilder.length() > 0 ? commentBuilder.substring(0, commentBuilder.length() - 1) : null, reconnectDelay, dataStream.toByteArray(), workers, annotations, mediaType, headers); } }
@Override public void onEvent(InboundEvent inboundEvent) { String eventName = inboundEvent.getName(); if (mainEventName.equals(eventName)) { Optional<Response<T>> resp; try { resp = responseBuilder.buildResponse(inboundEvent.readData(), clazz, uri); } catch (Exception e) { sseObserver.onParseError(new ParseError(inboundEvent.getId(), e, inboundEvent.getRawData())); return; } sseObserver.onEvent(resp.get()); } else { sseObserver.onSignal(eventName); } }
Client client = ClientBuilder.newBuilder() .register(SseFeature.class).build(); WebTarget target = client.target("http://localhost:9998/events"); EventInput eventInput = target.request().get(EventInput.class); while (!eventInput.isClosed()) { final InboundEvent inboundEvent = eventInput.read(); if (inboundEvent == null) { // connection has been closed break; } System.out.println(inboundEvent.getName() + "; " + inboundEvent.readData(String.class)); }
/** * Get the raw event data bytes. * * @return raw event data bytes. The returned byte array may be empty if the event does not * contain any data. */ @SuppressWarnings("unused") public byte[] getRawData() { if (isEmpty()) { return data; } return Arrays.copyOf(data, data.length); }
/** * Read event data as a given generic type. * * @param type generic type to be used for event data de-serialization. * @param mediaType {@link MediaType media type} to be used for event data de-serialization. * @return event data de-serialized as an instance of a given type. * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause. * @since 2.3 */ public <T> T readData(GenericType<T> type, MediaType mediaType) { final MediaType effectiveMediaType = mediaType == null ? this.mediaType : mediaType; final MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(type.getRawType(), type.getType(), annotations, mediaType); if (reader == null) { throw new MessageBodyProviderNotFoundException(LocalizationMessages.EVENT_DATA_READER_NOT_FOUND()); } return readAndCast(type, effectiveMediaType, reader); }
private InboundEvent(final String name, final String id, final String comment, final long reconnectDelay, final byte[] data, final MessageBodyWorkers messageBodyWorkers, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> headers) { this.name = name; this.id = id; this.comment = comment; this.reconnectDelay = reconnectDelay; this.data = stripLastLineBreak(data); this.messageBodyWorkers = messageBodyWorkers; this.annotations = annotations; this.mediaType = mediaType; this.headers = headers; }
/** * Get the raw event data bytes. * * @return raw event data bytes. The returned byte array may be empty if the event does not * contain any data. */ @SuppressWarnings("unused") public byte[] getRawData() { if (isEmpty()) { return data; } return Arrays.copyOf(data, data.length); }
/** * Read event data as a given generic type. * * @param type generic type to be used for event data de-serialization. * @param mediaType {@link MediaType media type} to be used for event data de-serialization. * @return event data de-serialized as an instance of a given type. * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause. * @since 2.3 */ public <T> T readData(GenericType<T> type, MediaType mediaType) { final MediaType effectiveMediaType = mediaType == null ? this.mediaType : mediaType; final MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(type.getRawType(), type.getType(), annotations, mediaType); if (reader == null) { throw new IllegalStateException(LocalizationMessages.EVENT_DATA_READER_NOT_FOUND()); } return readAndCast(type, effectiveMediaType, reader); }
private InboundEvent(final String name, final String id, final String comment, final long reconnectDelay, final byte[] data, final MessageBodyWorkers messageBodyWorkers, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> headers) { this.name = name; this.id = id; this.comment = comment; this.reconnectDelay = reconnectDelay; this.data = stripLastLineBreak(data); this.messageBodyWorkers = messageBodyWorkers; this.annotations = annotations; this.mediaType = mediaType; this.headers = headers; }
/** * Called by the event source when an inbound event is received. * * This listener aggregator method is responsible for invoking {@link EventSource#onEvent(InboundEvent)} * method on the owning event source as well as for notifying all registered {@link EventListener event listeners}. * * @param event incoming {@link InboundEvent inbound event}. */ @Override public void onEvent(final InboundEvent event) { if (event == null) { return; } LOGGER.debugLog("New event received."); if (event.getId() != null) { lastEventId = event.getId(); } if (event.isReconnectDelaySet()) { reconnectDelay = event.getReconnectDelay(); } notify(EventSource.this, event); notify(unboundListeners, event); final String eventName = event.getName(); if (eventName != null) { final List<EventListener> eventListeners = boundListeners.get(eventName); if (eventListeners != null) { notify(eventListeners, event); } } }
/** * Read event data as a given generic type. * * @param type generic type to be used for event data de-serialization. * @return event data de-serialized as an instance of a given type. * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause. * @since 2.3 */ @SuppressWarnings("unused") public <T> T readData(GenericType<T> type) { return readData(type, null); }
/** * Get the raw event data bytes. * * @return raw event data bytes. The returned byte array may be empty if the event does not * contain any data. */ @SuppressWarnings("unused") public byte[] getRawData() { if (isEmpty()) { return data; } return Arrays.copyOf(data, data.length); }
/** * Build a new inbound event instance using the supplied data. * * @return new inbound event instance. */ public InboundEvent build() { return new InboundEvent( name, id, commentBuilder.length() > 0 ? commentBuilder.substring(0, commentBuilder.length() - 1) : null, reconnectDelay, dataStream.toByteArray(), workers, annotations, mediaType, headers); } }
/** * Read event data as a given generic type. * * @param type generic type to be used for event data de-serialization. * @param mediaType {@link MediaType media type} to be used for event data de-serialization. * @return event data de-serialized as an instance of a given type. * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause. * @since 2.3 */ public <T> T readData(GenericType<T> type, MediaType mediaType) { final MediaType effectiveMediaType = mediaType == null ? this.mediaType : mediaType; final MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(type.getRawType(), type.getType(), annotations, mediaType); if (reader == null) { throw new IllegalStateException(LocalizationMessages.EVENT_DATA_READER_NOT_FOUND()); } return readAndCast(type, effectiveMediaType, reader); }
private InboundEvent(final String name, final String id, final String comment, final long reconnectDelay, final byte[] data, final MessageBodyWorkers messageBodyWorkers, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> headers) { this.name = name; this.id = id; this.comment = comment; this.reconnectDelay = reconnectDelay; this.data = stripLastLineBreak(data); this.messageBodyWorkers = messageBodyWorkers; this.annotations = annotations; this.mediaType = mediaType; this.headers = headers; }
/** * Called by the event source when an inbound event is received. * * This listener aggregator method is responsible for invoking {@link EventSource#onEvent(InboundEvent)} * method on the owning event source as well as for notifying all registered {@link EventListener event listeners}. * * @param event incoming {@link InboundEvent inbound event}. */ @Override public void onEvent(final InboundEvent event) { if (event == null) { return; } LOGGER.debugLog("New event received."); if (event.getId() != null) { lastEventId = event.getId(); } if (event.isReconnectDelaySet()) { reconnectDelay = event.getReconnectDelay(); } notify(EventSource.this, event); notify(unboundListeners, event); final String eventName = event.getName(); if (eventName != null) { final List<EventListener> eventListeners = boundListeners.get(eventName); if (eventListeners != null) { notify(eventListeners, event); } } }
@Override public String toString() { String s; try { s = readData(); } catch (ProcessingException e) { s = "<Error reading data into a string>"; } return "InboundEvent{" + "name='" + name + '\'' + ", id='" + id + '\'' + ", comment=" + (comment == null ? "[no comments]" : '\'' + comment + '\'') + ", data=" + s + '}'; }
/** * Get the raw event data bytes. * * @return raw event data bytes. The returned byte array may be empty if the event does not * contain any data. */ @SuppressWarnings("unused") public byte[] getRawData() { if (isEmpty()) { return data; } return Arrays.copyOf(data, data.length); }