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:com.ctc.storefront.filters.CustomerLocationRestorationFilter.java

@Override
public void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
        final FilterChain filterChain) throws ServletException, IOException {
    if (getCustomerLocationFacade().getUserLocationData() == null) {
        final Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            setUserLocationDataFromCookies(cookies);
        }/*from  w  w  w . jav  a  2  s  .c  o  m*/
    }

    filterChain.doFilter(request, response);
}

From source file:org.apache.lucene.gdata.servlet.handler.RequestAuthenticator.java

protected String getTokenFromRequest(HttpServletRequest request) {
    String token = request.getHeader(AuthenticationController.AUTHORIZATION_HEADER);
    if (token == null || !token.startsWith("GoogleLogin")) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        }//from   w  w  w . jav  a2  s .c om
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals(AuthenticationController.TOKEN_KEY)) {
                token = cookies[i].getValue();
                break;
            }

        }
    }
    if (token != null)
        token = token.substring(token.indexOf("=") + 1);
    return token;
}

From source file:com.acc.storefront.filters.CustomerLocationRestorationFilter.java

@Override
public void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
        final FilterChain filterChain) throws ServletException, IOException {
    if (getCustomerLocationFacade().getUserLocationData() == null) {
        final Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            for (final Cookie cookie : cookies) {
                if (getCustomerLocationCookieGenerator().getCookieName().equals(cookie.getName())) {
                    final UserLocationData cookieUserLocationData = decipherUserLocationData(cookie.getValue());
                    getCustomerLocationFacade().setUserLocationData(cookieUserLocationData);
                    break;
                }//w  w  w . j  a v  a2  s .c o m
            }
        }
    }

    filterChain.doFilter(request, response);
}

From source file:com.shenit.commons.utils.HttpUtils.java

/**
 * Cookie//from   w w w .j  av  a  2s. c  o m
 * 
 * @param name
 *            ??
 * @param defaultVal
 *            
 * @param req
 *            
 * @return
 */

public static String getCookieValue(String name, String defaultVal, HttpServletRequest req) {
    if (req.getCookies() != null) {
        for (Cookie cookie : req.getCookies()) {
            if (cookie.getName().equals(name))
                return cookie.getValue();
        }
    }
    // nothing found
    return defaultVal;
}

From source file:com.hypersocket.session.json.SessionUtils.java

public Locale getLocale(HttpServletRequest request) {

    if (request.getSession().getAttribute(USER_LOCALE) == null) {

        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies) {
            if (c.getName().equals(HYPERSOCKET_LOCALE)) {
                return new Locale(c.getValue());
            }//w  ww.  ja v  a 2 s .co  m
        }
        return configurationService.getDefaultLocale();
    } else {
        return new Locale((String) request.getSession().getAttribute(USER_LOCALE));
    }

}

From source file:cec.easyshop.storefront.filters.CustomerLocationRestorationFilter.java

@Override
public void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
        final FilterChain filterChain) throws ServletException, IOException {
    if (getCustomerLocationFacade().getUserLocationData() == null) {
        final Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            for (final Cookie cookie : cookies) {
                if (getCustomerLocationCookieGenerator().getCookieName().equals(cookie.getName())) {
                    final UserLocationData cookieUserLocationData = decipherUserLocationData(
                            StringUtils.remove(cookie.getValue(), "\""));
                    getCustomerLocationFacade().setUserLocationData(cookieUserLocationData);
                    break;
                }//from   w  ww  .j a  va 2  s  .  c om
            }
        }
    }

    filterChain.doFilter(request, response);
}

From source file:org.apache.sling.auth.xing.login.impl.XingLoginAuthenticationHandler.java

protected void deleteCookies(final HttpServletRequest request, final HttpServletResponse response) {
    final Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (final Cookie cookie : cookies) {
            final String name = cookie.getName();
            logger.debug("cookie found: '{}'", name);
            if (name.equals(xingCookie) || name.equals(userCookie) || name.equals(userIdCookie)) {
                logger.debug("deleting cookie '{}' with value '{}'", cookie.getName(), cookie.getValue());
                cookie.setValue(null);// www.j  a  v a  2 s.  com
                cookie.setMaxAge(0);
                response.addCookie(cookie);
            }
        }
    }
}

From source file:com.ebook.storefront.interceptors.beforecontroller.RequireHardLoginBeforeControllerHandler.java

protected boolean isGuidStoredinCookies(final HttpServletRequest request, final HttpServletResponse response,
        final String guid, final String guidCookieName) {
    for (final Cookie cookie : request.getCookies()) {
        if (guidCookieName.equals(cookie.getName())) {
            if (guid.equals(cookie.getValue())) {
                return true;
            } else {
                LOG.info("Found secure cookie with invalid value. expected [" + guid + "] actual ["
                        + cookie.getValue() + "]. removing.");
                getCookieGenerator().removeCookie(response);
            }//from  ww  w  .j a v a2  s . c o m
        }
    }
    return false;
}

From source file:MyServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    Cookie cookie = null;//from w  w w  .  j  a va 2 s  .co  m
    //Get an array of Cookies associated with this domain
    Cookie[] cookies = request.getCookies();
    boolean newCookie = false;

    //Get the 'mycookie' Cookie if it exists
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("mycookie")) {
                cookie = cookies[i];
            }
        } //end for
    } //end if

    if (cookie == null) {
        newCookie = true;
        //Get the cookie's Max-Age from a context-param element
        //If the 'cookie-age' param is not set properly
        //then set the cookie to a default of -1, 'never expires'
        int maxAge;
        try {
            maxAge = new Integer(getServletContext().getInitParameter("cookie-age")).intValue();
        } catch (Exception e) {
            maxAge = -1;
        }

        //Create the Cookie object

        cookie = new Cookie("mycookie", "" + getNextCookieValue());
        cookie.setPath(request.getContextPath());
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);

    } //end if
      // get some info about the cookie
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Cookie info</title>");
    out.println("</head>");
    out.println("<body>");

    out.println("<h2> Information about the cookie named \"mycookie\"</h2>");

    out.println("Cookie value: " + cookie.getValue() + "<br>");
    if (newCookie) {
        out.println("Cookie Max-Age: " + cookie.getMaxAge() + "<br>");
        out.println("Cookie Path: " + cookie.getPath() + "<br>");
    }

    out.println("</body>");
    out.println("</html>");

    out.close();
}

From source file:com.bosch.cr.examples.jwt.CustomProxyServlet.java

@Override
protected void service(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    final Optional<Cookie[]> cookies = Optional.ofNullable(req.getCookies());

    if (cookies.isPresent()) {
        final Optional<Cookie> authorizationCookie = Stream.of(cookies.get()) //
                .filter(CookieUtil::isJwtAuthenticationCookie) //
                .findFirst();/*from   ww w  . ja v a  2s  .  c  o  m*/

        if (authorizationCookie.isPresent()) {
            super.service(req, resp);
            return;
        }
    }

    resp.setStatus(HttpStatus.SC_UNAUTHORIZED);
}