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

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

Introduction

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

Prototype

public DefaultHttp2HeadersFrame(Http2Headers headers) 

Source Link

Document

Equivalent to new DefaultHttp2HeadersFrame(headers, false) .

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.
 *//*w w  w .  jav a 2 s  . com*/
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.
 *//*from ww w  .  j a  v a 2s. 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.  j ava2  s .  com
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.
 *//*from   w  w  w  . j a  v a2  s  .  co 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());
    }/*from  w ww  . j ava2 s .co 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));
    }//from   ww  w . ja  v a 2s .co m
}

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

License:Open Source License

public void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) throws Exception {
    if (headers.isEndStream() && (headers.headers().method().toString()).equalsIgnoreCase("GET")) {
        if (headers.headers().contains("http2-settings")) {
            return;
        }/*from  www . j av  a2 s  . c o  m*/
        Http2Headers headers1 = new DefaultHttp2Headers().status(OK.codeAsText());
        ctx.writeAndFlush(new DefaultHttp2HeadersFrame(headers1));
    }
}