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:org.apache.hive.service.cli.thrift.ThriftHttpServlet.java

/**
 * Validate the request cookie. This function iterates over the request cookie headers
 * and finds a cookie that represents a valid client/server session. If it finds one, it
 * returns the client name associated with the session. Else, it returns null.
 * @param request The HTTP Servlet Request send by the client
 * @return Client Username if the request has valid HS2 cookie, else returns null
 * @throws UnsupportedEncodingException//from  w w  w. j ava2s  .  c om
 */
private String validateCookie(HttpServletRequest request) throws UnsupportedEncodingException {
    // Find all the valid cookies associated with the request.
    Cookie[] cookies = request.getCookies();

    if (cookies == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No valid cookies associated with the request " + request);
        }
        return null;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Received cookies: " + toCookieStr(cookies));
    }
    return getClientNameFromCookie(cookies);
}

From source file:fr.paris.lutece.plugins.mylutece.modules.oauth.authentication.OAuthAuthentication.java

/**
 * Finds a cookie by its name/*w  w  w .  j a v a2  s.  c o  m*/
 * @param request the request
 * @param strKey the cookie name
 * @return the cookie found, <code>null</code> otherwise.
 */
private Cookie findCookie(HttpServletRequest request, String strKey) {
    if (StringUtils.isBlank(strKey)) {
        return null;
    }

    for (Cookie cookie : request.getCookies()) {
        if (strKey.equals(cookie.getName())) {
            return cookie;
        }
    }

    return null;
}

From source file:com.microsoft.azure.oidc.filter.helper.impl.SimpleAuthenticationHelper.java

private Cookie getCookie(final HttpServletRequest httpRequest, final String cookieName) {
    if (httpRequest == null || cookieName == null) {
        throw new PreconditionException("Required parameter is null");
    }/*w  w  w  .ja va 2  s  .co  m*/
    if (httpRequest.getCookies() == null) {
        return null;
    }
    if (httpRequest.getCookies() != null) {
        for (final Cookie cookie : httpRequest.getCookies()) {
            if (cookie == null || cookie.getName() == null) {
                continue;
            }
            if (cookie.getName().equals(cookieName)) {
                return cookie;
            }
        }
    }
    return null;
}

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

public String intercept(ActionInvocation invoke) throws Exception {
    try {/*from   w  w  w  . j a  v  a  2 s .  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:es.pode.soporte.seguridad.openId.ui.openid.OpenIDAuthenticationProcessingFilter.java

private String getCookieValor(HttpServletRequest request, String nombreCookie) {
    Cookie cookie = null;//from   www .  j a v  a  2s .c o  m
    String valor = null;
    cookie = getCookie(nombreCookie, request.getCookies());
    valor = cookie.getValue();
    return valor;
}

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

/**
 * Gets the authentication key from SAKAI-TRACKING cookie.
 * /*  w w  w . j  a v a 2 s. co  m*/
 * @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:org.apache.ranger.security.web.filter.RangerSSOAuthenticationFilter.java

/**
 * Encapsulate the acquisition of the JWT token from HTTP cookies within the
 * request.// w  w  w. j  ava2  s .  c  om
 *
 * @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();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookieName != null && cookieName.equals(cookie.getName())) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(cookieName + " cookie has been found and is being processed");
                }
                serializedJWT = cookie.getValue();
                break;
            }
        }
    }
    return serializedJWT;
}

From source file:org.itracker.web.util.LoginUtilities.java

public static boolean checkAutoLogin(HttpServletRequest request, boolean allowSaveLogin) {
    boolean foundLogin = false;

    if (request != null) {
        int authType = getRequestAuthType(request);

        // Check for auto login in request
        if (authType == AuthenticationConstants.AUTH_TYPE_REQUEST) {
            String redirectURL = request.getRequestURI().substring(request.getContextPath().length())
                    + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
            request.setAttribute(Constants.AUTH_TYPE_KEY, AuthenticationConstants.AUTH_TYPE_REQUEST);
            request.setAttribute(Constants.AUTH_REDIRECT_KEY, redirectURL);
            request.setAttribute("processLogin", "true");
            foundLogin = true;/*from   w w  w .  jav a 2 s .  co m*/

        }

        // Add in check for client certs

        // Check for auto login with cookies, this will only happen if users
        // are allowed to save
        // their logins to cookies
        if (allowSaveLogin && !foundLogin) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (Constants.COOKIE_NAME.equals(cookie.getName())) {
                        int seperator = cookie.getValue().indexOf('~');
                        final String login;
                        if (seperator > 0) {
                            login = cookie.getValue().substring(0, seperator);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Attempting autologin for user " + login + ".");
                            }

                            String redirectURL = request.getRequestURI()
                                    .substring(request.getContextPath().length())
                                    + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
                            request.setAttribute(Constants.AUTH_LOGIN_KEY,
                                    cookie.getValue().substring(0, seperator));
                            request.setAttribute(Constants.AUTH_TYPE_KEY,
                                    AuthenticationConstants.AUTH_TYPE_PASSWORD_ENC);

                            request.setAttribute(Constants.AUTH_VALUE_KEY,
                                    cookie.getValue().substring(seperator + 1));
                            request.setAttribute(Constants.AUTH_REDIRECT_KEY, redirectURL);
                            request.setAttribute("processLogin", "true");
                            foundLogin = true;
                        }
                    }
                }
            }
        }

    }

    return foundLogin;
}

From source file:com.xpn.xwiki.user.impl.xwiki.MyPersistentLoginManager.java

/**
 * {@inheritDoc}//from w w w .j a  v  a2  s .c  o  m
 * 
 * @see DefaultPersistentLoginManager#rememberingLogin(javax.servlet.http.HttpServletRequest)
 */
@Override
public boolean rememberingLogin(HttpServletRequest request) {
    if (getCookieValue(request.getCookies(), getCookiePrefix() + COOKIE_REMEMBERME, "false").equals("true")) {
        return true;
    } else {
        return false;
    }
}

From source file:org.apache.jsp.communities_jsp.java

public static String getBrowserInfiniteCookie(HttpServletRequest request) {
      Cookie[] cookieJar = request.getCookies();
      if (cookieJar != null) {
          for (Cookie cookie : cookieJar) {
              if (cookie.getName().equals("infinitecookie")) {
                  return cookie.getValue() + ";";
              }// w w w  . j  a va  2  s  .  c om
          }
      }
      return null;
  }