Java Code Examples for org.apache.commons.httpclient.Cookie#setDomain()

The following examples show how to use org.apache.commons.httpclient.Cookie#setDomain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RFC2965Spec.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Parse cookie domain attribute.
 */
public void parse(final Cookie cookie, String domain)
        throws MalformedCookieException {
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
    }
    if (domain == null) {
        throw new MalformedCookieException(
                "Missing value for domain attribute");
    }
    if (domain.trim().equals("")) {
        throw new MalformedCookieException(
                "Blank value for domain attribute");
    }
    domain = domain.toLowerCase();
    if (!domain.startsWith(".")) {
        // Per RFC 2965 section 3.2.2
        // "... If an explicitly specified value does not start with
        // a dot, the user agent supplies a leading dot ..."
        // That effectively implies that the domain attribute 
        // MAY NOT be an IP address of a host name
        domain = "." + domain;
    }
    cookie.setDomain(domain);
    cookie.setDomainAttributeSpecified(true);
}
 
Example 2
Source File: CrawlerPack.java    From CrawlerPack with Apache License 2.0 6 votes vote down vote up
/**
 * Return a Cookie array
 * and auto importing domain and path when domain was empty.
 *
 * @param uri required Apache Common VFS supported file systems and response JSON format content.
 * @return Cookie[]
 */
Cookie[] getCookies(String uri){
    if( null == cookies || 0 == cookies.size()) return null;

    for(Cookie cookie: cookies){

        if("".equals(cookie.getDomain())){
            String domain = uri.replaceAll("^.*:\\/\\/([^\\/]+)[\\/]?.*$", "$1");
            cookie.setDomain(domain);
            cookie.setPath("/");
            cookie.setExpiryDate(null);
            cookie.setSecure(false);
        }
    }

    return cookies.toArray(new Cookie[cookies.size()]);
}
 
Example 3
Source File: DavMailCookieSpec.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void validate(String host, int port, String path,
                     boolean secure, final Cookie cookie) throws MalformedCookieException {
    // workaround for space in cookie name
    String cookieName = cookie.getName();
    if (cookieName != null && cookieName.indexOf(' ') >= 0) {
        cookie.setName(cookieName.replaceAll(" ", ""));
    } else {
        cookieName = null;
    }
    // workaround for invalid cookie path
    String cookiePath = cookie.getPath();
    if (cookiePath != null && !path.startsWith(cookiePath)) {
        cookie.setPath(path);
    } else {
        cookiePath = null;
    }
    // workaround for invalid cookie domain
    int dotIndex = -1;
    if (host.endsWith(cookie.getDomain())) {
        String hostWithoutDomain = host.substring(0, host.length()
                - cookie.getDomain().length());
        dotIndex = hostWithoutDomain.indexOf('.');
    }
    if (".login.microsoftonline.com".equals(cookie.getDomain())) {
        cookie.setDomain(host);
    }
    if (dotIndex != -1) {
        // discard additional host name part
        super.validate(host.substring(dotIndex + 1), port, path, secure, cookie);
    } else {
        super.validate(host, port, path, secure, cookie);
    }
    if (cookieName != null) {
        cookie.setName(cookieName);
    }
    if (cookiePath != null) {
        cookie.setPath(cookiePath);
    }
}
 
Example 4
Source File: SsoUtil.java    From iaf with Apache License 2.0 5 votes vote down vote up
public static void addSsoCredential(HttpMethod method, HttpState state, String defaultForwardHost) {
	try {
		String name=SsoUtil.getSsoTokenName();
		String value=SsoUtil.getSsoToken();
		if (StringUtils.isEmpty(value)) {
			if (log.isDebugEnabled()) log.debug("no value for SsoCredential ["+name+"]");
		} else {
			if (log.isDebugEnabled()) log.debug("constructing SsoCredentialCookie ["+name+"]");
			Cookie ssoCookie = new Cookie();
			ssoCookie.setName(name);
		
			ssoCookie.setValue(value);
			String forwardHost;
			try {
				URI uri = method.getURI();
				forwardHost = uri.getHost();
				if (StringUtils.isEmpty(forwardHost)) {
					if (log.isDebugEnabled()) log.debug("did not find host from URI ["+uri.getURI()+"], will use default ["+defaultForwardHost+"] for SSO credential cookie");
					forwardHost=defaultForwardHost;
				}
			} catch (Throwable t) {
				log.warn("could not extract host from URI", t);
				forwardHost = defaultForwardHost;					
			}
			ssoCookie.setDomain(forwardHost);
			// path must have a value, otherwise cookie is not appended to request
			ssoCookie.setPath("/");
			if (log.isDebugEnabled()) log.debug("set SSOcookie attributes: domain ["+ssoCookie.getDomain()+"] path ["+ssoCookie.getPath()+"]");
			state.addCookie(ssoCookie);
		}
		
	} catch (Exception e) {
		log.warn("could not obtain SsoToken: "+e.getMessage());
	}
}
 
Example 5
Source File: CookieSpecBase.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
  * Parse the cookie attribute and update the corresponsing {@link Cookie}
  * properties.
  *
  * @param attribute {@link HeaderElement} 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();
    String paramValue = attribute.getValue();

    if (paramName.equals("path")) {

        if ((paramValue == null) || (paramValue.trim().equals(""))) {
            paramValue = "/";
        }
        cookie.setPath(paramValue);
        cookie.setPathAttributeSpecified(true);

    } else if (paramName.equals("domain")) {

        if (paramValue == null) {
            throw new MalformedCookieException(
                "Missing value for domain attribute");
        }
        if (paramValue.trim().equals("")) {
            throw new MalformedCookieException(
                "Blank value for domain attribute");
        }
        cookie.setDomain(paramValue);
        cookie.setDomainAttributeSpecified(true);

    } else if (paramName.equals("max-age")) {

        if (paramValue == null) {
            throw new MalformedCookieException(
                "Missing value for max-age attribute");
        }
        int age;
        try {
            age = Integer.parseInt(paramValue);
        } catch (NumberFormatException e) {
            throw new MalformedCookieException ("Invalid max-age "
                + "attribute: " + e.getMessage());
        }
        cookie.setExpiryDate(
            new Date(System.currentTimeMillis() + age * 1000L));

    } else if (paramName.equals("secure")) {

        cookie.setSecure(true);

    } else if (paramName.equals("comment")) {

        cookie.setComment(paramValue);

    } else if (paramName.equals("expires")) {

        if (paramValue == null) {
            throw new MalformedCookieException(
                "Missing value for expires attribute");
        }

        try {
            cookie.setExpiryDate(DateUtil.parseDate(paramValue, this.datepatterns));
        } catch (DateParseException dpe) {
            LOG.debug("Error parsing cookie date", dpe);
            throw new MalformedCookieException(
                "Unable to parse expiration date parameter: " 
                + paramValue);
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unrecognized cookie attribute: " 
                + attribute.toString());
        }
    }
}