public DefaultClientRequestBuilder(ClientRequest other) { Assert.notNull(other, "ClientRequest must not be null"); this.method = other.method(); this.url = other.url(); headers(headers -> headers.addAll(other.headers())); cookies(cookies -> cookies.addAll(other.cookies())); attributes(attributes -> attributes.putAll(other.attributes())); body(other.body()); }
private static String getBody(ClientRequest request) { final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>(); messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder())); messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly())); messageWriters.add(new ResourceHttpMessageWriter()); Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder(); messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder)); messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder)); messageWriters.add(new FormHttpMessageWriter()); messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes())); messageWriters.add(new MultipartHttpMessageWriter(messageWriters)); BodyInserter.Context context = new BodyInserter.Context() { @Override public List<HttpMessageWriter<?>> messageWriters() { return messageWriters; } @Override public Optional<ServerHttpRequest> serverRequest() { return Optional.empty(); } @Override public Map<String, Object> hints() { return new HashMap<>(); } }; MockClientHttpRequest body = new MockClientHttpRequest(HttpMethod.GET, "/"); request.body().insert(body, context).block(); return body.getBodyAsString().block(); }
request.body().insert(body, context).block(); return body.getBodyAsString().block();
@Override public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) { URI originalUrl = request.url(); String serviceId = originalUrl.getHost(); if(serviceId == null) { String msg = String.format("Request URI does not contain a valid hostname: %s", originalUrl.toString()); logger.warn(msg); return Mono.just(ClientResponse.create(HttpStatus.BAD_REQUEST).body(msg).build()); } //TODO: reactive lb client ServiceInstance instance = this.loadBalancerClient.choose(serviceId); if(instance == null) { String msg = String.format("Load balancer does not contain an instance for the service %s", serviceId); logger.warn(msg); return Mono.just(ClientResponse.create(HttpStatus.SERVICE_UNAVAILABLE).body(msg).build()); } URI uri = this.loadBalancerClient.reconstructURI(instance, originalUrl); ClientRequest newRequest = ClientRequest.method(request.method(), uri) .headers(headers -> headers.addAll(request.headers())) .cookies(cookies -> cookies.addAll(request.cookies())) .attributes(attributes -> attributes.putAll(request.attributes())) .body(request.body()) .build(); return next.exchange(newRequest); }
@Override public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) { URI originalUrl = request.url(); String serviceId = originalUrl.getHost(); if(serviceId == null) { String msg = String.format("Request URI does not contain a valid hostname: %s", originalUrl.toString()); logger.warn(msg); return Mono.just(ClientResponse.create(HttpStatus.BAD_REQUEST).body(msg).build()); } //TODO: reactive lb client ServiceInstance instance = this.loadBalancerClient.choose(serviceId); if(instance == null) { String msg = String.format("Load balancer does not contain an instance for the service %s", serviceId); logger.warn(msg); return Mono.just(ClientResponse.create(HttpStatus.SERVICE_UNAVAILABLE).body(msg).build()); } URI uri = this.loadBalancerClient.reconstructURI(instance, originalUrl); ClientRequest newRequest = ClientRequest.method(request.method(), uri) .headers(headers -> headers.addAll(request.headers())) .cookies(cookies -> cookies.addAll(request.cookies())) .attributes(attributes -> attributes.putAll(request.attributes())) .body(request.body()) .build(); return next.exchange(newRequest); }