Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement...
Blog Archive
Stats
set/remove cookie using applet
jdk/jre 1.4 later, the library is included in plugin.jar file.
  1. import java.applet.Applet;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4.  
  5. import netscape.javascript.JSObject;
  6. import sgb.util.Strings;
  7.  
  8. public class CookieManager {
  9.     private Applet applet;
  10.  
  11.     public CookieManager(Applet applet) {
  12.         this.applet = applet;
  13.     }
  14.  
  15.     private JSObject getDocument() {
  16.         JSObject win = (JSObject) JSObject.getWindow(applet);
  17.         return (JSObject) win.getMember("document");
  18.     }
  19.  
  20.     public void setCookie(String name, String value) {
  21.         if (Strings.isNullOrEmpty(name))
  22.             throw new IllegalArgumentException();
  23.         Cookie cookie = new Cookie();
  24.         cookie.setName(name);
  25.         cookie.setValue(value);
  26.         setCookie(cookie);
  27.     }
  28.  
  29.     public void setCookie(Cookie cookie) {
  30.         if (cookie == null)
  31.             throw new IllegalArgumentException();
  32.         JSObject doc = getDocument();
  33.         doc.setMember("cookie", cookie.toString());
  34.     }
  35.  
  36.     public String getCookieValue(String name) {
  37.         return getCookie(name).getValue();
  38.     }
  39.  
  40.     public Cookie getCookie(String name) {
  41.         if (Strings.isNullOrEmpty(name))
  42.             throw new IllegalArgumentException();
  43.         Cookie[] cookies = getCookies();
  44.         for (Cookie cookie : cookies)
  45.             if (cookie.getName().equals(name))
  46.                 return cookie;
  47.         return null;
  48.     }
  49.  
  50.     public Cookie[] getCookies() {
  51.         JSObject doc = getDocument();
  52.         String[] token = ((String) doc.getMember("cookie")).trim().split(";");
  53.         ArrayList<Cookie> cks = new ArrayList<Cookie>();
  54.         for (String tk : token) {
  55.             String[] tks = tk.trim().split("=");
  56.             String name = tks[0];
  57.             String value = tks.length > 1 ? tks[1] : null;
  58.             Cookie cookie = new Cookie();
  59.             cookie.setName(name);
  60.             cookie.setValue(value);
  61.             cks.add(cookie);
  62.         }
  63.         return cks.toArray(new Cookie[0]);
  64.     }
  65.  
  66.     public void removeCookie(String name) {
  67.         if (Strings.isNullOrEmpty(name))
  68.             throw new IllegalArgumentException();
  69.         Cookie cookie = new Cookie();
  70.         cookie.setName(name);
  71.         cookie.setExpireDate(new Date(0));
  72.         setCookie(cookie);
  73.     }
  74.  
  75. }