Exemple #1
0
 @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());
 }
Exemple #2
0
 public Cookie get(String name) {
   for (Cookie cookie : cookies) {
     if (cookie.name().toLowerCase().equals(name.toLowerCase())) {
       return cookie;
     }
   }
   return null;
 }
Exemple #3
0
 public String getValue(String name) {
   Cookie cookie = get(name);
   if (cookie == null) {
     return null;
   }
   if (cookie.maxAge() == 0) {
     return null;
   }
   return cookie.value();
 }
Exemple #4
0
 public CookieHolder(Set<Cookie> cookies) {
   if (cookies.isEmpty()) {
     this.cookies = new TreeSet<Cookie>();
   } else {
     this.cookies = cookies;
     for (Cookie cookie : cookies) {
       cookie.setPath("/");
     }
   }
 }
Exemple #5
0
 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);
 }
Exemple #6
0
 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);
     }
   }
 }
Exemple #7
0
 @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());
 }
Exemple #8
0
  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();
    }
  }
Exemple #9
0
 public void clear() {
   for (Cookie cookie : cookies) {
     cookie.setMaxAge(0);
   }
 }
Exemple #10
0
 public void remove(String name) {
   Cookie cookie = get(name);
   if (cookie != null) {
     cookie.setMaxAge(0);
   }
 }