001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.harvester.webinterface;
024
025import javax.servlet.http.Cookie;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029public class CookieUtils {
030
031    /**
032     * Some cookie lifespan to play with.
033     */
034    public static enum Lifespan {
035
036        MINUTE(60), HOUR(60 * 60), DAY(24 * 60 * 60), WEEK(7 * 24 * 60 * 60);
037
038        private final int seconds;
039
040        private Lifespan(int seconds) {
041            this.seconds = seconds;
042        }
043
044        public int getSeconds() {
045            return seconds;
046        }
047
048    }
049
050    /**
051     * Returns the value of a request parameter, or if not found tries to find a cookie with the same name.
052     *
053     * @param request the HTTP request
054     * @param name the parameter name
055     * @return the value (never null, may be empty)
056     */
057    public static final String getParameterValue(HttpServletRequest request, String name) {
058        String value = request.getParameter(name);
059        if ((value == null || value.isEmpty()) && request.getCookies() != null) {
060            for (Cookie c : request.getCookies()) {
061                if (name.equals(c.getName())) {
062                    value = c.getValue();
063                }
064            }
065        }
066        return (value != null ? value : "");
067    }
068
069    /**
070     * Set a cookie on the client.
071     *
072     * @param response the HTTP response wrapper
073     * @param name the cookie name
074     * @param value the cookie value
075     * @param lifeSpan the cookie TTL as an {@link Lifespan} enum value
076     */
077    public static final void setCookie(HttpServletResponse response, String name, String value, Lifespan lifeSpan) {
078        Cookie c = new Cookie(name, value);
079        c.setMaxAge(lifeSpan.getSeconds());
080        response.addCookie(c);
081    }
082
083    /**
084     * Set a cookie on the client, with a default lifespan of @see Lifespan#HOUR
085     *
086     * @param response the HTTP response wrapper
087     * @param name the cookie name
088     * @param value the cookie value
089     */
090    public static final void setCookie(HttpServletResponse response, String name, String value) {
091        setCookie(response, name, value, Lifespan.HOUR);
092    }
093
094}