commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1080102 - in /commons/proper/net/trunk/src: changes/ main/java/examples/nntp/ main/java/org/apache/commons/net/nntp/
Date Thu, 10 Mar 2011 01:55:56 GMT
Author: sebb
Date: Thu Mar 10 01:55:55 2011
New Revision: 1080102

URL: http://svn.apache.org/viewvc?rev=1080102&view=rev
Log:
NET-237 Add streaming methods (corresponding to array methods) to NNTPClient.

Added:
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NewsgroupIterator.java
  (with props)
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
  (with props)
Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    commons/proper/net/trunk/src/main/java/examples/nntp/ListNewsgroups.java
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1080102&r1=1080101&r2=1080102&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml (original)
+++ commons/proper/net/trunk/src/changes/changes.xml Thu Mar 10 01:55:55 2011
@@ -57,6 +57,9 @@ The <action> type attribute can be add,u
 
     <body>
         <release version="3.0" date="TBA" description="TBA">
+            <action issue="NET-237" dev="sebb" type="add">
+            Add streaming methods (corresponding to array methods) to NNTPClient.
+            </action>
             <action issue="NET-365" dev="sebb" type="fix">
             FTPClient.listFiles() does not work properly, if remote server speaks German.
             Match non-space{3} instead of A-Za-z{3}

