Example usage for org.apache.http.cookie SM SET_COOKIE

List of usage examples for org.apache.http.cookie SM SET_COOKIE

Introduction

In this page you can find the example usage for org.apache.http.cookie SM SET_COOKIE.

Prototype

String SET_COOKIE

To view the source code for org.apache.http.cookie SM SET_COOKIE.

Click Source Link

Usage

From source file:com.googlecode.noweco.webmail.httpclient.UnsecureResponseProcessCookies.java

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }/*from  www  .  jav a2  s.c om*/
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }

    // Obtain actual CookieSpec instance
    CookieSpec cookieSpec = (CookieSpec) context.getAttribute(ClientContext.COOKIE_SPEC);
    if (cookieSpec == null) {
        this.log.debug("Cookie spec not specified in HTTP context");
        return;
    }
    // Obtain cookie store
    CookieStore cookieStore = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
    if (cookieStore == null) {
        this.log.debug("Cookie store not specified in HTTP context");
        return;
    }
    // Obtain actual CookieOrigin instance
    CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(ClientContext.COOKIE_ORIGIN);
    if (cookieOrigin == null) {
        this.log.debug("Cookie origin not specified in HTTP context");
        return;
    }
    HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
    processCookies(it, cookieSpec, cookieOrigin, cookieStore);

    // see if the cookie spec supports cookie versioning.
    if (cookieSpec.getVersion() > 0) {
        // process set-cookie2 headers.
        // Cookie2 will replace equivalent Cookie instances
        it = response.headerIterator(SM.SET_COOKIE2);
        processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    }
}

From source file:org.droidparts.http.CookieJar.java

@Override
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {
    for (String key : responseHeaders.keySet()) {
        if (SM.SET_COOKIE.equalsIgnoreCase(key) || SM.SET_COOKIE2.equalsIgnoreCase(key)) {
            List<Cookie> cookies = parseCookies(uri, responseHeaders.get(key));
            for (Cookie c : cookies) {
                addCookie(c);//from   w  ww. jav a 2s . c om
            }
            return;
        }
    }
}

From source file:org.droidparts.http.CookieJar.java

private List<Cookie> parseCookies(URI uri, List<String> cookieHeaders) {
    ArrayList<Cookie> cookies = new ArrayList<Cookie>();
    int port = (uri.getPort() < 0) ? 80 : uri.getPort();
    boolean secure = "https".equals(uri.getScheme());
    CookieOrigin origin = new CookieOrigin(uri.getHost(), port, uri.getPath(), secure);
    for (String cookieHeader : cookieHeaders) {
        BasicHeader header = new BasicHeader(SM.SET_COOKIE, cookieHeader);
        try {/*from  w  ww . j a v a2  s . c o m*/
            cookies.addAll(cookieSpec.parse(header, origin));
        } catch (MalformedCookieException e) {
            L.d(e);
        }
    }
    return cookies;
}

From source file:com.gargoylesoftware.htmlunit.httpclient.HtmlUnitBrowserCompatCookieSpec.java

/**
 * {@inheritDoc}// w w w  .j a  va 2 s .  com
 */
