コード例 #1
0
ファイル: CookieTest.java プロジェクト: hhariri/RxNetty
 @Test
 public void testGetCookie() throws Exception {
   DefaultHttpRequest nettyRequest =
       new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
   String cookie1Name = "PREF";
   String cookie1Value = "ID=a95756377b78e75e:FF=0:TM=1392709628:LM=1392709628:S=a5mOVvTB7DBkexgi";
   String cookie1Domain = ".google.com";
   String cookie1Path = "/";
   String cookie1Header =
       cookie1Name
           + '='
           + cookie1Value
           + "; expires=Thu, 18-Feb-2016 07:47:08 GMT; path="
           + cookie1Path
           + "; domain="
           + cookie1Domain;
   nettyRequest.headers().add(HttpHeaders.Names.COOKIE, cookie1Header);
   HttpServerRequest<ByteBuf> request =
       new HttpServerRequest<ByteBuf>(nettyRequest, PublishSubject.<ByteBuf>create());
   Map<String, Set<Cookie>> cookies = request.getCookies();
   Assert.assertEquals("Unexpected number of cookies.", 1, cookies.size());
   Set<Cookie> cookies1 = cookies.get(cookie1Name);
   Assert.assertNotNull("No cookie found with name: " + cookie1Name, cookies1);
   Assert.assertEquals(
       "Unexpected number of cookies with name: " + cookie1Name, 1, cookies1.size());
   Cookie cookie = cookies1.iterator().next();
   Assert.assertEquals("Unexpected cookie name.", cookie1Name, cookie.getName());
   Assert.assertEquals("Unexpected cookie path.", cookie1Path, cookie.getPath());
 }
コード例 #2
0
ファイル: CookieHolder.java プロジェクト: panenming/hipush
 public Cookie get(String name) {
   for (Cookie cookie : cookies) {
     if (cookie.name().toLowerCase().equals(name.toLowerCase())) {
       return cookie;
     }
   }
   return null;
 }
コード例 #3
0
ファイル: CookieHolder.java プロジェクト: panenming/hipush
 public String getValue(String name) {
   Cookie cookie = get(name);
   if (cookie == null) {
     return null;
   }
   if (cookie.maxAge() == 0) {
     return null;
   }
   return cookie.value();
 }
コード例 #4
0
ファイル: CookieHolder.java プロジェクト: panenming/hipush
 public CookieHolder(Set<Cookie> cookies) {
   if (cookies.isEmpty()) {
     this.cookies = new TreeSet<Cookie>();
   } else {
     this.cookies = cookies;
     for (Cookie cookie : cookies) {
       cookie.setPath("/");
     }
   }
 }
コード例 #5
0
ファイル: CookieHolder.java プロジェクト: panenming/hipush
 public void setSessionId(String sid) {
   Cookie cookie = get("sid");
   if (cookie == null) {
     cookie = new DefaultCookie("sid", sid);
     cookies.add(cookie);
   } else if (!cookie.value().equals(sid)) {
     cookie.setValue(sid);
   }
   cookie.setPath("/");
   cookie.setMaxAge(Long.MIN_VALUE);
 }
コード例 #6
0
ファイル: HttpCookie.java プロジェクト: kidaak/higgs
 public HttpCookie(Cookie cookie) {
   this(cookie.getName(), cookie.getValue());
   Set<Field> fields = ReflectionUtil.getAllFields(new HashSet<Field>(), DefaultCookie.class, 1);
   for (Field field : fields) {
     try {
       if (!Modifier.isFinal(field.getModifiers())) {
         field.setAccessible(true);
         field.set(this, field.get(cookie));
       }
     } catch (Throwable t) {
       log.warn("Error copying cookie field", t);
     }
   }
 }
コード例 #7
0
ファイル: CookieTest.java プロジェクト: hhariri/RxNetty
 @Test
 public void testSetCookie() throws Exception {
   DefaultHttpResponse nettyResponse =
       new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
   HttpServerResponse<ByteBuf> response =
       new HttpServerResponse<ByteBuf>(
           new NoOpChannelHandlerContext(),
           nettyResponse,
           new MetricEventsSubject<HttpServerMetricsEvent<?>>());
   String cookieName = "name";
   String cookieValue = "value";
   response.addCookie(new DefaultCookie(cookieName, cookieValue));
   String cookieHeader = nettyResponse.headers().get(HttpHeaders.Names.SET_COOKIE);
   Assert.assertNotNull("Cookie header not found.", cookieHeader);
   Set<Cookie> decode = CookieDecoder.decode(cookieHeader);
   Assert.assertNotNull("Decoded cookie not found.", decode);
   Assert.assertEquals("Unexpected number of decoded cookie not found.", 1, decode.size());
   Cookie cookie = decode.iterator().next();
   Assert.assertEquals("Unexpected cookie name.", cookieName, cookie.getName());
   Assert.assertEquals("Unexpected cookie value.", cookieValue, cookie.getValue());
 }
コード例 #8
0
ファイル: DefaultRequest.java プロジェクト: kyleboon/ratpack
  public String oneCookie(String name) {
    Cookie found = null;
    List<Cookie> allFound = null;
    for (Cookie cookie : getCookies()) {
      if (cookie.getName().equals(name)) {
        if (found == null) {
          found = cookie;
        } else if (allFound == null) {
          allFound = new ArrayList<>(2);
          allFound.add(found);
        } else {
          allFound.add(cookie);
        }
      }
    }

    if (found == null) {
      return null;
    } else if (allFound != null) {
      StringBuilder s =
          new StringBuilder("Multiple cookies with name '").append(name).append("': ");
      int i = 0;
      for (Cookie cookie : allFound) {
        s.append(cookie.toString());
        if (++i < allFound.size()) {
          s.append(", ");
        }
      }

      throw new IllegalStateException(s.toString());
    } else {
      return found.getValue();
    }
  }
コード例 #9
0
ファイル: CookieHolder.java プロジェクト: panenming/hipush
 public void clear() {
   for (Cookie cookie : cookies) {
     cookie.setMaxAge(0);
   }
 }
コード例 #10
0
ファイル: CookieHolder.java プロジェクト: panenming/hipush
 public void remove(String name) {
   Cookie cookie = get(name);
   if (cookie != null) {
     cookie.setMaxAge(0);
   }
 }