Example usage for io.netty.buffer Unpooled unreleasableBuffer

List of usage examples for io.netty.buffer Unpooled unreleasableBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled unreleasableBuffer.

Prototype

public static ByteBuf unreleasableBuffer(ByteBuf buf) 

Source Link

Document

Return a unreleasable view on the given ByteBuf which will just ignore release and retain calls.

Usage

From source file:com.nettyhttpserver.server.util.HttpRequestHandler.java

public static void handle(ChannelHandlerContext ctx, HttpRequest req, DefaultChannelGroup channels) {
    allChannels = channels;// ww w. j  av a 2  s .  c om
    FullHttpResponse response = null;
    boolean keepAlive = isKeepAlive(req);
    String requestUri = req.getUri().toLowerCase();

    /*
     * Write data about request to database
     */
    ServerRequestDTO requestServiceDTO = new ServerRequestDTO();
    requestServiceDTO.setIP(((InetSocketAddress) ctx.channel().remoteAddress()).getHostString());
    requestServiceDTO.setLastTimestamp(new Timestamp(System.currentTimeMillis()).toString());
    serverRequestService.setServerRequest(requestServiceDTO);

    if (is100ContinueExpected(req)) {
        ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
    }

    /*
     * If request /hello
     */
    if (Requests.HELLO.toString().equalsIgnoreCase(requestUri)) {
        content = Unpooled
                .unreleasableBuffer(Unpooled.copiedBuffer("<h1>Hello World!</h1>", CharsetUtil.UTF_8));
        response = new DefaultFullHttpResponse(HTTP_1_1, OK, content.duplicate());
        /*
         * Create timer for /hello page.
         */
        timer.newTimeout(new ResponseTimerTask(ctx, response, keepAlive), 10, TimeUnit.SECONDS);

        writeToResponse(ctx, response, keepAlive);
        return;
    }

    /*
     * if request uri is /status
     */
    if (Requests.STATUS.toString().equalsIgnoreCase(requestUri)) {
        content = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(generateStatus(), CharsetUtil.UTF_8));
        response = new DefaultFullHttpResponse(HTTP_1_1, OK, content.duplicate());
        writeToResponse(ctx, response, keepAlive);
        return;
    }
    /*
     * If redirect
     */
    if (requestUri.matches(Requests.REDIRECT.toString())) {
        QueryStringDecoder qsd = new QueryStringDecoder(requestUri);
        List<String> redirectUrl = qsd.parameters().get("url");
        response = new DefaultFullHttpResponse(HTTP_1_1, FOUND);
        response.headers().set(LOCATION, redirectUrl);
        /*
         * Redirect database routine
         * Write url to database 
         */
        RedirectRequestDTO requestRedirectDTO = new RedirectRequestDTO();
        requestRedirectDTO.setUrl(redirectUrl.get(0));
        redirectRequestService.setRedirectRequest(requestRedirectDTO);
    } else {
        /*
         * If request URI is not handled by server.
         */
        response = new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN);
    }
    writeToResponse(ctx, response, keepAlive);

}

From source file:com.whizzosoftware.hobson.api.plugin.channel.ChannelIdleDetectionConfig.java

License:Open Source License

/**
 * Constructor./* w w w.j ava 2s .c o  m*/
 *
 * @param maxIdleTime the maximum number of seconds a channel can be idle before a heartbeat is sent
 * @param heartbeatFrame the heartbeat frame to send when a channel is considered idle
 */
public ChannelIdleDetectionConfig(int maxIdleTime, String heartbeatFrame) {
    this.maxIdleTime = maxIdleTime;
    this.heartbeatFrame = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(heartbeatFrame, CharsetUtil.UTF_8));
}

From source file:io.advantageous.conekt.buffer.impl.BufferImpl.java

License:Open Source License

BufferImpl(int initialSizeHint) {
    buffer = Unpooled.unreleasableBuffer(Unpooled.buffer(initialSizeHint, Integer.MAX_VALUE));
}

From source file:io.advantageous.conekt.buffer.impl.BufferImpl.java

License:Open Source License

BufferImpl(byte[] bytes) {
    buffer = Unpooled.unreleasableBuffer(Unpooled.buffer(bytes.length, Integer.MAX_VALUE)).writeBytes(bytes);
}

From source file:io.advantageous.conekt.buffer.impl.BufferImpl.java

License:Open Source License

BufferImpl(ByteBuf buffer) {
    this.buffer = Unpooled.unreleasableBuffer(buffer);
}

From source file:io.advantageous.conekt.http.impl.ws.WebSocketFrameImpl.java

License:Open Source License

/**
 * Creates a new frame with the specified frame type and the specified data.
 *
 * @param type         the type of the frame. {@code 0} is the only allowed type currently.
 * @param binaryData   the content of the frame.  If <tt>(type &amp; 0x80 == 0)</tt>,
 *                     it must be encoded in UTF-8.
 * @param isFinalFrame If this is the final frame in a sequence
 * @throws IllegalArgumentException if If <tt>(type &amp; 0x80 == 0)</tt> and the data is not encoded
 *                                  in UTF-8
 *//*from  w  w  w  . j  a  v  a2  s .com*/
public WebSocketFrameImpl(FrameType type, ByteBuf binaryData, boolean isFinalFrame) {
    this.type = type;
    this.isFinalFrame = isFinalFrame;
    this.binaryData = Unpooled.unreleasableBuffer(binaryData);
}

From source file:io.cettia.asity.bridge.netty4.NettyServerHttpExchange.java

License:Apache License

@Override
protected void doWrite(ByteBuffer byteBuffer) {
    ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.wrappedBuffer(byteBuffer));
    if (!written) {
        written = true;/*from  w  ww  . ja va  2  s  .  com*/
        context.write(response);
    }
    context.writeAndFlush(buf);
}

From source file:io.jsync.buffer.Buffer.java

License:Open Source License

/**
 * Creates a new empty Buffer that is expected to have a size of {@code initialSizeHint} after data has been
 * written to it.<p>/*  ww w .j  a v  a 2s .  c o m*/
 * Please note that {@code length} of the Buffer immediately after creation will be zero.<p>
 * The {@code initialSizeHint} is merely a hint to the system for how much memory to initially allocate to the buffer to prevent excessive
 * automatic re-allocations as data is written to it.
 */
public Buffer(int initialSizeHint) {
    buffer = Unpooled.unreleasableBuffer(Unpooled.buffer(initialSizeHint, Integer.MAX_VALUE));
}

From source file:io.jsync.buffer.Buffer.java

License:Open Source License

/**
 * Create a new Buffer that contains the contents of a {@code byte[]}
 *///w w  w.j a  v  a2  s.c  om
public Buffer(byte[] bytes) {
    buffer = Unpooled.unreleasableBuffer(Unpooled.buffer(bytes.length, Integer.MAX_VALUE)).writeBytes(bytes);
}

From source file:io.jsync.buffer.Buffer.java

License:Open Source License

/**
 * Create a new Buffer from a Netty {@code ByteBuf} instance.
 * This method is meant for internal use only.
 *///from   w ww  . j a va2 s  .  c om
public Buffer(ByteBuf buffer) {
    this.buffer = Unpooled.unreleasableBuffer(buffer);
}