Example usage for org.springframework.http MediaType TEXT_EVENT_STREAM

List of usage examples for org.springframework.http MediaType TEXT_EVENT_STREAM

Introduction

In this page you can find the example usage for org.springframework.http MediaType TEXT_EVENT_STREAM.

Prototype

MediaType TEXT_EVENT_STREAM

To view the source code for org.springframework.http MediaType TEXT_EVENT_STREAM.

Click Source Link

Document

Public constant media type for text/event-stream .

Usage

From source file:org.springframework.cloud.netflix.hystrix.HystrixWebfluxControllerTests.java

@Test
public void hystrixStreamWorks() {
    String url = "http://localhost:" + port;
    // you have to hit a Hystrix circuit breaker before the stream sends anything
    WebTestClient testClient = WebTestClient.bindToServer().baseUrl(url).build();
    testClient.get().uri("/").exchange().expectStatus().isOk();

    WebClient client = WebClient.create(url);

    Flux<String> result = client.get().uri(BASE_PATH + "/hystrix.stream").accept(MediaType.TEXT_EVENT_STREAM)
            .exchange().flatMapMany(res -> res.bodyToFlux(Map.class)).take(5)
            .filter(map -> "HystrixCommand".equals(map.get("type"))).map(map -> (String) map.get("type"));

    StepVerifier.create(result).expectNext("HystrixCommand").thenCancel().verify();
}

From source file:org.springframework.cloud.netflix.turbine.stream.TurbineStreamTests.java

@Test
@Ignore // FIXME 2.0.0 Elmurst stream missing class @Controller?
public void contextLoads() throws Exception {
    rest.getInterceptors().add(new NonClosingInterceptor());
    int count = ((MessageChannelMetrics) input).getSendCount();
    ResponseEntity<String> response = rest.execute(
            new URI("http://localhost:" + turbine.getTurbinePort() + "/"), HttpMethod.GET, null, this::extract);
    assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_EVENT_STREAM);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    Map<String, Object> metrics = extractMetrics(response.getBody());
    assertThat(metrics).containsEntry("type", "HystrixCommand");
    assertThat(((MessageChannelMetrics) input).getSendCount()).isEqualTo(count + 1);
}

From source file:org.springframework.web.servlet.mvc.method.annotation.ReactiveTypeHandler.java

/**
 * Process the given reactive return value and decide whether to adapt it
 * to a {@link ResponseBodyEmitter} or a {@link DeferredResult}.
 * @return an emitter for streaming or {@code null} if handled internally
 * with a {@link DeferredResult}./*from w w w . j  a v a 2  s. com*/
 */
@Nullable
public ResponseBodyEmitter handleValue(Object returnValue, MethodParameter returnType,
        ModelAndViewContainer mav, NativeWebRequest request) throws Exception {

    Assert.notNull(returnValue, "Expected return value");
    ReactiveAdapter adapter = this.reactiveRegistry.getAdapter(returnValue.getClass());
    Assert.state(adapter != null, "Unexpected return value: " + returnValue);

    ResolvableType elementType = ResolvableType.forMethodParameter(returnType).getGeneric(0);
    Class<?> elementClass = elementType.resolve(Object.class);

    Collection<MediaType> mediaTypes = getMediaTypes(request);
    Optional<MediaType> mediaType = mediaTypes.stream().filter(MimeType::isConcrete).findFirst();

    if (adapter.isMultiValue()) {
        if (mediaTypes.stream().anyMatch(MediaType.TEXT_EVENT_STREAM::includes)
                || ServerSentEvent.class.isAssignableFrom(elementClass)) {
            SseEmitter emitter = new SseEmitter(STREAMING_TIMEOUT_VALUE);
            new SseEmitterSubscriber(emitter, this.taskExecutor).connect(adapter, returnValue);
            return emitter;
        }
        if (CharSequence.class.isAssignableFrom(elementClass)) {
            ResponseBodyEmitter emitter = getEmitter(mediaType.orElse(MediaType.TEXT_PLAIN));
            new TextEmitterSubscriber(emitter, this.taskExecutor).connect(adapter, returnValue);
            return emitter;
        }
        if (mediaTypes.stream().anyMatch(MediaType.APPLICATION_STREAM_JSON::includes)) {
            ResponseBodyEmitter emitter = getEmitter(MediaType.APPLICATION_STREAM_JSON);
            new JsonEmitterSubscriber(emitter, this.taskExecutor).connect(adapter, returnValue);
            return emitter;
        }
    }

    // Not streaming...
    DeferredResult<Object> result = new DeferredResult<>();
    new DeferredResultSubscriber(result, adapter, elementType).connect(adapter, returnValue);
    WebAsyncUtils.getAsyncManager(request).startDeferredResultProcessing(result, mav);

    return null;
}