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:eu.semlibproject.annotationserver.managers.CookiesManager.java

/**
 * Check if the current user is authenticated/authorized verifying
 * the annotation server cookie/*  w w  w.ja  v a 2s .  c  om*/
 * 
 * @param httpRequest   the HTTP servlet request
 * @return              if the user is authorized/authenticaed, return the accessToken
 *                      otherwise return <code>null</code>
 */
public String isUserAuthorizedFromCookie(HttpServletRequest httpRequest, HttpServletResponse response) {

    boolean authorized = false;
    Cookie[] cookies = httpRequest.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            Cookie cCookie = cookies[i];
            if (SemlibConstants.COOCKIE_NAME.equalsIgnoreCase(cCookie.getName())) {
                String cookieValue = cCookie.getValue();
                authorized = TokenManager.getInstance().isTokenValid(cookieValue);
                if (authorized) {
                    return cookieValue;
                } else {
                    removeASCookie(response, cCookie);
                }
            }
        }
    }

    return null;
}

From source file:com.data2semantics.yasgui.server.servlets.AppCacheServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String strongName = getPermutationFromCookies(request.getCookies());

    if (strongName != null && strongName.length() > 0) {
        boolean getCount = request.getParameterMap().keySet().contains("count");

        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", new Date().getTime());

        String moduleDir = getServletContext().getRealPath("/Yasgui/");
        String appcacheFile = moduleDir + "/" + strongName;
        if (fetchManifestForDev(request)) {
            appcacheFile += ".dev";
        }//from w ww.  j  a v  a2 s .  co m
        appcacheFile += ".appcache";
        String manifestString = IOUtils.toString(new FileReader(appcacheFile));

        PrintWriter out = response.getWriter();
        if (getCount) {
            out.println(Integer.toString(getCacheCount(manifestString)));
        } else {
            response.setContentType("text/cache-manifest");
            out.println(manifestString);

        }
        out.close();
    }

}

From source file:com.netpace.vzdn.webapp.vzdninterceptors.SecurityInterceptor.java

public String getUserNameFromCookie(HttpServletRequest request, HttpServletResponse response) {

    try {/*from  ww  w .j  av  a2  s. c  om*/
        SSOTokenManager stm = SSOTokenManager.getInstance();
        Cookie[] requestCookies = request.getCookies();
        String userName = "";
        for (int i = 0; i < requestCookies.length; i++) {
            Cookie cookie = requestCookies[i];
            if (cookie.getName().equals(VzdnConstants.OPENSSO_COOKIE)) {
                SSOToken st = stm.createSSOToken(request);
                userName = st.getPrincipal().getName();
                userName = userName.substring(userName.indexOf("=") + 1, userName.indexOf(",")).toLowerCase();
                System.out.println("got the cookie user name : " + userName);
                Cookie ssoCookie = new Cookie("loggedInUserInfo", userName);
                response.addCookie(ssoCookie);
                break;
            }
        }
        return userName;
    } catch (Exception ex) {
        log.error("Some issue in SecurityInterceptor while reading user info from cookie" + ex.getMessage());
        ex.printStackTrace();
        return null;
    }
}

From source file:com.ctc.storefront.security.evaluator.impl.RequireHardLoginEvaluator.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 www  .  ja va 2  s  . c o m*/
        }
    }

    return false;
}

