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:com.shenit.commons.utils.HttpUtils.java

/**
 * ?cookie/*from   w  w  w . j  a  v  a 2  s. c  o m*/
 * 
 * @param req
 *            
 * @param names
 *            cookie??
 */
public static void purgeCookies(HttpServletRequest req, HttpServletResponse resp, String... names) {
    Set<String> nameSet = ValidationUtils.isEmpty(names) ? null : new HashSet<String>(Arrays.asList(names));
    boolean removeAll = ValidationUtils.isEmpty(nameSet);
    for (Cookie cookie : req.getCookies()) {
        if (removeAll || nameSet.contains(cookie.getName())) {
            cookie.setMaxAge(0);
            cookie.setValue(null);
            resp.addCookie(cookie);
            if (!removeAll)
                nameSet.remove(cookie.getName());
            ;
        }
    }
}

From source file:com.hangum.tadpole.preference.ui.GeneralPreferencePage.java

/**
 * initialize locale// w w w .  j av  a  2 s . co  m
 */
private void initLocale() {

    // ? ?   ?  ..
    HttpServletRequest request = RWT.getRequest();
    Cookie[] cookies = request.getCookies();

    boolean isExist = false;
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (PublicTadpoleDefine.TDB_COOKIE_USER_LANGUAGE.equals(cookie.getName())) {
                comboLanguage.setText(cookie.getValue());
                changeUILocale(comboLanguage.getText());
                isExist = true;

                break;
            }
        }
    }

    // ?  ?? ?  .
    if (!isExist)
        comboLanguage.setText(Locale.ENGLISH.getDisplayLanguage(Locale.ENGLISH));

}

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

public String getResponseCookiesAsString() {
    StringBuffer responseCookies = new StringBuffer();
    for (Cookie cookie : this.cookieList) {
        responseCookies.append(String.format("Cookie--->\n\n %s = %s ", cookie.getName(), cookie.getValue()));
    }//from  w  w  w. ja v a2  s.  c o  m
    return responseCookies.toString();
}

From source file:org.apache.hadoop.security.authentication.server.JWTRedirectAuthenticationHandler.java

/**
 * Encapsulate the acquisition of the JWT token from HTTP cookies within the
 * request.//from www. j  ava2  s  .  com
 *
 * @param req servlet request to get the JWT token from
 * @return serialized JWT token
 */
protected String getJWTFromCookie(HttpServletRequest req) {
    String serializedJWT = null;
    Cookie[] cookies = req.getCookies();
    String userName = null;
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookieName.equals(cookie.getName())) {
                LOG.info(cookieName + " cookie has been found and is being processed");
                serializedJWT = cookie.getValue();
                break;
            }
        }
    }
    return serializedJWT;
}

From source file:org.alfresco.web.app.servlet.LanguageCookieFilter.java

protected String positionCookie(ServletRequest servletRequest, ServletResponse servletResponse, String mlLang,
        String cookieName, boolean content) {
    if (mlLang == null || mlLang.length() == 0) {
        // try to get the cookie and set the content locale prop there
        Cookie[] cookies = ((HttpServletRequest) servletRequest).getCookies();
        Cookie cookie;
        boolean found = false;
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                cookie = cookies[i];/*from ww  w .  j  a  va2s . c o m*/
                if (cookie.getName().equals(cookieName)) {
                    String lang = cookie.getValue();
                    if (content) {
                        if (lang.length() == 0) {
                            I18NUtil.setContentLocale(null);
                        } else {
                            I18NUtil.setContentLocale(new Locale(lang));
                        }
                        return lang;
                    } else {
                        I18NUtil.setLocale(new Locale(lang));
                        return lang;
                    }

                }
            }
        }
    } else {
        Cookie mlpref = getCookie(servletRequest, cookieName);

        if (mlpref == null) {
            mlpref = new Cookie(cookieName, mlLang);
        } else
            mlpref.setValue(mlLang);
        // Set expiry date after 24 Hrs for both the cookies.
        //mlpref.setMaxAge(60 * 60 * 24);

        ((HttpServletResponse) servletResponse).addCookie(mlpref);
        if (content) {
            I18NUtil.setContentLocale(new Locale(mlLang));
            return mlLang;
        } else {
            I18NUtil.setLocale(new Locale(mlLang));
            return mlLang;
        }
        /*
         * { if(mlLang.equals("reset")) { Cookie mlpref = new
         * Cookie(cookieName, mlLang); // Set expiry date after 24 Hrs for
         * both the cookies. mlpref.setMaxAge(0); mlpref.setValue("");
         * ((HttpServletResponse) servletResponse).addCookie(mlpref); if
         * (content) { I18NUtil.setContentLocale(null); return mlLang; }
         * else { I18NUtil.setLocale(null); return mlLang; } } else
         */

    }

    return mlLang;
}

