Java 类io.netty.handler.codec.http.ServerCookieEncoder 实例源码

项目:ponycar    文件:Response.java   
/**
 * 转换为Netty所用Response
 * 
 * @return FullHttpResponse
 */
public FullHttpResponse toFullHttpResponse() {
    final FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(httpVersion, status, content);

    // headers
    final HttpHeaders httpHeaders = fullHttpResponse.headers().add(headers);
    httpHeaders.set(Names.CONTENT_TYPE, contentType + "; charset=" + charset);
    httpHeaders.set(Names.CONTENT_ENCODING, charset);
    httpHeaders.set(Names.CONTENT_LENGTH, content.readableBytes());

    // Cookies
    for (Cookie cookie : cookies) {
        httpHeaders.add(Names.SET_COOKIE, ServerCookieEncoder.encode(cookie));
    }

    return fullHttpResponse;
}
项目:jetstream    文件:DefaultHttpServletResponse.java   
@Override
public void addCookie(Cookie arg0) {
    String cookieString = m_nettyhttpresp.headers().get(HttpHeaders.Names.COOKIE);
    Set<io.netty.handler.codec.http.Cookie> cookies;
    if (cookieString != null) {
        cookies = CookieDecoder.decode(cookieString);
        cookies.add(new io.netty.handler.codec.http.DefaultCookie(arg0.getName(), arg0.getValue()));
    } else {
        cookies = new HashSet<io.netty.handler.codec.http.Cookie>();
        cookies.add(new io.netty.handler.codec.http.DefaultCookie(arg0.getName(), arg0.getValue()));
    }

    if (!cookies.isEmpty()) {
        // Reset the cookies if necessary.
        HttpHeaders.setHeader(m_nettyhttpresp, HttpHeaders.Names.SET_COOKIE, ServerCookieEncoder.encode(cookies));
    }
}
项目:web-utils    文件:CookieHelper.java   
public void setSigned(String name, String value, long timeout, String path, HttpServerRequest request) {
    Cookie cookie = new DefaultCookie(name, value);
    cookie.setMaxAge(timeout);
    cookie.setSecure("https".equals(Renders.getScheme(request)));
    cookie.setHttpOnly(true);
    if (path != null && !path.trim().isEmpty()) {
        cookie.setPath(path);
    }
    if (signKey != null) {
        try {
            signCookie(cookie);
        } catch (InvalidKeyException | NoSuchAlgorithmException
                | IllegalStateException | UnsupportedEncodingException e) {
            log.error(e);
            return;
        }
        request.response().headers().add("Set-Cookie", ServerCookieEncoder.encode(cookie));
    }
}
项目:Sparkngin    文件:TrackingPixelRouteHandler.java   
void setCookie(HttpRequest request, FullHttpResponse response) {
  // Encode the cookie.
  String cookieString = request.headers().get(COOKIE);
  if (cookieString != null) {
    Set<Cookie> cookies = CookieDecoder.decode(cookieString);
    if (!cookies.isEmpty()) {
      // Reset the cookies if necessary.
      for (Cookie cookie: cookies) {
        if("visit-count".equals(cookie.getName())) {
          int count = Integer.parseInt(cookie.getValue()) ;
          cookie.setValue(Integer.toString(count + 1));
          //System.out.println("Visit: " + count);
        }
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
      }
    }
  } else {
    // Browser sent no cookie.  Add some.
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("id", UUID.randomUUID().toString()));
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("visit-count", "1"));
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("first-visit-time", new Date().toString()));
  }
}
项目:mqttserver    文件:HttpJsonpTransport.java   
private void handleConnect(ChannelHandlerContext ctx, HttpRequest req) {
    boolean isConnected = false;
    String sessionId = HttpSessionStore.getClientSessionId(req);
    HttpChannelEntity httpChannelEntity = null;
    if (!HttpSessionStore.checkJSessionId(sessionId)) {
        sessionId = HttpSessionStore.genJSessionId();

        httpChannelEntity = new HttpJsonpChannelEntity(sessionId, true);
        MemoryMetaPool.registerClienId(sessionId, httpChannelEntity);
    } else {
        isConnected = true;
        httpChannelEntity = (HttpChannelEntity) MemoryMetaPool
                .getChannelEntryByClientId(sessionId);
    }

    Map<String, Object> map = new HashMap<String, Object>(2);
    map.put("status", true);
    map.put("clientId", sessionId);
    String result = gson.toJson(map);
    ByteBuf content = ctx.alloc().directBuffer()
            .writeBytes(result.getBytes());

    FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK,
            content);
    if (isConnected) {
        res.headers().add("Set-Cookie",
                ServerCookieEncoder.encode("JSESSIONID", sessionId));
    }
    res.headers().set(CONTENT_TYPE, HEADER_CONTENT_TYPE);
    setContentLength(res, content.readableBytes());

    ScheduledFuture<?> scheduleTask = ctx.executor().schedule(
            new SessionTimeoutTask(sessionId), 60, TimeUnit.SECONDS);
    httpChannelEntity.setScheduleTask(scheduleTask);

    sendHttpResponse(ctx, req, res);
}
项目:netty4.0.27Learn    文件:HttpSnoopServerHandler.java   
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = HttpHeaders.isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(
            HTTP_1_1, currentObj.getDecoderResult().isSuccess()? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }

    // Encode the cookie.
    String cookieString = request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = CookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie: cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}
