org.apache.http.message.BasicHeaderElement Java Examples

The following examples show how to use org.apache.http.message.BasicHeaderElement. 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: ApacheCloudStackClientTest.java    From apache-cloudstack-java-client with Apache License 2.0 6 votes vote down vote up
@Test
public void createCookieForHeaderElementTest() {
    String cookiePath = "/client/api";

    String paramName = "paramName1";
    String paramValue = "paramVale1";
    NameValuePair[] parameters = new NameValuePair[1];
    parameters[0] = new BasicNameValuePair(paramName, paramValue);

    String headerName = "headerElementName";
    String headerValue = "headerElementValue";
    HeaderElement headerElement = new BasicHeaderElement(headerName, headerValue, parameters);

    Mockito.doNothing().when(apacheCloudStackClient).configureDomainForCookie(Mockito.any(BasicClientCookie.class));

    BasicClientCookie cookieForHeaderElement = apacheCloudStackClient.createCookieForHeaderElement(headerElement);

    Assert.assertNotNull(cookieForHeaderElement);
    Assert.assertEquals(headerName, cookieForHeaderElement.getName());
    Assert.assertEquals(headerValue, cookieForHeaderElement.getValue());
    Assert.assertEquals(paramValue, cookieForHeaderElement.getAttribute(paramName));
    Assert.assertEquals(cookiePath, cookieForHeaderElement.getPath());

    Mockito.verify(apacheCloudStackClient).configureDomainForCookie(Mockito.eq(cookieForHeaderElement));
}
 
Example #2
Source File: HtmlUnitBrowserCompatCookieSpec.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public List<Header> formatCookies(final List<Cookie> cookies) {
    Collections.sort(cookies, COOKIE_COMPARATOR);

    final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
    buffer.append(SM.COOKIE);
    buffer.append(": ");
    for (int i = 0; i < cookies.size(); i++) {
        final Cookie cookie = cookies.get(i);
        if (i > 0) {
            buffer.append("; ");
        }
        final String cookieName = cookie.getName();
        final String cookieValue = cookie.getValue();
        if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
            HtmlUnitBrowserCompatCookieHeaderValueFormatter.INSTANCE.formatHeaderElement(
                    buffer,
                    new BasicHeaderElement(cookieName, cookieValue),
                    false);
        }
        else {
            // Netscape style cookies do not support quoted values
            buffer.append(cookieName);
            buffer.append("=");
            if (cookieValue != null) {
                buffer.append(cookieValue);
            }
        }
    }
    final List<Header> headers = new ArrayList<>(1);
    headers.add(new BufferedHeader(buffer));
    return headers;
}
 
Example #3
Source File: CookieUtil.java    From esigate with Apache License 2.0 4 votes vote down vote up
/**
 * Utility method to transform a Cookie into a Set-Cookie Header.
 * 
 * @param cookie
 *            the {@link Cookie} to format
 * 
 * @return the value of the Set-Cookie header
 */
public static String encodeCookie(Cookie cookie) {
    int maxAge = -1;
    if (cookie.getExpiryDate() != null) {
        maxAge = (int) ((cookie.getExpiryDate().getTime() - System.currentTimeMillis()) / ONE_SECOND);
        // According to Cookie class specification, a negative value
        // would be considered as no value. That is not what we want!
        if (maxAge < 0) {
            maxAge = 0;
        }
    }

    String cookieName = cookie.getName();
    String cookieValue = cookie.getValue();

    StringBuilder buffer = new StringBuilder();

    // Copied from org.apache.http.impl.cookie.BrowserCompatSpec.formatCookies(List<Cookie>)
    if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
        buffer.append(BasicHeaderValueFormatter.INSTANCE.formatHeaderElement(null,
                new BasicHeaderElement(cookieName, cookieValue), false).toString());
    } else {
        // Netscape style cookies do not support quoted values
        buffer.append(cookieName);
        buffer.append("=");
        if (cookieValue != null) {
            buffer.append(cookieValue);
        }
    }
    // End copy

    appendAttribute(buffer, "Comment", cookie.getComment());
    appendAttribute(buffer, "Domain", cookie.getDomain());
    appendAttribute(buffer, "Max-Age", maxAge);
    appendAttribute(buffer, "Path", cookie.getPath());
    if (cookie.isSecure()) {
        appendAttribute(buffer, "Secure");
    }
    if (((BasicClientCookie) cookie).containsAttribute(HTTP_ONLY_ATTR)) {
        appendAttribute(buffer, "HttpOnly");
    }
    appendAttribute(buffer, "Version", cookie.getVersion());

    return (buffer.toString());
}