From source file:com.salesmanager.core.util.www.SalesManagerInterceptor.java

public String intercept(ActionInvocation invoke) throws Exception {
    try {// ww  w  .j  a  v  a  2s . c  o m

        HttpServletRequest req = (HttpServletRequest) ServletActionContext.getRequest();
        HttpServletResponse resp = (HttpServletResponse) ServletActionContext.getResponse();

        req.setCharacterEncoding("UTF-8");

        // get cookies
        Map cookiesMap = new HashMap();
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                cookiesMap.put(cookie.getName(), cookie);
            }
        }

        /**
         * MERCHANT ID
         */

        // look at merchantId in url parameter
        String merchantId = (String) req.getParameter("merchantId");

        Cookie storeCookie = (Cookie) cookiesMap.get("STORE");

        int iMerchantId = Constants.DEFAULT_MERCHANT_ID;
        MerchantStore store = null;

        if (StringUtils.isBlank(merchantId)) {// no merchantId in the
            // request

            // check for store
            store = (MerchantStore) req.getSession().getAttribute("STORE");

            if (merchantId == null) {

                if (store != null) {

                    iMerchantId = store.getMerchantId();
                } else {
                    // check for cookie
                    Cookie c = (Cookie) cookiesMap.get("STORE");
                    if (c != null && !StringUtils.isBlank(c.getValue())) {

                        String v = c.getValue();
                        iMerchantId = Integer.valueOf(v);
                    } else {
                        // assign defaultMerchantId
                        iMerchantId = Constants.DEFAULT_MERCHANT_ID;
                    }
                    // set store
                    store = this.setMerchantStore(req, resp, merchantId);
                    if (store == null) {
                        return "NOSTORE";
                    }
                }
            }

        } else {// merchantId in the request

            // check for store in the session
            store = (MerchantStore) req.getSession().getAttribute("STORE");

            if (store != null) {
                // check if both match
                if (!merchantId.equals(String.valueOf(store.getMerchantId()))) {// if they differ
                    store = this.setMerchantStore(req, resp, merchantId);
                } else {
                    iMerchantId = store.getMerchantId();
                }

            } else {
                // set store
                store = this.setMerchantStore(req, resp, merchantId);
                if (store == null) {
                    return "NOSTORE";
                }
            }
        }

        req.setAttribute("STORE", store);

        if (StringUtils.isBlank(store.getTemplateModule())) {
            return "NOSTORE";
        }

        req.setAttribute("templateId", store.getTemplateModule());

        ActionContext ctx = ActionContext.getContext();
        LocaleUtil.setLocaleForRequest(req, resp, ctx, store);

        HttpSession session = req.getSession();
        Principal p = (Principal) session.getAttribute("PRINCIPAL");

        if (p != null) {
            try {
                SalesManagerPrincipalProxy proxy = new SalesManagerPrincipalProxy(p);
                BaseActionAware action = ((BaseActionAware) invoke.getAction());
                action.setPrincipalProxy(proxy);
            } catch (Exception e) {
                log.error("The current action does not implement PrincipalAware "
                        + invoke.getAction().getClass());
            }
        }

        String r = baseIntercept(invoke, req, resp);
        if (r != null) {
            return r;
        }

        return invoke.invoke();

    } catch (Exception e) {
        log.error(e);
        ActionSupport action = (ActionSupport) invoke.getAction();
        action.addActionError(action.getText("errors.technical") + " " + e.getMessage());
        if (e instanceof ActionException) {
            return Action.ERROR;
        } else {
            return "GENERICERROR";
        }

    }

}

From source file:org.sakaiproject.hybrid.util.NakamuraAuthenticationHelper.java

