Example usage for java.net HttpCookie hasExpired

List of usage examples for java.net HttpCookie hasExpired

Introduction

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

Prototype

public boolean hasExpired() 

Source Link

Document

Reports whether this HTTP cookie has expired or not.

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();

    List<HttpCookie> cookies = cm.getCookieStore().getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie.hasExpired());
        System.out.println();/*from  w  w w.  j  a  v  a2  s  .  c o  m*/
    }
}

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

public static String toString(HttpCookie... cookies) {
    if (isEmpty(cookies)) {
        return null;
    }/*from   w  ww .  j a  v a 2 s. c  om*/

    final StringBuilder cookieBuilder = new StringBuilder();

    for (HttpCookie cookie : cookies) {
        cookieBuilder.append(cookie.getName()).append("=").append(cookie.getValue());

        if (cookie.getComment() != null) {
            cookieBuilder.append(";").append("Comment=").append(cookie.getComment());
        }

        if (cookie.getDomain() != null) {
            cookieBuilder.append(";").append("Domain=").append(cookie.getDomain());
        }

        if (cookie.getPath() != null) {
            cookieBuilder.append(";").append("Path=").append(cookie.getPath());
        }

        if (cookie.getSecure()) {
            cookieBuilder.append(";").append("Secure");
        }

        if (cookie.isHttpOnly()) {
            cookieBuilder.append(";").append("HttpOnly");
        }

        if (cookie.getVersion() > 0) {
            cookieBuilder.append(";").append("Version=").append(cookie.getVersion());
        }

        if (cookie.hasExpired()) {
            cookieBuilder.append(";").append("Max-Age=0");
        } else {
            cookieBuilder.append(";").append("Max-Age=").append(cookie.getMaxAge());
        }

        cookieBuilder.append(", ");
    }

    return cookieBuilder.deleteCharAt(cookieBuilder.length() - 1).toString();
}

From source file:com.tremolosecurity.proxy.filter.PostProcess.java

protected void postProcess(HttpFilterRequest req, HttpFilterResponse resp, UrlHolder holder,
        HttpResponse response, String finalURL, HttpFilterChain curChain, HttpRequestBase httpRequest)
        throws IOException, Exception {
    boolean isText;
    HttpEntity entity = null;/*from  w  ww  .j  a v  a  2  s  .c  o m*/

    try {
        entity = response.getEntity();
        /*if (entity != null) {
            entity = new BufferedHttpEntity(entity);
        }*/
    } catch (Throwable t) {
        throw new Exception(t);
    }

    InputStream ins = null;
    boolean entExists = false;

    if (entity == null) {
        resp.setStatus(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
        ins = new StringBufferInputStream("");
    } else {
        try {
            ins = entity.getContent();
            resp.setStatus(response.getStatusLine().getStatusCode(),
                    response.getStatusLine().getReasonPhrase());
            entExists = true;
        } catch (IllegalStateException e) {
            //do nothing
        }
    }

    if (entExists) {
        org.apache.http.Header hdr = response.getFirstHeader("Content-Type");
        org.apache.http.Header encoding = response.getFirstHeader("Content-Encoding");

        /*if (hdr == null) {
           isText = false;
        } else {
           isText = response.getFirstHeader("Content-Type").getValue().startsWith("text");
                   
           if (encoding != null ) {
              isText = (! encoding.getValue().startsWith("gzip")) && (! encoding.getValue().startsWith("deflate"));
           }
                   
           if (isText) {
              resp.setContentType(response.getFirstHeader("Content-Type").getValue());
              resp.setLocale(response.getLocale());
           }
        }*/
        isText = false;

        try {
            resp.setCharacterEncoding(null);
        } catch (Throwable t) {
            //we're not doing anything
        }

        StringBuffer stmp = new StringBuffer();
        if (response.getFirstHeader("Content-Type") != null) {
            resp.setContentType(response.getFirstHeader("Content-Type").getValue());
        }

        if (response.getLocale() != null) {
            resp.setLocale(response.getLocale());
        }

        org.apache.http.Header[] headers = response.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            org.apache.http.Header header = headers[i];
            if (header.getName().equals("Content-Type")) {

                continue;
            } else if (header.getName().equals("Content-Type")) {

                continue;
            } else if (header.getName().equals("Content-Length")) {
                if (!header.getValue().equals("0")) {
                    continue;
                }
            } else if (header.getName().equals("Transfer-Encoding")) {
                continue;
            } else if (header.getName().equalsIgnoreCase("set-cookie")
                    || header.getName().equalsIgnoreCase("set-cookie2")) {
                //System.out.println(header.getValue());
                String cookieVal = header.getValue();
                /*if (cookieVal.endsWith("HttpOnly")) {
                   cookieVal = cookieVal.substring(0,cookieVal.indexOf("HttpOnly"));
                }
                        
                //System.out.println(cookieVal);*/

                List<HttpCookie> cookies = HttpCookie.parse(cookieVal);
                Iterator<HttpCookie> it = cookies.iterator();
                while (it.hasNext()) {
                    HttpCookie cookie = it.next();
                    String cookieFinalName = cookie.getName();
                    if (cookieFinalName.equalsIgnoreCase("JSESSIONID")) {
                        stmp.setLength(0);
                        stmp.append("JSESSIONID").append('-')
                                .append(holder.getApp().getName().replaceAll(" ", "|"));
                        cookieFinalName = stmp.toString();
                    }
                    Cookie respcookie = new Cookie(cookieFinalName, cookie.getValue());
                    respcookie.setComment(cookie.getComment());
                    if (cookie.getDomain() != null) {
                        respcookie.setDomain(cookie.getDomain());
                    }

                    if (cookie.hasExpired()) {
                        respcookie.setMaxAge(0);
                    } else {
                        respcookie.setMaxAge((int) cookie.getMaxAge());
                    }
                    respcookie.setPath(cookie.getPath());

                    respcookie.setSecure(cookie.getSecure());
                    respcookie.setVersion(cookie.getVersion());
                    resp.addCookie(respcookie);
                }
            } else if (header.getName().equals("Location")) {

                if (holder.isOverrideHost()) {
                    fixRedirect(req, resp, finalURL, header);
                } else {
                    resp.addHeader("Location", header.getValue());
                }
            } else {
                resp.addHeader(header.getName(), header.getValue());
            }

        }

        curChain.setIns(ins);
        curChain.setText(isText);
        curChain.setEntity(entity);
        curChain.setHttpRequestBase(httpRequest);

        //procData(req, resp, holder, isText, entity, ins);

    } else {
        isText = false;
    }
}

From source file:org.jwebsocket.util.Tools.java

/**
 * Indicates if a cookie is valid for a given URI
 *
 * @param aURI/*from  w  ww . java  2s  .co  m*/
 * @param aCookie
 * @return TRUE if the cookie is valid, FALSE otherwise
 */
public static boolean isCookieValid(URI aURI, HttpCookie aCookie) {
    return !aCookie.hasExpired()
            && (null == aCookie.getDomain() || HttpCookie.domainMatches(aCookie.getDomain(), aURI.getHost()))
            && (null == aCookie.getPath()
                    || (null != aURI.getPath() && aURI.getPath().startsWith(aCookie.getPath())))
            && (aCookie.getSecure() == (aURI.getScheme().equals("wss")));
}