Example usage for javax.servlet.http Cookie setMaxAge

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

Introduction

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

Prototype

public void setMaxAge(int expiry) 

Source Link

Document

Sets the maximum age in seconds for this Cookie.

Usage

From source file:org.apache.hive.service.cli.thrift.ThriftHttpServlet.java

/**
 * Generate a server side cookie given the cookie value as the input.
 * @param str Input string token./* w  w  w  .j a  v  a 2  s .c  o m*/
 * @return The generated cookie.
 * @throws UnsupportedEncodingException
 */
private Cookie createCookie(String str) throws UnsupportedEncodingException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Cookie name = " + AUTH_COOKIE + " value = " + str);
    }
    Cookie cookie = new Cookie(AUTH_COOKIE, str);

    cookie.setMaxAge(cookieMaxAge);
    if (cookieDomain != null) {
        cookie.setDomain(cookieDomain);
    }
    if (cookiePath != null) {
        cookie.setPath(cookiePath);
    }
    cookie.setSecure(isCookieSecure);
    return cookie;
}

From source file:com.salesmanager.checkout.flow.ComitOrderAction.java

/**
 * Process Payment Save Order entity/*from ww w.  j  av  a2 s.  c  o  m*/
 * 
 * @return
 */
public String comitOrder() {

    // Get all entities

    Order order = SessionUtil.getOrder(getServletRequest());
    MerchantStore store = SessionUtil.getMerchantStore(getServletRequest());

    PaymentMethod payment = SessionUtil.getPaymentMethod(getServletRequest());

    ShippingInformation shippingInformation = SessionUtil.getShippingInformation(getServletRequest());
    Customer customer = SessionUtil.getCustomer(getServletRequest());

    if (super.getServletRequest().getSession().getAttribute("TRANSACTIONCOMITED") != null) {
        addActionError(getText("error.transaction.duplicate",
                new String[] { String.valueOf(order.getOrderId()), store.getStoreemailaddress() }));
        return "GENERICERROR";
    }

    OrderService oservice = (OrderService) ServiceFactory.getService(ServiceFactory.OrderService);

    try {

        SystemService sservice = (SystemService) ServiceFactory.getService(ServiceFactory.SystemService);
        long nextOrderId = sservice.getNextOrderIdSequence();
        order.setOrderId(nextOrderId);

        OrderTotalSummary summary = SessionUtil.getOrderTotalSummary(getServletRequest());

        Shipping shipping = null;
        if (shippingInformation != null) {
            shipping = new Shipping();
            shipping.setHandlingCost(shippingInformation.getHandlingCost());
            shipping.setShippingCost(shippingInformation.getShippingOptionSelected().getOptionPrice());
            shipping.setShippingModule(shippingInformation.getShippingOptionSelected().getModule());
            shipping.setShippingDescription(shippingInformation.getShippingOptionSelected().getDescription());
        }

        Map orderProducts = SessionUtil.getOrderProducts(getServletRequest());

        Set s = new HashSet();

        for (Object o : orderProducts.values()) {

            OrderProduct op = (OrderProduct) o;
            s.add(op);
        }

        order.setOrderProducts(s);

        // ajust order object
        order.setCustomerEmailAddress(customer.getCustomerEmailAddress());

        String comments = null;
        if (this.getOrderHistory() != null) {
            comments = this.getOrderHistory().getComments();
        }

        // Order, PaymentMethod,
        ProcessorContext context = new ProcessorContext();

        Collection files = oservice.getOrderProductDownloads(order.getOrderId());
        if (files != null && files.size() > 0) {
            context.addObject("files", files);

        }

        context.addObject("Order", order);
        context.addObject("Customer", customer);
        context.addObject("MerchantStore", store);
        context.addObject("PaymentMethod", payment);
        context.addObject("Shipping", shipping);
        context.addObject("Locale", super.getLocale());
        context.addObject("OrderTotalSummary", summary);
        context.addObject("comments", comments);
        context.addObject("products", orderProducts.values());

        WorkflowProcessor wp = (WorkflowProcessor) SpringUtil.getBean("orderWorkflow");
        wp.doWorkflow(context);

        // set an indicator in HTTPSession to prevent duplicates
        super.getServletRequest().getSession().setAttribute("TRANSACTIONCOMITED", "true");

        if (!StringUtils.isBlank(comments)) {
            SessionUtil.setOrderStatusHistory(this.getOrderHistory(), getServletRequest());
        }

    } catch (Exception e) {
        if (e instanceof TransactionException) {
            super.addErrorMessage("error.payment.paymenterror");
            return "PAYMENTERROR";
        }

        if (e instanceof OrderException) {
            try {
                oservice.sendOrderProblemEmail(order.getMerchantId(), order, customer, store);
            } catch (Exception ee) {
                log.error(ee);
            }
        }

        addActionError(getText("message.error.comitorder.error",
                new String[] { String.valueOf(order.getOrderId()), store.getStoreemailaddress() }));
        log.error(e);
        return "GENERICERROR";
    }
    //cleanup

    //delete shopping cart cookie
    Cookie c = new Cookie(CatalogConstants.CART_COOKIE_NAME, "");
    c.setMaxAge(0);
    super.getServletResponse().addCookie(c);

    return SUCCESS;

}

