KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > imap4 > IMAPStore


1 /*
2  * IMAPStore.java
3  * Copyright (C) 2003 Chris Burdess <dog@gnu.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * You also have permission to link it with the Sun Microsystems, Inc.
11  * JavaMail(tm) extension and run that combination.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */

22
23 package gnu.mail.providers.imap4;
24
25 import java.io.IOException JavaDoc;
26 import java.net.UnknownHostException JavaDoc;
27 import javax.mail.Folder JavaDoc;
28 import javax.mail.MessagingException JavaDoc;
29 import javax.mail.Session JavaDoc;
30 import javax.mail.Store JavaDoc;
31 import javax.mail.URLName JavaDoc;
32
33 import javax.mail.Flags JavaDoc;
34
35 /**
36  * The storage class implementing the IMAP4rev1 mail protocol.
37  *
38  * @author <a HREF='mailto:dog@gnu.org'>Chris Burdess</a>
39  * @version 0.1
40  */

41 public class IMAPStore
42   extends Store JavaDoc
43 {
44
45   // -- TESTING --
46
public static void main(String JavaDoc[] args) {
47     try {
48       Session JavaDoc session = Session.getDefaultInstance(System.getProperties());
49       session.setDebug(true);
50       URLName JavaDoc url = new URLName JavaDoc("imap://localhost/");
51       Store JavaDoc s = session.getStore(url);
52       s.connect("localhost", "test", "imaptest");
53       Folder JavaDoc f = s.getFolder("INBOX");
54       f.open(Folder.READ_ONLY);
55       javax.mail.Message JavaDoc[] m = f.getMessages();
56       for (int i=0; i<m.length; i++) {
57         System.out.println(m[i].getSubject());
58         //System.out.println("------->"+m[i].getContentType());
59
//System.out.println(m[i].getContent());
60
Flags JavaDoc flags = m[i].getFlags();
61         if (flags.contains(Flags.Flag.FLAGGED))
62           flags.remove(Flags.Flag.FLAGGED);
63         else
64           flags.add(Flags.Flag.FLAGGED);
65         m[i].setFlags(flags, true);
66       }
67       f.close(false);
68       s.close();
69     } catch (Exception JavaDoc e) {
70       e.printStackTrace(System.err);
71     }
72   }
73   // -- END TESTING --
74

75   /**
76    * The default IMAP port.
77    */

78   public static final int DEFAULT_PORT = 143;
79
80   /**
81    * The connection to the IMAP server.
82    */

83   protected IMAPConnection connection;
84
85   /**
86    * Folder representing the root namespace of the IMAP connection.
87    */

88   protected IMAPFolder root;
89
90   /**
91    * Constructor.
92    */

93   public IMAPStore(Session JavaDoc session, URLName JavaDoc url)
94   {
95     super(session, url);
96   }
97
98   /**
99    * Connects to the IMAP server and authenticates with the specified
100    * parameters.
101    */

102   protected boolean protocolConnect(String JavaDoc host, int port, String JavaDoc username,
103       String JavaDoc password)
104     throws MessagingException JavaDoc
105   {
106     if (port<0) port = DEFAULT_PORT;
107     if (host==null || username==null || password==null)
108       return false;
109     if (connection!=null)
110       return true;
111     synchronized (this)
112     {
113       try
114       {
115         connection = new IMAPConnection(host, port);
116         if (session.getDebug())
117           connection.setDebug(true);
118         return connection.login(username, password);
119       }
120       catch (UnknownHostException JavaDoc e)
121       {
122         throw new MessagingException JavaDoc(e.getMessage(), e);
123       }
124       catch (IOException JavaDoc e)
125       {
126         throw new MessagingException JavaDoc(e.getMessage(), e);
127       }
128     }
129   }
130
131   /**
132    * Closes the connection.
133    */

134   public synchronized void close()
135     throws MessagingException JavaDoc
136   {
137     if (connection!=null)
138     {
139       synchronized (this)
140       {
141         try
142         {
143           connection.logout();
144         }
145         catch (IOException JavaDoc e)
146         {
147         }
148         connection = null;
149       }
150     }
151     super.close();
152   }
153
154   /**
155    * Returns the root folder.
156    */

157   public Folder JavaDoc getDefaultFolder()
158     throws MessagingException JavaDoc
159   {
160     return getFolder("");
161   }
162
163   /**
164    * Returns the folder with the specified name.
165    */

166   public Folder JavaDoc getFolder(String JavaDoc name)
167     throws MessagingException JavaDoc
168   {
169     return new IMAPFolder(this, name);
170   }
171
172   /**
173    * Returns the folder whose name is the file part of the specified URLName.
174    */

175   public Folder JavaDoc getFolder(URLName JavaDoc urlname)
176     throws MessagingException JavaDoc
177   {
178     return getFolder(urlname.getFile());
179   }
180
181 }
182
Popular Tags