io.netty.handler.codec.http.cookie.Cookie类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(163)

本文整理了Java中io.netty.handler.codec.http.cookie.Cookie类的一些代码示例,展示了Cookie类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cookie类的具体详情如下:
包路径:io.netty.handler.codec.http.cookie.Cookie
类名称:Cookie

Cookie介绍

[英]An interface defining an HTTP cookie.
[中]定义HTTP cookie的接口。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public MultiValueMap<String, ResponseCookie> getCookies() {
  MultiValueMap<String, ResponseCookie> result = new LinkedMultiValueMap<>();
  this.response.cookies().values().stream().flatMap(Collection::stream)
      .forEach(cookie ->
        result.add(cookie.name(), ResponseCookie.from(cookie.name(), cookie.value())
            .domain(cookie.domain())
            .path(cookie.path())
            .maxAge(cookie.maxAge())
            .secure(cookie.isSecure())
            .httpOnly(cookie.isHttpOnly())
            .build()));
  return CollectionUtils.unmodifiableMultiValueMap(result);
}

代码示例来源:origin: lets-blade/blade

@Override
public Response cookie(@NonNull com.blade.mvc.http.Cookie cookie) {
  Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(cookie.name(), cookie.value());
  if (cookie.domain() != null) {
    nettyCookie.setDomain(cookie.domain());
  }
  if (cookie.maxAge() > 0) {
    nettyCookie.setMaxAge(cookie.maxAge());
  }
  nettyCookie.setPath(cookie.path());
  nettyCookie.setHttpOnly(cookie.httpOnly());
  nettyCookie.setSecure(cookie.secure());
  this.cookies.add(nettyCookie);
  return this;
}

代码示例来源:origin: lets-blade/blade

@Override
public Response cookie(@NonNull String name, @NonNull String value, int maxAge, boolean secured) {
  Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value);
  nettyCookie.setPath("/");
  nettyCookie.setMaxAge(maxAge);
  nettyCookie.setSecure(secured);
  this.cookies.add(nettyCookie);
  return this;
}

代码示例来源:origin: lets-blade/blade

@Override
public Map<String, String> cookies() {
  Map<String, String> map = new HashMap<>(8);
  this.cookies.forEach(cookie -> map.put(cookie.name(), cookie.value()));
  return map;
}

代码示例来源:origin: lets-blade/blade

@Override
public Response cookie(@NonNull String name, @NonNull String value, int maxAge) {
  Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value);
  nettyCookie.setPath("/");
  nettyCookie.setMaxAge(maxAge);
  this.cookies.add(nettyCookie);
  return this;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Batch encodes cookies into Set-Cookie header values.
 *
 * @param cookies a bunch of cookies
 * @return the corresponding bunch of Set-Cookie headers
 */
public List<String> encode(Cookie... cookies) {
  if (checkNotNull(cookies, "cookies").length == 0) {
    return Collections.emptyList();
  }
  List<String> encoded = new ArrayList<String>(cookies.length);
  Map<String, Integer> nameToIndex = strict && cookies.length > 1 ? new HashMap<String, Integer>() : null;
  boolean hasDupdName = false;
  for (int i = 0; i < cookies.length; i++) {
    Cookie c = cookies[i];
    encoded.add(encode(c));
    if (nameToIndex != null) {
      hasDupdName |= nameToIndex.put(c.name(), i) != null;
    }
  }
  return hasDupdName ? dedup(encoded, nameToIndex) : encoded;
}

代码示例来源:origin: jooby-project/jooby

private org.jooby.Cookie cookie(final Cookie c) {
 org.jooby.Cookie.Definition cookie = new org.jooby.Cookie.Definition(c.name(), c.value());
 Optional.ofNullable(c.domain()).ifPresent(cookie::domain);
 Optional.ofNullable(c.path()).ifPresent(cookie::path);
 return cookie.toCookie();
}

代码示例来源:origin: AsyncHttpClient/async-http-client

private void returnMultipleCookiesEvenIfTheyHaveSameName() {
 CookieStore store = new ThreadSafeCookieStore();
 store.add(Uri.create("http://foo.com"), ClientCookieDecoder.LAX.decode("JSESSIONID=FOO; Domain=.foo.com"));
 store.add(Uri.create("http://sub.foo.com"), ClientCookieDecoder.LAX.decode("JSESSIONID=BAR; Domain=sub.foo.com"));
 Uri uri1 = Uri.create("http://sub.foo.com");
 List<Cookie> cookies1 = store.get(uri1);
 assertTrue(cookies1.size() == 2);
 assertTrue(cookies1.stream().filter(c -> c.value().equals("FOO") || c.value().equals("BAR")).count() == 2);
 String result = ClientCookieEncoder.LAX.encode(cookies1.get(0), cookies1.get(1));
 assertTrue(result.equals("JSESSIONID=FOO; JSESSIONID=BAR"));
}

代码示例来源:origin: line/armeria

CookieBasedSsoHandler(String cookieName, String cookieValue) {
  requireNonNull(cookieName, "cookieName");
  requireNonNull(cookieValue, "cookieValue");
  final Cookie cookie = new DefaultCookie(cookieName, cookieValue);
  cookie.setDomain(spHostname);
  cookie.setPath("/");
  cookie.setHttpOnly(true);
  setCookie = ServerCookieEncoder.STRICT.encode(cookie);
}

代码示例来源:origin: jamesdbloom/mockserver

