Java Code Examples for org.springframework.http.HttpStatus#TOO_MANY_REQUESTS

The following examples show how to use org.springframework.http.HttpStatus#TOO_MANY_REQUESTS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTwitterInboundChannelAdapter.java    From spring-cloud-stream-app-starters with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
	while (running.get()) {
		try {
			readStream(twitter.getRestTemplate());
		}
		catch (HttpStatusCodeException sce) {
			if (sce.getStatusCode() == HttpStatus.UNAUTHORIZED) {
				logger.error("Twitter authentication failed: " + sce.getMessage());
				running.set(false);
			}
			else if (sce.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {
				waitRateLimitBackoff();
			}
			else {
				waitHttpErrorBackoff();
			}
		}
		catch (Exception e) {
			logger.warn("Exception while reading stream.", e);
			waitLinearBackoff();
		}
	}
}
 
Example 2
Source File: StandardBullhornData.java    From sdk-rest with MIT License 6 votes vote down vote up
/**
 * @param uriVariables
 * @param tryNumber
 * @param error
 * @throws RestApiException if tryNumber >= API_RETRY.
 */
protected boolean handleHttpStatusCodeError(Map<String, String> uriVariables, int tryNumber, HttpStatusCodeException error) {
    boolean isTooManyRequestsError = false;
    if (error.getStatusCode() == HttpStatus.UNAUTHORIZED) {
        resetBhRestToken(uriVariables);
    } else if (error.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {
        isTooManyRequestsError = true;
    }
    log.error(
            "HttpStatusCodeError making api call. Try number:" + tryNumber + " out of " + API_RETRY + ". Http status code: "
                    + error.getStatusCode() + ". Response body: " + error.getResponseBodyAsString(), error);
    if (tryNumber >= API_RETRY && !isTooManyRequestsError) {
        throw new RestApiException("HttpStatusCodeError making api call with url variables " + uriVariables.toString()
                + ". Http status code: " + error.getStatusCode().toString() + ". Response body: " + error == null ? ""
                : error.getResponseBodyAsString());
    }
    return isTooManyRequestsError;
}
 
Example 3
Source File: HttpClientErrorException.java    From spring-analysis-note with MIT License 4 votes vote down vote up
TooManyRequests(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
	super(HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset);
}
 
Example 4
Source File: BasicController.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
@ExceptionHandler(TooManyRequestException.class)
@ResponseBody
ErrorResponse handleTooManyRequest(HttpServletRequest req, Exception ex) {
    return new ErrorResponse(req.getRequestURL().toString(), ex);
}
 
Example 5
Source File: HttpClientErrorException.java    From java-technology-stack with MIT License 4 votes vote down vote up
TooManyRequests(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
	super(HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset);
}
 
Example 6
Source File: CustomExceptionHandler.java    From lion with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {

    log.error(ex.getMessage());

    /**
     * 按照异常类型进行处理
     */
    HttpStatus httpStatus;
    String body;
    if (ex instanceof BlockException) {
        httpStatus = HttpStatus.TOO_MANY_REQUESTS;
        // Too Many Request Server
        body = JsonUtil.jsonObj2Str(Result.failure(httpStatus.value(), "前方拥挤,请稍后再试"));
    } else {
        //ResponseStatusException responseStatusException = (ResponseStatusException) ex;
        //httpStatus = responseStatusException.getStatus();
        //body = responseStatusException.getMessage();

        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        // Internal Server Error
        body = JsonUtil.jsonObj2Str(Result.failure(httpStatus.value(), "调用失败,服务不可用"));
    }
    /**
     * 封装响应体,此body可修改为自己的jsonBody
     */
    Map<String, Object> result = new HashMap<>(2, 1);
    result.put("httpStatus", httpStatus);
    result.put("body", body);
    /**
     * 错误记录
     */
    ServerHttpRequest request = exchange.getRequest();
    log.error("[全局异常处理]异常请求路径:{},记录异常信息:{}", request.getPath(), body);
    /**
     * 参考AbstractErrorWebExceptionHandler
     */
    if (exchange.getResponse().isCommitted()) {
        return Mono.error(ex);
    }
    exceptionHandlerResult.set(result);
    ServerRequest newRequest = ServerRequest.create(exchange, this.messageReaders);
    Mono<Void> mono = RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse).route(newRequest)
            .switchIfEmpty(Mono.error(ex))
            .flatMap((handler) -> handler.handle(newRequest))
            .flatMap((response) -> write(exchange, response));
    exceptionHandlerResult.remove();
    return mono;
}
 
Example 7
Source File: TooManyRequestsExceptionAdvice.java    From seckill with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public Object exceptionHandler(Exception e) {
  return new HttpEntity<>(e.getMessage());
}
 
Example 8
Source File: WebfluxRateLimitException.java    From bucket4j-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public WebfluxRateLimitException(String reason) {
	super(HttpStatus.TOO_MANY_REQUESTS, reason);
}
 
Example 9
Source File: ClientfacingErrorITest.java    From backstopper with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/throwTooManyRequestsException")
public void throwTooManyRequestsException() {
    HttpServerErrorException serverResponseEx = new HttpServerErrorException(HttpStatus.TOO_MANY_REQUESTS);
    throw new ServerHttpStatusCodeException(new Exception("Intentional test exception"), "FOO", serverResponseEx, serverResponseEx.getStatusCode().value(), serverResponseEx.getResponseHeaders(), serverResponseEx.getResponseBodyAsString());
}
 
Example 10
Source File: BasicController.java    From kylin with Apache License 2.0 4 votes vote down vote up
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
@ExceptionHandler(TooManyRequestException.class)
@ResponseBody
ErrorResponse handleTooManyRequest(HttpServletRequest req, Exception ex) {
    return new ErrorResponse(req.getRequestURL().toString(), ex);
}