@Override
public List<Cookie> parse(Header header, final CookieOrigin origin) throws MalformedCookieException {
    // first a hack to support empty headers
    final String text = header.getValue();
    int endPos = text.indexOf(';');
    if (endPos < 0) {
        endPos = text.indexOf('=');
    } else {
        final int pos = text.indexOf('=');
        if (pos > endPos) {
            endPos = -1;
        } else {
            endPos = pos;
        }
    }
    if (endPos < 0) {
        header = new BasicHeader(header.getName(), EMPTY_COOKIE_NAME + "=" + header.getValue());
    } else if (endPos == 0 || StringUtils.isBlank(text.substring(0, endPos))) {
        header = new BasicHeader(header.getName(), EMPTY_COOKIE_NAME + header.getValue());
    }

    final List<Cookie> cookies;

    final String headername = header.getName();
    if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) {
        throw new MalformedCookieException("Unrecognized cookie header '" + header.toString() + "'");
    }
    final HeaderElement[] helems = header.getElements();
    boolean versioned = false;
    boolean netscape = false;
    for (final HeaderElement helem : helems) {
        if (helem.getParameterByName("version") != null) {
            versioned = true;
        }
        if (helem.getParameterByName("expires") != null) {
            netscape = true;
        }
    }
    if (netscape || !versioned) {
        // Need to parse the header again, because Netscape style cookies do not correctly
        // support multiple header elements (comma cannot be treated as an element separator)
        final NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
        final CharArrayBuffer buffer;
        final ParserCursor cursor;
        if (header instanceof FormattedHeader) {
            buffer = ((FormattedHeader) header).getBuffer();
            cursor = new ParserCursor(((FormattedHeader) header).getValuePos(), buffer.length());
        } else {
            final String s = header.getValue();
            if (s == null) {
                throw new MalformedCookieException("Header value is null");
            }
            buffer = new CharArrayBuffer(s.length());
            buffer.append(s);
            cursor = new ParserCursor(0, buffer.length());
        }
        final HeaderElement elem = parser.parseHeader(buffer, cursor);
        final String name = elem.getName();
        final String value = elem.getValue();
        if (name == null || name.isEmpty()) {
            throw new MalformedCookieException("Cookie name may not be empty");
        }
        final BasicClientCookie cookie = new BasicClientCookie(name, value);
        cookie.setPath(getDefaultPath(origin));
        cookie.setDomain(getDefaultDomain(origin));

        // cycle through the parameters
        final NameValuePair[] attribs = elem.getParameters();
        for (int j = attribs.length - 1; j >= 0; j--) {
            final NameValuePair attrib = attribs[j];
            final String s = attrib.getName().toLowerCase(Locale.ROOT);
            cookie.setAttribute(s, attrib.getValue());
            final CookieAttributeHandler handler = findAttribHandler(s);
            if (handler != null) {
                handler.parse(cookie, attrib.getValue());
            }
        }
        // Override version for Netscape style cookies
        if (netscape) {
            cookie.setVersion(0);
        }
        cookies = Collections.<Cookie>singletonList(cookie);
    } else {
        cookies = parse(helems, origin);
    }

    for (final Cookie c : cookies) {
        // re-add quotes around value if parsing as incorrectly trimmed them
        if (header.getValue().contains(c.getName() + "=\"" + c.getValue())) {
            ((BasicClientCookie) c).setValue('"' + c.getValue() + '"');
        }
    }
    return cookies;
}

From source file:org.apache.http.client.protocol.ResponseProcessCookies.java

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    Args.notNull(response, "HTTP request");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    // Obtain actual CookieSpec instance
    final CookieSpec cookieSpec = clientContext.getCookieSpec();
    if (cookieSpec == null) {
        this.log.debug("Cookie spec not specified in HTTP context");
        return;//from w ww.j  a v a 2 s  .c  o m
    }
    // Obtain cookie store
    final CookieStore cookieStore = clientContext.getCookieStore();
    if (cookieStore == null) {
        this.log.debug("Cookie store not specified in HTTP context");
        return;
    }
    // Obtain actual CookieOrigin instance
    final CookieOrigin cookieOrigin = clientContext.getCookieOrigin();
    if (cookieOrigin == null) {
        this.log.debug("Cookie origin not specified in HTTP context");
        return;
    }
    HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
    processCookies(it, cookieSpec, cookieOrigin, cookieStore);

    // see if the cookie spec supports cookie versioning.
    if (cookieSpec.getVersion() > 0) {
        // process set-cookie2 headers.
        // Cookie2 will replace equivalent Cookie instances
        it = response.headerIterator(SM.SET_COOKIE2);
        processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    }
}