KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * NNTPTransport.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.IOException JavaDoc;
26 import javax.mail.Address JavaDoc;
27 import javax.mail.AuthenticationFailedException JavaDoc;
28 import javax.mail.Message JavaDoc;
29 import javax.mail.MessagingException JavaDoc;
30 import javax.mail.Session JavaDoc;
31 import javax.mail.Transport JavaDoc;
32 import javax.mail.URLName JavaDoc;
33 import javax.mail.internet.MimeMessage JavaDoc;
34 import javax.mail.internet.NewsAddress JavaDoc;
35
36 import gnu.inet.nntp.NNTPConnection;
37 import gnu.inet.nntp.PostStream;
38
39 /**
40  * An NNTP transport provider.
41  * This uses an NNTPConnection to handle all the protocol-related
42  * functionality.
43  *
44  * @author <a HREF='mailto:dog@gnu.org'>Chris Burdess</a>
45  * @version 2.0
46  */

47 public class NNTPTransport extends Transport JavaDoc
48 {
49
50   NNTPConnection connection;
51
52   /**
53    * Constructor.
54    * @param session the session
55    * @param url the connection URL
56    */

57   public NNTPTransport(Session JavaDoc session, URLName JavaDoc url)
58   {
59     super(session, url);
60   }
61
62   /**
63    * Performs the protocol connection.
64    * @see NNTPStore.protocolConnect()
65    */

66   protected boolean protocolConnect(String JavaDoc host, int port, String JavaDoc username,
67       String JavaDoc password)
68     throws MessagingException JavaDoc
69   {
70     try
71     {
72       if (port<0)
73         port = NNTPConnection.DEFAULT_PORT;
74       connection = new NNTPConnection(host, port, username, password,
75           debug);
76       if (username!=null && password!=null)
77       {
78         // TODO decide on authentication method
79
// Original authinfo
80
return connection.authinfo(username, password);
81       }
82       else
83         return true;
84     }
85     catch (IOException JavaDoc e)
86     {
87       throw new MessagingException JavaDoc(e.getMessage(), e);
88     }
89     catch (SecurityException JavaDoc e)
90     {
91       if (username!=null && password!=null)
92         throw new AuthenticationFailedException JavaDoc(e.getMessage());
93       else
94         return false;
95     }
96   }
97
98   /**
99    * Close the connection.
100    * @see NNTPStore.close()
101    */

102   public void close()
103     throws MessagingException JavaDoc
104   {
105     try
106     {
107       synchronized (connection)
108       {
109         connection.quit();
110       }
111     }
112     catch (IOException JavaDoc e)
113     {
114       throw new MessagingException JavaDoc(e.getMessage(), e);
115     }
116     super.close();
117   }
118
119   /**
120    * Post an article.
121    * @param message a MimeMessage
122    * @param addresses an array of Address (ignored!)
123    */

124   public void sendMessage(Message JavaDoc message, Address JavaDoc[] addresses)
125     throws MessagingException JavaDoc
126   {
127     // Ensure corrent recipient type, and that all newsgroup recipients are
128
// of type NewsAddress.
129
addresses = message.getRecipients(MimeMessage.RecipientType.NEWSGROUPS);
130     boolean ok = (addresses.length>0);
131     if (!ok)
132       throw new MessagingException JavaDoc("No recipients specified");
133     for (int i=0; i<addresses.length; i++)
134     {
135       if (!(addresses[i] instanceof NewsAddress JavaDoc))
136       {
137         ok = false;
138         break;
139       }
140     }
141     if (!ok)
142       throw new MessagingException JavaDoc("Newsgroup recipients must be specified "+
143         "as type NewsAddress");
144     
145     try
146     {
147       synchronized (connection)
148       {
149         PostStream out = connection.post();
150         message.writeTo(out);
151         out.close();
152       }
153     }
154     catch (IOException JavaDoc e)
155     {
156       throw new MessagingException JavaDoc(e.getMessage(), e);
157     }
158   }
159
160   // TEST
161
public static void main(String JavaDoc[] args)
162   {
163     try
164     {
165       // session
166
Session JavaDoc session = Session.getInstance(System.getProperties(), null);
167       
168       // create message
169
javax.mail.internet.MimeMessage JavaDoc message =
170         new javax.mail.internet.MimeMessage JavaDoc(session);
171       message.setFrom(new javax.mail.internet.InternetAddress JavaDoc("dog@gnu.org"));
172       Address JavaDoc[] recipients = { new NewsAddress JavaDoc("alt.test") };
173       message.setRecipients(MimeMessage.RecipientType.NEWSGROUPS,
174           recipients);
175       message.setSubject("Test");
176       message.setText("This is a test.", "iso-8859-1");
177       
178       // get transport
179
URLName JavaDoc url = new URLName JavaDoc("nntp-post://localhost");
180       Transport JavaDoc transport = session.getTransport(url);
181       transport.connect();
182       transport.sendMessage(message, message.getAllRecipients());
183       transport.close();
184     }
185     catch (MessagingException JavaDoc e)
186     {
187       e.printStackTrace();
188       Exception JavaDoc e2 = e.getNextException();
189       if (e2!=null)
190       {
191         System.out.println("Next exception:");
192         e2.printStackTrace();
193       }
194     }
195   }
196   
197 }
198
Popular Tags