Example usage for javax.servlet.http Cookie setPath

List of usage examples for javax.servlet.http Cookie setPath

Introduction

In this page you can find the example usage for javax.servlet.http Cookie setPath.

Prototype

public void setPath(String uri) 

Source Link

Document

Specifies a path for the cookie to which the client should return the cookie.

Usage

From source file:com.glaf.core.util.RequestUtils.java

public static void setTheme(HttpServletRequest request, HttpServletResponse response, String theme) {
    if (StringUtils.isNotEmpty(theme)) {
        Cookie cookie = new Cookie(Constants.THEME_COOKIE, theme);
        cookie.setPath("/");
        cookie.setMaxAge(-1);/*from   w  w  w.j a v  a2s  .  c om*/
        response.addCookie(cookie);

        Cookie cookie2 = new Cookie("data-theme", "theme-" + theme);
        cookie2.setPath("/");
        cookie2.setMaxAge(-1);
        response.addCookie(cookie2);
    }
}

From source file:cec.easyshop.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testServerSideCookieDynamicPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(true);//server side
    cookieGenerator.setUseDefaultPath(false);
    BDDMockito.given(request.getContextPath()).willReturn("/some_path");
    cookieGenerator.addCookie(response, "cookie_monster");

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/some_path");
    expectedCookie.setSecure(false);//from w w w  .j av a  2  s .c o  m
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addHeader(EnhancedCookieGenerator.HEADER_COOKIE,
            "JSESSIONID=cookie_monster; Version=1; Domain=\"what a domain\"; Path=/some_path; HttpOnly");
}

From source file:com.glaf.core.util.RequestUtils.java

public static void setTheme(HttpServletRequest request, HttpServletResponse response) {
    String theme = request.getParameter("theme");
    if (StringUtils.isNotEmpty(theme)) {
        Cookie cookie = new Cookie(Constants.THEME_COOKIE, theme);
        cookie.setPath("/");
        cookie.setMaxAge(-1);/*www . j  ava 2s .c o m*/
        response.addCookie(cookie);

        Cookie cookie2 = new Cookie("data-theme", "theme-" + theme);
        cookie2.setPath("/");
        cookie2.setMaxAge(-1);
        response.addCookie(cookie2);
    }
}

From source file:com.haulmont.cuba.web.sys.AppCookies.java

public void removeCookie(String name) {
    if (isCookiesEnabled()) {
        Cookie cookie = getCookie(name);
        if (cookie != null) {
            cookie.setValue(null);// w  w  w.j  a va2  s  .c  o m
            cookie.setPath(getCookiePath());
            cookie.setMaxAge(0);
            addCookie(cookie);
        }
    }
}

From source file:cec.easyshop.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testClientSideCookieDynamicPath() {
    cookieGenerator.setCookieName(JSESSIONID);
    cookieGenerator.setHttpOnly(false);//client side
    cookieGenerator.setCookieSecure(true);
    cookieGenerator.setUseDefaultPath(false);

    BDDMockito.given(request.getContextPath()).willReturn("/some_path");

    cookieGenerator.addCookie(response, "cookie_monster");

    final Cookie expectedCookie = new Cookie(JSESSIONID, "cookie_monster");
    expectedCookie.setPath("/some_path");
    expectedCookie.setSecure(true);/*from   w w w.j  av  a  2 s.  co  m*/
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addCookie(Mockito.argThat(new CookieArgumentMatcher(expectedCookie)));
    assertNoHeaderAdjustments();
}

From source file:co.id.app.sys.util.StringUtils.java

/**
 * Sets the given cookie values in the servlet response.
 * <p/>/* www  . ja  v a2s .co  m*/
 * This will also put the cookie in a list of cookies to send with this request's response
 * (so that in case of a redirect occurring down the chain, the first filter
 * will always try to set this cookie again)
 * <p/>
 * The cookie secure flag is set if the request is secure.
 * <p/>
 * This method was derived from Atlassian <tt>CookieUtils</tt> method of
 * the same name, release under the Apache License.
 *
 * @param request the servlet request
 * @param response the servlet response
 * @param name the cookie name
 * @param value the cookie value
 * @param maxAge the maximum age of the cookie in seconds. A negative
 * value will expire the cookie at the end of the session, while 0 will delete
 * the cookie.
 * @param path the cookie path
 * @return the Cookie object created and set in the response
 */
