@Test public void toStringFormat() { RequestSizeConfig config = new RequestSizeConfig(); config.setMaxSize(DataSize.ofBytes(1000L)); GatewayFilter filter = new RequestSizeGatewayFilterFactory().apply(config); assertThat(filter.toString()).contains("max").contains("1000"); }
@Override public GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig) { requestSizeConfig.validate(); return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String contentLength = request.getHeaders().getFirst("content-length"); if (!StringUtils.isEmpty(contentLength)) { Long currentRequestSize = Long.valueOf(contentLength); if (currentRequestSize > requestSizeConfig.getMaxSize().toBytes()) { exchange.getResponse().setStatusCode(HttpStatus.PAYLOAD_TOO_LARGE); if (!exchange.getResponse().isCommitted()) { exchange.getResponse().getHeaders().add("errorMessage", getErrorMessage(currentRequestSize, requestSizeConfig.getMaxSize().toBytes())); } return exchange.getResponse().setComplete(); } } return chain.filter(exchange); } @Override public String toString() { return filterToStringCreator(RequestSizeGatewayFilterFactory.this) .append("max", requestSizeConfig.getMaxSize()).toString(); } }; }
RequestSizeGatewayFilterFactory extends AbstractGatewayFilterFactory<RequestSizeGatewayFilterFactory.RequestSizeConfig> { @Override public GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig) { requestSizeConfig.validate(); return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String contentLength = request.getHeaders().getFirst("content-length"); if (!StringUtils.isEmpty(contentLength)) { Long currentRequestSize = Long.valueOf(contentLength); if (currentRequestSize > requestSizeConfig.getMaxSize().toBytes()) { exchange.getResponse().setStatusCode(HttpStatus.PAYLOAD_TOO_LARGE); if (!exchange.getResponse().isCommitted()) { exchange.getResponse().getHeaders().add("errorMessage", getErrorMessage(currentRequestSize, requestSizeConfig.getMaxSize().toBytes())); } return exchange.getResponse().setComplete(); } } return chain.filter(exchange); } @Override public String toString() { return filterToStringCreator(RequestSizeGatewayFilterFactory.this) .append("max", requestSizeConfig.getMaxSize()).toString(); } }; } }
RequestSizeGatewayFilterFactory extends AbstractGatewayFilterFactory<RequestSizeGatewayFilterFactory.RequestSizeConfig> { @Override public GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig) { requestSizeConfig.validate(); return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String contentLength = request.getHeaders().getFirst("content-length"); if (!StringUtils.isEmpty(contentLength)) { Long currentRequestSize = Long.valueOf(contentLength); if (currentRequestSize > requestSizeConfig.getMaxSize().toBytes()) { exchange.getResponse().setStatusCode(HttpStatus.PAYLOAD_TOO_LARGE); if (!exchange.getResponse().isCommitted()) { exchange.getResponse().getHeaders().add("errorMessage", getErrorMessage(currentRequestSize, requestSizeConfig.getMaxSize().toBytes())); } return exchange.getResponse().setComplete(); } } return chain.filter(exchange); } @Override public String toString() { return filterToStringCreator(RequestSizeGatewayFilterFactory.this) .append("max", requestSizeConfig.getMaxSize()).toString(); } }; } RequestSizeGatewayFilterFactory(); }
RequestSizeGatewayFilterFactory extends AbstractGatewayFilterFactory<RequestSizeGatewayFilterFactory.RequestSizeConfig> { @Override public GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig) { requestSizeConfig.validate(); return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String contentLength = request.getHeaders().getFirst("content-length"); if (!StringUtils.isEmpty(contentLength)) { Long currentRequestSize = Long.valueOf(contentLength); if (currentRequestSize > requestSizeConfig.getMaxSize().toBytes()) { exchange.getResponse().setStatusCode(HttpStatus.PAYLOAD_TOO_LARGE); if (!exchange.getResponse().isCommitted()) { exchange.getResponse().getHeaders().add("errorMessage", getErrorMessage(currentRequestSize, requestSizeConfig.getMaxSize().toBytes())); } return exchange.getResponse().setComplete(); } } return chain.filter(exchange); } @Override public String toString() { return filterToStringCreator(RequestSizeGatewayFilterFactory.this) .append("max", requestSizeConfig.getMaxSize()).toString(); } }; } RequestSizeGatewayFilterFactory(); @Override GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig); }
RequestSizeGatewayFilterFactory extends AbstractGatewayFilterFactory<RequestSizeGatewayFilterFactory.RequestSizeConfig> { @Override public GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig) { requestSizeConfig.validate(); return new GatewayFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String contentLength = request.getHeaders().getFirst("content-length"); if (!StringUtils.isEmpty(contentLength)) { Long currentRequestSize = Long.valueOf(contentLength); if (currentRequestSize > requestSizeConfig.getMaxSize().toBytes()) { exchange.getResponse().setStatusCode(HttpStatus.PAYLOAD_TOO_LARGE); if (!exchange.getResponse().isCommitted()) { exchange.getResponse().getHeaders().add("errorMessage", getErrorMessage(currentRequestSize, requestSizeConfig.getMaxSize().toBytes())); } return exchange.getResponse().setComplete(); } } return chain.filter(exchange); } @Override public String toString() { return filterToStringCreator(RequestSizeGatewayFilterFactory.this) .append("max", requestSizeConfig.getMaxSize()).toString(); } }; } RequestSizeGatewayFilterFactory(); @Override GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig);  }