Example usage for java.net CookieStore removeAll

List of usage examples for java.net CookieStore removeAll

Introduction

In this page you can find the example usage for java.net CookieStore removeAll.

Prototype

public boolean removeAll();

Source Link

Document

Remove all cookies in this cookie store.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);

    new URL("http://google.com").openConnection().getContent();
    CookieStore cookieStore = cm.getCookieStore();

    cookieStore.removeAll();
}

From source file:Main.java

/**
 * Clears the cookies in the given cookie handler. Cookies can only be cleared if the
 * cookieHandler is a CookieManager with a non-null CookieStore.
 *
 * @param cookieHandler the cookie handler where cookies should be cleared
 * @return true if cookies were cleared; false otherwise
 *///w  ww .  j  a  va2 s .com
public static boolean clearCookies(CookieHandler cookieHandler) {
    if (cookieHandler instanceof CookieManager) {
        CookieManager cookieManager = (CookieManager) cookieHandler;
        CookieStore cookieStore = cookieManager.getCookieStore();
        if (cookieStore != null) {
            cookieStore.removeAll();
            return true;
        }
    }
    return false;
}

From source file:net.qiujuer.common.okhttp.Http.java

public static void removeCookie() {
    CookieHandler handler = getClient().getCookieHandler();
    if (handler != null && handler instanceof CookieManager) {
        CookieManager manager = (CookieManager) handler;
        CookieStore store = manager.getCookieStore();
        if (store != null)
            store.removeAll();
    }//from w  ww. jav  a  2 s.  c  o  m
}

From source file:org.openhab.binding.amazonechocontrol.internal.Connection.java

private @Nullable Date tryRestoreSessionData(@Nullable String data, @Nullable String overloadedDomain) {
    // verify store data
    if (StringUtils.isEmpty(data)) {
        return null;
    }/*ww w  .  j  ava2s  .  com*/
    Scanner scanner = new Scanner(data);
    String version = scanner.nextLine();
    // check if serialize version
    if (!version.equals("5") && !version.equals("6")) {
        scanner.close();
        return null;
    }
    int intVersion = Integer.parseInt(version);

    frc = scanner.nextLine();
    serial = scanner.nextLine();
    deviceId = scanner.nextLine();

    // Recreate session and cookies
    refreshToken = scanner.nextLine();
    String domain = scanner.nextLine();
    if (overloadedDomain != null) {
        domain = overloadedDomain;
    }
    setAmazonSite(domain);

    deviceName = scanner.nextLine();

    if (intVersion > 5) {
        String accountCustomerId = scanner.nextLine();
        if (!StringUtils.equals(accountCustomerId, "null")) {
            this.accountCustomerId = accountCustomerId;
        }
    }

    Date loginTime = new Date(Long.parseLong(scanner.nextLine()));
    CookieStore cookieStore = cookieManager.getCookieStore();
    cookieStore.removeAll();

    Integer numberOfCookies = Integer.parseInt(scanner.nextLine());
    for (Integer i = 0; i < numberOfCookies; i++) {
        String name = readValue(scanner);
        String value = readValue(scanner);

        HttpCookie clientCookie = new HttpCookie(name, value);
        clientCookie.setComment(readValue(scanner));
        clientCookie.setCommentURL(readValue(scanner));
        clientCookie.setDomain(readValue(scanner));
        clientCookie.setMaxAge(Long.parseLong(readValue(scanner)));
        clientCookie.setPath(readValue(scanner));
        clientCookie.setPortlist(readValue(scanner));
        clientCookie.setVersion(Integer.parseInt(readValue(scanner)));
        clientCookie.setSecure(Boolean.parseBoolean(readValue(scanner)));
        clientCookie.setDiscard(Boolean.parseBoolean(readValue(scanner)));

        cookieStore.add(null, clientCookie);
    }
    scanner.close();
    try {
        checkRenewSession();

        if (StringUtils.isEmpty(this.accountCustomerId)) {
            List<Device> devices = this.getDeviceList();
            for (Device device : devices) {
                if (StringUtils.equals(device.serialNumber, this.serial)) {
                    this.accountCustomerId = device.deviceOwnerCustomerId;
                    break;
                }
            }
            if (StringUtils.isEmpty(this.accountCustomerId)) {
                for (Device device : devices) {
                    if (StringUtils.equals(device.accountName, "This Device")) {
                        this.accountCustomerId = device.deviceOwnerCustomerId;
                        String serial = device.serialNumber;
                        if (serial != null) {
                            this.serial = serial;
                        }
                        break;
                    }
                }
            }
        }
    } catch (URISyntaxException | IOException | ConnectionException e) {
        logger.debug("Getting account customer Id failed {}", e);
    }
    return loginTime;
}