Example usage for io.netty.handler.codec.http HttpResponseStatus TOO_MANY_REQUESTS

List of usage examples for io.netty.handler.codec.http HttpResponseStatus TOO_MANY_REQUESTS

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpResponseStatus TOO_MANY_REQUESTS.

Prototype

HttpResponseStatus TOO_MANY_REQUESTS

To view the source code for io.netty.handler.codec.http HttpResponseStatus TOO_MANY_REQUESTS.

Click Source Link

Document

429 Too Many Requests (RFC6585)

Usage

From source file:io.crate.protocols.http.HttpBlobHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (cause instanceof ClosedChannelException) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("channel closed: {}", cause.toString());
        }/* w w  w.  j a v  a2s  . co  m*/
        return;
    } else if (cause instanceof IOException) {
        String message = cause.getMessage();
        if (message != null && message.contains("Connection reset by peer")) {
            LOGGER.debug(message);
        } else if (cause instanceof NotSslRecordException) {
            // Raised when clients try to send unencrypted data over an encrypted channel
            // This can happen when old instances of the Admin UI are running because the
            // ports of HTTP/HTTPS are the same.
            LOGGER.debug("Received unencrypted message from '{}'", ctx.channel().remoteAddress());
        } else {
            LOGGER.warn(message, cause);
        }
        return;
    }

    HttpResponseStatus status;
    String body = null;
    if (cause instanceof DigestMismatchException || cause instanceof BlobsDisabledException
            || cause instanceof IllegalArgumentException) {
        status = HttpResponseStatus.BAD_REQUEST;
        body = String.format(Locale.ENGLISH, "Invalid request sent: %s", cause.getMessage());
    } else if (cause instanceof DigestNotFoundException || cause instanceof IndexNotFoundException) {
        status = HttpResponseStatus.NOT_FOUND;
    } else if (cause instanceof EsRejectedExecutionException) {
        status = HttpResponseStatus.TOO_MANY_REQUESTS;
        body = String.format(Locale.ENGLISH, "Rejected execution: %s", cause.getMessage());
    } else {
        status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
        body = String.format(Locale.ENGLISH, "Unhandled exception: %s", cause);
    }
    if (body != null) {
        LOGGER.debug(body);
    }
    simpleResponse(status, body);
}