Example usage for java.net HttpCookie setSecure

List of usage examples for java.net HttpCookie setSecure

Introduction

In this page you can find the example usage for java.net HttpCookie setSecure.

Prototype

public void setSecure(boolean flag) 

Source Link

Document

Indicates whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.

Usage

From source file:keywhiz.cli.JsonCookie.java

public static HttpCookie toHttpCookie(JsonCookie cookieContents) {
    HttpCookie cookie = new HttpCookie(cookieContents.name(), cookieContents.value());
    cookie.setDomain(cookieContents.domain());
    cookie.setPath(cookieContents.path());
    cookie.setSecure(cookieContents.isSecure());
    cookie.setHttpOnly(cookieContents.isHttpOnly());
    cookie.setVersion(1); // Always set version to 1 or important fields will be dropped
    return cookie;
}

From source file:Main.java

@Deprecated
// Deprecated because this uses org.apache.http, which is itself deprecated
public static HttpCookie servletCookieFromApacheCookie(org.apache.http.cookie.Cookie apacheCookie) {
    if (apacheCookie == null) {
        return null;
    }/*from   www .j ava2s .c om*/

    String name = apacheCookie.getName();
    String value = apacheCookie.getValue();

    HttpCookie cookie = new HttpCookie(name, value);

    value = apacheCookie.getDomain();
    if (value != null) {
        cookie.setDomain(value);
    }
    value = apacheCookie.getPath();
    if (value != null) {
        cookie.setPath(value);
    }
    cookie.setSecure(apacheCookie.isSecure());

    value = apacheCookie.getComment();
    if (value != null) {
        cookie.setComment(value);
    }

    // version
    cookie.setVersion(apacheCookie.getVersion());

    // From the Apache source code, maxAge is converted to expiry date using the following formula
    // if (maxAge >= 0) {
    //     setExpiryDate(new Date(System.currentTimeMillis() + maxAge * 1000L));
    // }
    // Reverse this to get the actual max age

    Date expiryDate = apacheCookie.getExpiryDate();
    if (expiryDate != null) {
        long maxAge = (expiryDate.getTime() - System.currentTimeMillis()) / 1000;
        // we have to lower down, no other option
        cookie.setMaxAge((int) maxAge);
    }

    // return the servlet cookie
    return cookie;
}

From source file:com.github.parisoft.resty.utils.CookieUtils.java

public static String toString(org.apache.http.cookie.Cookie... cookies) {
    if (isEmpty(cookies)) {
        return null;
    }/*from   w  ww  .j a v a2  s  .c  o m*/

    final HttpCookie[] httpCookies = new HttpCookie[cookies.length];

    for (int i = 0; i < cookies.length; i++) {
        final org.apache.http.cookie.Cookie srcCookie = cookies[i];
        final HttpCookie httpCookie = new HttpCookie(srcCookie.getName(), srcCookie.getValue());
        httpCookie.setComment(srcCookie.getComment());
        httpCookie.setDomain(srcCookie.getDomain());
        httpCookie.setPath(srcCookie.getPath());
        httpCookie.setSecure(srcCookie.isSecure());
        httpCookie.setVersion(srcCookie.getVersion());

        final Date now = new Date();

        if (srcCookie.isExpired(now)) {
            httpCookie.setMaxAge(0);
        } else {
            httpCookie.setMaxAge(
                    TimeUnit.MILLISECONDS.toSeconds(srcCookie.getExpiryDate().getTime() - now.getTime()));
        }

        httpCookies[i] = httpCookie;
    }

    return toString(httpCookies);
}

From source file:cn.ttyhuo.common.MyApplication.java

public static void getJavaCookieStore(java.net.CookieStore jCookieStore) {
    if (cookieStore == null)
        return;//from www  .  j  a v a2s.c  om

    for (Cookie h : cookieStore.getCookies()) {
        HttpCookie newCookie = new HttpCookie(h.getName(), h.getValue());
        newCookie.setVersion(h.getVersion());
        newCookie.setDomain(h.getDomain());
        newCookie.setPath(h.getPath());
        newCookie.setSecure(h.isSecure());
        newCookie.setComment(h.getComment());
        jCookieStore.add(URI.create("http://" + h.getDomain()), newCookie);
    }
}

From source file:interactivespaces.service.web.server.internal.netty.NettyHttpRequest.java

