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:fr.paris.lutece.plugins.mylutece.modules.openam.service.OpenamService.java

/**
 * Extract the value of the connection cookie
 *
 * @param request//from   w w w.  j av  a  2 s . co  m
 *            The HTTP request
 * @return The cookie's value
 */
public String getConnectionCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    String strOpenamCookie = null;

    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(COOKIE_OPENAM_NAME)) {
                strOpenamCookie = cookie.getValue();
                OpenamAPI._logger.debug("getHttpAuthenticatedUser : cookie '" + COOKIE_OPENAM_NAME
                        + "' found - value=" + strOpenamCookie);
            }
        }
    }

    return strOpenamCookie;
}

From source file:fi.hoski.web.forms.RaceEntryServlet.java

private JSONObject fromCookie(HttpServletRequest request) throws JSONException {
    if (useCookies) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (COOKIENAME.equals(cookie.getName())) {
                    Base64 decoder = new Base64();
                    try {
                        return new JSONObject(new String(decoder.decode(cookie.getValue()), "UTF-8"));
                    } catch (UnsupportedEncodingException ex) {
                        log(ex.getMessage(), ex);
                        return new JSONObject();
                    }/*from  www  .  j a va2 s  .c  om*/
                }
            }
        }
    }
    return new JSONObject();

}

From source file:net.nan21.dnet.core.web.controller.ui.extjs.AbstractUiExtjsController.java

/**
 * Resolve the user's current theme from the cookie.
 * //from  w w  w .java2s.  co m
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
private String resolveTheme(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Cookie[] cookies = request.getCookies();
    Cookie c = this.getCookie(cookies, Constants.COOKIE_NAME_THEME);
    if (c == null) {

        String value = this.getSettings().getParam(SysParams_Core.CORE_DEFAULT_THEME_EXTJS);
        c = this.createCookie(Constants.COOKIE_NAME_THEME, value, 60 * 60 * 24 * 365);
        response.addCookie(c);
    }

    String theme = request.getParameter(Constants.REQUEST_PARAM_THEME);
    if (theme == null || theme.equals("")) {
        theme = c.getValue();
    } else {
        c.setMaxAge(0);
        c = this.createCookie(Constants.COOKIE_NAME_THEME, theme, 60 * 60 * 24 * 365);
        response.addCookie(c);
    }
    return theme;
}

From source file:net.nan21.dnet.core.web.controller.ui.extjs.AbstractUiExtjsController.java

/**
 * Resolve the user's current language from the cookie.
 * /*from   w  w  w.j a va 2s.c o m*/
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
private String resolveLang(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Cookie[] cookies = request.getCookies();
    Cookie c = this.getCookie(cookies, Constants.COOKIE_NAME_LANG);
    if (c == null) {

        String value = this.getSettings().getParam(SysParams_Core.CORE_DEFAULT_LANGUAGE);

        c = this.createCookie(Constants.COOKIE_NAME_LANG, value, 60 * 60 * 24 * 365);
        response.addCookie(c);
    }

    String lang = request.getParameter(Constants.REQUEST_PARAM_LANG);
    if (lang == null || lang.equals("")) {
        lang = c.getValue();
    } else {
        c.setMaxAge(0);
        c = this.createCookie(Constants.COOKIE_NAME_LANG, lang, 60 * 60 * 24 * 365);
        response.addCookie(c);
    }
    return lang;
}

From source file:com.foilen.smalltools.spring.security.CookiesGeneratedCsrfTokenRepository.java

@Override
public CsrfToken generateToken(HttpServletRequest request) {
    AssertTools.assertNotNull(salt, "You must set the salt");
    AssertTools.assertFalse(cookieNames.isEmpty(), "You must set at least one cookie");

    // Search all the cookies
    Map<String, String> valuesByName = new HashMap<>();
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookieNames.contains(cookie.getName())) {
                String previousValue = valuesByName.put(cookie.getName(), cookie.getValue());
                if (previousValue != null) {
                    throw new SmallToolsException(
                            "The cookie with name " + cookie.getName() + " contains more than one value");
                }/*from  ww w .  j  av a2 s.c  o  m*/
            }
        }
    }

    // Generate the token
    StringBuilder allValues = new StringBuilder(salt);
    for (String cookieName : cookieNames) {
        String cookieValue = valuesByName.get(cookieName);
        logger.debug("Adding cookie {} with value {}", cookieName, cookieValue);
        allValues.append(cookieName).append(cookieValue);
    }

    String token = HashSha256.hashString(allValues.toString());
    logger.debug("Token is {}", token);
    return new DefaultCsrfToken(HEADER_NAME, PARAMETER_NAME, token);
}

From source file:com.sunrun.crportal.util.CRPortalUtil.java

public static String getValueFromCookies(HttpServletRequest request, String cookieName) {
    String cookieValue = "";
    Cookie[] cookieArray = request.getCookies();
    if (cookieArray != null) {
        for (int i = 0; i < cookieArray.length; i++) {
            Cookie aCookie = cookieArray[i];
            if (cookieName.equals(aCookie.getName())) {
                cookieValue = aCookie.getValue();
            }//from www  . j a  v  a  2  s  .c o m
        }
    }
    return cookieValue;
}

