Example usage for javax.servlet.http Cookie getDomain

List of usage examples for javax.servlet.http Cookie getDomain

Introduction

In this page you can find the example usage for javax.servlet.http Cookie getDomain.

Prototype

public String getDomain() 

Source Link

Document

Gets the domain name of this Cookie.

Usage

From source file:io.syndesis.credential.CredentialFlowStateHelper.java

static javax.ws.rs.core.Cookie toJaxRsCookie(final Cookie cookie) {
    return new javax.ws.rs.core.Cookie(cookie.getName(), cookie.getValue(), cookie.getPath(),
            cookie.getDomain());
}

From source file:com.usefullc.platform.common.utils.CookieUtils.java

/**
 * cookie //from  ww w .j  a  va2 s .  co m
 * 
 * @param request
 * @param name
 * @param domain
 * @return
 */
public static Cookie getCookie(HttpServletRequest request, String name, String domain) {
    Cookie cookies[] = request.getCookies();
    if (cookies == null) {
        return null;
    }
    for (Cookie cookie : cookies) {
        String cookieName = cookie.getName();
        String cookieDomain = cookie.getDomain();
        if (!cookieName.equals(name)) {
            continue;
        }
        if (StringUtils.isNotEmpty(domain) && !cookieDomain.equals(domain)) {
            continue;
        }
        return cookie;
    }
    return null;
}

From source file:com.hortonworks.example.util.Util.java

public static String getCookieInfo(HttpServletRequest request, String name) {
    String info = null;/*from  www . ja  v a  2s.co m*/
    Cookie cookie = getCookie(request, name);
    if (cookie != null) {
        info = String.format("[value=%s,domain=%s,path=%s,expiry=%d]", cookie.getValue(), cookie.getDomain(),
                cookie.getPath(), cookie.getMaxAge());
    }
    return info;
}

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

/**
 * Transforms Servlet cookies into Apache Cookies
 * /*from   w  w  w .  j a va  2  s .  co  m*/
 * @param servletCookie servlet cookie
 * 
 * @return apache cookie
 */
public static org.apache.commons.httpclient.Cookie transformServletCookie(
        javax.servlet.http.Cookie servletCookie) {

    org.apache.commons.httpclient.Cookie newCookie = null;

    if (servletCookie != null) {
        newCookie = new org.apache.commons.httpclient.Cookie(servletCookie.getDomain(), servletCookie.getName(),
                servletCookie.getValue(), servletCookie.getPath() != null ? servletCookie.getPath() : "/",
                servletCookie.getMaxAge(), servletCookie.getSecure());
    }
    return newCookie;
}

From source file:net.bluehornreader.web.WebUtils.java

public static String cookieAsString(Cookie cookie) {
    StringBuilder bld = new StringBuilder();
    bld.append("Name=").append(cookie.getName()).append(" ");
    bld.append("Value=").append(cookie.getValue()).append(" ");
    bld.append("Domain=").append(cookie.getDomain()).append(" ");
    bld.append("MaxAge=").append(cookie.getMaxAge()).append(" ");
    bld.append("Path=").append(cookie.getPath()).append(" ");
    bld.append("Secure=").append(cookie.getSecure()).append(" ");
    bld.append("Comment=").append(cookie.getComment()).append(" ");
    bld.append("Version=").append(cookie.getVersion()).append(" ");
    return bld.toString().trim();
}

From source file:org.springframework.test.web.servlet.htmlunit.MockWebResponseBuilder.java

static com.gargoylesoftware.htmlunit.util.Cookie createCookie(Cookie cookie) {
    Date expires = null;//from   w  ww  .j a v  a2 s .  co  m
    if (cookie.getMaxAge() > -1) {
        expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000);
    }
    BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue());
    result.setDomain(cookie.getDomain());
    result.setComment(cookie.getComment());
    result.setExpiryDate(expires);
    result.setPath(cookie.getPath());
    result.setSecure(cookie.getSecure());
    if (cookie.isHttpOnly()) {
        result.setAttribute("httponly", "true");
    }
    return new com.gargoylesoftware.htmlunit.util.Cookie(result);
}

