org.springframework.http.server.reactive.AbstractServerHttpResponse Java Examples

The following examples show how to use org.springframework.http.server.reactive.AbstractServerHttpResponse. 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: ReactorNettyRequestUpgradeStrategy.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
		@Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {

	ServerHttpResponse response = exchange.getResponse();
	HttpServerResponse reactorResponse = ((AbstractServerHttpResponse) response).getNativeResponse();
	HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
	NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();

	return reactorResponse.sendWebsocket(subProtocol, this.maxFramePayloadLength,
			(in, out) -> {
				ReactorNettyWebSocketSession session =
						new ReactorNettyWebSocketSession(
								in, out, handshakeInfo, bufferFactory, this.maxFramePayloadLength);
				URI uri = exchange.getRequest().getURI();
				return handler.handle(session).checkpoint(uri + " [ReactorNettyRequestUpgradeStrategy]");
			});
}
 
Example #2
Source File: ReactorNettyRequestUpgradeStrategy.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
		@Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {

	ServerHttpResponse response = exchange.getResponse();
	HttpServerResponse reactorResponse = ((AbstractServerHttpResponse) response).getNativeResponse();
	HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
	NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();

	return reactorResponse.sendWebsocket(subProtocol, this.maxFramePayloadLength,
			(in, out) -> {
				ReactorNettyWebSocketSession session =
						new ReactorNettyWebSocketSession(
								in, out, handshakeInfo, bufferFactory, this.maxFramePayloadLength);
				return handler.handle(session);
			});
}
 
Example #3
Source File: ServerWebExchangeUtils.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
public static boolean setResponseStatus(ServerWebExchange exchange,
		HttpStatusHolder statusHolder) {
	if (exchange.getResponse().isCommitted()) {
		return false;
	}
	if (log.isDebugEnabled()) {
		log.debug("Setting response status to " + statusHolder);
	}
	if (statusHolder.getHttpStatus() != null) {
		return setResponseStatus(exchange, statusHolder.getHttpStatus());
	}
	if (statusHolder.getStatus() != null
			&& exchange.getResponse() instanceof AbstractServerHttpResponse) { // non-standard
		((AbstractServerHttpResponse) exchange.getResponse())
				.setStatusCodeValue(statusHolder.getStatus());
		return true;
	}
	return false;
}
 
Example #4
Source File: NettyRoutingFilter.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
private void setResponseStatus(HttpClientResponse clientResponse,
		ServerHttpResponse response) {
	HttpStatus status = HttpStatus.resolve(clientResponse.status().code());
	if (status != null) {
		response.setStatusCode(status);
	}
	else {
		while (response instanceof ServerHttpResponseDecorator) {
			response = ((ServerHttpResponseDecorator) response).getDelegate();
		}
		if (response instanceof AbstractServerHttpResponse) {
			((AbstractServerHttpResponse) response)
					.setStatusCodeValue(clientResponse.status().code());
		}
		else {
			// TODO: log warning here, not throw error?
			throw new IllegalStateException("Unable to set status code "
					+ clientResponse.status().code() + " on response of type "
					+ response.getClass().getName());
		}
	}
}
 
Example #5
Source File: DefaultServerResponseBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void writeStatusAndHeaders(ServerHttpResponse response) {
	if (response instanceof AbstractServerHttpResponse) {
		((AbstractServerHttpResponse) response).setStatusCodeValue(this.statusCode);
	}
	else {
		HttpStatus status = HttpStatus.resolve(this.statusCode);
		if (status == null) {
			throw new IllegalStateException(
					"Unresolvable HttpStatus for general ServerHttpResponse: " + this.statusCode);
		}
		response.setStatusCode(status);
	}
	copy(this.headers, response.getHeaders());
	copy(this.cookies, response.getCookies());
}
 
Example #6
Source File: DefaultServerResponseBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void writeStatusAndHeaders(ServerHttpResponse response) {
	if (response instanceof AbstractServerHttpResponse) {
		((AbstractServerHttpResponse) response).setStatusCodeValue(this.statusCode);
	}
	else {
		HttpStatus status = HttpStatus.resolve(this.statusCode);
		if (status == null) {
			throw new IllegalStateException(
					"Unresolvable HttpStatus for general ServerHttpResponse: " + this.statusCode);
		}
		response.setStatusCode(status);
	}
	copy(this.headers, response.getHeaders());
	copy(this.cookies, response.getCookies());
}
 
Example #7
Source File: GatewayHttpTagsProvider.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public Tags apply(ServerWebExchange exchange) {
	String outcome = "CUSTOM";
	String status = "CUSTOM";
	String httpStatusCodeStr = "NA";

	String httpMethod = exchange.getRequest().getMethodValue();

	// a non standard HTTPS status could be used. Let's be defensive here
	// it needs to be checked for first, otherwise the delegate response
	// who's status DIDN'T change, will be used
	if (exchange.getResponse() instanceof AbstractServerHttpResponse) {
		Integer statusInt = ((AbstractServerHttpResponse) exchange.getResponse())
				.getStatusCodeValue();
		if (statusInt != null) {
			status = String.valueOf(statusInt);
			httpStatusCodeStr = status;
			HttpStatus resolved = HttpStatus.resolve(statusInt);
			if (resolved != null) {
				// this is not a CUSTOM status, so use series here.
				outcome = resolved.series().name();
				status = resolved.name();
			}
		}
	}
	else {
		HttpStatus statusCode = exchange.getResponse().getStatusCode();
		if (statusCode != null) {
			httpStatusCodeStr = String.valueOf(statusCode.value());
			outcome = statusCode.series().name();
			status = statusCode.name();
		}
	}

	return Tags.of("outcome", outcome, "status", status, "httpStatusCode",
			httpStatusCodeStr, "httpMethod", httpMethod);
}
 
