KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > nntp > NNTPStore


1 /*
2  * NNTPStore.java
3  * Copyright (C) 2002 dog <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.nntp;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.lang.reflect.Constructor JavaDoc;
28 import javax.mail.AuthenticationFailedException JavaDoc;
29 import javax.mail.Flags JavaDoc;
30 import javax.mail.Folder JavaDoc;
31 import javax.mail.MessagingException JavaDoc;
32 import javax.mail.Session JavaDoc;
33 import javax.mail.Store JavaDoc;
34 import javax.mail.URLName JavaDoc;
35
36 import gnu.inet.nntp.FileNewsrc;
37 import gnu.inet.nntp.Newsrc;
38 import gnu.inet.nntp.NNTPConnection;
39
40 /**
41  * An NNTP store provider.
42  * This uses an NNTPConnection to handle all the protocol-related
43  * functionality.
44  *
45  * @author <a HREF='mailto:dog@gnu.org'>Chris Burdess</a>
46  * @version 2.0
47  */

48 public class NNTPStore extends Store JavaDoc
49 {
50
51   NNTPConnection connection;
52   Newsrc newsrc;
53   Folder JavaDoc root;
54
55   /*
56    * The permanent flags for NNTPFolders.
57    */

58   Flags JavaDoc permanentFlags;
59   
60   /**
61    * Constructor.
62    * @param session the session
63    * @param url the connection URL
64    */

65   public NNTPStore(Session JavaDoc session, URLName JavaDoc url)
66   {
67     super(session, url);
68
69     // The permanent flags for NNTPFolders.
70
permanentFlags = new Flags JavaDoc();
71     permanentFlags.add(Flags.Flag.RECENT);
72     permanentFlags.add(Flags.Flag.SEEN);
73
74     // Init newsrc
75
String JavaDoc tn = session.getProperty("nntp.newsrc");
76     if (tn!=null)
77     {
78       // TODO implement independent way to instantiate newsrcs
79
Session.log("ERROR: nntp: unable to instantiate newsrc");
80     }
81     else
82     {
83       // ${HOME}/.newsrc[-${hostname}]
84
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(".newsrc");
85       if (url!=null)
86       {
87         buffer.append('-');
88         buffer.append(url.getHost());
89       }
90       String JavaDoc filename = buffer.toString();
91       String JavaDoc home = System.getProperty("user.home");
92       File JavaDoc file = new File JavaDoc(home, filename);
93       newsrc = new FileNewsrc(file, session.getDebug());
94     }
95   }
96
97   /**
98    * Performs the protocol connection.
99    */

100   protected boolean protocolConnect(String JavaDoc host, int port, String JavaDoc username,
101       String JavaDoc password)
102     throws MessagingException JavaDoc
103   {
104     try
105     {
106       if (port<0)
107         port = NNTPConnection.DEFAULT_PORT;
108       connection = new NNTPConnection(host, port, username, password,
109           debug);
110       if (username!=null && password!=null)
111       {
112         // TODO decide on authentication method
113
// Original authinfo
114
return connection.authinfo(username, password);
115       }
116       else
117         return true;
118     }
119     catch (IOException JavaDoc e)
120     {
121       throw new MessagingException JavaDoc(e.getMessage(), e);
122     }
123     catch (SecurityException JavaDoc e)
124     {
125       if (username!=null && password!=null)
126         throw new AuthenticationFailedException JavaDoc(e.getMessage());
127       else
128         return false;
129     }
130   }
131
132   /**
133    * Close the connection.
134    */

135   public void close()
136     throws MessagingException JavaDoc
137   {
138     try
139     {
140       newsrc.close();
141       synchronized (connection)
142       {
143         connection.quit();
144       }
145     }
146     catch (IOException JavaDoc e)
147     {
148       throw new MessagingException JavaDoc(e.getMessage(), e);
149     }
150     super.close();
151   }
152
153   /**
154    * Returns the folder representing the &quot;root&quot; namespace.
155    * This folder can be used to browse the folder hierarchy.
156    */

157   public Folder JavaDoc getDefaultFolder()
158     throws MessagingException JavaDoc
159   {
160     if (root==null)
161       root = new NNTPRootFolder(this);
162     return root;
163   }
164
165   /**
166    * Returns a folder by name.
167    */

168   public Folder JavaDoc getFolder(String JavaDoc name)
169     throws MessagingException JavaDoc
170   {
171     return getDefaultFolder().getFolder(name);
172   }
173
174   /**
175    * Returns the folder whose name corresponds to the <code>file</code> part
176    * of the specified URL.
177    */

178   public Folder JavaDoc getFolder(URLName JavaDoc url)
179     throws MessagingException JavaDoc
180   {
181     return getDefaultFolder().getFolder(url.getFile());
182   }
183
184   /*
185    * Indicates whether we should attempt to list all newsgroups.
186    * There are >30,000 newsgroups on Usenet. A naive client is unlikely to
187    * expect upwards of 30,000 folders to be returned from list().
188    */

189   boolean isListAll()
190   {
191     String JavaDoc listAll = session.getProperty("mail.nntp.listAll");
192     return (listAll!=null && "true".equals(listAll));
193   }
194   
195 }
196
Popular Tags