Example usage for javax.servlet.http HttpServletResponse addCookie

List of usage examples for javax.servlet.http HttpServletResponse addCookie

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addCookie.

Prototype

public void addCookie(Cookie cookie);

Source Link

Document

Adds the specified cookie to the response.

Usage

From source file:com.tenduke.example.scribeoauth.SessionManager.java

/**
 * Creates an authenticated session.//w  ww  . jav  a 2 s  . c  o m
 * @param request Client HTTP request.
 * @param response HTTP response.
 * @param user User information as a JSON object.
 */
public void createSession(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    //
    final String sessionId = UUID.randomUUID().toString();
    final SessionInformation sessionInfo = new SessionInformation(sessionId, user);
    //
    final String cookieValue = MessageFormat.format(SESSION_COOKIE_PATTERN, sessionId,
            resolveUserProfileId(user));
    //
    setSessionInformation(sessionInfo);
    //
    final Cookie cookie = new Cookie(SIGNED_SESSION_COOKIE_NAME, cookieValue);
    cookie.setMaxAge(COOKIE_MAX_AGE);
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:com.orangeleap.webtools.controller.json.LogoutController.java

@RequestMapping("/logout.json")
public void logout(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String guid = request.getParameter("guid");

    final String sessionId = request.getParameter("sessionId");

    if (StringUtils.isNotBlank(sessionId)) {
        final Cookie sessionCookies[] = request.getCookies();
        Cookie sessionCookie = null;//from   w  w w. j  a va2 s  .c o m

        for (final Cookie aCookie : sessionCookies) {
            if (aCookie.getName().equals("sessionId")) {
                aCookie.setMaxAge(0);
                aCookie.setValue("");
                sessionCookie = aCookie;
                break;
            }
        }
        if (sessionCookie == null) {
            sessionCookie = new Cookie("sessionId", "");
        }
        response.addCookie(sessionCookie);
        sessionCache.remove(sessionId);
    }

    final Widget widget = widgetService.getWidget(guid);
    final String authenticationUrl = widget.getWidgetAuthenticationURL();

    response.sendRedirect(authenticationUrl);
}

From source file:com.tenduke.example.webadmin.SessionManager.java

/**
 * Creates an authenticated session.//w w  w  . jav  a 2s .  c  om
 * @param request Client HTTP request.
 * @param response HTTP response.
 * @param user User information as a JSON object.
 * @return Session information object as result.
 */
public SessionInformation createSession(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    //
    final String sessionId = UUID.randomUUID().toString();
    final SessionInformation sessionInfo = new SessionInformation(sessionId, user);
    //
    final String cookieValue = MessageFormat.format(SESSION_COOKIE_PATTERN, sessionId,
            resolveUserProfileId(user));
    //
    setSessionInformation(sessionInfo);
    //
    final Cookie cookie = new Cookie(SIGNED_SESSION_COOKIE_NAME, cookieValue);
    cookie.setMaxAge(COOKIE_MAX_AGE);
    cookie.setPath("/");
    response.addCookie(cookie);
    //
    return sessionInfo;
}

From source file:org.apache.sling.auth.xing.login.impl.XingLoginAuthenticationHandler.java

protected void deleteCookies(final HttpServletRequest request, final HttpServletResponse response) {
    final Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (final Cookie cookie : cookies) {
            final String name = cookie.getName();
            logger.debug("cookie found: '{}'", name);
            if (name.equals(xingCookie) || name.equals(userCookie) || name.equals(userIdCookie)) {
                logger.debug("deleting cookie '{}' with value '{}'", cookie.getName(), cookie.getValue());
                cookie.setValue(null);/*from   ww  w .  j  av a 2 s. co m*/
                cookie.setMaxAge(0);
                response.addCookie(cookie);
            }
        }
    }
}

From source file:com.nkapps.billing.services.SearchServiceImpl.java

@Override
public String execSearchByDate(HttpServletRequest request, HttpServletResponse response) {
    Cookie sbdCookie = null;//www  . jav a2 s .  c o m

    String searchByDate = request.getParameter("searchByDate");
    if (searchByDate == null) {
        Cookie[] requestCookies = request.getCookies();
        for (Cookie c : requestCookies) {
            if (c.getName().equals("searchByDate")) {
                sbdCookie = c;
            }
        }
        if (sbdCookie != null) {
            searchByDate = sbdCookie.getValue();
        } else {
            searchByDate = new SimpleDateFormat("dd.MM.yyyy").format(Calendar.getInstance().getTime());
        }
    } else {
        sbdCookie = new Cookie("searchByDate", searchByDate);
        sbdCookie.setPath("/");
        response.addCookie(sbdCookie);
    }
    return searchByDate;
}

