Search in sources :

Example 21 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project netty by netty.

the class SpdyHttpDecoder method createHttpResponse.

private static FullHttpResponse createHttpResponse(SpdyHeadersFrame responseFrame, ByteBufAllocator alloc, boolean validateHeaders) throws Exception {
    // Create the first line of the response from the name/value pairs
    SpdyHeaders headers = responseFrame.headers();
    HttpResponseStatus status = HttpResponseStatus.parseLine(headers.get(STATUS));
    HttpVersion version = HttpVersion.valueOf(headers.getAsString(VERSION));
    headers.remove(STATUS);
    headers.remove(VERSION);
    boolean release = true;
    ByteBuf buffer = alloc.buffer();
    try {
        FullHttpResponse res = new DefaultFullHttpResponse(version, status, buffer, validateHeaders);
        for (Map.Entry<CharSequence, CharSequence> e : responseFrame.headers()) {
            res.headers().add(e.getKey(), e.getValue());
        }
        // The Connection and Keep-Alive headers are no longer valid
        HttpUtil.setKeepAlive(res, true);
        // Transfer-Encoding header is not valid
        res.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
        res.headers().remove(HttpHeaderNames.TRAILER);
        release = false;
        return res;
    } finally {
        if (release) {
            buffer.release();
        }
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project ribbon by Netflix.

the class RxMovieServerTest method testMovieRegistration.

@Test
public void testMovieRegistration() {
    String movieFormatted = ORANGE_IS_THE_NEW_BLACK.toString();
    HttpResponseStatus statusCode = RxNetty.createHttpPost(baseURL + "/movies", Observable.just(movieFormatted), new StringTransformer()).flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<HttpResponseStatus>>() {

        @Override
        public Observable<HttpResponseStatus> call(HttpClientResponse<ByteBuf> httpClientResponse) {
            return Observable.just(httpClientResponse.getStatus());
        }
    }).toBlocking().first();
    assertEquals(HttpResponseStatus.CREATED, statusCode);
    assertEquals(ORANGE_IS_THE_NEW_BLACK, movieServer.movies.get(ORANGE_IS_THE_NEW_BLACK.getId()));
}
Also used : StringTransformer(io.reactivex.netty.channel.StringTransformer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) Test(org.junit.Test)

Example 23 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project ribbon by Netflix.

the class RxMovieServerTest method testUpateRecommendations.

@Test
public void testUpateRecommendations() {
    movieServer.movies.put(ORANGE_IS_THE_NEW_BLACK.getId(), ORANGE_IS_THE_NEW_BLACK);
    HttpResponseStatus statusCode = RxNetty.createHttpPost(baseURL + "/users/" + TEST_USER_ID + "/recommendations", Observable.just(ORANGE_IS_THE_NEW_BLACK.getId()), new StringTransformer()).flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<HttpResponseStatus>>() {

        @Override
        public Observable<HttpResponseStatus> call(HttpClientResponse<ByteBuf> httpClientResponse) {
            return Observable.just(httpClientResponse.getStatus());
        }
    }).toBlocking().first();
    assertEquals(HttpResponseStatus.OK, statusCode);
    assertTrue(movieServer.userRecommendations.get(TEST_USER_ID).contains(ORANGE_IS_THE_NEW_BLACK.getId()));
}
Also used : StringTransformer(io.reactivex.netty.channel.StringTransformer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) Test(org.junit.Test)

Example 24 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project vert.x by eclipse.

the class Http1xServerResponse method setStatusMessage.

@Override
public HttpServerResponse setStatusMessage(String statusMessage) {
    synchronized (conn) {
        checkHeadWritten();
        this.statusMessage = statusMessage;
        this.status = new HttpResponseStatus(status.code(), statusMessage);
        return this;
    }
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus)

Example 25 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project crate by crate.

the class HttpBlobHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (cause instanceof ClosedChannelException) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("channel closed: {}", cause.toString());
        }
        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(null, status, body);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) NotSslRecordException(io.netty.handler.ssl.NotSslRecordException) DigestNotFoundException(io.crate.blob.exceptions.DigestNotFoundException) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) IOException(java.io.IOException) BlobsDisabledException(io.crate.blob.v2.BlobsDisabledException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Aggregations

HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)53 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)16 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)15 ByteBuf (io.netty.buffer.ByteBuf)14 HttpResponse (io.netty.handler.codec.http.HttpResponse)9 HttpVersion (io.netty.handler.codec.http.HttpVersion)8 IOException (java.io.IOException)8 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)7 HttpMethod (io.netty.handler.codec.http.HttpMethod)7 Test (org.junit.Test)7 Map (java.util.Map)5 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)4 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 NoSuchElementException (java.util.NoSuchElementException)4 ChannelFuture (io.netty.channel.ChannelFuture)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)3 PrintWriter (java.io.PrintWriter)3 List (java.util.List)3 Unpooled (io.netty.buffer.Unpooled)2