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

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

Introduction

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

Prototype

public Date getExpiryDate() 

Source Link

Usage

From source file:GetCookiePrintAndSetValue.java

public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    GetMethod method = new GetMethod("http://localhost:8080/");
    try {/*  ww w. j  a v  a  2s  .c  om*/
        client.executeMethod(method);
        Cookie[] cookies = client.getState().getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            System.err.println("Cookie: " + cookie.getName() + ", Value: " + cookie.getValue()
                    + ", IsPersistent?: " + cookie.isPersistent() + ", Expiry Date: " + cookie.getExpiryDate()
                    + ", Comment: " + cookie.getComment());

            cookie.setValue("My own value");
        }
        client.executeMethod(method);
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.twinsoft.convertigo.engine.util.CookiesUtils.java

public static String formatCookie(Cookie cookie) {
    StringBuffer buf = new StringBuffer();
    Date d = cookie.getExpiryDate();
    String[][] datas = {/*from  w  w  w  .j av a  2 s  . c om*/
            // {"$Version",Integer.toString(cookie.getVersion())},
            { cookie.getName(), cookie.getValue() }, { "$Domain", cookie.getDomain() },
            { "$Path", cookie.getPath() }, { "$Secure", Boolean.toString(cookie.getSecure()) },
            { "$Date", d == null ? "null" : DateFormat.getDateTimeInstance().format(d) } };
    buf.append(datas[0][0] + "=" + datas[0][1]);
    for (int i = 1; i < datas.length; i++) {
        if (datas[i][1] != null)
            buf.append("; " + datas[i][0] + "=" + datas[i][1]);
    }
    return buf.toString();
}

From source file:com.google.gsa.valve.modules.utils.CookieManagement.java

/**
 * Transforms Apache cookies into Servlet Cookies
 * /*from  www.ja v  a  2  s.co  m*/
 * @param apacheCookie apache cookie 
 * 
 * @return servlet cookie
 */
public static javax.servlet.http.Cookie transformApacheCookie(
        org.apache.commons.httpclient.Cookie apacheCookie) {

    javax.servlet.http.Cookie newCookie = null;

    if (apacheCookie != null) {
        Date expire = apacheCookie.getExpiryDate();
        int maxAge = -1;

        if (expire == null) {
            maxAge = -1;
        } else {
            Date now = Calendar.getInstance().getTime();
            // Convert milli-second to second
            Long second = new Long((expire.getTime() - now.getTime()) / 1000);
            maxAge = second.intValue();
        }

        newCookie = new javax.servlet.http.Cookie(apacheCookie.getName(), apacheCookie.getValue());
        //Hardcoding the domain
        newCookie.setDomain(apacheCookie.getDomain());
        newCookie.setPath(apacheCookie.getPath());
        newCookie.setMaxAge(maxAge);
        newCookie.setSecure(apacheCookie.getSecure());
    }
    return newCookie;
}

From source file:com.celamanzi.liferay.portlets.rails286.OnlineClient.java

protected void debugCookies(Cookie[] cookies) {
    log.debug("Cookie inspector found " + cookies.length + " cookies ------v");
    for (Cookie cookie : cookies)
        log.debug(cookie.toString() + ", domain=" + cookie.getDomain() + ", path=" + cookie.getPath()
                + ", max-age=" + cookie.getExpiryDate() + ", secure=" + cookie.getSecure());
    log.debug("----------------------------");
}

From source file:com.owncloud.android.lib.common.OwnCloudClient.java

@SuppressWarnings("unused")
private void logCookie(Cookie cookie) {
    Log_OC.d(TAG, "Cookie name: " + cookie.getName());
    Log_OC.d(TAG, "       value: " + cookie.getValue());
    Log_OC.d(TAG, "       domain: " + cookie.getDomain());
    Log_OC.d(TAG, "       path: " + cookie.getPath());
    Log_OC.d(TAG, "       version: " + cookie.getVersion());
    Log_OC.d(TAG, "       expiryDate: "
            + (cookie.getExpiryDate() != null ? cookie.getExpiryDate().toString() : "--"));
    Log_OC.d(TAG, "       comment: " + cookie.getComment());
    Log_OC.d(TAG, "       secure: " + cookie.getSecure());
}

From source file:com.cerema.cloud2.lib.common.OwnCloudClient.java

private void logCookie(Cookie cookie) {
    Log_OC.d(TAG, "Cookie name: " + cookie.getName());
    Log_OC.d(TAG, "       value: " + cookie.getValue());
    Log_OC.d(TAG, "       domain: " + cookie.getDomain());
    Log_OC.d(TAG, "       path: " + cookie.getPath());
    Log_OC.d(TAG, "       version: " + cookie.getVersion());
    Log_OC.d(TAG, "       expiryDate: "
            + (cookie.getExpiryDate() != null ? cookie.getExpiryDate().toString() : "--"));
    Log_OC.d(TAG, "       comment: " + cookie.getComment());
    Log_OC.d(TAG, "       secure: " + cookie.getSecure());
}