private boolean cookieHeaderAlreadyExists(HttpResponse response, Cookie cookieValue) {
  List<String> setCookieHeaders = response.getHeader(SET_COOKIE.toString());
  for (String setCookieHeader : setCookieHeaders) {
    String existingCookieName = ClientCookieDecoder.LAX.decode(setCookieHeader).name();
    String existingCookieValue = ClientCookieDecoder.LAX.decode(setCookieHeader).value();
    if (existingCookieName.equalsIgnoreCase(cookieValue.getName().getValue()) && existingCookieValue.equalsIgnoreCase(cookieValue.getValue().getValue())) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: AsyncHttpClient/async-http-client

private void add(String requestDomain, String requestPath, Cookie cookie) {
 AbstractMap.SimpleEntry<String, Boolean> pair = cookieDomain(cookie.domain(), requestDomain);
 String keyDomain = pair.getKey();
 boolean hostOnly = pair.getValue();
 String keyPath = cookiePath(cookie.path(), requestPath);
 CookieKey key = new CookieKey(cookie.name().toLowerCase(), keyDomain, keyPath);
 if (hasCookieExpired(cookie, 0))
  cookieJar.remove(key);
 else
  cookieJar.put(key, new StoredCookie(cookie, hostOnly, cookie.maxAge() != Cookie.UNDEFINED_MAX_AGE));
}

代码示例来源:origin: wildfly/wildfly

@Override
public int compareTo(Cookie c) {
  int v = name().compareTo(c.name());
  if (v != 0) {
    return v;
  }
  if (path() == null) {
    if (c.path() != null) {
      return -1;
    }
  } else if (c.path() == null) {
    return 1;
  } else {
    v = path().compareTo(c.path());
    if (v != 0) {
      return v;
    }
  }
  if (domain() == null) {
    if (c.domain() != null) {
      return -1;
    }
  } else if (c.domain() == null) {
    return 1;
  } else {
    v = domain().compareToIgnoreCase(c.domain());
    return v;
  }
  return 0;
}

代码示例来源:origin: AsyncHttpClient/async-http-client

private void shouldServeCookiesBasedOnTheUriScheme() {
 CookieStore store = new ThreadSafeCookieStore();
 store.add(Uri.create("https://foo.org/moodle/"), ClientCookieDecoder.LAX.decode("cookie1=VALUE1; Path=/"));
 store.add(Uri.create("https://foo.org:443/moodle/login"), ClientCookieDecoder.LAX.decode("cookie1=VALUE2; Path=/"));
 store.add(Uri.create("https://foo.org:443/moodle/login"), ClientCookieDecoder.LAX.decode("cookie1=VALUE3; Path=/; Secure"));
 Uri uri = Uri.create("https://foo.org/moodle/login");
 assertTrue(store.getAll().size() == 1);
 assertTrue(store.get(uri).get(0).value().equals("VALUE3"));
 assertTrue(store.get(uri).get(0).isSecure());
}

代码示例来源:origin: lets-blade/blade

@Override
public Response removeCookie(@NonNull String name) {
  Optional<Cookie> cookieOpt = this.cookies.stream().filter(cookie -> cookie.name().equals(name)).findFirst();
  cookieOpt.ifPresent(cookie -> {
    cookie.setValue("");
    cookie.setMaxAge(-1);
  });
  Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, "");
  nettyCookie.setMaxAge(-1);
  this.cookies.add(nettyCookie);
  return this;
}

代码示例来源:origin: vert-x3/vertx-web

static MultiMap removeCookieHeaders(MultiMap headers) {
  // We don't want to remove the JSESSION cookie.
  String cookieHeader = headers.get(COOKIE);
  if (cookieHeader != null) {
   headers.remove(COOKIE);
   Set<Cookie> nettyCookies = ServerCookieDecoder.STRICT.decode(cookieHeader);
   for (Cookie cookie: nettyCookies) {
    if (cookie.name().equals("JSESSIONID")) {
     headers.add(COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
     break;
    }
   }
  }
  return headers;
 }
}

代码示例来源:origin: dreamhead/moco

@Test
public void should_set_and_recognize_cookie_with_path() throws IOException {
  runWithConfiguration("cookie.json");
  Cookie decodeCookie = getCookie("/cookie-with-path");
  assertThat(decodeCookie.name(), is("login"));
  assertThat(decodeCookie.value(), is("true"));
  assertThat(decodeCookie.path(), is("/"));
}

代码示例来源:origin: dreamhead/moco

@Test
public void should_set_and_recognize_cookie_with_domain() throws IOException {
  runWithConfiguration("cookie.json");
  Cookie decodeCookie = getCookie("/cookie-with-domain");
  assertThat(decodeCookie.name(), is("login"));
  assertThat(decodeCookie.value(), is("true"));
  assertThat(decodeCookie.domain(), is("github.com"));
}

代码示例来源:origin: dreamhead/moco

@Test
public void should_set_and_recognize_cookie_with_max_age() throws IOException {
  runWithConfiguration("cookie.json");
  Cookie decodeCookie = getCookie("/cookie-with-max-age");
  assertThat(decodeCookie.name(), is("login"));
  assertThat(decodeCookie.value(), is("true"));
  assertThat(decodeCookie.maxAge(), is(3600L));
}

代码示例来源:origin: dreamhead/moco

@Test
public void should_set_and_recognize_cookie_with_secure() throws IOException {
  runWithConfiguration("cookie.json");
  Cookie decodeCookie = getCookie("/cookie-with-secure");
  assertThat(decodeCookie.name(), is("login"));
  assertThat(decodeCookie.value(), is("true"));
  assertThat(decodeCookie.isSecure(), is(true));
}

代码示例来源:origin: dreamhead/moco

@Test
public void should_set_and_recognize_cookie_with_http_only() throws IOException {
  runWithConfiguration("cookie.json");
  Cookie decodeCookie = getCookie("/cookie-with-http-only");
  assertThat(decodeCookie.name(), is("login"));
  assertThat(decodeCookie.value(), is("true"));
  assertThat(decodeCookie.isHttpOnly(), is(true));
}

相关文章