Example usage for org.apache.http.cookie MalformedCookieException getLocalizedMessage

List of usage examples for org.apache.http.cookie MalformedCookieException getLocalizedMessage

Introduction

In this page you can find the example usage for org.apache.http.cookie MalformedCookieException getLocalizedMessage.

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

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

@Override
public void addCookieFromHeader(CookieManager cookieManager, boolean checkCookies, String cookieHeader,
        URL url) {//from  w  ww.j  a  v  a 2 s  .  com
    boolean debugEnabled = log.isDebugEnabled();
    if (debugEnabled) {
        log.debug("Received Cookie: " + cookieHeader + " From: " + url.toExternalForm());
    }
    String protocol = url.getProtocol();
    String host = url.getHost();
    int port = HTTPSamplerBase.getDefaultPort(protocol, url.getPort());
    String path = url.getPath();
    boolean isSecure = HTTPSamplerBase.isSecure(protocol);

    List<org.apache.http.cookie.Cookie> cookies = null;

    CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, isSecure);
    BasicHeader basicHeader = new BasicHeader(HTTPConstants.HEADER_SET_COOKIE, cookieHeader);

    try {
        cookies = cookieSpec.parse(basicHeader, cookieOrigin);
    } catch (MalformedCookieException e) {
        log.error("Unable to add the cookie", e);
    }
    if (cookies == null) {
        return;
    }
    for (org.apache.http.cookie.Cookie cookie : cookies) {
        try {
            if (checkCookies) {
                cookieSpec.validate(cookie, cookieOrigin);
            }
            Date expiryDate = cookie.getExpiryDate();
            long exp = 0;
            if (expiryDate != null) {
                exp = expiryDate.getTime();
            }
            Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(),
                    cookie.getPath(), cookie.isSecure(), exp / 1000,
                    ((BasicClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR),
                    ((BasicClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR),
                    cookie.getVersion());

            // Store session cookies as well as unexpired ones
            if (exp == 0 || exp >= System.currentTimeMillis()) {
                cookieManager.add(newCookie); // Has its own debug log; removes matching cookies
            } else {
                cookieManager.removeMatchingCookies(newCookie);
                if (debugEnabled) {
                    log.info("Dropping expired Cookie: " + newCookie.toString());
                }
            }
        } catch (MalformedCookieException e) { // This means the cookie was wrong for the URL
            log.warn("Not storing invalid cookie: <" + cookieHeader + "> for URL " + url + " ("
                    + e.getLocalizedMessage() + ")");
        } catch (IllegalArgumentException e) {
            log.warn(cookieHeader + e.getLocalizedMessage());
        }
    }
}