Example usage for io.netty.handler.codec.http HttpResponse status

List of usage examples for io.netty.handler.codec.http HttpResponse status

Introduction

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

Prototype

HttpResponseStatus status();

Source Link

Document

Returns the status of this HttpResponse .

Usage

From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java

License:Apache License

/**
 * Returns the content length of the specified web socket message.  If the
 * specified message is not a web socket message, {@code -1} is returned.
 *///from   ww  w. java 2s .  c  o  m
private static int getWebSocketContentLength(HttpMessage message) {
    // WebSockset messages have constant content-lengths.
    HttpHeaders h = message.headers();
    if (message instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) message;
        if (HttpMethod.GET.equals(req.method()) && h.contains(Names.SEC_WEBSOCKET_KEY1)
                && h.contains(Names.SEC_WEBSOCKET_KEY2)) {
            return 8;
        }
    } else if (message instanceof HttpResponse) {
        HttpResponse res = (HttpResponse) message;
        if (res.status().code() == 101 && h.contains(Names.SEC_WEBSOCKET_ORIGIN)
                && h.contains(Names.SEC_WEBSOCKET_LOCATION)) {
            return 16;
        }
    }

    // Not a web socket message
    return -1;
}

From source file:ccwihr.client.t1.HttpUploadClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }/*from w  w w .  j  a  v  a  2s.c om*/
            }
        }

        if (response.status().code() == 200 && HttpUtil.isTransferEncodingChunked(response)) {
            readingChunks = true;
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent chunk = (HttpContent) msg;
        System.err.println(chunk.content().toString(CharsetUtil.UTF_8));

        if (chunk instanceof LastHttpContent) {
            if (readingChunks) {
                System.err.println("} END OF CHUNKED CONTENT");
            } else {
                System.err.println("} END OF CONTENT");
            }
            readingChunks = false;
        } else {
            System.err.println(chunk.content().toString(CharsetUtil.UTF_8));
        }
    }
}

From source file:com.cmz.http.snoop.HttpSnoopClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();/*from  w w  w .  j  a v a 2 s  .  c o  m*/

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
            System.err.println();
        }

        if (HttpUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            ctx.close();
        }
    }
}

From source file:com.dwarf.netty.guide.http.snoop.HttpSnoopClientHandler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();//from  w  ww  . ja  v  a2s. c  o m

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
            System.err.println();
        }

        if (HttpHeaderUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            ctx.close();
        }
    }
}

From source file:com.flysoloing.learning.network.netty.spdy.client.HttpResponseClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.out.println("STATUS: " + response.status());
        System.out.println("VERSION: " + response.protocolVersion());
        System.out.println();//  w  ww. j  a  v  a2s.co m

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.out.println("HEADER: " + name + " = " + value);
                }
            }
            System.out.println();
        }

        if (HttpUtil.isTransferEncodingChunked(response)) {
            System.out.println("CHUNKED CONTENT {");
        } else {
            System.out.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.out.print(content.content().toString(CharsetUtil.UTF_8));
        System.out.flush();

        if (content instanceof LastHttpContent) {
            System.out.println("} END OF CONTENT");
            queue.add(ctx.channel().newSucceededFuture());
        }
    }
}

From source file:com.github.picto.network.exemple.HttpSnoopClientHandler.java

License:Apache License

public void messageReceived(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();// w  w w. j  a v  a 2s . c o  m

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
            System.err.println();
        }

        if (HttpHeaderUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            ctx.close();
            shouldEnd.set(true);
        }
    }
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpBlobStore.java

License:Open Source License

