View Javadoc

1   // Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2   // This file is part of iGallery.
3   //
4   // iGallery is free software; you can redistribute it and/or modify
5   // it under the terms of the GNU General Public License as published by
6   // the Free Software Foundation; either version 2 of the License, or
7   // (at your option) any later version.
8   //
9   // iGallery is distributed in the hope that it will be useful,
10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  // GNU General Public License for more details.
13  //
14  // You should have received a copy of the GNU General Public License
15  // along with Foobar; if not, write to the Free Software
16  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  package org.ninjasoft.igallery.logic;
18  
19  import java.util.*;
20  import java.net.*;
21  
22  /***
23   * Persist cookies across URLConnections
24   * @author enigma
25   */
26  public class Cookiejar {
27      private StatusListener listener = null;
28      public static boolean DEBUG = false;
29      
30      private Vector cookies = new Vector();
31      
32      /***
33       * Given an incoming connection, grab any Set-Cookie: headers from the
34       * connection and store them for later
35       * @param connection
36       */
37      public void grabCookies(URLConnection connection) {
38          // Headers are on a ONE-BASED index instead of zero-based
39          int i = 1;
40          String key;
41          while ((key = connection.getHeaderFieldKey(i)) != null) {
42              if (key.equalsIgnoreCase("set-cookie")) {
43                  String value = connection.getHeaderField(i);
44                  addCookie(value);
45              }
46              i++;
47          }
48      }
49      
50      /***
51       * Given an outbound connection, set all cookies in that connection.
52       * @param connection
53       */
54      public void setCookies(URLConnection connection ) {
55          for (Iterator i = cookies.iterator(); i.hasNext(); ) {
56              String value = (String) i.next();
57              if (DEBUG)
58              	sendStatus("Adding cookie " + value);
59              connection.addRequestProperty("Cookie", value);
60          }
61      }
62      
63      
64      private void addCookie(String cookieData) {
65          // TODO: parse cookie data and replace/expire stuff
66          int pos = cookieData.indexOf(';');
67          if (pos > 0)
68              cookieData = cookieData.substring(0, pos);
69          pos = cookieData.indexOf('=');
70          if (pos > 0) {
71              String key = cookieData.substring(0, pos+1);
72              Object[] cookieArray = cookies.toArray();
73              for (int i=0; i<cookieArray.length; i++) {
74                  String testValue = (String) cookieArray[i];
75                  if (testValue.startsWith(key)) {
76                      if (DEBUG)
77                          sendStatus("Removing existing cookie " + testValue);
78                      cookies.remove(testValue);
79                  }
80              }
81          }
82          if (DEBUG)
83              sendStatus("Adding cookie " + cookieData);
84          cookies.add(cookieData);
85      }
86      
87      private void cleanupCookies() {
88          // TODO: expire cookies, etc 
89      }
90      
91      public void setStatusListener(StatusListener l)     {this.listener = l;}
92  
93      private void sendStatus(String message) {
94          if (listener != null)
95              listener.setStatus(message);
96      }
97      private void sendError(String message) {
98          if (listener != null)
99              listener.setError(message);
100     }
101 }