Example usage for javax.servlet.http Cookie getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the cookie.

Usage

From source file:org.apache.accumulo.monitor.servlets.BasicServlet.java

public static final String getCookieValue(HttpServletRequest req, String name) {
    if (req.getCookies() != null)
        for (Cookie c : req.getCookies())
            if (c.getName().equals(name))
                return c.getValue();
    return null;//from  w  ww .j a  v  a  2s. c  om
}

From source file:com.adobe.acs.commons.util.CookieUtil.java

/**
 * Get the named cookie from the HTTP Request
 *
 * @param request    Request to get the Cookie from
 * @param cookieName name of Cookie to get
 * @return the named Cookie, null if the named Cookie cannot be found
 *//*w  ww  . ja  v a  2s.com*/
public static Cookie getCookie(final HttpServletRequest request, final String cookieName) {
    if (StringUtils.isBlank(cookieName)) {
        return null;
    }

    final Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return null;
    }

    if (cookies.length > 0) {
        for (final Cookie cookie : cookies) {
            if (StringUtils.equals(cookieName, cookie.getName())) {
                return cookie;
            }
        }
    }

    return null;
}

From source file:de.arago.portlet.util.UserContainer.java

private static User getUserFromCookies(HttpServletRequest request) throws Exception {
    // https://www.everit.biz/web/guest/everit-blog/-/blogs/getting-current-liferay-user-in-a-standalone-webapp
    final Cookie[] cookies = request.getCookies() == null ? new Cookie[0] : request.getCookies();
    String userId = null;//w ww .  j a v a  2s . co  m
    String password = null;
    String companyId = null;

    for (Cookie c : cookies) {
        if ("COMPANY_ID".equals(c.getName())) {
            companyId = c.getValue();
        } else if ("ID".equals(c.getName())) {
            userId = hexStringToStringByAscii(c.getValue());
        } else if ("PASSWORD".equals(c.getName())) {
            password = hexStringToStringByAscii(c.getValue());
        }
    }

    if (userId != null && password != null && companyId != null) {
        final KeyValuePair kvp = UserLocalServiceUtil.decryptUserId(Long.parseLong(companyId), userId,
                password);
        return getUser(kvp.getKey());
    }

    return null;
}

From source file:com.activecq.api.utils.CookieUtil.java

/**
 * Gets Cookies from the Request whose's names match the provides Regex
 *
 * @param request Request to get the Cookie from
 * @param regex Regex to match against Cookie names
 * @return Cookies which match the Regex
 *///from w  w w  .j  a va 2 s  .c  o m
public static List<Cookie> getCookies(HttpServletRequest request, String regex) {
    ArrayList<Cookie> foundCookies = new ArrayList<Cookie>();
    regex = StringUtils.trimToNull(regex);
    if (regex == null) {
        return foundCookies;
    }

    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return null;
    }

    for (Cookie cookie : cookies) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(cookie.getName());
        if (m.matches()) {
            foundCookies.add(cookie);
        }
    }

    return foundCookies;
}

From source file:com.google.gerrit.httpd.ProjectOAuthFilter.java

private static Cookie findGitCookie(HttpServletRequest req) {
    Cookie[] cookies = req.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().startsWith(GIT_COOKIE_PREFIX)) {
                return cookie;
            }/*  www. j a  v  a2s  . c o m*/
        }
    }
    return null;
}

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

static com.gargoylesoftware.htmlunit.util.Cookie createCookie(Cookie cookie) {
    Date expires = null;/*  w  ww.j  a  v a2 s .c om*/
    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:com.mhe.imagebanksearch.controller.LoginController.java

/**
 * <p>//from  w  w w  .j av  a2  s . com
 * API to get cookie value.
 * </p>
 * @param cookies
 * @param cookieName
 * @param defaultValue
 * @return
 */
public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        if (cookieName.equals(cookie.getName())) {
            return (cookie.getValue());
        }
    }
    return (defaultValue);
}

From source file:com.iterzp.momo.utils.WebUtils.java

/**
 * ?cookie//w w w  .  j a  va  2s  . c  om
 * 
 * @param request
 *            HttpServletRequest
 * @param name
 *            cookie??
 * @return ?null
 */
public static String getCookie(HttpServletRequest request, String name) {
    Assert.notNull(request);
    Assert.hasText(name);
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        try {
            name = URLEncoder.encode(name, "UTF-8");
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return URLDecoder.decode(cookie.getValue(), "UTF-8");
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:gov.nih.nci.cabig.caaers.web.utils.WebUtils.java

public static String getBuildInfoCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null)
        return null;
    for (Cookie c : cookies) {
        if (c.getName().equals(BUILD_INFO_COOKIE))
            return c.getValue();
    }//  ww  w .j av a  2 s. c o m
    return null;
}

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  .ja  va  2s  .  c om
 * @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());
}