Example #1
1
  public String oneCookie(String name) {
    Cookie found = null;
    List<Cookie> allFound = null;
    for (Cookie cookie : getCookies()) {
      if (cookie.name().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.value();
    }
  }
Example #2
0
 public String getUri() {
   if (uri == null) {
     if (rawUri.startsWith("/")) {
       uri = rawUri;
     } else {
       URI parsed = URI.create(rawUri);
       String path = parsed.getPath();
       if (Strings.isNullOrEmpty(path)) {
         path = "/";
       }
       StringBuilder sb = new StringBuilder();
       sb.append(path);
       if (parsed.getQuery() != null) {
         sb.append("?").append(parsed.getQuery());
       }
       if (parsed.getFragment() != null) {
         sb.append("#").append(parsed.getFragment());
       }
       uri = sb.toString();
     }
   }
   return uri;
 }