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

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

Introduction

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

Prototype

public abstract Cookie[] parse(String paramString1, int paramInt, String paramString2, boolean paramBoolean,
            Header paramHeader) throws MalformedCookieException, IllegalArgumentException;

Source Link

Usage

From source file:org.apache.cactus.WebResponse.java

/**
 * @return the cookies returned by the server
 *//*from   w  w  w  . j  av a 2  s .c o m*/
public Cookie[] getCookies() {
    Cookie[] returnCookies = null;

    // There can be several headers named "Set-Cookie", so loop through
    // all the headers, looking for cookies
    String headerName = this.connection.getHeaderFieldKey(0);
    String headerValue = this.connection.getHeaderField(0);

    Vector cookieVector = new Vector();
    CookieSpec cookieSpec = CookiePolicy.getDefaultSpec();

    for (int i = 1; (headerName != null) || (headerValue != null); i++) {
        LOGGER.debug("Header name  = [" + headerName + "]");
        LOGGER.debug("Header value = [" + headerValue + "]");

        if ((headerName != null) && (headerName.toLowerCase().equals("set-cookie")
                || headerName.toLowerCase().equals("set-cookie2"))) {
            // Parse the cookie definition
            org.apache.commons.httpclient.Cookie[] cookies;
            try {
                cookies = cookieSpec.parse(
                        CookieUtil.getCookieDomain(getWebRequest(), getConnection().getURL().getHost()),
                        CookieUtil.getCookiePort(getWebRequest(), getConnection().getURL().getPort()),
                        CookieUtil.getCookiePath(getWebRequest(), getConnection().getURL().getFile()), false,
                        new Header(headerName, headerValue));
            } catch (HttpException e) {
                throw new ChainedRuntimeException("Error parsing cookies", e);
            }

            // Transform the HttpClient cookies into Cactus cookies and
            // add them to the cookieVector vector
            for (int j = 0; j < cookies.length; j++) {
                Cookie cookie = new Cookie(cookies[j].getDomain(), cookies[j].getName(), cookies[j].getValue());

                cookie.setComment(cookies[j].getComment());
                cookie.setExpiryDate(cookies[j].getExpiryDate());
                cookie.setPath(cookies[j].getPath());
                cookie.setSecure(cookies[j].getSecure());

                cookieVector.addElement(cookie);
            }
        }

        headerName = this.connection.getHeaderFieldKey(i);
        headerValue = this.connection.getHeaderField(i);
    }

    returnCookies = new Cookie[cookieVector.size()];
    cookieVector.copyInto(returnCookies);

    return returnCookies;
}

From source file:org.mule.transport.http.CookieHelper.java

/**
 * This method parses the value of {@linkplain HttpConstants#HEADER_COOKIE_SET
 * "Set-Cookie"} HTTP header, returning an array with all the {@link Cookie}s
 * found. This method is intended to be used from the client side of the HTTP
 * connection.//w  w  w .ja v  a2 s.com
 *
 * @param cookieHeaderValue the value with the cookie/s to parse.
 * @param spec the spec according to {@link #getCookieSpec(String)} (can be null)
 * @param uri the uri information that will be use to complete Cookie information
 *            (host, port and path). If null then the
 *            <code>DEFAULT_URI_STRING</code> will be used.
 */
public static Cookie[] parseCookiesAsAClient(String cookieHeaderValue, String spec, URI uri)
        throws MalformedCookieException {
    if (uri == null) {
        try {
            uri = new URI(DEFAULT_URI_STRING);
        } catch (URISyntaxException e) {
            throw new RuntimeException("This should have not happened", e);
        }
    }
    CookieSpec cookieSpec = getCookieSpec(spec);
    boolean secure = uri.getScheme() != null && uri.getScheme().equalsIgnoreCase("https");
    String host = uri.getHost();
    int port = getPortFromURI(uri);
    String path = uri.getPath();

    return cookieSpec.parse(host, port, path, secure, cookieHeaderValue);
}