Java Code Examples for io.netty.handler.codec.http.FullHttpRequest#retain()

The following examples show how to use io.netty.handler.codec.http.FullHttpRequest#retain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtil.java    From InChat with Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example 2
Source File: HttpUtil.java    From InChat with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: WebSocketFullRequestHandler.java    From timely with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: WebSocketHttpCookieHandler.java    From qonduit with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: BaseAsyncHttpResource.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@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();
      }
   });
}
 
Example 6
Source File: WebSocketHttpCookieHandler.java    From qonduit with Apache License 2.0 4 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
    msg.retain();
    out.add(msg);
}
 
Example 7
Source File: WebSocketFullRequestHandler.java    From timely with Apache License 2.0 4 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
    msg.retain();
    out.add(msg);
}
 
Example 8
Source File: HttpTransitHandler.java    From happor with MIT License 4 votes vote down vote up
@Override
protected void handle(FullHttpRequest request, FullHttpResponse response) {
	// TODO Auto-generated method stub
	transRequest = request.retain();
	incoming(transRequest);
}