From source file:architecture.ee.web.util.CookieUtils.java

public static Cookie getCookie(HttpServletRequest request, String name) {
    if (null == request || null == name)
        return null;
    Cookie cookies[];/* w w  w  .  j a v  a 2  s.c o m*/
    cookies = request.getCookies();
    if (cookies == null || name == null || name.length() == 0)
        return null;
    try {
        Cookie cookie = null;
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i] == null || cookies[i].getName() == null || !cookies[i].getName().equals(name))
                continue;
            cookie = cookies[i];
            if (request.getServerName() != null && request.getServerName().equals(cookie.getDomain()))
                break;
        }

        return cookie;
    } catch (NullPointerException e) {
        log.debug("NPE retrieving cookies from request, returning null", e);
    }
    return null;
}

From source file:com.aurel.track.master.ModuleBL.java

public static Cookie sendPOSTRequest(String urlString) {
    Cookie responseCookie = null;/*from   www .ja  va2s  .c  o m*/
    try {
        HttpClient httpclient = new DefaultHttpClient();//HttpClients.createDefault();
        HttpPost httppost = new HttpPost(urlString);
        // Request parameters and other properties.
        //Execute and get the response.
        HttpContext localContext = new BasicHttpContext();
        CookieStore cookieStore = new BasicCookieStore();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        HttpResponse response = httpclient.execute(httppost, localContext);

        if (cookieStore.getCookies().size() > 0) {
            List<org.apache.http.cookie.Cookie> cookies = cookieStore.getCookies();
            for (org.apache.http.cookie.Cookie cookie : cookies) {
                if (cookie.getName().equals("JSESSIONID")) {
                    responseCookie = new Cookie(cookie.getName(), cookie.getValue());
                    responseCookie.setPath(cookie.getPath());
                    responseCookie.setDomain(cookie.getDomain());
                }
            }
        }
        if (response.getEntity() != null) {
            response.getEntity().consumeContent();
        }

    } catch (Exception ex) {
        LOGGER.debug(ExceptionUtils.getStackTrace(ex));
    }
    return responseCookie;
}

From source file:de.betterform.agent.web.WebUtil.java

private static Vector<BasicClientCookie> saveAsBasicClientCookie(Iterator iterator,
        Vector<BasicClientCookie> commonsCookies) {
    while (iterator.hasNext()) {
        javax.servlet.http.Cookie c = (Cookie) iterator.next();
        BasicClientCookie commonsCookie = new BasicClientCookie(c.getName(), c.getValue());
        commonsCookie.setDomain(c.getDomain());
        commonsCookie.setPath(c.getPath());
        commonsCookie.setAttribute(ClientCookie.MAX_AGE_ATTR, Integer.toString(c.getMaxAge()));
        commonsCookie.setSecure(c.getSecure());

        commonsCookies.add(commonsCookie);

        if (WebUtil.LOGGER.isDebugEnabled()) {
            WebUtil.LOGGER.debug("adding cookie >>>>>");
            WebUtil.LOGGER.debug("name: " + c.getName());
            WebUtil.LOGGER.debug("value: " + c.getValue());
            WebUtil.LOGGER.debug("path: " + c.getPath());
            WebUtil.LOGGER.debug("maxAge: " + c.getMaxAge());
            WebUtil.LOGGER.debug("secure: " + c.getSecure());
            WebUtil.LOGGER.debug("adding cookie done <<<<<");
        }/*from  w w  w . j  a v  a 2  s.  c o  m*/
    }

    return commonsCookies;
}

From source file:RequestUtil.java

/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the
 * value for a <code>Set-Cookie</code> header.
 * //from w  w  w  .  j  a  v  a2s .c o  m
 * @param cookie
 *            The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

    StringBuffer buf = new StringBuffer(cookie.getName());
    buf.append("=");
    buf.append(cookie.getValue());

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

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

    if (cookie.getMaxAge() >= 0) {
        buf.append("; Max-Age=\"");
        buf.append(cookie.getMaxAge());
        buf.append("\"");
    }

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

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

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

    return (buf.toString());
}