From source file:dk.clarin.tools.userhandle.java

public static String getUserHandle(HttpServletRequest request, List<FileItem> items) {
    /// The eSciDoc userHandle
    String userHandle = null;//from  w w w .jav a  2s  . com
    logger.debug("getUserHandle");

    //if(userHandle == null || userHandle.equals(""))
    {
        logger.debug("userHandle == null");
        userHandle = "";
        /*
                    HttpSession session = request.getSession(true);
                    UserSessionBean bean = (UserSessionBean) session.getAttribute("user");
                
                    if (bean != null) 
        {
        logger.debug("userHandle from bean: {}",bean.getUserHandle());
        return bean.getUserHandle();
        }
                    logger.debug("bean == null, trying to use parameter 'handle'");
        */
        userHandle = request.getParameter("handle");
        if (userHandle == null) {
            logger.debug("No 'handle' parameter, trying to use cookie 'escidocCookie'");

            String cookieName = "escidocCookie";
            javax.servlet.http.Cookie cookies[] = request.getCookies();
            if (cookies != null) {
                logger.debug("cookies != null");
                for (int i = 0; i < cookies.length; i++) {
                    logger.debug("cookie {} = {}", cookies[i].getName(), cookies[i].getValue());
                    if (cookies[i].getName().equals(cookieName)) {
                        userHandle = cookies[i].getValue();
                        logger.debug("userHandle == {}", userHandle);
                        if (userHandle != null && userHandle.equals("0"))
                            userHandle = null;
                        //break;
                    }
                }
            } else
                logger.debug("cookies == null");

            if (userHandle == null || userHandle.equals("")) {
                /*
                * Parse the request
                */
                userHandle = getParmFromMultipartFormData(request, items, "handle");
            }
        }
    }
    /*else
    {
    logger.debug("userHandle already set to {}",userHandle);
    }*/
    if (userHandle != null && !userHandle.equals("")) {
        logger.info("userHandle = " + userHandle);
        return userHandle;
    } else {
        logger.info("userHandle = null");
        return null;
    }
}

From source file:com.mockey.model.RequestFromClient.java

/**
 * an org.apache.commons.httpclient.Cookie is NOT a
 * javax.servlet.http.Cookie - and it looks like the two don't map onto each
 * other without data loss...//from  www.ja v a  2  s  .c o  m
 * */

private void parseCookies(HttpServletRequest rawRequest) {
    javax.servlet.http.Cookie[] cookies = rawRequest.getCookies();
    if (cookies != null) {
        // ******************
        // This doesn't seem right.
        // We have to map javax Cookies to httpclient Cookies?!?!
        // 
        // ******************
        for (int i = 0; i < cookies.length; i++) {
            javax.servlet.http.Cookie c = cookies[i];
            String domain = c.getDomain();
            if (domain == null) {
                domain = rawRequest.getServerName();
            }
            String cpath = c.getPath();
            if (cpath == null) {
                cpath = rawRequest.getContextPath();
            }
            BasicClientCookie basicClientCookie = new BasicClientCookie(c.getName(), c.getValue());
            basicClientCookie.setDomain(domain);
            if (c.getMaxAge() > -1) {
                int seconds = c.getMaxAge();
                long currentTime = System.currentTimeMillis();
                Date expiryDate = new Date(currentTime + (seconds * 1000));
                basicClientCookie.setExpiryDate(expiryDate);
            }
            this.httpClientCookies.add(basicClientCookie);
        }
    }

}

From source file:SettingandReadingCookies.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>");
    out.println("A Web Page");
    out.println("</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY");

    Cookie[] cookies = request.getCookies();
    boolean foundCookie = false;

    for (int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {
        Cookie cookie1 = cookies[loopIndex];
        if (cookie1.getName().equals("color")) {
            out.println("bgcolor = " + cookie1.getValue());
            foundCookie = true;/*from  w w w  .  j  a va2s. co m*/
        }
    }

    if (!foundCookie) {
        Cookie cookie1 = new Cookie("color", "cyan");
        cookie1.setMaxAge(24 * 60 * 60);
        response.addCookie(cookie1);
    }

    out.println(">");
    out.println("<H1>Setting and Reading Cookies</H1>");
    out.println("This page will set its background color using a cookie when reloaded.");
    out.println("</BODY>");
    out.println("</HTML>");
}

From source file:MyServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>");
    out.println("A Web Page");
    out.println("</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY");

    Cookie[] cookies = request.getCookies();
    boolean foundCookie = false;

    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie1 = cookies[i];/*  ww  w  .  j  a va 2  s  .c  o m*/
        if (cookie1.getName().equals("color")) {
            out.println("bgcolor = " + cookie1.getValue());
            foundCookie = true;
        }
    }

    if (!foundCookie) {
        Cookie cookie1 = new Cookie("color", "cyan");
        cookie1.setMaxAge(24 * 60 * 60);
        response.addCookie(cookie1);
    }

    out.println(">");
    out.println("<H1>Setting and Reading Cookies</H1>");
    out.println("This page will set its background color using a cookie when reloaded.");
    out.println("</BODY>");
    out.println("</HTML>");
}