@SuppressWarnings("FutureReturnValueIgnored")
private boolean get(String key, final OutputStream out, boolean casDownload)
        throws IOException, InterruptedException {
    final AtomicBoolean dataWritten = new AtomicBoolean();

    OutputStream wrappedOut = new OutputStream() {
        // OutputStream.close() does nothing, which is what we want to ensure that the
        // OutputStream can't be closed somewhere in the Netty pipeline, so that we can support
        // retries. The OutputStream is closed in the finally block below.

        @Override/* w  w  w  .ja v  a 2s .  c om*/
        public void write(byte[] b, int offset, int length) throws IOException {
            dataWritten.set(true);
            out.write(b, offset, length);
        }

        @Override
        public void write(int b) throws IOException {
            dataWritten.set(true);
            out.write(b);
        }

        @Override
        public void flush() throws IOException {
            out.flush();
        }
    };
    DownloadCommand download = new DownloadCommand(uri, casDownload, key, wrappedOut);

    Channel ch = null;
    try {
        ch = acquireDownloadChannel();
        ChannelFuture downloadFuture = ch.writeAndFlush(download);
        downloadFuture.sync();
        return true;
    } catch (Exception e) {
        // e can be of type HttpException, because Netty uses Unsafe.throwException to re-throw a
        // checked exception that hasn't been declared in the method signature.
        if (e instanceof HttpException) {
            HttpResponse response = ((HttpException) e).response();
            if (!dataWritten.get() && authTokenExpired(response)) {
                // The error is due to an auth token having expired. Let's try again.
                refreshCredentials();
                return getAfterCredentialRefresh(download);
            }
            if (cacheMiss(response.status())) {
                return false;
            }
        }
        throw e;
    } finally {
        if (ch != null) {
            downloadChannels.release(ch);
        }
    }
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpBlobStore.java

License:Open Source License

@SuppressWarnings("FutureReturnValueIgnored")
private boolean getAfterCredentialRefresh(DownloadCommand cmd) throws InterruptedException {
    Channel ch = null;/*from w ww  .  ja  v a2s. c o m*/
    try {
        ch = acquireDownloadChannel();
        ChannelFuture downloadFuture = ch.writeAndFlush(cmd);
        downloadFuture.sync();
        return true;
    } catch (Exception e) {
        if (e instanceof HttpException) {
            HttpResponse response = ((HttpException) e).response();
            if (cacheMiss(response.status())) {
                return false;
            }
        }
        throw e;
    } finally {
        if (ch != null) {
            downloadChannels.release(ch);
        }
    }
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpBlobStore.java

License:Open Source License

/** See https://tools.ietf.org/html/rfc6750#section-3.1 */
private boolean authTokenExpired(HttpResponse response) {
    synchronized (credentialsLock) {
        if (creds == null) {
            return false;
        }/*from w  w w . j ava2s.c o m*/
    }
    List<String> values = response.headers().getAllAsString(HttpHeaderNames.WWW_AUTHENTICATE);
    String value = String.join(",", values);
    if (value != null && value.startsWith("Bearer")) {
        return INVALID_TOKEN_ERROR.matcher(value).find();
    } else {
        return response.status().equals(HttpResponseStatus.UNAUTHORIZED);
    }
}

From source file:com.linecorp.armeria.client.http.Http1ResponseDecoder.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof HttpObject)) {
        ctx.fireChannelRead(msg);//from  w  w w  .  j a va 2 s. com
        return;
    }

    try {
        switch (state) {
        case NEED_HEADERS:
            if (msg instanceof HttpResponse) {
                final HttpResponse nettyRes = (HttpResponse) msg;
                final DecoderResult decoderResult = nettyRes.decoderResult();
                if (!decoderResult.isSuccess()) {
                    fail(ctx, new ProtocolViolationException(decoderResult.cause()));
                    return;
                }

                if (!HttpUtil.isKeepAlive(nettyRes)) {
                    disconnectWhenFinished();
                }

                final HttpResponseWrapper res = getResponse(resId);
                assert res != null;
                this.res = res;

                if (nettyRes.status().codeClass() == HttpStatusClass.INFORMATIONAL) {
                    state = State.NEED_INFORMATIONAL_DATA;
                } else {
                    state = State.NEED_DATA_OR_TRAILING_HEADERS;
                    res.scheduleTimeout(ctx);
                }

                res.write(ArmeriaHttpUtil.toArmeria(nettyRes));
            } else {
                failWithUnexpectedMessageType(ctx, msg);
            }
            break;
        case NEED_INFORMATIONAL_DATA:
            if (msg instanceof LastHttpContent) {
                state = State.NEED_HEADERS;
            } else {
                failWithUnexpectedMessageType(ctx, msg);
            }
            break;
        case NEED_DATA_OR_TRAILING_HEADERS:
            if (msg instanceof HttpContent) {
                final HttpContent content = (HttpContent) msg;
                final DecoderResult decoderResult = content.decoderResult();
                if (!decoderResult.isSuccess()) {
                    fail(ctx, new ProtocolViolationException(decoderResult.cause()));
                    return;
                }

                final ByteBuf data = content.content();
                final int dataLength = data.readableBytes();
                if (dataLength > 0) {
                    final long maxContentLength = res.maxContentLength();
                    if (maxContentLength > 0 && res.writtenBytes() > maxContentLength - dataLength) {
                        fail(ctx, ContentTooLargeException.get());
                        return;
                    } else {
                        res.write(HttpData.of(data));
                    }
                }

                if (msg instanceof LastHttpContent) {
                    final HttpResponseWriter res = removeResponse(resId++);
                    assert this.res == res;
                    this.res = null;

                    state = State.NEED_HEADERS;

                    final HttpHeaders trailingHeaders = ((LastHttpContent) msg).trailingHeaders();
                    if (!trailingHeaders.isEmpty()) {
                        res.write(ArmeriaHttpUtil.toArmeria(trailingHeaders));
                    }

                    res.close();

                    if (needsToDisconnect()) {
                        ctx.close();
                    }
                }
            } else {
                failWithUnexpectedMessageType(ctx, msg);
            }
            break;
        case DISCARD:
            break;
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}