From source file:com.lti.system.MyLogoutFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!(request instanceof HttpServletRequest)) {
        throw new ServletException("Can only process HttpServletRequest");
    }//from w w w .  j a v  a 2  s . c  om

    if (!(response instanceof HttpServletResponse)) {
        throw new ServletException("Can only process HttpServletResponse");
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    if (requiresLogout(httpRequest, httpResponse)) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (logger.isDebugEnabled()) {
            logger.debug("Logging out user '" + auth + "' and redirecting to logout page");
        }

        for (int i = 0; i < handlers.length; i++) {
            handlers[i].logout(httpRequest, httpResponse, auth);
        }

        Cookie cookie = new Cookie("jforumSSOCookie", null);
        cookie.setMaxAge(0);
        cookie.setPath("/jforum");
        httpResponse.addCookie(cookie);

        cookie = new Cookie("jforumSSOGroupCookie", null);
        cookie.setMaxAge(0);
        cookie.setPath("/jforum");
        httpResponse.addCookie(cookie);

        request.removeAttribute("legalDate");

        sendRedirect(httpRequest, httpResponse, logoutSuccessUrl);

        return;
    }

    chain.doFilter(request, response);
}

From source file:com.silverpeas.authentication.AuthenticationServlet.java

/**
 * Write connections cookie./*ww w .j  av  a2s  . c  om*/
 *
 * @param name
 * @param value
 * @param duration
 * @return
 */
private void writeCookie(HttpServletResponse response, String name, String value, int duration,
        boolean secure) {
    String cookieValue;
    try {
        cookieValue = URLEncoder.encode(value, CharEncoding.UTF_8);
    } catch (UnsupportedEncodingException ex) {
        cookieValue = value;
    }
    Cookie cookie = new Cookie(name, cookieValue);
    cookie.setMaxAge(duration);
    cookie.setPath("/");
    if (secure) {
        cookie.setSecure(true);
    }
    response.addCookie(cookie);
}

From source file:fr.mby.portal.coreimpl.session.MemorySessionManager.java

@Override
public void initPortalSession(final HttpServletRequest request, final HttpServletResponse response) {
    String portalSessionId = this.getPortalSessionId(request);

    if (portalSessionId == null) {
        // Can't find session Id => session wasn't initialized
        portalSessionId = this.genSessionId(request);

        this.initSessionBucket(portalSessionId);

        // Put sessionId in Cookie
        final Cookie portalSessionCookie = new Cookie(IPortal.PORTAL_SESSION_ID_COOKIE_NAME, portalSessionId);
        portalSessionCookie.setPath("/");
        response.addCookie(portalSessionCookie);

        // Put sessionId in current Http request
        request.setAttribute(IPortal.PORTAL_SESSION_ID_PARAM_NAME, portalSessionId);
    }/*from  w  w  w  .  j  a  v a 2 s . c  o m*/

}

From source file:ShoppingCartViewerCookie.java

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

    String sessionid = null;/*from   w  w w .  ja v  a 2s.c  om*/
    Cookie[] cookies = req.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("sessionid")) {
                sessionid = cookies[i].getValue();
                break;
            }
        }
    }

    // If the session ID wasn't sent, generate one.
    // Then be sure to send it to the client with the response.
    if (sessionid == null) {
        sessionid = generateSessionId();
        Cookie c = new Cookie("sessionid", sessionid);
        res.addCookie(c);
    }

    out.println("<HEAD><TITLE>Current Shopping Cart Items</TITLE></HEAD>");
    out.println("<BODY>");

    // Cart items are associated with the session ID
    String[] items = getItemsFromCart(sessionid);

    // Print the current cart items.
    out.println("You currently have the following items in your cart:<BR>");
    if (items == null) {
        out.println("<B>None</B>");
    } else {
        out.println("<UL>");
        for (int i = 0; i < items.length; i++) {
            out.println("<LI>" + items[i]);
        }
        out.println("</UL>");
    }

    // Ask if they want to add more items or check out.
    out.println("<FORM ACTION=\"/servlet/ShoppingCart\" METHOD=POST>");
    out.println("Would you like to<BR>");
    out.println("<INPUT TYPE=SUBMIT VALUE=\" Add More Items \">");
    out.println("<INPUT TYPE=SUBMIT VALUE=\" Check Out \">");
    out.println("</FORM>");

    // Offer a help page.
    out.println("For help, click <A HREF=\"/servlet/Help" + "?topic=ShoppingCartViewerCookie\">here</A>");

    out.println("</BODY></HTML>");
}