Example usage for io.netty.handler.codec.http2 DefaultHttp2DataFrame DefaultHttp2DataFrame

List of usage examples for io.netty.handler.codec.http2 DefaultHttp2DataFrame DefaultHttp2DataFrame

Introduction

In this page you can find the example usage for io.netty.handler.codec.http2 DefaultHttp2DataFrame DefaultHttp2DataFrame.

Prototype

public DefaultHttp2DataFrame(ByteBuf content, boolean endStream) 

Source Link

Document

Equivalent to new DefaultHttp2DataFrame(content, endStream, 0) .

Usage

From source file:com.flysoloing.learning.network.netty.http2.helloworld.multiplex.server.HelloWorldHttp2Handler.java

License:Apache License

/**
 * Sends a "Hello World" DATA frame to the client.
 *//*from   w ww .j  a va  2s.  c  o  m*/
private static void sendResponse(ChannelHandlerContext ctx, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    ctx.write(new DefaultHttp2HeadersFrame(headers));
    ctx.writeAndFlush(new DefaultHttp2DataFrame(payload, true));
}

From source file:com.hop.hhxx.example.http2.helloworld.multiplex.server.HelloWorldHttp2Handler.java

License:Apache License

/**
 * Sends a "Hello World" DATA frame to the client.
 *///  w  w  w.  ja v  a2 s.  c  o m
private void sendResponse(ChannelHandlerContext ctx, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    ctx.write(new DefaultHttp2HeadersFrame(headers));
    ctx.writeAndFlush(new DefaultHttp2DataFrame(payload, true));
}

From source file:example.http2.helloworld.frame.server.HelloWorldHttp2Handler.java

License:Apache License

/**
 * Sends a "Hello World" DATA frame to the client.
 *///from   w w w . ja  v a  2 s  . c o  m
private static void sendResponse(ChannelHandlerContext ctx, Http2FrameStream stream, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    ctx.write(new DefaultHttp2HeadersFrame(headers).stream(stream));
    ctx.write(new DefaultHttp2DataFrame(payload, true).stream(stream));
}

From source file:example.http2.helloworld.multiplex.server.HelloWorldHttp2Handler.java

License:Apache License

/**
 * Sends a "Hello World" DATA frame to the client.
 */// w  w  w .j av  a2s .  c o  m
private static void sendResponse(ChannelHandlerContext ctx, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    ctx.write(new DefaultHttp2HeadersFrame(headers));
    ctx.write(new DefaultHttp2DataFrame(payload, true));
}

From source file:org.glassfish.jersey.netty.httpserver.NettyHttp2ResponseWriter.java

License:Open Source License

@Override
public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext)
        throws ContainerException {

    String reasonPhrase = responseContext.getStatusInfo().getReasonPhrase();
    int statusCode = responseContext.getStatus();

    HttpResponseStatus status = reasonPhrase == null ? HttpResponseStatus.valueOf(statusCode)
            : new HttpResponseStatus(statusCode, reasonPhrase);

    DefaultHttp2Headers response = new DefaultHttp2Headers();
    response.status(Integer.toString(responseContext.getStatus()));

    for (final Map.Entry<String, List<String>> e : responseContext.getStringHeaders().entrySet()) {
        response.add(e.getKey().toLowerCase(), e.getValue());
    }//ww w  .j ava2  s  .c o  m

    response.set(HttpHeaderNames.CONTENT_LENGTH, Long.toString(contentLength));

    ctx.writeAndFlush(new DefaultHttp2HeadersFrame(response));

    if (!headersFrame.headers().method().equals(HttpMethod.HEAD.asciiName())
            && (contentLength > 0 || contentLength == -1)) {

        return new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                write(new byte[] { (byte) b });
            }

            @Override
            public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {

                ByteBuf buffer = ctx.alloc().buffer(len);
                buffer.writeBytes(b, off, len);

                ctx.writeAndFlush(new DefaultHttp2DataFrame(buffer, false));
            }

            @Override
            public void flush() throws IOException {
                ctx.flush();
            }

            @Override
            public void close() throws IOException {
                ctx.write(new DefaultHttp2DataFrame(true)).addListener(NettyResponseWriter.FLUSH_FUTURE);
            }
        };

    } else {
        ctx.writeAndFlush(new DefaultHttp2DataFrame(true));
        return null;
    }
}

From source file:org.wso2.esb.integration.common.utils.servers.http2.Http2Handler.java

License:Open Source License

public void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception {
    if (data.isEndStream()) {
        ByteBuf content = ctx.alloc().buffer();
        content.writeBytes(DATA_RESPONSE.duplicate());
        Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
        headers.add(HttpHeaderNames.CONTENT_TYPE, "text/xml");
        ctx.write(new DefaultHttp2HeadersFrame(headers));
        ctx.writeAndFlush(new DefaultHttp2DataFrame(content, true));
    }// w  ww.j a v  a2  s .  co m
}