Using CookieHandler in Java 5 : Cookie « JDK 6 « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » JDK 6 » CookieScreenshots 
Using CookieHandler in Java 5
 

#Code referenced from 
#Chapter - Extensible Markup Language (XML)
#Java Platform Revealed
#by John Zukowski 
#ISBN: 1590596609
#http://www.apress.com/book/bookDisplay.html?bID=10109

import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class Fetch5 {
  public static void main(String args[]) throws Exception {
    String urlString = "java.sun.com";
    CookieHandler.setDefault(new ListCookieHandler());
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Object obj = connection.getContent();
    url = new URL(urlString);
    connection = url.openConnection();
    obj = connection.getContent();
  }
}

class ListCookieHandler extends CookieHandler {
  private List<Cookie> cookieJar = new LinkedList<Cookie>();

  public void put(URI uri, Map<String, List<String>> responseHeadersthrows IOException {
    List<String> setCookieList = responseHeaders.get("Set-Cookie");
    if (setCookieList != null) {
      for (String item : setCookieList) {
        Cookie cookie = new Cookie(uri, item);
        for (Cookie existingCookie : cookieJar) {
          if ((cookie.getURI().equals(existingCookie.getURI()))
              && (cookie.getName().equals(existingCookie.getName()))) {
            cookieJar.remove(existingCookie);
            break;
          }
        }
        cookieJar.add(cookie);
      }
    }
  }

  public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders)
      throws IOException {
    StringBuilder cookies = new StringBuilder();
    for (Cookie cookie : cookieJar) {
      // Remove cookies that have expired
      if (cookie.hasExpired()) {
        cookieJar.remove(cookie);
      else if (cookie.matches(uri)) {
        if (cookies.length() 0) {
          cookies.append(", ");
        }
        cookies.append(cookie.toString());
      }
    }

    Map<String, List<String>> cookieMap = new HashMap<String, List<String>>(requestHeaders);

    if (cookies.length() 0) {
      List<String> list = Collections.singletonList(cookies.toString());
      cookieMap.put("Cookie", list);
    }
    System.out.println("CookieMap: " + cookieMap);
    return Collections.unmodifiableMap(cookieMap);
  }
}

class Cookie {

  String name;

  String value;

  URI uri;

  String domain;

  Date expires;

  String path;

  private static DateFormat expiresFormat1 = new SimpleDateFormat("E, dd MMM yyyy k:m:s 'GMT'",
      Locale.US);

  private static DateFormat expiresFormat2 = new SimpleDateFormat("E, dd-MMM-yyyy k:m:s 'GMT'",
      Locale.US);

  public Cookie(URI uri, String header) {
    String attributes[] = header.split(";");
    String nameValue = attributes[0].trim();
    this.uri = uri;
    this.name = nameValue.substring(0, nameValue.indexOf('='));
    this.value = nameValue.substring(nameValue.indexOf('='1);
    this.path = "/";
    this.domain = uri.getHost();

    for (int i = 1; i < attributes.length; i++) {
      nameValue = attributes[i].trim();
      int equals = nameValue.indexOf('=');
      if (equals == -1) {
        continue;
      }
      String name = nameValue.substring(0, equals);
      String value = nameValue.substring(equals + 1);
      if (name.equalsIgnoreCase("domain")) {
        String uriDomain = uri.getHost();
        if (uriDomain.equals(value)) {
          this.domain = value;
        else {
          if (!value.startsWith(".")) {
            value = "." + value;
          }
          uriDomain = uriDomain.substring(uriDomain.indexOf('.'));
          if (!uriDomain.equals(value)) {
            throw new IllegalArgumentException("Trying to set foreign cookie");
          }
          this.domain = value;
        }
      else if (name.equalsIgnoreCase("path")) {
        this.path = value;
      else if (name.equalsIgnoreCase("expires")) {
        try {
          this.expires = expiresFormat1.parse(value);
        catch (ParseException e) {
          try {
            this.expires = expiresFormat2.parse(value);
          catch (ParseException e2) {
            throw new IllegalArgumentException("Bad date format in header: " + value);
          }
        }
      }
    }
  }

  public boolean hasExpired() {
    if (expires == null) {
      return false;
    }
    Date now = new Date();
    return now.after(expires);
  }

  public String getName() {
    return name;
  }

  public URI getURI() {
    return uri;
  }

  public boolean matches(URI uri) {

    if (hasExpired()) {
      return false;
    }
    String path = uri.getPath();
    if (path == null) {
      path = "/";
    }

    return path.startsWith(this.path);
  }

  public String toString() {
    StringBuilder result = new StringBuilder(name);
    result.append("=");
    result.append(value);
    return result.toString();
  }
}

        
Related examples in the same category
1. Usng CookieStore
2. Fetch Cookie with CookieHandler, CookieManager and CookieStore
ww___w__.j_a_v_a_2s__.c__om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.