Java Code Examples for org.springframework.core.ReactiveAdapter#toPublisher()

The following examples show how to use org.springframework.core.ReactiveAdapter#toPublisher() . 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: AbstractEncoderMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Flux<DataBuffer> encodeContent(
		@Nullable Object content, MethodParameter returnType, DataBufferFactory bufferFactory,
		@Nullable MimeType mimeType, Map<String, Object> hints) {

	ResolvableType returnValueType = ResolvableType.forMethodParameter(returnType);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(returnValueType.resolve(), content);

	Publisher<?> publisher;
	ResolvableType elementType;
	if (adapter != null) {
		publisher = adapter.toPublisher(content);
		boolean isUnwrapped = KotlinDetector.isKotlinReflectPresent() &&
				KotlinDetector.isKotlinType(returnType.getContainingClass()) &&
				KotlinDelegate.isSuspend(returnType.getMethod()) &&
				!COROUTINES_FLOW_CLASS_NAME.equals(returnValueType.toClass().getName());
		ResolvableType genericType = isUnwrapped ? returnValueType : returnValueType.getGeneric();
		elementType = getElementType(adapter, genericType);
	}
	else {
		publisher = Mono.justOrEmpty(content);
		elementType = (returnValueType.toClass() == Object.class && content != null ?
				ResolvableType.forInstance(content) : returnValueType);
	}

	if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
		return Flux.from(publisher).cast(DataBuffer.class);
	}

	Encoder<?> encoder = getEncoder(elementType, mimeType);
	return Flux.from((Publisher) publisher).map(value ->
			encodeValue(value, elementType, encoder, bufferFactory, mimeType, hints));
}
 
Example 2
Source File: AbstractMessageWriterResultHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Write a given body to the response with {@link HttpMessageWriter}.
 * @param body the object to write
 * @param bodyParameter the {@link MethodParameter} of the body to write
 * @param actualParam the actual return type of the method that returned the value;
 * could be different from {@code bodyParameter} when processing {@code HttpEntity}
 * for example
 * @param exchange the current exchange
 * @return indicates completion or error
 * @since 5.0.2
 */
@SuppressWarnings({"unchecked", "rawtypes", "ConstantConditions"})
protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParameter,
		@Nullable MethodParameter actualParam, ServerWebExchange exchange) {

	ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter);
	ResolvableType actualType = (actualParam != null ? ResolvableType.forMethodParameter(actualParam) : bodyType);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(bodyType.resolve(), body);

	Publisher<?> publisher;
	ResolvableType elementType;
	ResolvableType actualElementType;
	if (adapter != null) {
		publisher = adapter.toPublisher(body);
		boolean isUnwrapped = KotlinDetector.isKotlinReflectPresent() &&
				KotlinDetector.isKotlinType(bodyParameter.getContainingClass()) &&
				KotlinDelegate.isSuspend(bodyParameter.getMethod()) &&
				!COROUTINES_FLOW_CLASS_NAME.equals(bodyType.toClass().getName());
		ResolvableType genericType = isUnwrapped ? bodyType : bodyType.getGeneric();
		elementType = getElementType(adapter, genericType);
		actualElementType = elementType;
	}
	else {
		publisher = Mono.justOrEmpty(body);
		actualElementType = body != null ? ResolvableType.forInstance(body) : bodyType;
		elementType = (bodyType.toClass() == Object.class && body != null ? actualElementType : bodyType);
	}

	if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
		return Mono.from((Publisher<Void>) publisher);
	}

	MediaType bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType));
	if (bestMediaType != null) {
		String logPrefix = exchange.getLogPrefix();
		if (logger.isDebugEnabled()) {
			logger.debug(logPrefix +
					(publisher instanceof Mono ? "0..1" : "0..N") + " [" + elementType + "]");
		}
		for (HttpMessageWriter<?> writer : getMessageWriters()) {
			if (writer.canWrite(actualElementType, bestMediaType)) {
				return writer.write((Publisher) publisher, actualType, elementType,
						bestMediaType, exchange.getRequest(), exchange.getResponse(),
						Hints.from(Hints.LOG_PREFIX_HINT, logPrefix));
			}
		}
	}

	List<MediaType> mediaTypes = getMediaTypesFor(elementType);
	if (bestMediaType == null && mediaTypes.isEmpty()) {
		return Mono.error(new IllegalStateException("No HttpMessageWriter for " + elementType));
	}
	return Mono.error(new NotAcceptableStatusException(mediaTypes));
}
 
Example 3
Source File: ReactiveTypeHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void connect(ReactiveAdapter adapter, Object returnValue) {
	Publisher<Object> publisher = adapter.toPublisher(returnValue);
	publisher.subscribe(this);
}
 
Example 4
Source File: ReactiveTypeHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void connect(ReactiveAdapter adapter, Object returnValue) {
	Publisher<Object> publisher = adapter.toPublisher(returnValue);
	publisher.subscribe(this);
}
 
Example 5
Source File: AbstractMessageWriterResultHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Write a given body to the response with {@link HttpMessageWriter}.
 * @param body the object to write
 * @param bodyParameter the {@link MethodParameter} of the body to write
 * @param actualParam the actual return type of the method that returned the value;
 * could be different from {@code bodyParameter} when processing {@code HttpEntity}
 * for example
 * @param exchange the current exchange
 * @return indicates completion or error
 * @since 5.0.2
 */
@SuppressWarnings({"unchecked", "rawtypes"})
protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParameter,
		@Nullable MethodParameter actualParam, ServerWebExchange exchange) {

	ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParameter);
	ResolvableType actualType = (actualParam != null ? ResolvableType.forMethodParameter(actualParam) : bodyType);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(bodyType.resolve(), body);

	Publisher<?> publisher;
	ResolvableType elementType;
	if (adapter != null) {
		publisher = adapter.toPublisher(body);
		ResolvableType genericType = bodyType.getGeneric();
		elementType = getElementType(adapter, genericType);
	}
	else {
		publisher = Mono.justOrEmpty(body);
		elementType = (bodyType.toClass() == Object.class && body != null ?
				ResolvableType.forInstance(body) : bodyType);
	}

	if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
		return Mono.from((Publisher<Void>) publisher);
	}

	ServerHttpRequest request = exchange.getRequest();
	ServerHttpResponse response = exchange.getResponse();
	MediaType bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType));
	if (bestMediaType != null) {
		String logPrefix = exchange.getLogPrefix();
		if (logger.isDebugEnabled()) {
			logger.debug(logPrefix +
					(publisher instanceof Mono ? "0..1" : "0..N") + " [" + elementType + "]");
		}
		for (HttpMessageWriter<?> writer : getMessageWriters()) {
			if (writer.canWrite(elementType, bestMediaType)) {
				return writer.write((Publisher) publisher, actualType, elementType, bestMediaType,
						request, response, Hints.from(Hints.LOG_PREFIX_HINT, logPrefix));
			}
		}
	}
	else {
		if (getMediaTypesFor(elementType).isEmpty()) {
			return Mono.error(new IllegalStateException("No writer for : " + elementType));
		}
	}

	return Mono.error(new NotAcceptableStatusException(getMediaTypesFor(elementType)));
}
 
Example 6
Source File: ReactiveTypeHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
public void connect(ReactiveAdapter adapter, Object returnValue) {
	Publisher<Object> publisher = adapter.toPublisher(returnValue);
	publisher.subscribe(this);
}
 
Example 7
Source File: ReactiveTypeHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
public void connect(ReactiveAdapter adapter, Object returnValue) {
	Publisher<Object> publisher = adapter.toPublisher(returnValue);
	publisher.subscribe(this);
}