Java Code Examples for org.springframework.http.codec.HttpMessageWriter#write()

The following examples show how to use org.springframework.http.codec.HttpMessageWriter#write() . 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: BodyInserters.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Void> insert(ClientHttpRequest outputMessage, Context context) {
	HttpMessageWriter<MultiValueMap<String, String>> messageWriter =
			findWriter(context, FORM_DATA_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
	return messageWriter.write(Mono.just(this.data), FORM_DATA_TYPE,
			MediaType.APPLICATION_FORM_URLENCODED,
			outputMessage, context.hints());
}
 
Example 2
Source File: BodyInserters.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Void> insert(ClientHttpRequest outputMessage, Context context) {
	HttpMessageWriter<MultiValueMap<String, HttpEntity<?>>> messageWriter =
			findWriter(context, MULTIPART_DATA_TYPE, MediaType.MULTIPART_FORM_DATA);
	MultiValueMap<String, HttpEntity<?>> body = this.builder.build();
	return messageWriter.write(Mono.just(body), MULTIPART_DATA_TYPE,
			MediaType.MULTIPART_FORM_DATA, outputMessage, context.hints());
}
 
Example 3
Source File: BodyInserters.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<Void> insert(ClientHttpRequest outputMessage, Context context) {
	HttpMessageWriter<MultiValueMap<String, String>> messageWriter =
			findWriter(context, FORM_DATA_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
	return messageWriter.write(Mono.just(this.data), FORM_DATA_TYPE,
			MediaType.APPLICATION_FORM_URLENCODED,
			outputMessage, context.hints());
}
 
Example 4
Source File: BodyInserters.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<Void> insert(ClientHttpRequest outputMessage, Context context) {
	HttpMessageWriter<MultiValueMap<String, HttpEntity<?>>> messageWriter =
			findWriter(context, MULTIPART_DATA_TYPE, MediaType.MULTIPART_FORM_DATA);
	MultiValueMap<String, HttpEntity<?>> body = this.builder.build();
	return messageWriter.write(Mono.just(body), MULTIPART_DATA_TYPE,
			MediaType.MULTIPART_FORM_DATA, outputMessage, context.hints());
}
 
Example 5
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 6
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)));
}