From source file:com.pokerweb.Area.CheckBetArea.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/* w ww.j a v  a 2 s.co m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String Token = "";
    for (Cookie object : request.getCookies())
        if (object.getName().equals("JSESSIONID"))
            Token = object.getValue();
    if (Token.length() <= 0)
        return;
    JSONObject js = new JSONObject();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    long UserId = DBManager.GetInstance().GetCurrentUserId(auth.getName());
    for (Map.Entry<Long, StatisticBet> object : TableStatus.GetInstance().RequestStatisticBet.entrySet())
        if (object.getValue().ToketUserRequest.equals(Token) && object.getKey() == UserId)
            if (TableStatus.GetInstance().StatisticBetCurrentUser.containsKey(object.getValue().IdBet)) {
                try {
                    Game GMData = new Game();
                    String data = data = GMData.GetDateFromBet(object.getValue().IdBet);
                    TableStatus.GetInstance().StatisticBetCurrentUser.get(object.getValue().IdBet).put("date",
                            data);
                    TableStatus.GetInstance().StatisticBetCurrentUser.get(object.getValue().IdBet).put("round",
                            GMData.GetRoundFromBet(object.getValue().IdBet));
                    js.put("StatisticCurrentUser",
                            TableStatus.GetInstance().StatisticBetCurrentUser.get(object.getValue().IdBet));
                    TableStatus.GetInstance().StatisticBetCurrentUser.remove(object.getValue().IdBet);
                    TableStatus.GetInstance().RequestStatisticBet.remove(
                            TableStatus.GetInstance().StatisticBetCurrentUser.get(object.getValue().IdBet));
                } catch (JSONException ex) {
                    Logger.getLogger(CheckBetArea.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    response.setContentType("application/json; charset=utf-8");
    response.setHeader("Cache-Control", "no-cache");
    response.getWriter().write(js.toString());
}

From source file:CookieReader.java

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

    Cookie cookie = null;/*  w ww  .java  2 s .  c  o m*/
    //Get an array of Cookies associated with this domain
    Cookie[] cookies = request.getCookies();
    boolean hasCookies = false;

    if (cookies != null)
        hasCookies = true;

    // display the name/value of each cookie
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Cookie information</title>");
    out.println("</head>");
    out.println("<body>");
    if (hasCookies) {
        out.println("<h2> The name and value of each found cookie</h2>");
        for (int i = 0; i < cookies.length; i++) {
            cookie = cookies[i];
            out.println("Name of cookie #" + (i + 1) + ": " + cookie.getName() + "<br>");
            out.println("Value of cookie #" + (i + 1) + ": " + cookie.getValue() + "<br><br>");

        }
    } else {
        out.println("<h2> This request did not include any cookies</h2>");
    }

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

    out.close();
}

From source file:nl.surfnet.coin.teams.interceptor.LoginInterceptor.java

private String getTeamsCookie(HttpServletRequest request) {
    String result = "";
    Cookie[] cookies = request.getCookies();
    if (null != cookies) {
        for (Cookie current : cookies) {
            if (current.getName().equals(TEAMS_COOKIE)) {
                result = current.getValue();
                break;
            }//from w  ww.jav  a  2 s .c  om
        }
    }
    return result;
}

From source file:jp.co.opentone.bsol.linkbinder.view.filter.LoginFilter.java

private boolean hasJsessionId(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie c : cookies) {
            if (c.getName() != null && c.getName().equals(JSESSIONID)) {
                return true;
            }//from  www. java2s . 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 av  a 2  s.c om*/
    //Get an array of Cookies associated with this domain
    Cookie[] cookies = request.getCookies();
    boolean hasCookies = false;

    if (cookies != null)
        hasCookies = true;

    // display the name/value of each cookie
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Cookie information</title>");
    out.println("</head>");
    out.println("<body>");
    if (hasCookies) {
        out.println("<h2> The name and value of each found cookie</h2>");

        for (int i = 0; i < cookies.length; i++) {
            cookie = cookies[i];
            out.println("Name of cookie #" + (i + 1) + ": " + cookie.getName() + "<br>");
            out.println("Value of cookie #" + (i + 1) + ": " + cookie.getValue() + "<br><br>");

        }

    } else {
        out.println("<h2> This request did not include any cookies</h2>");
    }

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

    out.close();
}

From source file:com.junly.service.helper.TicketHelper.java

/** <p class="detail">
* ???ticket/*from ww w  .j  a  v  a 2 s  .co  m*/
* </p>
* @author junly
* @date 2017324 
* @param request
* @param httpChannelType
* @return    
*/
public String getTicket(HttpServletRequest request, HttpChannelType httpChannelType) {

    Cookie cookies[] = request.getCookies();

    String ticket = null;

    if (null != cookies) {
        for (Cookie cookie : cookies) {
            if (StringUtils.equals(ViewContants.LOGIN_TICKET_KEY, cookie.getName())) {

                ticket = StringUtils.trim(cookie.getValue());

                StringBuilder builder = new StringBuilder(httpChannelType.name());
                builder.append("?cookieticket=").append(ticket);
                builder.append("?=").append(request.getServerName()).append("====");
                builder.append(cookie.getDomain());
                logger.info(builder.toString());

                break;
            }
        }
    }

    // java??cookieheader???,
    // ???cookie? ??
    if (StringUtils.isBlank(ticket)) {
        ticket = customHeadTicket(request, httpChannelType);
    }

    if (StringUtils.isBlank(ticket)) {
        ticket = StringUtils.trim(request.getParameter(ViewContants.LOGIN_TICKET_KEY));
    }

    return ticket;
}