@Override public <T> Mono<T> commence(ServerWebExchange exchange, AuthenticationException e) { ServerHttpResponse response = exchange.getResponse(); if (exchange.getRequest().getMethod().equals(HttpMethod.OPTIONS)) { response.setStatusCode(HttpStatus.OK); response.getHeaders().set("WWW-Authenticate", "Basic realm=\"Reactive\""); response.getHeaders().set("Access-Control-Allow-Credentials", "true"); response.getHeaders().set("Access-Control-Allow-Headers", "authorization, content-type"); response.getHeaders().set("Access-Control-Allow-Methods", "POST"); response.getHeaders().set("Access-Control-Allow-Origin", "http://localhost:3000"); response.getHeaders().set("Access-Control-Max-Age", "1800"); return Mono.empty(); } response.setStatusCode(HttpStatus.UNAUTHORIZED); response.getHeaders().set("WWW-Authenticate", "Basic realm=\"Reactive\""); return Mono.empty(); }
@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added // until the WebHandler is run return chain.filter(exchange).then(Mono.defer(() -> { HttpClientResponse clientResponse = exchange.getAttribute(CLIENT_RESPONSE_ATTR); if (clientResponse == null) { return Mono.empty(); } log.trace("NettyWriteResponseFilter start"); ServerHttpResponse response = exchange.getResponse(); NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory(); //TODO: what if it's not netty final Flux<NettyDataBuffer> body = clientResponse.receive() .retain() //TODO: needed? .map(factory::wrap); return response.writeWith(body); })); }
/** Override to add annotations not defined in {@link TraceKeys}. */ protected void addResponseTags(ServerHttpResponse response, Throwable e) { HttpStatus httpStatus = response.getStatusCode(); if (httpStatus != null && httpStatus == HttpStatus.OK && e != null) { // Filter chain threw exception but the response status may not have been set // yet, so we have to guess. tracer().addTag(traceKeys().getHttp().getStatusCode(), String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value())); } // only tag valid http statuses else if (httpStatus != null && (httpStatus.value() >= 100 && (httpStatus.value() < 200) || (httpStatus.value() > 399))) { tracer().addTag(traceKeys().getHttp().getStatusCode(), String.valueOf(response.getStatusCode().value())); } }
@RequestMapping(path = REQUEST_MAPPING_PATH, method = {RequestMethod.GET, RequestMethod.HEAD, RequestMethod.POST, RequestMethod.PUT, RequestMethod.PATCH, RequestMethod.DELETE, RequestMethod.OPTIONS}) public Mono<Void> endpointProxy(@PathVariable("instanceId") String instanceId, ServerHttpRequest request, ServerHttpResponse response) { String endpointLocalPath = getEndpointLocalPath(request.getPath().pathWithinApplication().value()); URI uri = UriComponentsBuilder.fromPath(endpointLocalPath) .query(request.getURI().getRawQuery()) .build(true) .toUri(); return super.forward(instanceId, uri, request.getMethod(), request.getHeaders(), () -> BodyInserters.fromDataBuffers(request.getBody())).flatMap(clientResponse -> { response.setStatusCode(clientResponse.statusCode()); response.getHeaders().addAll(filterHeaders(clientResponse.headers().asHttpHeaders())); return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers())); }); }
@GetMapping("/exchange") public Mono<Void> exchange(ServerWebExchange webExchange) { ServerHttpResponse response = webExchange.getResponse(); response.setStatusCode(HttpStatus.OK); response.getHeaders().setContentType(MediaType.TEXT_PLAIN); DataBuffer dataBuffer = factory.allocateBuffer().write("Using exchange!".getBytes(StandardCharsets.UTF_8)); return response.writeWith(Mono.just(dataBuffer)); }
@Override public <T> Mono<T> commence(ServerWebExchange exchange, AuthenticationException e) { ServerHttpResponse response = exchange.getResponse(); response.setStatusCode(HttpStatus.UNAUTHORIZED); response.getHeaders().set("WWW-Authenticate", "Basic realm=\"Reactive\""); return Mono.empty(); }
public GatewayFilter apply(HttpStatus httpStatus, URL url) { return (exchange, chain) -> chain.filter(exchange).then(Mono.defer(() -> { if (!exchange.getResponse().isCommitted()) { setResponseStatus(exchange, httpStatus); final ServerHttpResponse response = exchange.getResponse(); response.getHeaders().set(HttpHeaders.LOCATION, url.toString()); return response.setComplete(); } return Mono.empty(); })); }
@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added // until the WebHandler is run return chain.filter(exchange).then(Mono.defer(() -> { ClientResponse clientResponse = exchange.getAttribute(CLIENT_RESPONSE_ATTR); if (clientResponse == null) { return Mono.empty(); } log.trace("WebClientWriteResponseFilter start"); ServerHttpResponse response = exchange.getResponse(); return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers())).log("webClient response"); })); }
private void addResponseTagsForSpanWithoutParent(ServerWebExchange exchange, ServerHttpResponse response) { if (spanWithoutParent(exchange) && response.getStatusCode() != null) { tracer().addTag(traceKeys().getHttp().getStatusCode(), String.valueOf(response.getStatusCode().value())); } }
@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR); String scheme = requestUrl.getScheme(); if (isAlreadyRouted(exchange) || (!"http".equals(scheme) && !"https".equals(scheme))) { return chain.filter(exchange); } setAlreadyRouted(exchange); ServerHttpRequest request = exchange.getRequest(); HttpMethod method = request.getMethod(); RequestBodySpec bodySpec = this.webClient.method(method) .uri(requestUrl) .headers(httpHeaders -> { httpHeaders.addAll(request.getHeaders()); //TODO: can this support preserviceHostHeader? httpHeaders.remove(HttpHeaders.HOST); }); RequestHeadersSpec<?> headersSpec; if (requiresBody(method)) { headersSpec = bodySpec.body(BodyInserters.fromDataBuffers(request.getBody())); } else { headersSpec = bodySpec; } return headersSpec.exchange() // .log("webClient route") .flatMap(res -> { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().putAll(res.headers().asHttpHeaders()); response.setStatusCode(res.statusCode()); // Defer committing the response until all route filters have run // Put client response as ServerWebExchange attribute and write response later NettyWriteResponseFilter exchange.getAttributes().put(CLIENT_RESPONSE_ATTR, res); return chain.filter(exchange); }); }