io.netty.handler.codec.http.FullHttpRequest # retain ( ) 源码实例Demo

下面列出了 io.netty.handler.codec.http.FullHttpRequest # retain ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: InChat   文件: HttpUtil.java

public static String checkType(FullHttpRequest msg){
    msg.retain();
    String url = msg.uri();
    System.out.println(url);
    HttpMethod method = msg.method();
    String meName = method.name();
    if (url.equals(HttpConstant.URI_GET_SIZE) && meName.equals(HttpConstant.GET)){
        return HttpConstant.GET_SIZE;
    }else if (url.equals(HttpConstant.URI_SEND_FROM_SERVER) && meName.equals(HttpConstant.POST)){
        return HttpConstant.SEND_FROM_SERVER;
    }else if (url.equals(HttpConstant.URI_GET_LIST) && meName.equals(HttpConstant.GET)){
        return HttpConstant.GET_LIST;
    }else if (url.equals(HttpConstant.URI_GET_STATE) && meName.equals(HttpConstant.POST)){
        return HttpConstant.GET_STATE;
    }else if (url.equals(HttpConstant.URI_SEND_IN_CHAT) && meName.equals(HttpConstant.POST)){
        return HttpConstant.SEND_IN_CHAT;
    }else {
        return HttpConstant.NOT_FIND_URI;
    }
}
 
源代码2 项目: InChat   文件: HttpUtil.java

public static SendServerVO getToken(FullHttpRequest msg) throws UnsupportedEncodingException {
    msg.retain();
    SendServerVO sendServerVO = new SendServerVO();
    String content = msg.content().toString(CharsetUtil.UTF_8);
    String[] stars = content.split("&");
    for (int i=0,len=stars.length;i<len;i++){
        String item = stars[i].toString();
        String firstType = item.substring(0,5);
        String value = item.substring(6,item.length());
        if (Constants.TOKEN.equals(firstType)){
            //Token
            sendServerVO.setToken(value);
        }else if(Constants.VALUE.endsWith(firstType)){
            //value
            sendServerVO.setValue(value);
        }
    }
    return sendServerVO;
}
 

@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
    msg.retain();
    out.add(msg);
    // If the session cookie exists, set its value on the ctx.
    final String sessionId = HttpRequestDecoder.getSessionId(msg);
    if (sessionId != null) {
        ctx.channel().attr(SubscriptionRegistry.SESSION_ID_ATTR).set(sessionId);
        LOG.trace("Found sessionId in WebSocket channel, setting sessionId {} on context", sessionId);
    }
    if (msg.headers() != null) {
        ctx.channel().attr(WebSocketRequestDecoder.HTTP_HEADERS_ATTR)
                .set(HttpHeaderUtils.toMultimap(msg.headers()));
    }

    X509Certificate clientCert = AuthenticationService.getClientCertificate(ctx);
    if (clientCert != null) {
        ctx.channel().attr(WebSocketRequestDecoder.CLIENT_CERT_ATTR).set(clientCert);
    }
}
 

@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
    msg.retain();
    out.add(msg);
    // If the session cookie exists, set its value on the ctx.
    final String sessionId = HttpRequestDecoder.getSessionId(msg, this.anonymousAccessAllowed);
    ctx.channel().attr(SESSION_ID_ATTR).set(sessionId);
    LOG.info("Found session id in WebSocket channel, setting sessionId {} on context", sessionId);
}
 

@Override
public void handleRequest(FullHttpRequest req, ChannelHandlerContext ctx) throws Exception {
   if (!authorizer.isAuthorized(ctx, req)) {
   	logger.warn("Authorization failed for request {}", req);
      if (!authorizer.handleFailedAuth(ctx, req))  {
      	logger.error("Failed to handle authorization failure for request {}", req);
         throw new HttpException(HttpResponseStatus.FORBIDDEN.code());
      }
      return;
   }

   logger.debug("Handling request for [{} {}]", req.getMethod(), req.getUri());

   /*
    * This call prevents an "io.netty.util.IllegalReferenceCountException: refCnt: 0" when we try to read the content
    * ByteBuf in the Executor thread.  Without it, the FullHttpRequest's content ByteBuf would get deallocated by
    * Netty's SimpleChannelInboundHandler.channelRead(), which calls ReferenceCountUtil.release(msg) in its finally
    * block.
    * 
    * Netty does manual reference-counting and de-allocating of certain significant resources like request bodies for
    * high performance: http://netty.io/wiki/reference-counted-objects.html
    * 
    * More info:
    * https://stackoverflow.com/questions/28647048/why-do-we-need-to-manually-handle-reference-counting-for-netty-bytebuf-if-jvm-gc
    */
   req.retain();

   executor.execute(() ->
   {
      try
      {
         doHandleRequest(req, ctx);
      }
      finally
      {
         /*
          * This call prevents a Netty ByteBuf pool leak.  Without it, the content ByteBuf would be GC'ed without the
          * Netty ByteBuf pool's knowledge, which would result in a Netty ByteBuf pool leak, and eventually a memory
          * leak as the Netty ByteBuf pool grows in size without realizing that some of its buffers have been GC'ed
          * and should no longer be in service.
          */
         req.release();
      }
   });
}
 

@Override
protected void encode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
    msg.retain();
    out.add(msg);
}
 

@Override
protected void encode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
    msg.retain();
    out.add(msg);
}
 
源代码8 项目: happor   文件: HttpTransitHandler.java

@Override
protected void handle(FullHttpRequest request, FullHttpResponse response) {
	// TODO Auto-generated method stub
	transRequest = request.retain();
	incoming(transRequest);
}