Example usage for org.apache.http.impl.cookie BasicClientCookie setAttribute

List of usage examples for org.apache.http.impl.cookie BasicClientCookie setAttribute

Introduction

In this page you can find the example usage for org.apache.http.impl.cookie BasicClientCookie setAttribute.

Prototype

public void setAttribute(final String name, final String value) 

Source Link

Usage

From source file:org.springframework.test.web.servlet.htmlunit.MockWebResponseBuilder.java

static com.gargoylesoftware.htmlunit.util.Cookie createCookie(Cookie cookie) {
    Date expires = null;//from w w  w.j  a v  a  2s. c  om
    if (cookie.getMaxAge() > -1) {
        expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
    }
    BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue());
    result.setDomain(cookie.getDomain());
    result.setComment(cookie.getComment());
    result.setExpiryDate(expires);
    result.setPath(cookie.getPath());
    result.setSecure(cookie.getSecure());
    if (cookie.isHttpOnly()) {
        result.setAttribute("httponly", "true");
    }
    return new com.gargoylesoftware.htmlunit.util.Cookie(result);
}

From source file:org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection.java

private static com.gargoylesoftware.htmlunit.util.Cookie createCookie(javax.servlet.http.Cookie cookie) {
    Date expires = null;//ww w  .  j  av a2s  .c  o  m
    if (cookie.getMaxAge() > -1) {
        expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
    }
    BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue());
    result.setDomain(cookie.getDomain());
    result.setComment(cookie.getComment());
    result.setExpiryDate(expires);
    result.setPath(cookie.getPath());
    result.setSecure(cookie.getSecure());
    if (cookie.isHttpOnly()) {
        result.setAttribute("httponly", "true");
    }
    return new com.gargoylesoftware.htmlunit.util.Cookie(result);
}

From source file:de.betterform.agent.web.WebUtil.java

private static Vector<BasicClientCookie> saveAsBasicClientCookie(Iterator iterator,
        Vector<BasicClientCookie> commonsCookies) {
    while (iterator.hasNext()) {
        javax.servlet.http.Cookie c = (Cookie) iterator.next();
        BasicClientCookie commonsCookie = new BasicClientCookie(c.getName(), c.getValue());
        commonsCookie.setDomain(c.getDomain());
        commonsCookie.setPath(c.getPath());
        commonsCookie.setAttribute(ClientCookie.MAX_AGE_ATTR, Integer.toString(c.getMaxAge()));
        commonsCookie.setSecure(c.getSecure());

        commonsCookies.add(commonsCookie);

        if (WebUtil.LOGGER.isDebugEnabled()) {
            WebUtil.LOGGER.debug("adding cookie >>>>>");
            WebUtil.LOGGER.debug("name: " + c.getName());
            WebUtil.LOGGER.debug("value: " + c.getValue());
            WebUtil.LOGGER.debug("path: " + c.getPath());
            WebUtil.LOGGER.debug("maxAge: " + c.getMaxAge());
            WebUtil.LOGGER.debug("secure: " + c.getSecure());
            WebUtil.LOGGER.debug("adding cookie done <<<<<");
        }//  ww w . j a  va 2s  . co  m
    }

    return commonsCookies;
}

From source file:com.gargoylesoftware.htmlunit.util.Cookie.java

/**
 * Creates a new cookie with the specified name and value which applies to the specified domain,
 * the specified path, and expires on the specified date.
 * @param domain the domain to which this cookie applies
 * @param name the cookie name//  w  w  w . ja  va2  s.c om
 * @param value the cookie name
 * @param path the path to which this cookie applies
 * @param expires the date on which this cookie expires
 * @param secure whether or not this cookie is secure (i.e. HTTPS vs HTTP)
 * @param httpOnly whether or not this cookie should be only used for HTTP(S) headers
 */
public Cookie(final String domain, final String name, final String value, final String path, final Date expires,
        final boolean secure, final boolean httpOnly) {
    if (domain == null) {
        throw new IllegalArgumentException("Cookie domain must be specified");
    }

    final BasicClientCookie cookie = new BasicClientCookie(name, value != null ? value : "");
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setExpiryDate(expires);
    cookie.setSecure(secure);
    if (httpOnly) {
        cookie.setAttribute("httponly", "true");
    }
    httpClientCookie_ = cookie;
}

