io.netty.handler.codec.http.ServerCookieEncoder Java Examples

The following examples show how to use io.netty.handler.codec.http.ServerCookieEncoder. 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: TrackingPixelRouteHandler.java    From Sparkngin with GNU Affero General Public License v3.0 6 votes vote down vote up
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()));
  }
}
 
Example #2
Source File: HttpSnoopServerHandler.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #3
Source File: HttpSnoopServerHandler.java    From netty.book.kor with MIT License 5 votes vote down vote up
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;
}
 
Example #4
Source File: HttpUploadServerHandler.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
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);
    }
}
 
Example #5
Source File: SampleHandler.java    From xio with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #6
Source File: SampleHandler.java    From xio with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #7
Source File: ResponseBuilder.java    From ob1k with Apache License 2.0 4 votes vote down vote up
public ResponseBuilder bake() {
  addCookie(ServerCookieEncoder.encode(cookie));
  return ResponseBuilder.this;
}
 
Example #8
Source File: HttpResponseMessageImpl.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public void addSetCookie(Cookie cookie)
{
    getHeaders().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
 
Example #9
Source File: HttpResponseMessageImpl.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public void setSetCookie(Cookie cookie)
{
    getHeaders().set(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode(cookie));
}