Example usage for javax.servlet.http Cookie getValue

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

Introduction

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

Prototype

public String getValue() 

Source Link

Document

Gets the current value of this Cookie.

Usage

From source file:com.persistent.cloudninja.controller.AuthFilterUtils.java

/**
 * //from   w w w .ja v  a  2s. c o  m
 * @param cookie
 * @param fieldName
 * @return
 */
public static String getFieldValueFromCookie(Cookie cookie, String fieldName) {
    if (cookie == null || fieldName == null) {
    }

    String cookieValue = cookie.getValue();
    return getFieldValueFromCookieString(fieldName, cookieValue);
}

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

/**
 * Return a cookie value or default//from w  w  w  .  jav  a2 s .  com
 * @param cookies
 * @param cookieName
 * @param defaultValue
 * @return
 */
public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
    if (cookies != null) {
        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:org.carewebframework.ui.FrameworkWebSupport.java

/**
 * Returns the value from the named cookie from the specified request. The value is decoded with
 * for security and consistency (Version 0+ of Cookies and web containers)
 * //from  w  w w. ja  v a 2s.c o m
 * @see #getCookie(String, HttpServletRequest)
 * @see #decodeCookieValue(String)
 * @param cookieName Name of cookie
 * @param httpRequest Request containing cookie. If null, it will check
 *            {@link Execution#getNativeRequest()}
 * @return A cookie value, or null if not found.
 * @throws IllegalArgumentException if arguments are null
 */
public static String getCookieValue(final String cookieName, final HttpServletRequest httpRequest) {
    final Cookie cookie = getCookie(cookieName, httpRequest);
    return cookie == null ? null : decodeCookieValue(cookie.getValue());
}

From source file:ai.susi.server.AbstractAPIHandler.java

/**
 * Checks a request for valid login data, either a existing session, a cookie or an access token
 * @return user identity if some login is active, anonymous identity otherwise
 *//* w w w  . j av a2 s . c o m*/
public static ClientIdentity getIdentity(HttpServletRequest request, HttpServletResponse response,
        Query query) {

    if (getLoginCookie(request) != null) { // check if login cookie is set

        Cookie loginCookie = getLoginCookie(request);

        ClientCredential credential = new ClientCredential(ClientCredential.Type.cookie,
                loginCookie.getValue());
        Authentication authentication = new Authentication(credential, DAO.authentication);

        if (authentication.getIdentity() != null && authentication.checkExpireTime()) {

            //reset cookie validity time
            authentication.setExpireTime(defaultCookieTime);
            loginCookie.setMaxAge(defaultCookieTime.intValue());
            loginCookie.setPath("/"); // bug. The path gets reset
            response.addCookie(loginCookie);

            return authentication.getIdentity();
        }

        authentication.delete();

        // delete cookie if set
        deleteLoginCookie(response);

        Log.getLog().info("Invalid login try via cookie from host: " + query.getClientHost());
    } else if (request.getSession().getAttribute("identity") != null) { // check session is set
        return (ClientIdentity) request.getSession().getAttribute("identity");
    } else if (request.getParameter("access_token") != null) { // access tokens can be used by api calls, somehow the stateless equivalent of sessions for browsers
        ClientCredential credential = new ClientCredential(ClientCredential.Type.access_token,
                request.getParameter("access_token"));
        Authentication authentication = new Authentication(credential, DAO.authentication);

        // check if access_token is valid
        if (authentication.getIdentity() != null) {
            ClientIdentity identity = authentication.getIdentity();

            if (authentication.checkExpireTime()) {
                Log.getLog().info("login for user: " + identity.getName() + " via access token from host: "
                        + query.getClientHost());

                if ("true".equals(request.getParameter("request_session"))) {
                    request.getSession().setAttribute("identity", identity);
                }
                if (authentication.has("one_time") && authentication.getBoolean("one_time")) {
                    authentication.delete();
                }
                return identity;
            }
        }
        Log.getLog().info("Invalid access token from host: " + query.getClientHost());
        return getAnonymousIdentity(query.getClientHost());
    }

    return getAnonymousIdentity(query.getClientHost());
}

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

/**
 * ?cookie//from   ww w .j av  a  2s. co m
 * 
 * @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:com.lushapp.common.web.utils.WebUtils.java

/**
 * ?Cookie./*from  w ww  .j ava2s.  c  o  m*/
 * @param request
 * @param name
 * @return
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();
    Cookie returnCookie = null;

    if (cookies == null) {
        return returnCookie;
    }

    for (Cookie thisCookie : cookies) {
        if (thisCookie.getName().equals(name)) {
            if (!thisCookie.getValue().equals("")) {
                returnCookie = thisCookie;

                break;
            }
        }
    }

    return returnCookie;
}

From source file:com.ofbizcn.securityext.login.LoginEvents.java

public static String getUsername(HttpServletRequest request) {
    String cookieUsername = null;
    Cookie[] cookies = request.getCookies();
    if (Debug.verboseOn())
        Debug.logVerbose("Cookies:" + cookies, module);
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(usernameCookieName)) {
                cookieUsername = cookie.getValue();
                break;
            }/*from   w  ww  .  j av  a2  s  .  co m*/
        }
    }
    return cookieUsername;
}

From source file:edu.stanford.epad.epadws.security.EPADSessionOperations.java

public static String getJSessionIDFromRequest(HttpServletRequest servletRequest) {
    String jSessionID = null;//from  w  ww. j a v a  2s  .com

    Cookie[] cookies = servletRequest.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {
                jSessionID = cookie.getValue();
                break;
            }
        }
    }
    if (jSessionID == null) {
        log.warning("No JSESSIONID cookie present in request " + servletRequest.getRequestURL());
    } else {
        int comma = jSessionID.indexOf(",");
        if (comma != -1) {
            log.warning("Multiple cookies:" + jSessionID);
            jSessionID = jSessionID.substring(0, comma);
        }
    }
    return jSessionID;
}

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 {/*  ww  w.j  a v  a2s  .  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: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 ww . ja v a  2  s .c o m*/
    }

    return commonsCookies;
}