From source file:org.esxx.js.protocol.CookieJar.java

private void setRawValue(Object raw, BasicClientCookie cookie, String name) {
    if (raw instanceof Scriptable) {
        Object value = ((Scriptable) raw).get(name, (Scriptable) raw);

        if (value != Scriptable.NOT_FOUND) {
            cookie.setAttribute(name, Context.toString(value));
        }//from www.ja  v  a2  s  . c  o  m
    }
}

From source file:org.xwiki.wysiwyg.internal.plugin.alfresco.server.SiteMinderAuthenticator.java

/**
 * @return the list of SiteMinder cookies that have to be added to the HTTP request in order to authenticate it.
 *//*w  w w  .j  a va 2  s .c o m*/
private List<Cookie> getSiteMinderCookies() {
    javax.servlet.http.Cookie[] receivedCookies = ((ServletRequest) container.getRequest())
            .getHttpServletRequest().getCookies();
    List<Cookie> cookies = new ArrayList<Cookie>();
    // Look for the SMSESSION cookie.
    for (int i = 0; i < receivedCookies.length; i++) {
        javax.servlet.http.Cookie receivedCookie = receivedCookies[i];
        if (SITE_MINDER_COOKIES.contains(receivedCookie.getName())) {
            BasicClientCookie cookie = new BasicClientCookie(receivedCookie.getName(),
                    receivedCookie.getValue());
            cookie.setVersion(receivedCookie.getVersion());
            cookie.setDomain(receivedCookie.getDomain());
            cookie.setPath(receivedCookie.getPath());
            cookie.setSecure(receivedCookie.getSecure());
            // Set attributes EXACTLY as sent by the browser.
            cookie.setAttribute(ClientCookie.VERSION_ATTR, String.valueOf(receivedCookie.getVersion()));
            cookie.setAttribute(ClientCookie.DOMAIN_ATTR, receivedCookie.getDomain());
            cookies.add(cookie);
        }
    }
    return cookies;
}

From source file:org.apache.jmeter.protocol.http.control.HC4CookieHandler.java

/**
 * Create an HttpClient cookie from a JMeter cookie
 */// w w  w .  j  a  v a  2  s.co m
private org.apache.http.cookie.Cookie makeCookie(Cookie jmc) {
    long exp = jmc.getExpiresMillis();
    BasicClientCookie ret = new BasicClientCookie(jmc.getName(), jmc.getValue());
    ret.setDomain(jmc.getDomain());
    ret.setPath(jmc.getPath());
    ret.setExpiryDate(exp > 0 ? new Date(exp) : null); // use null for no expiry
    ret.setSecure(jmc.getSecure());
    ret.setVersion(jmc.getVersion());
    if (jmc.isDomainSpecified()) {
        ret.setAttribute(ClientCookie.DOMAIN_ATTR, jmc.getDomain());
    }
    if (jmc.isPathSpecified()) {
        ret.setAttribute(ClientCookie.PATH_ATTR, jmc.getPath());
    }
    return ret;
}

From source file:br.com.autonomiccs.apacheCloudStack.client.ApacheCloudStackClient.java

/**
 *  This method will create a {@link BasicClientCookie} with the given {@link HeaderElement}.
 *  It sill set the cookie's name and value according to the {@link HeaderElement#getName()} and {@link HeaderElement#getValue()} methods.
 *  Moreover, it will transport every {@link HeaderElement} parameter to the cookie using the {@link BasicClientCookie#setAttribute(String, String)}.
 *  Additionally, it configures the cookie path ({@link BasicClientCookie#setPath(String)}) to value '/client/api' and the cookie domain using {@link #configureDomainForCookie(BasicClientCookie)} method.
 *///from  ww w  .  j  a  v  a2 s. c o  m
protected BasicClientCookie createCookieForHeaderElement(HeaderElement element) {
    BasicClientCookie cookie = new BasicClientCookie(element.getName(), element.getValue());
    for (NameValuePair parameter : element.getParameters()) {
        cookie.setAttribute(parameter.getName(), parameter.getValue());
    }
    cookie.setPath("/client/api");
    configureDomainForCookie(cookie);
    return cookie;
}

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

/**
 * {@inheritDoc}//from   w  w w. ja  v  a2 s .c  o  m
 */
@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;
}