Example usage for io.netty.buffer ByteBufUtil writeAscii

List of usage examples for io.netty.buffer ByteBufUtil writeAscii

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufUtil writeAscii.

Prototype

public static int writeAscii(ByteBuf buf, CharSequence seq) 

Source Link

Document

Encode a CharSequence in <a href="http://en.wikipedia.org/wiki/ASCII">ASCII</a> and write it to a ByteBuf .

Usage

From source file:io.gatling.http.client.body.form.FormUrlEncodedRequestBody.java

License:Apache License

@Override
public WritableContent build(boolean zeroCopy, ByteBufAllocator alloc) {

    StringBuilder sb = encode();/*  www.j  av a 2 s. com*/

    ByteBuf bb = ByteBufUtil.writeAscii(alloc, sb);
    return new WritableContent(bb, bb.readableBytes());
}

From source file:io.netty.example.http2.helloworld.server.HelloWorldHttp1Handler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
    if (HttpUtil.is100ContinueExpected(req)) {
        ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE, Unpooled.EMPTY_BUFFER));
    }/*w ww  .  j  a v a  2  s . c om*/
    boolean keepAlive = HttpUtil.isKeepAlive(req);

    ByteBuf content = ctx.alloc().buffer();
    content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());
    ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")");

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

    if (keepAlive) {
        if (req.protocolVersion().equals(HTTP_1_0)) {
            response.headers().set(CONNECTION, KEEP_ALIVE);
        }
        ctx.write(response);
    } else {
        // Tell the client we're going to close the connection.
        response.headers().set(CONNECTION, CLOSE);
        ctx.write(response).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:me.netty.http.handler.HelloWorldHttp2Handler.java

License:Apache License

/**
 * If receive a frame with end-of-stream set, send a pre-canned response.
 *//*from  www  .ja v  a2 s. co m*/
public void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) throws Exception {
    if (headers.isEndStream()) {
        ByteBuf content = ctx.alloc().buffer();
        content.writeBytes(RESPONSE_BYTES.duplicate());
        ByteBufUtil.writeAscii(content, " - via HTTP/2");
        sendResponse(ctx, content);
    }

    logger.debug("get header stream id is : " + headers.streamId());
}

From source file:netty5.http.server.HelloWorldHttp1Handler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
    if (HttpHeaderUtil.is100ContinueExpected(req)) {
        ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
    }//from  w ww.  j  a v  a 2 s.  c  o m
    boolean keepAlive = HttpHeaderUtil.isKeepAlive(req);

    ByteBuf content = ctx.alloc().buffer();
    content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());
    ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")");

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

    if (!keepAlive) {
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        ctx.writeAndFlush(response);
    }
}

From source file:org.curioswitch.curiostack.gcloud.core.auth.ServiceAccountAccessTokenProvider.java

License:Open Source License

@Override
ByteBuf refreshRequestContent(Type type) {
    long currentTimeMillis = clock().millis();
    String assertion = createAssertion(type, currentTimeMillis);
    QueryStringEncoder formEncoder = new QueryStringEncoder("");
    formEncoder.addParam("grant_type", GRANT_TYPE);
    formEncoder.addParam("assertion", assertion);
    String contentWithQuestionMark = formEncoder.toString();

    ByteBufAllocator alloc = RequestContext.mapCurrent(RequestContext::alloc,
            () -> PooledByteBufAllocator.DEFAULT);
    assert alloc != null;
    ByteBuf content = alloc.buffer(contentWithQuestionMark.length() - 1);
    ByteBufUtil.writeAscii(content, contentWithQuestionMark.substring(1));
    return content;
}

From source file:org.curioswitch.curiostack.gcloud.core.auth.UserCredentialsAccessTokenProvider.java

License:Open Source License

private static ByteBuf createRefreshRequestContent(UserCredentials credentials) {
    QueryStringEncoder formEncoder = new QueryStringEncoder("");
    formEncoder.addParam("client_id", credentials.getClientId());
    formEncoder.addParam("client_secret", credentials.getClientSecret());
    formEncoder.addParam("refresh_token", credentials.getRefreshToken());
    formEncoder.addParam("grant_type", GRANT_TYPE);
    String contentWithQuestionMark = formEncoder.toString();

    ByteBuf content = Unpooled.buffer(contentWithQuestionMark.length() - 1);
    ByteBufUtil.writeAscii(content, contentWithQuestionMark.substring(1));
    return content;
}