Example usage for org.apache.commons.httpclient Cookie setDomainAttributeSpecified

List of usage examples for org.apache.commons.httpclient Cookie setDomainAttributeSpecified

Introduction

In this page you can find the example usage for org.apache.commons.httpclient Cookie setDomainAttributeSpecified.

Prototype

public void setDomainAttributeSpecified(boolean paramBoolean) 

Source Link

Usage

From source file:com.hp.alm.ali.rest.client.AliRestClient.java

private void addTenantCookie(Cookie ssoCookie) {
    if (ssoCookie != null) {
        Cookie tenant_id_cookie = new Cookie(ssoCookie.getDomain(), "TENANT_ID_COOKIE", "0");
        tenant_id_cookie.setDomainAttributeSpecified(true);
        tenant_id_cookie.setPath(ssoCookie.getPath());
        tenant_id_cookie.setPathAttributeSpecified(true);
        httpClient.getState().addCookie(tenant_id_cookie);
    }/*from  w  ww . j a va2  s . c o m*/
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

/**
 * Load cookies from a file before the first fetch.
 * <p>//  w  ww .  java 2s  . c om
 * The file is a text file in the Netscape's 'cookies.txt' file format.<br>
 * Example entry of cookies.txt file:<br>
 * <br>
 * www.archive.org FALSE / FALSE 1074567117 details-visit texts-cralond<br>
 * <br>
 * Each line has 7 tab-separated fields:<br>
 * <li>1. DOMAIN: The domain that created and have access to the cookie
 * value.
 * <li>2. FLAG: A TRUE or FALSE value indicating if hosts within the given
 * domain can access the cookie value.
 * <li>3. PATH: The path within the domain that the cookie value is valid
 * for.
 * <li>4. SECURE: A TRUE or FALSE value indicating if to use a secure
 * connection to access the cookie value.
 * <li>5. EXPIRATION: The expiration time of the cookie value (unix style.)
 * <li>6. NAME: The name of the cookie value
 * <li>7. VALUE: The cookie value
 *
 * @param cookiesFile file in the Netscape's 'cookies.txt' format.
 */
public void loadCookies(String cookiesFile) {
    // Do nothing if cookiesFile is not specified.
    if (cookiesFile == null || cookiesFile.length() <= 0) {
        return;
    }
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(cookiesFile, "r");
        String[] cookieParts;
        String line;
        Cookie cookie = null;
        while ((line = raf.readLine()) != null) {
            // Line that starts with # is commented line, therefore skip it.
            if (!line.startsWith("#")) {
                cookieParts = line.split("\\t");
                if (cookieParts.length == 7) {
                    // Create cookie with not expiration date (-1 value).
                    // TODO: add this as an option.
                    cookie = new Cookie(cookieParts[0], cookieParts[5], cookieParts[6], cookieParts[2], -1,
                            Boolean.valueOf(cookieParts[3]).booleanValue());

                    if (cookieParts[1].toLowerCase().equals("true")) {
                        cookie.setDomainAttributeSpecified(true);
                    } else {
                        cookie.setDomainAttributeSpecified(false);
                    }
                    this.http.getState().addCookie(cookie);
                    logger.fine("Adding cookie: " + cookie.toExternalForm());
                }
            }
        }
    } catch (FileNotFoundException e) {
        // We should probably throw FatalConfigurationException.
        System.out.println("Could not find file: " + cookiesFile + " (Element: " + ATTR_LOAD_COOKIES + ")");

    } catch (IOException e) {
        // We should probably throw FatalConfigurationException.
        e.printStackTrace();
    } finally {
        try {
            if (raf != null) {
                raf.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * Load cookies from a file before the first fetch.
 * <p>//  w  w  w.  j  ava  2  s . c om
 * The file is a text file in the Netscape's 'cookies.txt' file format.<br>
 * Example entry of cookies.txt file:<br>
 * <br>
 * www.archive.org FALSE / FALSE 1074567117 details-visit texts-cralond<br>
 * <br>
 * Each line has 7 tab-separated fields:<br>
 * <li>1. DOMAIN: The domain that created and have access to the cookie
 * value.
 * <li>2. FLAG: A TRUE or FALSE value indicating if hosts within the given
 * domain can access the cookie value.
 * <li>3. PATH: The path within the domain that the cookie value is valid
 * for.
 * <li>4. SECURE: A TRUE or FALSE value indicating if to use a secure
 * connection to access the cookie value.
 * <li>5. EXPIRATION: The expiration time of the cookie value (unix style.)
 * <li>6. NAME: The name of the cookie value
 * <li>7. VALUE: The cookie value
 *
 * @param cookiesFile file in the Netscape's 'cookies.txt' format.
 */
public void loadCookies(String cookiesFile) {
    // Do nothing if cookiesFile is not specified.
    if (cookiesFile == null || cookiesFile.length() <= 0) {
        return;
    }
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(cookiesFile, "r");
        String[] cookieParts;
        String line;
        Cookie cookie = null;
        while ((line = raf.readLine()) != null) {
            // Line that starts with # is commented line, therefore skip it.
            if (!line.startsWith("#")) {
                cookieParts = line.split("\\t");
                if (cookieParts.length == 7) {
                    // Create cookie with not expiration date (-1 value).
                    // TODO: add this as an option.
                    cookie = new Cookie(cookieParts[0], cookieParts[5], cookieParts[6], cookieParts[2], -1,
                            Boolean.valueOf(cookieParts[3]).booleanValue());

                    if (cookieParts[1].toLowerCase().equals("true")) {
                        cookie.setDomainAttributeSpecified(true);
                    } else {
                        cookie.setDomainAttributeSpecified(false);
                    }
                    HttpClient http = this.getClient();
                    http.getState().addCookie(cookie);
                    logger.debug("Adding cookie: " + cookie.toExternalForm());
                }
            }
        }
    } catch (FileNotFoundException e) {
        // We should probably throw FatalConfigurationException.
        System.out.println("Could not find file: " + cookiesFile + " (Element: " + ATTR_LOAD_COOKIES + ")");

    } catch (IOException e) {
        // We should probably throw FatalConfigurationException.
        e.printStackTrace();
    } finally {
        try {
            if (raf != null) {
                raf.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.archive.modules.fetcher.AbstractCookieStorage.java

/**
 * Load cookies. The input is text in the Netscape's 'cookies.txt' file
 * format. Example entry of cookies.txt file:
 * <p>// w w  w  .  j  a v  a2 s .c o  m
 * www.archive.org FALSE / FALSE 1311699995 details-visit texts-cralond
 * </p>
 * <p>
 * Each line has 7 tab-separated fields:
 * </p>
 * <ol>
 * <li>DOMAIN: The domain that created and have access to the cookie value.</li>
 * <li>FLAG: A TRUE or FALSE value indicating if hosts within the given
 * domain can access the cookie value.</li>
 * <li>PATH: The path within the domain that the cookie value is valid for.</li>
 * <li>SECURE: A TRUE or FALSE value indicating if to use a secure
 * connection to access the cookie value.</li>
 * <li>EXPIRATION: The expiration time of the cookie value, or -1 for no
 * expiration</li>
 * <li>NAME: The name of the cookie value</li>
 * <li>VALUE: The cookie value</li>
 * </ol>
 * 
 * @param reader
 *            input
 * @param cookiesFile
 *            file in the Netscape's 'cookies.txt' format.
 */
public static void loadCookies(Reader reader, SortedMap<String, Cookie> cookies) {
    BufferedReader br = new BufferedReader(reader);
    try {
        String line;
        int lineNo = 1;
        while ((line = br.readLine()) != null) {
            if (!line.matches("\\s*(?:#.*)?")) { // skip blank links and comments
                String[] tokens = line.split("\\t");
                if (tokens.length == 7) {
                    long epochSeconds = Long.parseLong(tokens[4]);
                    Date expirationDate = (epochSeconds >= 0 ? new Date(epochSeconds * 1000) : null);
                    Cookie cookie = new Cookie(tokens[0], tokens[5], tokens[6], tokens[2], expirationDate,
                            Boolean.valueOf(tokens[3]).booleanValue());
                    cookie.setDomainAttributeSpecified(Boolean.valueOf(tokens[1]).booleanValue());

                    LOGGER.fine("Adding cookie: domain " + cookie.getDomain() + " cookie "
                            + cookie.toExternalForm());
                    cookies.put(cookie.getSortKey(), cookie);
                } else {
                    LOGGER.warning(
                            "cookies input line " + lineNo + " invalid, expected 7 tab-delimited tokens");
                }
            }

            lineNo++;
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, e.getMessage(), e);
    }
}