Example usage for org.springframework.http HttpStatus TOO_MANY_REQUESTS

List of usage examples for org.springframework.http HttpStatus TOO_MANY_REQUESTS

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus TOO_MANY_REQUESTS.

Prototype

HttpStatus TOO_MANY_REQUESTS

To view the source code for org.springframework.http HttpStatus TOO_MANY_REQUESTS.

Click Source Link

Document

429 Too Many Requests .

Usage

From source file:org.spring.data.gemfire.rest.GemFireRestInterfaceTest.java

@SuppressWarnings("deprecation")
private RestTemplate setErrorHandler(final RestTemplate restTemplate) {
    restTemplate.setErrorHandler(new ResponseErrorHandler() {
        private final Set<HttpStatus> errorStatuses = new HashSet<>();

        /* non-static */ {
            errorStatuses.add(HttpStatus.BAD_REQUEST);
            errorStatuses.add(HttpStatus.UNAUTHORIZED);
            errorStatuses.add(HttpStatus.FORBIDDEN);
            errorStatuses.add(HttpStatus.NOT_FOUND);
            errorStatuses.add(HttpStatus.METHOD_NOT_ALLOWED);
            errorStatuses.add(HttpStatus.NOT_ACCEPTABLE);
            errorStatuses.add(HttpStatus.REQUEST_TIMEOUT);
            errorStatuses.add(HttpStatus.CONFLICT);
            errorStatuses.add(HttpStatus.REQUEST_ENTITY_TOO_LARGE);
            errorStatuses.add(HttpStatus.REQUEST_URI_TOO_LONG);
            errorStatuses.add(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
            errorStatuses.add(HttpStatus.TOO_MANY_REQUESTS);
            errorStatuses.add(HttpStatus.INTERNAL_SERVER_ERROR);
            errorStatuses.add(HttpStatus.NOT_IMPLEMENTED);
            errorStatuses.add(HttpStatus.BAD_GATEWAY);
            errorStatuses.add(HttpStatus.SERVICE_UNAVAILABLE);
        }/* ww w. j  a v  a2 s  .  c  o m*/

        @Override
        public boolean hasError(final ClientHttpResponse response) throws IOException {
            return errorStatuses.contains(response.getStatusCode());
        }

        @Override
        public void handleError(final ClientHttpResponse response) throws IOException {
            System.err.printf("%1$d - %2$s%n", response.getRawStatusCode(), response.getStatusText());
            System.err.println(readBody(response));
        }

        private String readBody(final ClientHttpResponse response) throws IOException {
            BufferedReader responseBodyReader = null;

            try {
                responseBodyReader = new BufferedReader(new InputStreamReader(response.getBody()));

                StringBuilder buffer = new StringBuilder();
                String line;

                while ((line = responseBodyReader.readLine()) != null) {
                    buffer.append(line).append(System.getProperty("line.separator"));
                }

                return buffer.toString().trim();
            } finally {
                FileSystemUtils.close(responseBodyReader);
            }
        }
    });

    return restTemplate;
}

From source file:org.springframework.boot.actuate.endpoint.web.HeapDumpWebEndpoint.java

@ReadOperation
public WebEndpointResponse<Resource> heapDump(Boolean live) {
    try {/*from  w  w w  .j a v  a 2s. com*/
        if (this.lock.tryLock(this.timeout, TimeUnit.MILLISECONDS)) {
            try {
                return new WebEndpointResponse<>(dumpHeap(live == null ? true : live));
            } finally {
                this.lock.unlock();
            }
        }
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    } catch (IOException ex) {
        return new WebEndpointResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value());
    } catch (HeapDumperUnavailableException ex) {
        return new WebEndpointResponse<>(HttpStatus.SERVICE_UNAVAILABLE.value());
    }
    return new WebEndpointResponse<>(HttpStatus.TOO_MANY_REQUESTS.value());
}

From source file:org.springframework.cloud.gateway.sample.ThrottleGatewayFilter.java

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

    TokenBucket tokenBucket = TokenBuckets.builder().withCapacity(capacity)
            .withFixedIntervalRefillStrategy(refillTokens, refillPeriod, refillUnit).build();

    // TODO: get a token bucket for a key
    log.debug("TokenBucket capacity: " + tokenBucket.getCapacity());
    boolean consumed = tokenBucket.tryConsume();
    if (consumed) {
        return chain.filter(exchange);
    }//  w  ww. j  a  v a 2  s  . co  m
    exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
    return exchange.getResponse().setComplete();
}