From source file:com.codeabovelab.dm.gateway.proxy.common.HttpProxy.java

/**
 * Copy cookie from the proxy to the servlet client.
 * Replaces cookie path to local path and renames cookie to avoid collisions.
 *//*from w  ww  .  j a  v a  2s . c  om*/
private void copyProxyCookie(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
        Header header) {
    List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
    String path = servletRequest.getContextPath(); // path starts with / or is empty string
    path += servletRequest.getServletPath(); // servlet path starts with / or is empty string
    for (int i = 0, l = cookies.size(); i < l; i++) {
        HttpCookie cookie = cookies.get(i);
        //set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies
        String proxyCookieName = getCookieNamePrefix() + cookie.getName();
        Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
        servletCookie.setComment(cookie.getComment());
        servletCookie.setMaxAge((int) cookie.getMaxAge());
        servletCookie.setPath(path); //set to the path of the proxy servlet
        // don't set cookie domain
        servletCookie.setSecure(cookie.getSecure());
        servletCookie.setVersion(cookie.getVersion());
        servletResponse.addCookie(servletCookie);
    }
}

From source file:com.appeligo.search.actions.BaseAction.java

public String getLineup() {
    String lineup = null;/*from www  .  j a va  2 s.  co  m*/
    //Get if from the user if there is one
    User user = getUser();
    if (user != null) {
        lineup = user.getLineupId();
        getServletRequest().getSession().setAttribute(LINEUP_ID, lineup);
    } else {
        lineup = (String) getServletRequest().getSession().getAttribute(LINEUP_ID);
        if (lineup == null) {
            // No user, and its not stored in the session, so check for a cookie. If there is no cookie, default them to pacific
            //Right now the lineup is not getting stored in the session when it is loaded by the cookie.
            //The reason is that the cookie gets set before they login and it would not get set with
            //The lineup from the user.
            Cookie[] cookies = getServletRequest().getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(LINEUP_ID)) {
                        cookie.setMaxAge(Integer.MAX_VALUE);
                        lineup = cookie.getValue();
                        break;
                    }
                }
            }
            if (lineup == null) {
                lineup = DEFAULT_LINEUP;
                Cookie cookie = new Cookie(LINEUP_ID, lineup);
                cookie.setMaxAge(Integer.MAX_VALUE);
                response.addCookie(cookie);
                getServletRequest().getSession().setAttribute(LINEUP_ID, lineup);
            }
        }
    }
    return lineup;
}

From source file:com.appeligo.search.actions.BaseAction.java

public TimeZone getTimeZone() {
    User user = getUser();//from  w  w w.  ja  va  2s  .c  om
    if (user != null) {
        getServletRequest().getSession().setAttribute(TIMEZONE_ID, user.getTimeZone());
        return user.getTimeZone();
    } else {
        TimeZone zone = (TimeZone) getServletRequest().getSession().getAttribute(TIMEZONE_ID);
        if (zone == null) {
            String timeZoneId = null;
            Cookie[] cookies = getServletRequest().getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(TIMEZONE_ID)) {
                        cookie.setMaxAge(Integer.MAX_VALUE);
                        timeZoneId = cookie.getValue();
                        break;
                    }
                }
            }
            if (timeZoneId == null) {
                timeZoneId = DEFAULT_TIMEZONE_ID;
                Cookie cookie = new Cookie(TIMEZONE_ID, timeZoneId);
                cookie.setMaxAge(Integer.MAX_VALUE);
                response.addCookie(cookie);
            }
            zone = TimeZone.getTimeZone(timeZoneId);
            getServletRequest().getSession().setAttribute(TIMEZONE_ID, zone);
            return zone;
        } else {
            return zone;
        }
    }
}

