CookieMap.java :  » J2EE » jfox » com » ibatis » struts » httpmap » Java Open Source

Java Open Source » J2EE » jfox 
jfox » com » ibatis » struts » httpmap » CookieMap.java
package com.ibatis.struts.httpmap;

import java.util.Enumeration;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Map to wrap cookie names and values (READ ONLY). <p/>Date: Mar 11, 2004
 * 11:31:35 PM
 * 
 * @author Clinton Begin
 */
public class CookieMap extends BaseHttpMap {

    private Cookie[] cookies;

    public CookieMap(HttpServletRequest request) {
        cookies = request.getCookies();
    }

    protected Enumeration getNames() {
        return new CookieEnumerator(cookies);
    }

    protected Object getValue(Object key) {
        for (int i = 0; i < cookies.length; i++) {
            if (key.equals(cookies[i].getName())) {
                return cookies[i].getValue();
            }
        }
        return null;
    }

    protected void putValue(Object key, Object value) {
        throw new UnsupportedOperationException();
    }

    protected void removeValue(Object key) {
        throw new UnsupportedOperationException();
    }

    /**
     * Cookie Enumerator Class
     */
    private class CookieEnumerator implements Enumeration {

        private int i = 0;

        private Cookie[] cookieArray;

        public CookieEnumerator(Cookie[] cookies) {
            this.cookieArray = cookies;
        }

        public synchronized boolean hasMoreElements() {
            return cookieArray.length > i;
        }

        public synchronized Object nextElement() {
            Object element = cookieArray[i];
            i++;
            return element;
        }

    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.