public static Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name,
        String value, int maxAge, String path) {

    Cookie cookie = new Cookie(name, value);
    cookie.setMaxAge(maxAge);
    cookie.setPath(path);
    cookie.setSecure(request.isSecure());
    response.addCookie(cookie);

    return cookie;
}

From source file:com.atlassian.jira.security.xsrf.SimpleXsrfTokenGenerator.java

private void addNewCookie(HttpServletRequest httpServletRequest, String token,
        HttpServletResponse httpServletResponse) {
    final Cookie cookie = new Cookie(TOKEN_HTTP_SESSION_KEY, token);
    cookie.setPath(getRequestContext(httpServletRequest));
    cookie.setMaxAge(-1); // expire with the browser exit
    cookie.setSecure(httpServletRequest.isSecure());
    httpServletResponse.addCookie(cookie);
    httpServletRequest.setAttribute(SET_COOKIE_PENDING, token);
}

From source file:org.apereo.portal.url.UrlCanonicalizingFilter.java

protected void clearRedirectCount(HttpServletRequest request, HttpServletResponse response) {
    final Cookie cookie = new Cookie(COOKIE_NAME, "");
    cookie.setPath(request.getContextPath());
    cookie.setMaxAge(0);// www  .j  av  a  2s  .c o m
    response.addCookie(cookie);
}

From source file:com.acc.storefront.security.cookie.EnhancedCookieGeneratorTest.java

@Test
public void testServerSideCookieDefaultPath() {
    cookieGenerator.setCookieName("guid");
    cookieGenerator.setHttpOnly(true);//server side

    BDDMockito.given(request.getContextPath()).willReturn("/some_path");

    cookieGenerator.addCookie(response, "cookie_monster");

    final Cookie expectedCookie = new Cookie("guid", "cookie_monster");
    expectedCookie.setPath("/");
    expectedCookie.setSecure(false);/*  w w w.  ja  va 2  s  .com*/
    expectedCookie.setMaxAge(NEVER_EXPIRES);
    expectedCookie.setDomain("what a domain");

    Mockito.verify(response).addCookie(Mockito.argThat(new CookieArgumentMatcher(expectedCookie)));
    Mockito.verify(response).addHeader(EnhancedCookieGenerator.HEADER_COOKIE,
            "guid=cookie_monster; Domain=\"what a domain\"; Path=/; HttpOnly");

}

From source file:controllers.LoginController.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//w  w  w .ja  v  a  2s  . c  o  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 {
    response.setContentType("text/html;charset=UTF-8");
    //User user = (User) request.getAttribute("user");
    //if (user != null) {
    // User has been already registered
    //response.sendRedirect(request.getContextPath() + "/home");
    //} else {
    // Login in user
    String email = request.getParameter("email"), password = request.getParameter("pass");

    JSONObject object = null;
    object = (JSONObject) ISConnector.validateLogin(email, password);
    if (object.containsKey("token")) {
        Cookie cookie = new Cookie("token", (String) object.get("token"));
        cookie.setPath("/");
        long expiredDate = -1;
        if (object.containsKey("expiry_date")) {
            expiredDate = (long) object.get("expiry_date") - new Timestamp(new Date().getTime()).getTime();
            expiredDate /= 1000;
            cookie.setMaxAge((int) expiredDate);
        }
        response.addCookie(cookie);
        response.sendRedirect(request.getContextPath() + "/home");
    } else if (object.containsKey("error")) {
        request.setAttribute("error", (String) object.get("error"));
        String error = (String) object.get("error");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet coba2</title>");
            out.println("</head>");
            out.println("<body>");
            out.println(error);
            out.println("</body>");
            out.println("</html>");

        }
        //doGet(request, response);
    } else {
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet coba2</title>");
            out.println("</head>");
            out.println("<body>");
            out.println(object);
            out.println("</body>");
            out.println("</html>");

        }
    }
    //}
}