Immutable object representing a single browser cookie : Cookie « GWT « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » GWT » Cookie 




Immutable object representing a single browser cookie


/*
 * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
 *    http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gwtwidgets.client.util;

import java.util.Date;


/**
 * Immutable object representing a single browser cookie. 
 
 @author Brian Glick
 */
public class Cookie
{
    private String name;
    private String value;



    Cookie (String name, String value)
    {
        super();
        this.name = name;
        this.value = value;
    }

    /**
     * Gets the name of the cookie
     @return
     */
    public String getName ()
    {
        return name;
    }

    /**
     * Get's the cookie's value
     @return
     */
    public String getValue ()
    {
        return value;
    }

    public void write (Date expires)
    {
        CookieUtils.write(this, expires);
    }
}
/////////////////////////////////
/*
 * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
 *    http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gwtwidgets.client.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Class that is responsible for reading and writing cookies.
 
 @author Brian Glick
 @author rhanson
 */
public class CookieUtils {

    /**
     * Creates, sets, and returns the Cookie object
     @param name
     @param value
     @param expires
     @param path
     @return the Cookie created or reset
     */
    public static Cookie write (String name, String value, Date expires, String path)
    {
        boolean useExp = false;
        int year = -1, month = -1, day = -1;
        if (expires != null) {
            useExp = true;
            year = expires.getYear() 1900;
            month = expires.getMonth();
            day = expires.getDate();
        }
        writeNative(name, value, useExp, year, month, day, path);
        return new Cookie(name, value);
    }

    public static Cookie write (String name, String value, Date expires)
    {
        return write(name, value, expires, "/");
    }

    
    private static void writeNative(String name, String value, boolean useExpires, int year, int month, int day)
    {
        writeNative(name, value, useExpires, year, month, day, "/");
    }

    private static native void writeNative(String name, String value, boolean useExpires, int year, int month, int day, String path/*-{
        var expStr = (useExpires) ? "; expires=" + new Date(year, month, day).toGMTString() : "";
        $doc.cookie = name + "=" + escape(value) + expStr + "; path=" + path;
    }-*/;
    
    /**
     * Erases cookie with given name.
     @param name
     */
    public static void erase (String name)
    {
        writeNative(name, "", true, 197011);
    }

    /**
     * Gets the value of the cookie with the given name.
     @param name Name of cookie whose value should be retrieved
     @return Cookie value or null if cookie is not found
     */
    public static String readValue (String name)
    {
        Cookie c = read(name);
        return c == null null : c.getValue();
    }
    
    /**
     * Gets the cookie with the given name.
     @param name Name of cookie to get.
     @return Cookie or null if the cookie is not found.
     */
    public static Cookie read (String name)
    {
        String val = getValue(name);
        return (val == nullnull new Cookie(name, val);
    }

    /**
     * Tries to load the cookie, but only sets fields if they are found
     @param name
     @param c
     */
    private static native String getValue (String name/*-{
         var start = $doc.cookie.indexOf(name + '=');
        var len = start + name.length + 1;
        if ((!start) && (name != $doc.cookie.substring(0,name.length))) {
            return null;
        }
        if (start == -1) {
            return null;
        }
        var end = document.cookie.indexOf(';',len);
        if (end == -1) {
            end = $doc.cookie.length;
        }
        return unescape($doc.cookie.substring(len,end));
    }-*/;
    
    /**
     * Erase the given cookie
     @param c
     */
    public static void erase (Cookie c)
    {
        erase(c.getName());
    }
    
    /**
     * Write the given cookie
     @param c
     */
    public static void write (Cookie c, Date expires)
    {
        write(c.getName(), c.getValue(), expires);
    }

    public static Cookie[] getAll ()
    {
        Map cookies = getCookieMap();
        Cookie[] rVal = new Cookie[cookies.size()];
        int counter = 0;
        for (Iterator i = cookies.keySet().iterator(); i.hasNext(); counter++) {
            String name = (Stringi.next();
            rVal[counternew Cookie(name, (Stringcookies.get(name));
        }
        return rVal;
    }

    private static Map getCookieMap ()
    {
        Map jar = new HashMap();
        fillCookieJar(jar);
        return jar;
    }
    
    private static native void fillCookieJar (Map jar/*-{
        var cookies = $doc.cookie;
        if (cookies && cookies != '') {
            var cl = cookies.split('; ');
            for (var i = 0; i < cl.length; ++i) {
                var parts = cl[i].split('=');
                [email protected]::put(Ljava/lang/Object;Ljava/lang/Object;)(parts[0], unescape(parts[1]));
            }
        }
    }-*/;

}


           
       














Related examples in the same category
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.