001    package de.deepamehta.core.service;
002    
003    import com.sun.jersey.spi.container.ContainerRequest;
004    
005    import javax.ws.rs.core.Cookie;
006    
007    import java.util.Collection;
008    import java.util.HashMap;
009    import java.util.Map;
010    
011    
012    
013    /**
014     * Cookies obtained from request headers.
015     */
016    public class Cookies {
017    
018        // ---------------------------------------------------------------------------------------------- Instance Variables
019    
020        private Map<String, String> values = new HashMap();
021    
022        // ------------------------------------------------------------------------------------------------- Class Variables
023    
024        private static final ThreadLocal<ContainerRequest> threadLocalRequest = new ThreadLocal();
025    
026        // ---------------------------------------------------------------------------------------------------- Constructors
027    
028        private Cookies() {
029        }
030    
031        private Cookies(Collection<Cookie> cookies) {
032            for (Cookie cookie : cookies) {
033                values.put(cookie.getName(), cookie.getValue());
034            }
035        }
036    
037        // -------------------------------------------------------------------------------------------------- Public Methods
038    
039        /**
040         * Returns the value of the cookie for the given name, or throws an exception if no such cookie exists.
041         */
042        public String get(String name) {
043            String value = values.get(name);
044            //
045            if (value == null) {
046                throw new RuntimeException("Missing \"" + name + "\" cookie (cookies=" + values + ")");
047            }
048            //
049            return value;
050        }
051    
052        /**
053         * Convenience method to access a long value of the cookie for the given name, or throws an exception
054         * if no such cookie exists.
055         */
056        public long getLong(String name) {
057            try {
058                return Long.parseLong(get(name));
059            } catch (Exception e) {
060                throw new RuntimeException("Getting a long value for the \"" + name + "\" cookie failed", e);
061            }
062        }
063    
064        // ---
065    
066        /**
067         * Checks if there is a cookie with the given name.
068         */
069        public boolean has(String name) {
070            return values.get(name) != null;
071        }
072    
073        // ---
074    
075        public static Cookies get() {
076            ContainerRequest request = threadLocalRequest.get();
077            if (request != null) {
078                return new Cookies(request.getCookies().values());
079            } else {
080                return new Cookies();
081            }
082        }
083    
084        // ### TODO: define public Cookies interface and hide this internal method
085        public static void set(ContainerRequest request) {
086            threadLocalRequest.set(request);
087        }
088    
089        // ---
090    
091        @Override
092        public String toString() {
093            return values.toString();
094        }
095    }