项目:netty.book.kor    文件:HttpSnoopServerHandler.java   
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = HttpHeaders.isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(
            HTTP_1_1, currentObj.decoderResult().isSuccess()? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }

    // Encode the cookie.
    String cookieString = request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = CookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie: cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}
项目:netty4study    文件:HttpSnoopServerHandler.java   
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(
            HTTP_1_1, currentObj.getDecoderResult().isSuccess()? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }

    // Encode the cookie.
    String cookieString = request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = CookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie: cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}
项目:detective    文件:HttpServerTask.java   
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
  // Decide whether to close the connection or not.
  boolean keepAlive = isKeepAlive(request);
  // Build the response object.
  FullHttpResponse response =
      new DefaultFullHttpResponse(HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK
          : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

  response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

  if (keepAlive) {
    // Add 'Content-Length' header only for a keep-alive connection.
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    // Add keep alive header as per:
    // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
    response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
  }

  // Encode the cookie.
  String cookieString = request.headers().get(COOKIE);
  if (cookieString != null) {
    Set<Cookie> cookies = CookieDecoder.decode(cookieString);
    if (!cookies.isEmpty()) {
      // Reset the cookies if necessary.
      for (Cookie cookie : cookies) {
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
      }
    }
  } else {
    // Browser sent no cookie. Add some.
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
  }

  // Write the response.
  ctx.write(response);

  return keepAlive;
}
项目:xio    文件:SampleHandler.java   
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
  // Decide whether to close the connection or not.
  boolean keepAlive = HttpHeaders.isKeepAlive(request);
  // Build the response object.
  FullHttpResponse response = new DefaultFullHttpResponse(
      HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
      Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

  response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

  if (keepAlive) {
    // Add 'Content-Length' header only for a keep-alive connection.
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    // Add keep alive header as per:
    // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
    response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
  }

  // Encode the cookie.
  String cookieString = request.headers().get(COOKIE);
  if (cookieString != null) {
    Set<Cookie> cookies = CookieDecoder.decode(cookieString);
    if (!cookies.isEmpty()) {
      // Reset the cookies if necessary.
      for (Cookie cookie : cookies) {
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
      }
    }
  } else {
    // Browser sent no cookie.  Add some.
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
  }

  // Write the response.
  ctx.write(response);

  return keepAlive;
}
项目:web-utils    文件:CookieHelper.java   
public static void set(String name, String value, long timeout, String path, HttpServerRequest request) {
    Cookie cookie = new DefaultCookie(name, value);
    cookie.setMaxAge(timeout);
    cookie.setSecure("https".equals(Renders.getScheme(request)));
    if (path != null && !path.trim().isEmpty()) {
        cookie.setPath(path);
    }
    request.response().headers().add("Set-Cookie", ServerCookieEncoder.encode(cookie));
}
项目:netty-netty-5.0.0.Alpha1    文件:HttpSnoopServerHandler.java   
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(
            HTTP_1_1, currentObj.getDecoderResult().isSuccess()? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }

    // Encode the cookie.
    String cookieString = request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = CookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie: cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}