/**
 * Gets the authentication key from SAKAI-TRACKING cookie.
 * /* ww  w .  ja  v  a2s .  c om*/
 * @param request
 * @return null if no secret can be found.
 */
protected String getSecret(final HttpServletRequest request) {
    LOG.debug("getSecret(HttpServletRequest request)");
    if (request == null) {
        throw new IllegalArgumentException("HttpServletRequest == null");
    }
    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
    String secret = null;
    final Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookieName.equals(cookie.getName())) {
                secret = cookie.getValue();
            }
        }
    }
    return secret;
}

From source file:m.c.m.proxyma.resource.ProxymaResponseDataBeanTest.java

/**
 * Test of getCookies method, of class ProxymaResponseDataBean.
 *//*from   www .  j a  v  a  2 s  . c o  m*/
public void testGetCookies() {
    System.out.println("getCookies");
    ProxymaResponseDataBean instance = new ProxymaResponseDataBean();
    instance.addCookie(new Cookie("name1", "value1"));
    instance.addCookie(new Cookie("name2", "value2"));
    instance.addCookie(new Cookie("name1", "value3"));

    Collection<Cookie> result = instance.getCookies();
    assertEquals(2, result.size());

    //Test multi values header
    Iterator<Cookie> iter = result.iterator();
    Cookie cookie = iter.next();
    if ("name1".equals(cookie.getName())) {
        assertEquals("value3", cookie.getValue());
        assertEquals("value2", iter.next().getValue());
    } else {
        assertEquals("name2", cookie.getName());
        assertEquals("value2", cookie.getValue());
        assertEquals("value3", iter.next().getValue());
    }

    instance = new ProxymaResponseDataBean();
    result = instance.getCookies();
    assertEquals(0, result.size());
}

From source file:com.skilrock.lms.web.roleMgmt.common.PrivsInterceptor.java

public void createCookie() {
    boolean found = false;
    Cookie userSessionId = null;
    Cookie[] cookies = request.getCookies();
    for (Cookie element : cookies) {
        userSessionId = element;//from   ww  w . ja v  a 2 s.  c  o m
        if (userSessionId.getName().equals("LMSCookie")) {
            found = true;
            break;
        }
    }
    if (!found) {
        userSessionId = new Cookie("LMSCookie", "");
        userSessionId.setMaxAge(24 * 60 * 60);
        userSessionId.setPath("/");
        response.addCookie(userSessionId);
    } else {
        userSessionId.setMaxAge(24 * 60 * 60);
        userSessionId.setPath("/");
        response.addCookie(userSessionId);
    }

}

From source file:com.hypersocket.netty.HttpResponseServletWrapper.java

@Override
public void addCookie(Cookie cookie) {

    StringBuffer cookieHeader = new StringBuffer();

    cookieHeader.append(cookie.getName());
    cookieHeader.append("=");
    cookieHeader.append(cookie.getValue());
    if (cookie.getPath() != null) {
        cookieHeader.append("; Path=");
        cookieHeader.append(cookie.getPath());
    }/*from w  w w. j ava2  s  .  c  o  m*/
    if (cookie.getDomain() != null) {
        cookieHeader.append("; Domain=");
        cookieHeader.append(cookie.getDomain());
    }
    if (cookie.getMaxAge() > 0) {
        cookieHeader.append("; Max-Age=");
        cookieHeader.append(cookie.getMaxAge());
        /**
         * This breaks IE when date of server and browser do not match
         */
        cookieHeader.append("; Expires=");
        if (cookie.getMaxAge() == 0) {
            cookieHeader.append(DateUtils.formatDate(new Date(10000), DateUtils.PATTERN_RFC1036));
        } else {
            cookieHeader.append(
                    DateUtils.formatDate(new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L),
                            DateUtils.PATTERN_RFC1036));
        }
    }

    if (cookie.getSecure()) {
        cookieHeader.append("; Secure");
    }

    /**
     * Make sure we are not adding duplicate cookies
     */
    for (Entry<String, String> entry : response.getHeaders()) {
        if (entry.getKey().equals("Set-Cookie") && entry.getValue().equals(cookieHeader.toString())) {
            return;
        }
    }
    addHeader("Set-Cookie", cookieHeader.toString());

}