Example #8
Source File: JettyRequestUpgradeStrategy.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private HttpServletResponse getHttpServletResponse(ServerHttpResponse response) {
	Assert.isInstanceOf(AbstractServerHttpResponse.class, response);
	return ((AbstractServerHttpResponse) response).getNativeResponse();
}
 
Example #9
Source File: TomcatRequestUpgradeStrategy.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private HttpServletResponse getHttpServletResponse(ServerHttpResponse response) {
	Assert.isInstanceOf(AbstractServerHttpResponse.class, response, "ServletServerHttpResponse required");
	return ((AbstractServerHttpResponse) response).getNativeResponse();
}
 
Example #10
Source File: ResponseEntityResultHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {

	Mono<?> returnValueMono;
	MethodParameter bodyParameter;
	ReactiveAdapter adapter = getAdapter(result);
	MethodParameter actualParameter = result.getReturnTypeSource();

	if (adapter != null) {
		Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
		returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
		bodyParameter = actualParameter.nested().nested();
	}
	else {
		returnValueMono = Mono.justOrEmpty(result.getReturnValue());
		bodyParameter = actualParameter.nested();
	}

	return returnValueMono.flatMap(returnValue -> {
		HttpEntity<?> httpEntity;
		if (returnValue instanceof HttpEntity) {
			httpEntity = (HttpEntity<?>) returnValue;
		}
		else if (returnValue instanceof HttpHeaders) {
			httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK);
		}
		else {
			throw new IllegalArgumentException(
					"HttpEntity or HttpHeaders expected but got: " + returnValue.getClass());
		}

		if (httpEntity instanceof ResponseEntity) {
			ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
			ServerHttpResponse response = exchange.getResponse();
			if (response instanceof AbstractServerHttpResponse) {
				((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
			}
			else {
				response.setStatusCode(responseEntity.getStatusCode());
			}
		}

		HttpHeaders entityHeaders = httpEntity.getHeaders();
		HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
		if (!entityHeaders.isEmpty()) {
			entityHeaders.entrySet().stream()
					.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
		}

		if (httpEntity.getBody() == null || returnValue instanceof HttpHeaders) {
			return exchange.getResponse().setComplete();
		}

		String etag = entityHeaders.getETag();
		Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
		HttpMethod httpMethod = exchange.getRequest().getMethod();
		if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
			return exchange.getResponse().setComplete();
		}

		return writeBody(httpEntity.getBody(), bodyParameter, actualParameter, exchange);
	});
}
 
Example #11
Source File: JettyRequestUpgradeStrategy.java    From java-technology-stack with MIT License 4 votes vote down vote up
private HttpServletResponse getHttpServletResponse(ServerHttpResponse response) {
	Assert.isInstanceOf(AbstractServerHttpResponse.class, response);
	return ((AbstractServerHttpResponse) response).getNativeResponse();
}
 
Example #12
Source File: TomcatRequestUpgradeStrategy.java    From java-technology-stack with MIT License 4 votes vote down vote up
private HttpServletResponse getHttpServletResponse(ServerHttpResponse response) {
	Assert.isInstanceOf(AbstractServerHttpResponse.class, response, "ServletServerHttpResponse required");
	return ((AbstractServerHttpResponse) response).getNativeResponse();
}
 
Example #13
Source File: ResponseEntityResultHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {

	Mono<?> returnValueMono;
	MethodParameter bodyParameter;
	ReactiveAdapter adapter = getAdapter(result);
	MethodParameter actualParameter = result.getReturnTypeSource();

	if (adapter != null) {
		Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
		returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
		bodyParameter = actualParameter.nested().nested();
	}
	else {
		returnValueMono = Mono.justOrEmpty(result.getReturnValue());
		bodyParameter = actualParameter.nested();
	}

	return returnValueMono.flatMap(returnValue -> {
		HttpEntity<?> httpEntity;
		if (returnValue instanceof HttpEntity) {
			httpEntity = (HttpEntity<?>) returnValue;
		}
		else if (returnValue instanceof HttpHeaders) {
			httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK);
		}
		else {
			throw new IllegalArgumentException(
					"HttpEntity or HttpHeaders expected but got: " + returnValue.getClass());
		}

		if (httpEntity instanceof ResponseEntity) {
			ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
			ServerHttpResponse response = exchange.getResponse();
			if (response instanceof AbstractServerHttpResponse) {
				((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
			}
			else {
				response.setStatusCode(responseEntity.getStatusCode());
			}
		}

		HttpHeaders entityHeaders = httpEntity.getHeaders();
		HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
		if (!entityHeaders.isEmpty()) {
			entityHeaders.entrySet().stream()
					.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
		}

		if (httpEntity.getBody() == null || returnValue instanceof HttpHeaders) {
			return exchange.getResponse().setComplete();
		}

		String etag = entityHeaders.getETag();
		Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
		HttpMethod httpMethod = exchange.getRequest().getMethod();
		if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
			return exchange.getResponse().setComplete();
		}

		return writeBody(httpEntity.getBody(), bodyParameter, actualParameter, exchange);
	});
}