org.springframework.web.reactive.function.BodyExtractor Java Examples

The following examples show how to use org.springframework.web.reactive.function.BodyExtractor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultServerRequest.java    From java-technology-stack with MIT License 6 votes vote down vote up
private <T> T bodyInternal(BodyExtractor<T, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	return extractor.extract(request(),
			new BodyExtractor.Context() {
				@Override
				public List<HttpMessageReader<?>> messageReaders() {
					return messageReaders;
				}
				@Override
				public Optional<ServerHttpResponse> serverResponse() {
					return Optional.of(exchange().getResponse());
				}
				@Override
				public Map<String, Object> hints() {
					return hints;
				}
			});
}
 
Example #2
Source File: DefaultServerRequest.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private <T> T bodyInternal(BodyExtractor<T, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	return extractor.extract(request(),
			new BodyExtractor.Context() {
				@Override
				public List<HttpMessageReader<?>> messageReaders() {
					return messageReaders;
				}
				@Override
				public Optional<ServerHttpResponse> serverResponse() {
					return Optional.of(exchange().getResponse());
				}
				@Override
				public Map<String, Object> hints() {
					return hints;
				}
			});
}
 
Example #3
Source File: DefaultClientResponse.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ClientHttpResponse> extractor) {
	return extractor.extract(this.response, new BodyExtractor.Context() {
		@Override
		public List<HttpMessageReader<?>> messageReaders() {
			return strategies.messageReaders();
		}
		@Override
		public Optional<ServerHttpResponse> serverResponse() {
			return Optional.empty();
		}
		@Override
		public Map<String, Object> hints() {
			return Hints.from(Hints.LOG_PREFIX_HINT, logPrefix);
		}
	});
}
 
Example #4
Source File: DefaultClientResponse.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T body(BodyExtractor<T, ? super ClientHttpResponse> extractor) {
	T result = extractor.extract(this.response, new BodyExtractor.Context() {
		@Override
		public List<HttpMessageReader<?>> messageReaders() {
			return strategies.messageReaders();
		}

		@Override
		public Optional<ServerHttpResponse> serverResponse() {
			return Optional.empty();
		}

		@Override
		public Map<String, Object> hints() {
			return Hints.from(Hints.LOG_PREFIX_HINT, logPrefix);
		}
	});
	String description = "Body from " + this.requestDescription + " [DefaultClientResponse]";
	if (result instanceof Mono) {
		return (T) ((Mono<?>) result).checkpoint(description);
	}
	else if (result instanceof Flux) {
		return (T) ((Flux<?>) result).checkpoint(description);
	}
	else {
		return result;
	}
}
 
Example #5
Source File: AbstractWebfluxImporter.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
public Mono<ServerResponse> handle(ServerRequest request) {
    if (refuseRequestService.isRefuseRequest()) {
        return error("lookout gateway is in refuse requests mode");
    }
    counter.inc();

    RawMetric rm = initRawMetric(request);

    // 目前获取客户端IP可以使用如下的方式 关键是要获取到 ServerHttpRequest 对象
    // extractor 可否重用?
    BodyExtractor<Mono<byte[]>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(byte[].class);
    return request.body((inputMessage, context) -> {
        RawMetricHead head = rm.getHead();
        head.setClientIp(inputMessage.getRemoteAddress().getAddress().getHostAddress());
        return extractor.extract(inputMessage, context);
    }).flatMap(body -> {
        size.record(body.length);
        rm.setRawBody(body);
        return doHandle(request, rm);
    }).onErrorResume(error -> {
        if (error instanceof FilterException) {
            FilterException fe = (FilterException) error;
            return error(fe.getResult().getMsg());
        } else {
            return error(error.getMessage());
        }
    });
}
 
Example #6
Source File: ServerRequestWrapperTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bodyExtractor() {
	Mono<String> result = Mono.just("foo");
	BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
	when(mockRequest.body(extractor)).thenReturn(result);

	assertSame(result, wrapper.body(extractor));
}
 
Example #7
Source File: ClientResponseWrapperTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bodyExtractor() {
	Mono<String> result = Mono.just("foo");
	BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
	when(mockResponse.body(extractor)).thenReturn(result);

	assertSame(result, wrapper.body(extractor));
}
 
Example #8
Source File: ServerRequestWrapperTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void bodyExtractor() {
	Mono<String> result = Mono.just("foo");
	BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
	given(mockRequest.body(extractor)).willReturn(result);

	assertSame(result, wrapper.body(extractor));
}
 
Example #9
Source File: ClientResponseWrapperTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void bodyExtractor() {
	Mono<String> result = Mono.just("foo");
	BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
	given(mockResponse.body(extractor)).willReturn(result);

	assertSame(result, wrapper.body(extractor));
}
 
Example #10
Source File: RequestPredicates.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor) {
	return this.request.body(extractor);
}
 
Example #11
Source File: HttpResponse.java    From charon-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ClientHttpResponse> extractor) {
    throw requestForwardingError("Method not implemented");
}
 
Example #12
Source File: MockClientResponse.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ClientHttpResponse> extractor) {
	return (T) responseBody;
}
 
Example #13
Source File: ClientResponseWrapper.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ClientHttpResponse> extractor) {
	return this.delegate.body(extractor);
}
 
Example #14
Source File: MockServerRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	Assert.state(this.body != null, "No body");
	return (S) this.body;
}
 
Example #15
Source File: MockServerRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor) {
	Assert.state(this.body != null, "No body");
	return (S) this.body;
}
 
Example #16
Source File: MockServerRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	Assert.state(this.body != null, "No body");
	return (S) this.body;
}
 
Example #17
Source File: MockServerRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor) {
	Assert.state(this.body != null, "No body");
	return (S) this.body;
}
 
Example #18
Source File: DefaultServerRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor) {
	return bodyInternal(extractor, Hints.from(Hints.LOG_PREFIX_HINT, exchange().getLogPrefix()));
}
 
Example #19
Source File: RequestPredicates.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	return this.request.body(extractor, hints);
}
 
Example #20
Source File: RequestPredicates.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor) {
	return this.request.body(extractor);
}
 
Example #21
Source File: ServerRequestWrapper.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	return this.delegate.body(extractor, hints);
}
 
Example #22
Source File: ServerRequestWrapper.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor) {
	return this.delegate.body(extractor);
}
 
Example #23
Source File: DefaultServerRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	hints = Hints.merge(hints, Hints.LOG_PREFIX_HINT, exchange().getLogPrefix());
	return bodyInternal(extractor, hints);
}
 
Example #24
Source File: DefaultServerRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	hints = Hints.merge(hints, Hints.LOG_PREFIX_HINT, exchange().getLogPrefix());
	return bodyInternal(extractor, hints);
}
 
Example #25
Source File: DefaultServerRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ServerHttpRequest> extractor) {
	return bodyInternal(extractor, Hints.from(Hints.LOG_PREFIX_HINT, exchange().getLogPrefix()));
}
 
Example #26
Source File: ClientResponseWrapper.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T> T body(BodyExtractor<T, ? super ClientHttpResponse> extractor) {
	return this.delegate.body(extractor);
}
 
Example #27
Source File: MockServerRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	Assert.state(this.body != null, "No body");
	return (S) this.body;
}
 
Example #28
Source File: MockServerRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor) {
	Assert.state(this.body != null, "No body");
	return (S) this.body;
}
 
Example #29
Source File: MockServerRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
	Assert.state(this.body != null, "No body");
	return (S) this.body;
}
 
Example #30
Source File: MockServerRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor) {
	Assert.state(this.body != null, "No body");
	return (S) this.body;
}