From source file:com.twinsoft.convertigo.engine.Context.java

public Vector<String> getCookieStrings() {
    // Use the HandleCookie Property of the transaction to return or not the cookies.
    ////from  ww w .  j a  v  a 2  s  .  co  m
    // We noticed a Bug in tomcat when too much cookies where set in the response to the client. This causes a 
    // IndexOutOfBoundException:  4096 in coyote.
    // To overcome this situation, now you can configure HandleCookies to false in the transaction to prevent cookies to be reflected
    // to the client.
    if (requestedObject instanceof AbstractHttpTransaction) {
        if (!((AbstractHttpTransaction) requestedObject).isHandleCookie())
            return new Vector<String>();
    }

    Vector<String> cookies = new Vector<String>();
    if (httpState != null) {
        Cookie[] httpCookies = httpState.getCookies();
        int len = httpCookies.length;
        Cookie cookie = null;
        String sCookie;

        DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        for (int i = 0; i < len; i++) {
            cookie = httpCookies[i];
            sCookie = cookie.getName() + "=" + cookie.getValue() + ";";
            sCookie += (cookie.getExpiryDate() != null) ? "expires=" + df.format(cookie.getExpiryDate()) + ";"
                    : "";
            sCookie += "path=" + cookie.getPath() + ";";
            sCookie += "domain=" + cookie.getDomain() + ";";
            sCookie += cookie.getSecure() ? "secure" : "";
            cookies.add(sCookie);
        }
    }
    return cookies;
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.DocumentTest.java

private void checkCookie(final Cookie cookie, final String name, final String value, final String path,
        final String domain, final boolean secure, final Date date) {
    assertEquals(name, cookie.getName());
    assertEquals(value, cookie.getValue());
    assertNull(cookie.getComment());//w w w. j av a  2  s.c o m
    assertEquals(path, cookie.getPath());
    assertEquals(domain, cookie.getDomain());
    assertEquals(secure, cookie.getSecure());
    assertEquals(date, cookie.getExpiryDate());
}

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

public static void saveCookies(String saveCookiesFile, Map<String, Cookie> cookies) {
    // Do nothing if cookiesFile is not specified. 
    if (saveCookiesFile == null || saveCookiesFile.length() <= 0) {
        return;/*from  www.  ja v a2  s .c  o  m*/
    }

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(new File(saveCookiesFile));
        String tab = "\t";
        out.write("# Heritrix Cookie File\n".getBytes());
        out.write("# This file is the Netscape cookies.txt format\n\n".getBytes());
        for (Cookie cookie : cookies.values()) {
            // Guess an initial size 
            MutableString line = new MutableString(1024 * 2);
            line.append(cookie.getDomain());
            line.append(tab);
            line.append(cookie.isDomainAttributeSpecified() ? "TRUE" : "FALSE");
            line.append(tab);
            line.append(cookie.getPath());
            line.append(tab);
            line.append(cookie.getSecure() ? "TRUE" : "FALSE");
            line.append(tab);
            line.append(cookie.getExpiryDate() != null ? cookie.getExpiryDate().getTime() / 1000 : -1);
            line.append(tab);
            line.append(cookie.getName());
            line.append(tab);
            line.append(cookie.getValue() != null ? cookie.getValue() : "");
            line.append("\n");
            out.write(line.toString().getBytes());
        }
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Unable to write " + saveCookiesFile, e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:org.mozilla.zest.core.v1.ZestRequest.java

@Override
public ZestRequest deepCopy() {
    ZestRequest zr = new ZestRequest(this.getIndex());
    zr.setUrl(this.url);
    zr.setUrlToken(this.urlToken);
    zr.setData(this.data);
    zr.setMethod(this.method);
    zr.setHeaders(this.headers);
    zr.setFollowRedirects(this.followRedirects);

    if (this.getResponse() != null) {
        zr.setResponse(this.getResponse().deepCopy());
    }/* w w w.j  a v  a2  s .  c o m*/
    for (ZestAssertion zt : this.getAssertions()) {
        zr.addAssertion((ZestAssertion) zt.deepCopy());
    }

    for (Cookie cookie : this.cookies) {
        zr.addCookie(new Cookie(cookie.getDomain(), cookie.getName(), cookie.getValue(), cookie.getPath(),
                cookie.getExpiryDate(), cookie.getSecure()));
    }
    zr.cookies = this.cookies;
    zr.setEnabled(this.isEnabled());

    return zr;
}