001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package examples.nntp;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020:
021: import org.apache.commons.net.nntp.Article;
022: import org.apache.commons.net.nntp.NNTPClient;
023: import org.apache.commons.net.nntp.NewsgroupInfo;
024:
025: import examples.PrintCommandListener;
026:
027: /**
028: * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
029: *
030: * @author Rory Winston <rwinston@checkfree.com>
031: */
032: public class ExtendedNNTPOps {
033:
034: NNTPClient client;
035:
036: public ExtendedNNTPOps() {
037: client = new NNTPClient();
038: client.addProtocolCommandListener(new PrintCommandListener(
039: new PrintWriter(System.out)));
040: }
041:
042: public void demo(String host, String user, String password) {
043: try {
044: client.connect(host);
045:
046: // AUTHINFO USER/AUTHINFO PASS
047: boolean success = client.authenticate(user, password);
048: if (success) {
049: System.out.println("Authentication succeeded");
050: } else {
051: System.out.println("Authentication failed, error ="
052: + client.getReplyString());
053: }
054:
055: // XOVER
056: NewsgroupInfo testGroup = new NewsgroupInfo();
057: client.selectNewsgroup("alt.test", testGroup);
058: int lowArticleNumber = testGroup.getFirstArticle();
059: int highArticleNumber = lowArticleNumber + 100;
060: Article[] articles = NNTPUtils.getArticleInfo(client,
061: lowArticleNumber, highArticleNumber);
062:
063: for (int i = 0; i < articles.length; ++i) {
064: System.out.println(articles[i].getSubject());
065: }
066:
067: // LIST ACTIVE
068: NewsgroupInfo[] fanGroups = client
069: .listNewsgroups("alt.fan.*");
070: for (int i = 0; i < fanGroups.length; ++i) {
071: System.out.println(fanGroups[i].getNewsgroup());
072: }
073:
074: } catch (IOException e) {
075: e.printStackTrace();
076: }
077: }
078:
079: public static void main(String[] args) {
080: ExtendedNNTPOps ops;
081:
082: if (args.length != 3) {
083: System.err
084: .println("usage: ExtendedNNTPOps nntpserver username password");
085: System.exit(1);
086: }
087:
088: ops = new ExtendedNNTPOps();
089: ops.demo(args[0], args[1], args[2]);
090: }
091:
092: }
093:
094: /* Emacs configuration
095: * Local variables: **
096: * mode: java **
097: * c-basic-offset: 4 **
098: * indent-tabs-mode: nil **
099: * End: **
100: */
|