Popular Posts
Enable edit option in Shutter in Linux sudo apt-get install libgoo-canvas-perl Reference: How To Fix Disabled Edit Option In Shutter in Linux Mint CORS in Asp.net MVC Web API v2 Step 1. Install cors from NeGet Step 2. Enable cors in config using System; using System.Collections.Generic; using System.Linq; using ... DNS SERVER LIST Google 8.8.8.8 8.8.4.4 TWNIC 192.83.166.11 211.72.210.250 HiNet 168.95.1.1 168.95.192.1 Seednet 北區 DNS (台北, 桃園, 新竹, 宜蘭, 花蓮, 苗栗) 139....
Blog Archive
Stats
set/remove cookie using applet
jdk/jre 1.4 later, the library is included in plugin.jar file.
import java.applet.Applet;
import java.util.ArrayList;
import java.util.Date;

import netscape.javascript.JSObject;
import sgb.util.Strings;

public class CookieManager {
    private Applet applet;

    public CookieManager(Applet applet) {
        this.applet = applet;
    }

    private JSObject getDocument() {
        JSObject win = (JSObject) JSObject.getWindow(applet);
        return (JSObject) win.getMember("document");
    }

    public void setCookie(String name, String value) {
        if (Strings.isNullOrEmpty(name))
            throw new IllegalArgumentException();
        Cookie cookie = new Cookie();
        cookie.setName(name);
        cookie.setValue(value);
        setCookie(cookie);
    }

    public void setCookie(Cookie cookie) {
        if (cookie == null)
            throw new IllegalArgumentException();
        JSObject doc = getDocument();
        doc.setMember("cookie", cookie.toString());
    }

    public String getCookieValue(String name) {
        return getCookie(name).getValue();
    }

    public Cookie getCookie(String name) {
        if (Strings.isNullOrEmpty(name))
            throw new IllegalArgumentException();
        Cookie[] cookies = getCookies();
        for (Cookie cookie : cookies)
            if (cookie.getName().equals(name))
                return cookie;
        return null;
    }

    public Cookie[] getCookies() {
        JSObject doc = getDocument();
        String[] token = ((String) doc.getMember("cookie")).trim().split(";");
        ArrayList<Cookie> cks = new ArrayList<Cookie>();
        for (String tk : token) {
            String[] tks = tk.trim().split("=");
            String name = tks[0];
            String value = tks.length > 1 ? tks[1] : null;
            Cookie cookie = new Cookie();
            cookie.setName(name);
            cookie.setValue(value);
            cks.add(cookie);
        }
        return cks.toArray(new Cookie[0]);
    }

    public void removeCookie(String name) {
        if (Strings.isNullOrEmpty(name))
            throw new IllegalArgumentException();
        Cookie cookie = new Cookie();
        cookie.setName(name);
        cookie.setExpireDate(new Date(0));
        setCookie(cookie);
    }

}