Example usage for javax.servlet.http HttpServletRequest getCookies

List of usage examples for javax.servlet.http HttpServletRequest getCookies

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getCookies.

Prototype

public Cookie[] getCookies();

Source Link

Document

Returns an array containing all of the Cookie objects the client sent with this request.

Usage

From source file:hudson.plugins.timestamper.format.TimestampFormatterImplTest.java

private static HttpServletRequest request(String... cookieValues) {
    HttpServletRequest request = mock(HttpServletRequest.class);
    Cookie[] cookies = null;// ww  w . java  2 s . c  o  m
    if (cookieValues != null) {
        cookies = new Cookie[cookieValues.length];
        for (int i = 0; i < cookieValues.length; i++) {
            cookies[i] = new Cookie("jenkins-timestamper", cookieValues[i]);
        }
    }
    when(request.getCookies()).thenReturn(cookies);
    return request;
}

From source file:com.o2o.util.WebUtils.java

/**
 * ?cookie// ww w  .jav a 2s.com
 * 
 * @param request
 *            HttpServletRequest
 * @param name
 *            cookie??
 * @return ?null
 */
public static String getCookie(HttpServletRequest request, String name) {
    if (null == name) {
        return null;
    }
    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:cn.vlabs.umt.ui.servlet.login.LoginMethod.java

public static Cookie getCookieByName(HttpServletRequest request, String cookieName) {
    Cookie fcookie = null;/* w ww.  j a v a  2 s  . c  o m*/
    if (cookieName != null) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookieName.equals(cookie.getName())) {
                    fcookie = cookie;
                    break;
                }
            }
        }
    }
    return fcookie;
}

From source file:com.o2o.util.WebUtils.java

public static void clearCookie(HttpServletRequest request, HttpServletResponse response, String path,
        String domain) {/*from  ww  w.  ja  v  a  2 s .  c o  m*/
    Assert.notNull(request);
    Assert.notNull(response);
    try {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie_old : cookies) {
            String name = URLEncoder.encode(cookie_old.getName(), "UTF-8");
            Cookie cookie = new Cookie(name, null);
            cookie.setMaxAge(0);
            if (StringUtils.isNotEmpty(path)) {
                cookie.setPath(path);
            }
            if (StringUtils.isNotEmpty(domain)) {
                cookie.setDomain(domain);
            }
            response.addCookie(cookie);
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:com.vmware.identity.samlservice.Shared.java

public static Session getSession(SessionManager sessionManager, HttpServletRequest request, String tenant) {
    Session retval = null;/*  w w  w. ja  v  a  2 s  .  c om*/
    Validate.notEmpty(tenant);

    // first find session id in the cookies
    String sessionId = Shared.getCookieValue(request.getCookies(), Shared.getTenantSessionCookieName(tenant),
            null);

    try {
        if (sessionId != null) {
            // get the session
            retval = sessionManager.get(sessionId);
            if (retval != null && !retval.isValid()) {
                // invalid session
                retval = null;
            }
        }
    } catch (Exception e) {
        retval = null; // something went wrong when looking for session
    }

    return retval;
}

From source file:com.aaasec.sigserv.csspserver.SpServlet.java

private static SpSession getSession(HttpServletRequest request, HttpServletResponse response) {
    BigInteger sessionID = new BigInteger(32, new Random(System.currentTimeMillis()));
    Cookie[] cookies = request.getCookies();
    try {//w  ww  . j  a  v  a 2 s.c o  m
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("SigSpSession")) {
                sessionID = new BigInteger(cookie.getValue());
            }
        }
    } catch (Exception ex) {
    }
    response.addCookie(new Cookie("SigSpSession", sessionID.toString()));
    return getSessionFromID(sessionID);
}

From source file:no.sesat.search.http.servlet.SearchServlet.java

static String getCookieValue(final HttpServletRequest request, final String cookieName) {

    String value = "";
    // Look in attributes (it could have already been updated this request)
    if (null != request) {

        // Look through cookies
        if (null != request.getCookies()) {
            for (Cookie c : request.getCookies()) {
                if (c.getName().equals(cookieName)) {
                    value = c.getValue();
                    break;
                }/* www .  j a va 2  s  .com*/
            }
        }
    }

    return value;
}

From source file:org.carewebframework.ui.FrameworkWebSupport.java

/**
 * Returns the named cookie from the specified request. When values are retrieved, they should
 * be decoded.// w  w  w.ja v a  2s  .  c  o  m
 * 
 * @see #decodeCookieValue(String)
 * @param cookieName Name of cookie
 * @param httpRequest Request containing cookie.
 * @return A cookie, or null if not found.
 * @throws IllegalArgumentException if arguments are null
 */
public static Cookie getCookie(final String cookieName, final HttpServletRequest httpRequest) {
    Validate.notNull(cookieName, "The cookieName must not be null");
    Validate.notNull(httpRequest, "The httpRequest must not be null");
    final Cookie[] cookies = httpRequest.getCookies();

    if (cookies != null) {
        for (final Cookie cookie : httpRequest.getCookies()) {
            if (cookieName.equals(cookie.getName())) {
                return cookie;
            }
        }
    }

    return null;
}

From source file:com.shishu.utility.string.StringUtil.java

public static String[] getCookie(HttpServletRequest request) throws UnsupportedEncodingException {
    Cookie cookies[] = request.getCookies();
    ArrayList al = new ArrayList();
    if (cookies == null)
        return null;
    for (int i = 0; i < cookies.length; i++)
        al.add(cookies[i].getName() + " = " + URLDecoder.decode(cookies[i].getValue(), "utf-8"));

    return (String[]) al.toArray(new String[0]);
}

From source file:com.paladin.mvc.RequestContext.java

/**
 * ?  // ww w.jav  a  2  s  .  co  m
 *
 * @param ctx
 * @param req
 * @param res
 */
public static RequestContext begin(ServletContext ctx, HttpServletRequest req, HttpServletResponse res) {
    RequestContext rc = new RequestContext();
    rc.context = ctx;
    rc.request = req;// _AutoUploadRequest(_AutoEncodingRequest(req));
    rc.response = res;
    rc.response.setCharacterEncoding(UTF_8);
    rc.session = req.getSession(false);
    rc.cookies = new HashMap<String, Cookie>();
    Cookie[] cookies = req.getCookies();
    if (cookies != null)
        for (Cookie ck : cookies) {
            rc.cookies.put(ck.getName(), ck);
        }
    contexts.set(rc);
    return rc;
}