public static HttpCookie convertFromNettyCookie(Cookie cookie) {
    HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
    httpCookie.setComment(cookie.getComment());
    httpCookie.setDomain(cookie.getDomain());
    httpCookie.setMaxAge(cookie.getMaxAge());
    httpCookie.setPath(cookie.getPath());
    httpCookie.setPortlist(createPortString(cookie.getPorts()));
    httpCookie.setVersion(cookie.getVersion());
    httpCookie.setSecure(cookie.isSecure());
    httpCookie.setDiscard(cookie.isDiscard());

    return httpCookie;
}

From source file:at.becast.youploader.youtube.data.Cookie.java

public HttpCookie getCookie() {
    final HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);/* w  ww  . j  av  a 2s. c  o  m*/
    cookie.setCommentURL(commentUrl);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setMaxAge(maxAge);
    cookie.setSecure(secure);
    cookie.setVersion(version);
    return cookie;
}

From source file:org.nextlets.erc.defaults.http.ERCHttpInvokerImpl.java

private HttpCookie toHttpCookie(Cookie cookie) {
    HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
    httpCookie.setComment(cookie.getComment());
    httpCookie.setDomain(cookie.getDomain());
    if (cookie.getExpiryDate() != null) {
        httpCookie.setMaxAge(cookie.getExpiryDate().getTime() / 1000);
    }//from  w  ww .jav a2 s .c  o  m
    httpCookie.setPath(cookie.getPath());
    httpCookie.setSecure(cookie.isSecure());
    httpCookie.setVersion(cookie.getVersion());
    return httpCookie;
}

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;
    }//from ww w . ja v  a 2  s .  c om
    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;
}

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

private void exhangeToken() throws IOException, URISyntaxException {

    this.renewTime = 0;
    String cookiesJson = "{\"cookies\":{\"." + getAmazonSite() + "\":[]}}";
    String cookiesBase64 = Base64.getEncoder().encodeToString(cookiesJson.getBytes());

    String exchangePostData = "di.os.name=iOS&app_version=2.2.223830.0&domain=." + getAmazonSite()
            + "&source_token=" + URLEncoder.encode(this.refreshToken, "UTF8")
            + "&requested_token_type=auth_cookies&source_token_type=refresh_token&di.hw.version=iPhone&di.sdk.version=6.10.0&cookies="
            + cookiesBase64 + "&app_name=Amazon%20Alexa&di.os.version=11.4.1";

    HashMap<String, String> exchangeTokenHeader = new HashMap<String, String>();
    exchangeTokenHeader.put("Cookie", "");

    String exchangeTokenJson = makeRequestAndReturnString("POST",
            "https://www." + getAmazonSite() + "/ap/exchangetoken", exchangePostData, false,
            exchangeTokenHeader);/*w  w  w . j a  v a  2  s. com*/
    JsonExchangeTokenResponse exchangeTokenResponse = gson.fromJson(exchangeTokenJson,
            JsonExchangeTokenResponse.class);

    org.openhab.binding.amazonechocontrol.internal.jsons.JsonExchangeTokenResponse.Response response = exchangeTokenResponse.response;
    if (response != null) {
        org.openhab.binding.amazonechocontrol.internal.jsons.JsonExchangeTokenResponse.Tokens tokens = response.tokens;
        if (tokens != null) {
            @Nullable
            Map<String, Cookie[]> cookiesMap = tokens.cookies;
            if (cookiesMap != null) {
                for (String domain : cookiesMap.keySet()) {
                    Cookie[] cookies = cookiesMap.get(domain);
                    for (Cookie cookie : cookies) {
                        if (cookie != null) {
                            HttpCookie httpCookie = new HttpCookie(cookie.Name, cookie.Value);
                            httpCookie.setPath(cookie.Path);
                            httpCookie.setDomain(domain);
                            Boolean secure = cookie.Secure;
                            if (secure != null) {
                                httpCookie.setSecure(secure);
                            }
                            this.cookieManager.getCookieStore().add(null, httpCookie);
                        }
                    }
                }
            }
        }
    }
    if (!verifyLogin()) {
        throw new ConnectionException("Verify login failed after token exchange");
    }
    this.renewTime = (long) (System.currentTimeMillis() + Connection.expiresIn * 1000d / 0.8d); // start renew at
}