项目:netty4.0.27Learn    文件:HttpUploadServerHandler.java   
private void writeResponse(Channel channel) {
    // Convert the response content to a ChannelBuffer.
    ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8);
    responseContent.setLength(0);

    // Decide whether to close the connection or not.
    boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION))
            || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)
            && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION));

    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (!close) {
        // There's no need to add 'Content-Length' header
        // if this is the last response.
        response.headers().set(CONTENT_LENGTH, buf.readableBytes());
    }

    Set<Cookie> cookies;
    String value = request.headers().get(COOKIE);
    if (value == null) {
        cookies = Collections.emptySet();
    } else {
        cookies = CookieDecoder.decode(value);
    }
    if (!cookies.isEmpty()) {
        // Reset the cookies if necessary.
        for (Cookie cookie : cookies) {
            response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
        }
    }
    // Write the response.
    ChannelFuture future = channel.writeAndFlush(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
项目:netty4study    文件:HttpUploadServerHandler.java   
private void writeResponse(Channel channel) {
    // Convert the response content to a ChannelBuffer.
    ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8);
    responseContent.setLength(0);

    // Decide whether to close the connection or not.
    boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION))
            || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)
            && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION));

    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (!close) {
        // There's no need to add 'Content-Length' header
        // if this is the last response.
        response.headers().set(CONTENT_LENGTH, buf.readableBytes());
    }

    Set<Cookie> cookies;
    String value = request.headers().get(COOKIE);
    if (value == null) {
        cookies = Collections.emptySet();
    } else {
        cookies = CookieDecoder.decode(value);
    }
    if (!cookies.isEmpty()) {
        // Reset the cookies if necessary.
        for (Cookie cookie : cookies) {
            response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
        }
    }
    // Write the response.
    ChannelFuture future = channel.writeAndFlush(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
项目:xio    文件:SampleHandler.java   
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
  // Decide whether to close the connection or not.
  boolean keepAlive = HttpHeaders.isKeepAlive(request);
  // Build the response object.
  FullHttpResponse response =
      new DefaultFullHttpResponse(
          HTTP_1_1,
          currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
          Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

  response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

  if (keepAlive) {
    // Add 'Content-Length' header only for a keep-alive connection.
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    // Add keep alive header as per:
    // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
    response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
  }

  // Encode the cookie.
  String cookieString = request.headers().get(COOKIE);
  if (cookieString != null) {
    Set<Cookie> cookies = CookieDecoder.decode(cookieString);
    if (!cookies.isEmpty()) {
      // Reset the cookies if necessary.
      for (Cookie cookie : cookies) {
        response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
      }
    }
  } else {
    // Browser sent no cookie.  Add some.
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
    response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
  }

  // Write the response.
  ctx.write(response);

  return keepAlive;
}
项目:titanite    文件:Cookie.java   
String encode() {
    return ServerCookieEncoder.encode(c);
}
项目:ob1k    文件:ResponseBuilder.java   
public ResponseBuilder bake() {
  addCookie(ServerCookieEncoder.encode(cookie));
  return ResponseBuilder.this;
}
项目:netty-netty-5.0.0.Alpha1    文件:HttpUploadServerHandler.java   
private void writeResponse(Channel channel) {
    // Convert the response content to a ChannelBuffer.
    ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8);
    responseContent.setLength(0);

    // Decide whether to close the connection or not.
    boolean close = request.headers().contains(CONNECTION, HttpHeaders.Values.CLOSE, true)
            || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)
            && !request.headers().contains(CONNECTION, HttpHeaders.Values.KEEP_ALIVE, true);

    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (!close) {
        // There's no need to add 'Content-Length' header
        // if this is the last response.
        response.headers().set(CONTENT_LENGTH, buf.readableBytes());
    }

    Set<Cookie> cookies;
    String value = request.headers().get(COOKIE);
    if (value == null) {
        cookies = Collections.emptySet();
    } else {
        cookies = CookieDecoder.decode(value);
    }
    if (!cookies.isEmpty()) {
        // Reset the cookies if necessary.
        for (Cookie cookie : cookies) {
            response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
        }
    }
    // Write the response.
    ChannelFuture future = channel.writeAndFlush(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
项目:RxNetty    文件:HttpServerResponse.java   
public void addCookie(Cookie cookie) {
    headers.add(HttpHeaders.Names.SET_COOKIE, ServerCookieEncoder.encode(cookie));
}