From source file:org.craftercms.social.util.support.security.CrafterProfileFilter.java

/**
 * Build cipher auth cookie/* w  ww.j  ava2 s  . c  o  m*/
 *
 * @param cipher
 * @param profileToken
 * @param userProfile
 * @return
 * @throws org.craftercms.social.exceptions.AuthenticationException
 */
private Cookie getCipherCookie(SimpleDesCipher cipher, String profileToken, Profile userProfile)
        throws org.craftercms.social.exceptions.AuthenticationException {
    Cookie cipherAuth = new Cookie(cipherTokenCookieKey,
            generateEncryptedToken(cipher, profileToken, userProfile));
    cipherAuth.setMaxAge(60 * 60 * 8);
    cipherAuth.setPath(CRAFTER_SOCIAL_COOKIE_PATH);
    return cipherAuth;
}

From source file:com.adito.core.CoreUtil.java

/**
 * Store the provided user interface state name / value pair in a cookie
 * /*  ww w . j  a va 2  s  . c  o  m*/
 * @param name ui state cookie name
 * @param value ui state cookie value
 * @param request request
 * @param response response
 */
public static void storeUIState(String name, String value, HttpServletRequest request,
        HttpServletResponse response) {
    Cookie c = getCookie(name, request);
    if (c != null) {
        c.setValue(value);
    } else {
        c = new Cookie(name, value);
    }
    c.setMaxAge(-1);
    response.addCookie(c);
}

From source file:custom.application.login.java

public void logout() {
    HttpServletRequest request = (HttpServletRequest) this.context.getAttribute("HTTP_REQUEST");
    HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");

    try {/*from  www. j a v  a  2 s. c  om*/
        this.passport = new passport(request, response, "waslogined");
        this.passport.logout();

        if (request.getCookies() != null) {
            Cookie[] cookies = request.getCookies();
            int i = 0;
            Cookie cookie;
            while (cookies.length > i) {
                cookie = cookies[i];
                cookie.setMaxAge(0);
                cookie.setValue("");
                response.addCookie(cookie);
                i++;
            }
        }

        Reforward reforward = new Reforward(request, response);

        reforward.setDefault(this.getLink(this.context.getAttribute("default.login.page").toString()));
        reforward.forward();
    } catch (ApplicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:fr.paris.lutece.plugins.mylutece.modules.openam.service.OpenamService.java

/**
 * set a paris connect cokkie in the HttpServletResponse
 *
 * @param strPCUID//  w ww .  j  ava 2 s  . c  om
 *            the user PCUID
 * @param response
 *            The HTTP response
 */
public void removeConnectionCookie(HttpServletResponse response) {
    // remove  openam cookie using the setMaxAgeParameters
    Cookie openamCookie = new Cookie(COOKIE_OPENAM_NAME, null);
    openamCookie.setDomain(COOKIE_OPENAM_DOMAIN);
    openamCookie.setSecure(COOKIE_OPENAM_SECURE);
    openamCookie.setMaxAge(0);
    openamCookie.setPath(COOKIE_OPENAM_PATH);
    response.addCookie(openamCookie);
}

From source file:hudson.Functions.java

/**
 * Used by <tt>layout.jelly</tt> to control the auto refresh behavior.
 *
 * @param noAutoRefresh//from w  w  w .  ja v  a2s .  c o m
 *      On certain pages, like a page with forms, will have annoying interference
 *      with auto refresh. On those pages, disable auto-refresh.
 */
public static void configureAutoRefresh(HttpServletRequest request, HttpServletResponse response,
        boolean noAutoRefresh) {
    if (noAutoRefresh)
        return;

    String param = request.getParameter("auto_refresh");
    boolean refresh = isAutoRefresh(request);
    if (param != null) {
        refresh = Boolean.parseBoolean(param);
        Cookie c = new Cookie("hudson_auto_refresh", Boolean.toString(refresh));
        // Need to set path or it will not stick from e.g. a project page to the dashboard.
        // Using request.getContextPath() might work but it seems simpler to just use the hudson_ prefix
        // to avoid conflicts with any other web apps that might be on the same machine.
        c.setPath("/");
        c.setMaxAge(60 * 60 * 24 * 30); // persist it roughly for a month
        response.addCookie(c);
    }
    if (refresh) {
        response.addHeader("Refresh", "10");
    }
}