Modified: commons/proper/net/trunk/src/main/java/examples/nntp/ListNewsgroups.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/examples/nntp/ListNewsgroups.java?rev=1080102&r1=1080101&r2=1080102&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/examples/nntp/ListNewsgroups.java (original)
+++ commons/proper/net/trunk/src/main/java/examples/nntp/ListNewsgroups.java Thu Mar 10 01:55:55
2011
@@ -35,33 +35,35 @@ public final class ListNewsgroups
 
     public final static void main(String[] args)
     {
-        NNTPClient client;
-        NewsgroupInfo[] list;
-
         if (args.length < 1)
         {
             System.err.println("Usage: newsgroups newsserver");
-            System.exit(1);
+            return;
         }
 
-        client = new NNTPClient();
+        NNTPClient client = new NNTPClient();
 
         try
         {
             client.connect(args[0]);
 
-            list = client.listNewsgroups();
-
-            if (list != null)
-            {
-                for (int i = 0; i < list.length; i++)
-                    System.out.println(list[i].getNewsgroup());
+            int j = 0;
+            try {
+                for(String s : client.iterateNewsgroupListing("demon.*")) {
+                    j++;
+//                    System.out.println(s);
+                }
+            } catch (IOException e1) {
+                e1.printStackTrace();
             }
-            else
-            {
-                System.err.println("LIST command failed.");
-                System.err.println("Server reply: " + client.getReplyString());
+            System.out.println(j);
+
+            j = 0;
+            for(NewsgroupInfo n : client.iterateNewsgroups("demon.*")) {
+                j++;
+                //System.out.println(n.getNewsgroup());
             }
+            System.out.println(j);
         }
         catch (IOException e)
         {

Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java?rev=1080102&r1=1080101&r2=1080102&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java (original)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java Thu
Mar 10 01:55:55 2011
@@ -122,7 +122,7 @@ public class NNTPClient extends NNTP
      *     s = name of the group.)
      */
 
-    private void __parseGroupReply(String reply, NewsgroupInfo info)
+    private static void __parseGroupReply(String reply, NewsgroupInfo info)
     throws MalformedServerReplyException
     {
         String tokens[] = reply.split(" ");
@@ -154,7 +154,7 @@ public class NNTPClient extends NNTP
 
 
     // Format: group last first p
-    private NewsgroupInfo __parseNewsgroupListEntry(String entry)
+    static NewsgroupInfo __parseNewsgroupListEntry(String entry)
     {
         String tokens[] = entry.split(" ");
         if (tokens.length < 4) {
@@ -917,6 +917,8 @@ public class NNTPClient extends NNTP
      *      as an IOException or independently as itself.
      * @exception IOException  If an I/O error occurs while either sending a
      *      command to the server or receiving a reply from the server.
+     * @see #iterateNewsgroupListing()
+     * @see #iterateNewsgroups()
      ***/
     public NewsgroupInfo[] listNewsgroups() throws IOException
     {
@@ -927,9 +929,51 @@ public class NNTPClient extends NNTP
     }
 
     /**
-     * An overloaded listNewsgroups() command that allows us to
-     * specify with a pattern what groups we want to list. Wraps the
-     * LIST ACTIVE command.
+     * List all newsgroups served by the NNTP server.  If no newsgroups
+     * are served, no entries will be returned.
+     * <p>
+     * @return An iterable of NewsgroupInfo instances containing the information
+     *    for each newsgroup served by the NNTP server.   If no newsgroups
+     *    are served, no entries will be returned.
+     * @exception NNTPConnectionClosedException
+     *      If the NNTP server prematurely closes the connection as a result
+     *      of the client being idle or some other reason causing the server
+     *      to send NNTP reply code 400.  This exception may be caught either
+     *      as an IOException or independently as itself.
+     * @exception IOException  If an I/O error occurs while either sending a
+     *      command to the server or receiving a reply from the server.
+     * @since 3.0
+     */
+    public Iterable<String> iterateNewsgroupListing() throws IOException {
+        if (NNTPReply.isPositiveCompletion(list())) {
+            return new ReplyIterator(_reader_);            
+        }
+        throw new IOException(getReplyString());
+    }
+
+    /**
+     * List all newsgroups served by the NNTP server.  If no newsgroups
+     * are served, no entries will be returned.
+     * <p>
+     * @return An iterable of Strings containing the raw information
+     *    for each newsgroup served by the NNTP server.   If no newsgroups
+     *    are served, no entries will be returned.
+     * @exception NNTPConnectionClosedException
+     *      If the NNTP server prematurely closes the connection as a result
+     *      of the client being idle or some other reason causing the server
+     *      to send NNTP reply code 400.  This exception may be caught either
+     *      as an IOException or independently as itself.
+     * @exception IOException  If an I/O error occurs while either sending a
+     *      command to the server or receiving a reply from the server.
+     * @since 3.0
+     */
+    public Iterable<NewsgroupInfo> iterateNewsgroups() throws IOException {
+        return new NewsgroupIterator(iterateNewsgroupListing());
+    }
+
+    /**
+     * List the newsgroups that match a given pattern.
+     * Uses the LIST ACTIVE command.
      * <p>
      * @param wildmat a pseudo-regex pattern (cf. RFC 2980)
      * @return An array of NewsgroupInfo instances containing the information
@@ -937,6 +981,8 @@ public class NNTPClient extends NNTP
      *    supplied pattern.   If no such newsgroups are served, a zero length
      *    array will be returned.  If the command fails, null will be returned.
      * @throws IOException
+     * @see #iterateNewsgroupListing(String)
+     * @see #iterateNewsgroups(String)
      */
     public NewsgroupInfo[] listNewsgroups(String wildmat) throws IOException
     {
@@ -946,6 +992,41 @@ public class NNTPClient extends NNTP
     }
 
 
+    /**
+     * List the newsgroups that match a given pattern.
+     * Uses the LIST ACTIVE command.
+     * <p>
+     * @param wildmat a pseudo-regex pattern (cf. RFC 2980)
+     * @return An iterable of Strings containing the raw information
+     *    for each newsgroup served by the NNTP server corresponding to the
+     *    supplied pattern.   If no such newsgroups are served, no entries
+     *    will be returned.
+     * @throws IOException
+     * @since 3.0
+     */
+    public Iterable<String> iterateNewsgroupListing(String wildmat) throws IOException
{
+        if(NNTPReply.isPositiveCompletion(listActive(wildmat))) {
+            return new ReplyIterator(_reader_);            
+        }
+        throw new IOException(getReplyString());
+    }
+
+    /**
+     * List the newsgroups that match a given pattern.
+     * Uses the LIST ACTIVE command.
+     * <p>
+     * @param wildmat a pseudo-regex pattern (cf. RFC 2980)
+     * @return An iterable NewsgroupInfo instances containing the information
+     *    for each newsgroup served by the NNTP server corresponding to the
+     *    supplied pattern.   If no such newsgroups are served, no entries
+     *    will be returned.
+     * @throws IOException
+     * @since 3.0
+     */
+    public Iterable<NewsgroupInfo> iterateNewsgroups(String wildmat) throws IOException
{
+        return new NewsgroupIterator(iterateNewsgroupListing(wildmat));
+    }
+    
     /***
      * List all new newsgroups added to the NNTP server since a particular
      * date subject to the conditions of the specified query.  If no new
@@ -964,6 +1045,8 @@ public class NNTPClient extends NNTP
      *      as an IOException or independently as itself.
      * @exception IOException  If an I/O error occurs while either sending a
      *      command to the server or receiving a reply from the server.
+     * @see #iterateNewNewsgroups(NewGroupsOrNewsQuery)  
+     * @see #iterateNewNewsgroupListing(NewGroupsOrNewsQuery) 
      ***/
     public NewsgroupInfo[] listNewNewsgroups(NewGroupsOrNewsQuery query)
     throws IOException
@@ -976,7 +1059,55 @@ public class NNTPClient extends NNTP
         return __readNewsgroupListing();
     }
 
+    /**
+     * List all new newsgroups added to the NNTP server since a particular
+     * date subject to the conditions of the specified query.  If no new
+     * newsgroups were added, no entries will be returned.
+     * <p>
+     * @param query  The query restricting how to search for new newsgroups.
+     * @return An iterable of Strings containing the raw information
+     *    for each new newsgroup added to the NNTP server.   If no newsgroups
+     *    were added, no entries will be returned.
+     * @exception NNTPConnectionClosedException
+     *      If the NNTP server prematurely closes the connection as a result
+     *      of the client being idle or some other reason causing the server
+     *      to send NNTP reply code 400.  This exception may be caught either
+     *      as an IOException or independently as itself.
+     * @exception IOException  If an I/O error occurs while either sending a
+     *      command to the server or receiving a reply from the server.
+     * @since 3.0
+     */
+    public Iterable<String> iterateNewNewsgroupListing(NewGroupsOrNewsQuery query)
throws IOException {
+        if (NNTPReply.isPositiveCompletion(newgroups(
+                query.getDate(), query.getTime(),
+                query.isGMT(), query.getDistributions()))) {
+            return new ReplyIterator(_reader_);            
+        }
+        throw new IOException(getReplyString());
+    }
 
+    /**
+     * List all new newsgroups added to the NNTP server since a particular
+     * date subject to the conditions of the specified query.  If no new
+     * newsgroups were added, no entries will be returned.
+     * <p>
+     * @param query  The query restricting how to search for new newsgroups.
+     * @return An iterable of NewsgroupInfo instances containing the information
+     *    for each new newsgroup added to the NNTP server.   If no newsgroups
+     *    were added, no entries will be returned.
+     * @exception NNTPConnectionClosedException
+     *      If the NNTP server prematurely closes the connection as a result
+     *      of the client being idle or some other reason causing the server
+     *      to send NNTP reply code 400.  This exception may be caught either
+     *      as an IOException or independently as itself.
+     * @exception IOException  If an I/O error occurs while either sending a
+     *      command to the server or receiving a reply from the server.
+     * @since 3.0
+     */
+    public Iterable<NewsgroupInfo> iterateNewNewsgroups(NewGroupsOrNewsQuery query)
throws IOException {
+        return new NewsgroupIterator(iterateNewNewsgroupListing(query));
+    }
+    
     /***
      * List all new articles added to the NNTP server since a particular
      * date subject to the conditions of the specified query.  If no new
@@ -999,6 +1130,8 @@ public class NNTPClient extends NNTP
      *      as an IOException or independently as itself.
      * @exception IOException  If an I/O error occurs while either sending a
      *      command to the server or receiving a reply from the server.
+     *
+     * @see #iterateNewNews(NewGroupsOrNewsQuery)
      ***/
     public String[] listNewNews(NewGroupsOrNewsQuery query)
     throws IOException
@@ -1031,6 +1164,37 @@ public class NNTPClient extends NNTP
         return result;
     }
 
+    /**
+     * List all new articles added to the NNTP server since a particular
+     * date subject to the conditions of the specified query.  If no new
+     * new news is found, no entries will be returned. 
+     * You must add at least one newsgroup to the query, else the command will fail.
+     * Each String which is returned is a unique message identifier including the
+     * enclosing &lt and &gt.
+     * <p>
+     * @param query  The query restricting how to search for new news.  You
+     *    must add at least one newsgroup to the query.
+     * @return An iterator of String instances containing the unique message
+     *    identifiers for each new article added to the NNTP server.  If no
+     *    new news is found, no strings will be returned.
+     * @exception NNTPConnectionClosedException
+     *      If the NNTP server prematurely closes the connection as a result
+     *      of the client being idle or some other reason causing the server
+     *      to send NNTP reply code 400.  This exception may be caught either
+     *      as an IOException or independently as itself.
+     * @exception IOException  If an I/O error occurs while either sending a
+     *      command to the server or receiving a reply from the server.
+     * @since 3.0
+     */
+    public Iterable<String> iterateNewNews(NewGroupsOrNewsQuery query) throws IOException
{
+        if (NNTPReply.isPositiveCompletion(newnews(
+                query.getNewsgroups(), query.getDate(), query.getTime(),
+                query.isGMT(), query.getDistributions()))) {
+            return new ReplyIterator(_reader_);            
+        }
+        throw new IOException(getReplyString());
+    }
+
     /***
      * There are a few NNTPClient methods that do not complete the
      * entire sequence of NNTP commands to complete a transaction.  These

Added: commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NewsgroupIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NewsgroupIterator.java?rev=1080102&view=auto
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NewsgroupIterator.java
(added)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NewsgroupIterator.java
Thu Mar 10 01:55:55 2011
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+package org.apache.commons.net.nntp;
+
+import java.util.Iterator;
+/**
+ * Class which wraps an {@code Iterable<String>} of raw newgroup information
+ * to generate an {@code Iterable<NewsgroupInfo>} of the parsed information.
+ * @since 3.0
+ */
+class NewsgroupIterator implements Iterator<NewsgroupInfo>, Iterable<NewsgroupInfo>
{
+
+    private  final Iterator<String> stringIterator;
+
+    public NewsgroupIterator(Iterable<String> iterableString) {
+        stringIterator = iterableString.iterator();
+    }
+
+    public boolean hasNext() {
+        return stringIterator.hasNext();
+    }
+
+    public NewsgroupInfo next() {
+        String line = stringIterator.next();
+        return NNTPClient.__parseNewsgroupListEntry(line);
+    }
+
+    public void remove() {
+        stringIterator.remove();
+    }
+    public Iterator<NewsgroupInfo> iterator() {
+        return this;
+    }
+}

Propchange: commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NewsgroupIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NewsgroupIterator.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java?rev=1080102&view=auto
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
(added)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
Thu Mar 10 01:55:55 2011
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+package org.apache.commons.net.nntp;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.apache.commons.net.io.DotTerminatedMessageReader;
+import org.apache.commons.net.io.Util;
+
+/**
+ * Wraps a {@link Reader} and returns an {@code Iterable<String>}
+ * which returns the individual lines from the reader.
+ * @since 3.0
+ */
+class ReplyIterator implements Iterator<String>, Iterable<String> {
+
+    private final BufferedReader reader;        
+
+    private String line;
+    
+    private Exception savedException;
+
+    ReplyIterator(Reader _reader) throws IOException {
+        reader = new BufferedReader(new DotTerminatedMessageReader(_reader));
+        line = reader.readLine(); // prime the iterator
+        if (line == null) {
+            Util.closeQuietly(reader);
+        }
+    }
+
+    public boolean hasNext() {
+        if (savedException != null){
+            throw new NoSuchElementException(savedException.toString());            
+        }
+        return line != null;
+    }
+
+    public String next() throws NoSuchElementException {
+        if (savedException != null){
+            throw new NoSuchElementException(savedException.toString());            
+        }
+        String prev = line;
+        if (prev == null) {
+            throw new NoSuchElementException();
+        }
+        try {
+            line = reader.readLine(); // save next line
+            if (line == null) {
+                Util.closeQuietly(reader);
+            }
+        } catch (IOException ex) {
+            savedException = ex; // if it fails, save the exception, as it does not apply
to this call
+            Util.closeQuietly(reader);
+        }
+        return prev;
+    }
+
+    public void remove() {
+        throw new UnsupportedOperationException();
+    }
+
+    public Iterator<String> iterator() {
+        return this;
+    }
+}

Propchange: commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message