Ejemplo n.º 1
0
 /**
  * Return a name/value string suitable for sending in a <tt>"Cookie"</tt> header as defined in RFC
  * 2109 for backward compatibility with cookie version 0
  *
  * @param buffer The string buffer to use for output
  * @param param The parameter.
  * @param version The cookie version
  */
 private void formatParam(final StringBuffer buffer, final NameValuePair param, int version) {
   if (version < 1) {
     buffer.append(param.getName());
     buffer.append("=");
     if (param.getValue() != null) {
       buffer.append(param.getValue());
     }
   } else {
     this.formatter.format(buffer, param);
   }
 }
Ejemplo n.º 2
0
  /**
   * Parse RFC 2109 specific cookie attribute and update the corresponsing {@link Cookie}
   * properties.
   *
   * @param attribute {@link NameValuePair} cookie attribute from the <tt>Set- Cookie</tt>
   * @param cookie {@link Cookie} to be updated
   * @throws MalformedCookieException if an exception occurs during parsing
   */
  public void parseAttribute(final NameValuePair attribute, final Cookie cookie)
      throws MalformedCookieException {

    if (attribute == null) {
      throw new IllegalArgumentException("Attribute may not be null.");
    }
    if (cookie == null) {
      throw new IllegalArgumentException("Cookie may not be null.");
    }
    final String paramName = attribute.getName().toLowerCase();
    final String paramValue = attribute.getValue();

    if (paramName.equals("path")) {
      if (paramValue == null) {
        throw new MalformedCookieException("Missing value for path attribute");
      }
      if (paramValue.trim().equals("")) {
        throw new MalformedCookieException("Blank value for path attribute");
      }
      cookie.setPath(paramValue);
      cookie.setPathAttributeSpecified(true);
    } else if (paramName.equals("version")) {

      if (paramValue == null) {
        throw new MalformedCookieException("Missing value for version attribute");
      }
      try {
        cookie.setVersion(Integer.parseInt(paramValue));
      } catch (NumberFormatException e) {
        throw new MalformedCookieException("Invalid version: " + e.getMessage());
      }

    } else {
      super.parseAttribute(attribute, cookie);
    }
  }