Update to new APIs

fixes gh-39
This commit is contained in:
Spencer Gibb
2017-04-26 11:47:51 -06:00
parent 7d1a1e09de
commit 82dd269489
7 changed files with 16 additions and 15 deletions

View File

@@ -128,21 +128,22 @@ public class GatewayEndpoint implements ApplicationEventPublisherAware {/*extend
http POST :8080/admin/gateway/routes/apiaddreqhead uri=http://httpbin.org:80 predicates:='["Host=**.apiaddrequestheader.org", "Path=/headers"]' filters:='["AddRequestHeader=X-Request-ApiFoo, ApiBar"]'
*/
@PostMapping("/routes/{id}")
@SuppressWarnings("unchecked")
public Mono<ResponseEntity<Void>> save(@PathVariable String id, @RequestBody Mono<RouteDefinition> route) {
return this.routeDefinitionWriter.save(route.map(r -> {
r.setId(id);
log.debug("Saving route: " + route);
return r;
})).then(() ->
})).then(Mono.defer(() ->
Mono.just(ResponseEntity.created(URI.create("/routes/"+id)).build())
);
));
}
@DeleteMapping("/routes/{id}")
public Mono<ResponseEntity<Object>> delete(@PathVariable String id) {
return this.routeDefinitionWriter.delete(Mono.just(id))
.then(() -> Mono.just(ResponseEntity.ok().build()))
.otherwise(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build()));
.then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build())))
.onErrorResume(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build()));
}
@GetMapping("/routes/{id}")
@@ -151,7 +152,7 @@ http POST :8080/admin/gateway/routes/apiaddreqhead uri=http://httpbin.org:80 pre
.filter(route -> route.getId().equals(id))
.singleOrEmpty()
.map(route -> ResponseEntity.ok(route))
.otherwiseIfEmpty(Mono.just(ResponseEntity.notFound().build()));
.switchIfEmpty(Mono.just(ResponseEntity.notFound().build()));
}
@GetMapping("/routes/{id}/combinedfilters")

View File

@@ -51,7 +51,7 @@ public class WriteResponseFilter implements GlobalFilter, Ordered {
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
// NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added
// until the WebHandler is run
return chain.filter(exchange).then(() -> {
return chain.filter(exchange).then(Mono.defer(() -> {
HttpClientResponse clientResponse = getAttribute(exchange, CLIENT_RESPONSE_ATTR, HttpClientResponse.class);
if (clientResponse == null) {
return Mono.empty();
@@ -67,7 +67,7 @@ public class WriteResponseFilter implements GlobalFilter, Ordered {
.map(factory::wrap);
return response.writeWith(body);
});
}));
}
}

View File

@@ -61,7 +61,7 @@ public class RedirectToWebFilterFactory implements WebFilterFactory {
}
return (exchange, chain) ->
chain.filter(exchange).then(() -> {
chain.filter(exchange).then(Mono.defer(() -> {
if (!exchange.getResponse().isCommitted()) {
setResponseStatus(exchange, httpStatus);
@@ -70,7 +70,7 @@ public class RedirectToWebFilterFactory implements WebFilterFactory {
return response.setComplete();
}
return Mono.empty();
});
}));
}
}

View File

@@ -56,13 +56,13 @@ public class SetStatusWebFilterFactory implements WebFilterFactory {
return chain.filter(exchange);*/
// option 2 (runs in reverse filter order)
return chain.filter(exchange).then(() -> {
return chain.filter(exchange).then(Mono.defer(() -> {
// check not really needed, since it is guarded in setStatusCode, but it's a good example
if (!exchange.getResponse().isCommitted()) {
setResponseStatus(exchange, httpStatus);
}
return Mono.empty();
});
}));
};
}

View File

@@ -58,12 +58,12 @@ public class RoutePredicateHandlerMapping extends AbstractHandlerMapping {
exchange.getAttributes().put(GATEWAY_ROUTE_ATTR, r);
return Mono.just(webHandler);
}).otherwiseIfEmpty(Mono.empty().then(() -> {
}).switchIfEmpty(Mono.empty().then(Mono.defer(() -> {
if (logger.isTraceEnabled()) {
logger.trace("No RouteDefinition found for [" + getExchangeDesc(exchange) + "]");
}
return Mono.empty();
}));
})));
}
//TODO: get desc from factory?

View File

@@ -64,7 +64,7 @@ public class GatewayIntegrationTests extends BaseWebClientTests {
Mono<Map> result = webClient.post()
.uri("/headers")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body("testdata")
.syncBody("testdata")
.header("Host", "www.complexcontenttype.org")
.exchange()
.flatMap(response -> response.body(toMono(Map.class)));

View File

@@ -47,7 +47,7 @@ public class PostTests extends BaseWebClientTests {
Mono<Map> result = webClient.post()
.uri("/post")
.header("Host", "www.example.org")
.body("testdata")
.syncBody("testdata")
.exchange()
.flatMap(response -> response.body(toMono(Map.class)));