Java Code Examples for org.springframework.web.server.ServerWebExchange#getLogPrefix()

The following examples show how to use org.springframework.web.server.ServerWebExchange#getLogPrefix() . These examples are extracted from open source projects. 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
@Override
protected Mono<Resource> resolveResourceInternal(@Nullable ServerWebExchange exchange,
		String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {

	String key = computeKey(exchange, requestPath);
	Resource cachedResource = this.cache.get(key, Resource.class);

	if (cachedResource != null) {
		String logPrefix = exchange != null ? exchange.getLogPrefix() : "";
		logger.trace(logPrefix + "Resource resolved from cache");
		return Mono.just(cachedResource);
	}

	return chain.resolveResource(exchange, requestPath, locations)
			.doOnNext(resource -> this.cache.put(key, resource));
}
 
Example 2
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
	HttpStatus status = resolveStatus(ex);
	if (status == null || !exchange.getResponse().setStatusCode(status)) {
		return Mono.error(ex);
	}

	// Mirrors AbstractHandlerExceptionResolver in spring-webmvc...
	String logPrefix = exchange.getLogPrefix();
	if (this.warnLogger != null && this.warnLogger.isWarnEnabled()) {
		this.warnLogger.warn(logPrefix + formatError(ex, exchange.getRequest()), ex);
	}
	else if (logger.isDebugEnabled()) {
		logger.debug(logPrefix + formatError(ex, exchange.getRequest()));
	}

	return exchange.getResponse().setComplete();
}
 
Example 3
private Mono<Void> handleUnresolvedError(ServerWebExchange exchange, Throwable ex) {
	ServerHttpRequest request = exchange.getRequest();
	ServerHttpResponse response = exchange.getResponse();
	String logPrefix = exchange.getLogPrefix();

	if (isDisconnectedClientError(ex)) {
		if (lostClientLogger.isTraceEnabled()) {
			lostClientLogger.trace(logPrefix + "Client went away", ex);
		}
		else if (lostClientLogger.isDebugEnabled()) {
			lostClientLogger.debug(logPrefix + "Client went away: " + ex +
					" (stacktrace at TRACE level for '" + DISCONNECTED_CLIENT_LOG_CATEGORY + "')");
		}
		return Mono.empty();
	}
	else if (response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR)) {
		logger.error(logPrefix + "500 Server Error for " + formatRequest(request), ex);
		return Mono.empty();
	}
	else {
		// After the response is committed, propagate errors to the server...
		logger.error(logPrefix + "Error [" + ex + "] for " + formatRequest(request) +
				", but ServerHttpResponse already committed (" + response.getStatusCode() + ")");
		return Mono.error(ex);
	}
}
 
Example 4
@Override
protected Mono<Resource> resolveResourceInternal(@Nullable ServerWebExchange exchange,
		String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {

	String key = computeKey(exchange, requestPath);
	Resource cachedResource = this.cache.get(key, Resource.class);

	if (cachedResource != null) {
		String logPrefix = exchange != null ? exchange.getLogPrefix() : "";
		logger.trace(logPrefix + "Resource resolved from cache");
		return Mono.just(cachedResource);
	}

	return chain.resolveResource(exchange, requestPath, locations)
			.doOnNext(resource -> this.cache.put(key, resource));
}
 
Example 5
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
	HttpStatus status = resolveStatus(ex);
	if (status == null || !exchange.getResponse().setStatusCode(status)) {
		return Mono.error(ex);
	}

	// Mirrors AbstractHandlerExceptionResolver in spring-webmvc...
	String logPrefix = exchange.getLogPrefix();
	if (this.warnLogger != null && this.warnLogger.isWarnEnabled()) {
		this.warnLogger.warn(logPrefix + formatError(ex, exchange.getRequest()), ex);
	}
	else if (logger.isDebugEnabled()) {
		logger.debug(logPrefix + formatError(ex, exchange.getRequest()));
	}

	return exchange.getResponse().setComplete();
}
 
Example 6
private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttpRequest request,
		@Nullable String protocol, Map<String, Object> attributes) {

	URI uri = request.getURI();
	// Copy request headers, as they might be pooled and recycled by
	// the server implementation once the handshake HTTP exchange is done.
	HttpHeaders headers = new HttpHeaders();
	headers.addAll(request.getHeaders());
	Mono<Principal> principal = exchange.getPrincipal();
	String logPrefix = exchange.getLogPrefix();
	InetSocketAddress remoteAddress = request.getRemoteAddress();
	return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix);
}
 
Example 7
private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttpRequest request,
		@Nullable String protocol, Map<String, Object> attributes) {

	URI uri = request.getURI();
	// Copy request headers, as they might be pooled and recycled by
	// the server implementation once the handshake HTTP exchange is done.
	HttpHeaders headers = new HttpHeaders();
	headers.addAll(request.getHeaders());
	Mono<Principal> principal = exchange.getPrincipal();
	String logPrefix = exchange.getLogPrefix();
	InetSocketAddress remoteAddress = request.getRemoteAddress();
	return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix);
}
 
Example 8
private Mono<Void> handleUnresolvedError(ServerWebExchange exchange, Throwable ex) {

		ServerHttpRequest request = exchange.getRequest();
		ServerHttpResponse response = exchange.getResponse();
		String logPrefix = exchange.getLogPrefix();

		if (isDisconnectedClientError(ex)) {
			if (lostClientLogger.isTraceEnabled()) {
				lostClientLogger.trace(logPrefix + "Client went away", ex);
			}
			else if (lostClientLogger.isDebugEnabled()) {
				lostClientLogger.debug(logPrefix + "Client went away: " + ex +
						" (stacktrace at TRACE level for '" + DISCONNECTED_CLIENT_LOG_CATEGORY + "')");
			}
			return Mono.empty();
		}
		else if (response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR)) {
			logger.error(logPrefix + "500 Server Error for " + formatRequest(request), ex);
			return Mono.empty();
		}
		else {
			// After the response is committed, propagate errors to the server..
			logger.error(logPrefix + "Error [" + ex + "] for " + formatRequest(request) +
					", but ServerHttpResponse already committed (" + response.getStatusCode() + ")");
			return Mono.error(ex);
		}
	}
 
Example 9
/**
 * 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 10
/**
 * 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)));
}