Example usage for org.apache.commons.httpclient.cookie CookieSpec formatCookieHeader

List of usage examples for org.apache.commons.httpclient.cookie CookieSpec formatCookieHeader

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.cookie CookieSpec formatCookieHeader.

Prototype

public abstract Header formatCookieHeader(Cookie[] paramArrayOfCookie) throws IllegalArgumentException;

Source Link

Usage

From source file:org.apache.cactus.internal.util.CookieUtil.java

/**
 * Create a HttpClient {@link Header} for cookies that matches
 * the domain and path.//from  ww w .j av a 2 s .c o m
 * 
 * @param theDomain the cookie domain to match
 * @param thePath the cookie path to match
 * @param theCookies the list of potential cookies
 * @return the HttpClient {@link Header} containing the matching 
 *         cookies
 * @throws ClientException if no cookie was matching the domain
 *         and path
 */
public static Header createCookieHeader(String theDomain, String thePath,
        org.apache.commons.httpclient.Cookie[] theCookies) throws ClientException {
    Header cookieHeader = null;

    // separate domain into host and port
    int port = 80;
    String host = theDomain;
    int portIndex = theDomain.indexOf(":");
    if (portIndex != -1) {
        host = host.substring(0, portIndex);
        port = Integer.parseInt(theDomain.substring(portIndex + 1));
    }

    CookieSpec matcher = CookiePolicy.getDefaultSpec();
    org.apache.commons.httpclient.Cookie[] cookies = matcher.match(host, port, thePath, false, theCookies);
    if ((cookies != null) && (cookies.length > 0)) {
        cookieHeader = matcher.formatCookieHeader(cookies);
    }

    if (cookieHeader == null) {
        throw new ClientException("Failed to create Cookie header for [" + "domain = [" + theDomain
                + ", path = [" + thePath + ", cookies = [" + theCookies + "]]. Turn on HttpClient "
                + "logging for more information about the error");
    }

    return cookieHeader;
}