hbedi       2003/01/18 11:01:45

  Modified:    src/java/org/apache/james/nntpserver NNTPHandler.java
               src/java/org/apache/james/nntpserver/repository
                        NNTPArticleImpl.java NNTPGroupImpl.java
                        NNTPSpooler.java
  Log:
  ported Noel's NNTP patches to 3.0 branch.
  made patch jdk 1.3 compatible.
  
  Revision  Changes    Path
  1.31      +110 -82   
jakarta-james/src/java/org/apache/james/nntpserver/NNTPHandler.java
  
  Index: NNTPHandler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-james/src/java/org/apache/james/nntpserver/NNTPHandler.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- NNTPHandler.java  14 Jan 2003 13:41:49 -0000      1.30
  +++ NNTPHandler.java  18 Jan 2003 19:01:45 -0000      1.31
  @@ -290,7 +290,7 @@
        */
       void idleClose() {
           if (getLogger() != null) {
  -            getLogger().error("Remote Manager Connection has idled out.");
  +            getLogger().error("NNTP Connection has idled out.");
           }
           try {
               if (socket != null) {
  @@ -332,16 +332,16 @@
               if ( theConfigData.getNNTPRepository().isReadOnly() ) {
                   StringBuffer respBuffer =
                       new StringBuffer(128)
  -                        .append("201 ")
  -                        .append(theConfigData.getHelloName())
  -                        .append(" NNTP Service Ready, posting prohibited");
  +                    .append("201 ")
  +                    .append(theConfigData.getHelloName())
  +                    .append(" NNTP Service Ready, posting prohibited");
                   writeLoggedFlushedResponse(respBuffer.toString());
               } else {
                   StringBuffer respBuffer =
                       new StringBuffer(128)
  -                            .append("200 ")
  -                            .append(theConfigData.getHelloName())
  -                            .append(" NNTP Service Ready, posting permitted");
  +                    .append("200 ")
  +                    .append(theConfigData.getHelloName())
  +                    .append(" NNTP Service Ready, posting permitted");
                   writeLoggedFlushedResponse(respBuffer.toString());
               }
   
  @@ -353,7 +353,12 @@
   
               getLogger().info("Connection closed");
           } catch (Exception e) {
  -            doQUIT(null);
  +            // unexpected error. try to send quit msg.
  +            // this may fail if socket has been closed by peer.
  +            try {
  +                doQUIT(null);
  +            } catch(Throwable t) { }
  +
               getLogger().error( "Exception during connection:" + e.getMessage(), e );
           } finally {
               resetHandler();
  @@ -513,10 +518,10 @@
           if (getLogger().isDebugEnabled()) {
               StringBuffer logBuffer =
                   new StringBuffer(128)
  -                    .append("Received unknown command ")
  -                    .append(command)
  -                    .append(" with argument ")
  -                    .append(argument);
  +                .append("Received unknown command ")
  +                .append(command)
  +                .append(" with argument ")
  +                .append(argument);
               getLogger().debug(logBuffer.toString());
           }
           writeLoggedFlushedResponse("500 Unknown command");
  @@ -593,10 +598,28 @@
        * an argument.
        *
        * @param argument the argument passed in with the NEWNEWS command.
  -     *                 Should be a date.
  +     *                 Should be NEWNEWS newsgroups date time [GMT] [<distribution>]
  +     *                 see RFC 977 #3.8, RFC 2980 #4.5.
        */
       private void doNEWNEWS(String argument) {
  -        // see section 11.4
  +
  +        String wildmat = "*";
  +        if (argument != null) {
  +            int spaceIndex = argument.indexOf(" ");
  +            if (spaceIndex >= 0) {
  +                wildmat = argument.substring(0, spaceIndex);
  +                argument = argument.substring(spaceIndex + 1);
  +            } else {
  +                getLogger().error("NEWNEWS had an invalid argument");
  +                writeLoggedFlushedResponse("501 Syntax error");
  +                return;
  +            }
  +        } else {
  +            getLogger().error("NEWNEWS had a null argument");
  +            writeLoggedFlushedResponse("501 Syntax error");
  +            return;
  +        }
  +
           Date theDate = null;
           try {
               theDate = getDateFrom(argument);
  @@ -605,15 +628,20 @@
               writeLoggedFlushedResponse("501 Syntax error");
               return;
           }
  -        Iterator iter = theConfigData.getNNTPRepository().getArticlesSince(theDate);
  +
           writeLoggedFlushedResponse("230 list of new articles by message-id 
follows");
  -        while ( iter.hasNext() ) {
  -            StringBuffer iterBuffer =
  -                new StringBuffer(64)
  +
  +        Iterator groups = 
theConfigData.getNNTPRepository().getMatchedGroups(wildmat);
  +        while (groups.hasNext() ) {
  +            Iterator articles = 
((NNTPGroup)(groups.next())).getArticlesSince(theDate);
  +            while ( articles.hasNext() ) {
  +                StringBuffer iterBuffer =
  +                    new StringBuffer(64)
                       .append("<")
  -                    .append(((NNTPArticle)iter.next()).getUniqueID())
  +                    .append(((NNTPArticle)articles.next()).getUniqueID())
                       .append(">");
  -            writeLoggedResponse(iterBuffer.toString());
  +                writeLoggedResponse(iterBuffer.toString());
  +            }
           }
           writeLoggedFlushedResponse(".");
       }
  @@ -649,13 +677,13 @@
               NNTPGroup currentGroup = (NNTPGroup)iter.next();
               StringBuffer iterBuffer =
                   new StringBuffer(128)
  -                    .append(currentGroup.getName())
  -                    .append(" ")
  -                    .append(currentGroup.getLastArticleNumber())
  -                    .append(" ")
  -                    .append(currentGroup.getFirstArticleNumber())
  -                    .append(" ")
  -                    .append((currentGroup.isPostAllowed()?"y":"n"));
  +                .append(currentGroup.getName())
  +                .append(" ")
  +                .append(currentGroup.getLastArticleNumber())
  +                .append(" ")
  +                .append(currentGroup.getFirstArticleNumber())
  +                .append(" ")
  +                .append((currentGroup.isPostAllowed()?"y":"n"));
               writeLoggedResponse(iterBuffer.toString());
           }
           writeLoggedFlushedResponse(".");
  @@ -811,8 +839,8 @@
               } else {
                   StringBuffer respBuffer =
                       new StringBuffer(64)
  -                            .append("223 0 ")
  -                            .append(param);
  +                    .append("223 0 ")
  +                    .append(param);
                   writeLoggedFlushedResponse(respBuffer.toString());
               }
           } else {
  @@ -844,10 +872,10 @@
                       }
                       StringBuffer respBuffer =
                           new StringBuffer(128)
  -                                .append("223 ")
  -                                .append(article.getArticleNumber())
  -                                .append(" ")
  -                                .append(articleID);
  +                        .append("223 ")
  +                        .append(article.getArticleNumber())
  +                        .append(" ")
  +                        .append(articleID);
                       writeLoggedFlushedResponse(respBuffer.toString());
                   }
               }
  @@ -874,8 +902,8 @@
               } else {
                   StringBuffer respBuffer =
                       new StringBuffer(64)
  -                            .append("222 0 ")
  -                            .append(param);
  +                    .append("222 0 ")
  +                    .append(param);
                   writeLoggedFlushedResponse(respBuffer.toString());
               }
           } else {
  @@ -907,10 +935,10 @@
                       }
                       StringBuffer respBuffer =
                           new StringBuffer(128)
  -                                .append("222 ")
  -                                .append(article.getArticleNumber())
  -                                .append(" ")
  -                                .append(articleID);
  +                        .append("222 ")
  +                        .append(article.getArticleNumber())
  +                        .append(" ")
  +                        .append(articleID);
                       writeLoggedFlushedResponse(respBuffer.toString());
                   }
               }
  @@ -941,8 +969,8 @@
               } else {
                   StringBuffer respBuffer =
                       new StringBuffer(64)
  -                            .append("221 0 ")
  -                            .append(param);
  +                    .append("221 0 ")
  +                    .append(param);
                   writeLoggedFlushedResponse(respBuffer.toString());
               }
           } else {
  @@ -974,10 +1002,10 @@
                       }
                       StringBuffer respBuffer =
                           new StringBuffer(128)
  -                                .append("221 ")
  -                                .append(article.getArticleNumber())
  -                                .append(" ")
  -                                .append(articleID);
  +                        .append("221 ")
  +                        .append(article.getArticleNumber())
  +                        .append(" ")
  +                        .append(articleID);
                       writeLoggedFlushedResponse(respBuffer.toString());
                   }
               }
  @@ -1008,8 +1036,8 @@
               } else {
                   StringBuffer respBuffer =
                       new StringBuffer(64)
  -                            .append("220 0 ")
  -                            .append(param);
  +                    .append("220 0 ")
  +                    .append(param);
                   writeLoggedResponse(respBuffer.toString());
               }
           } else {
  @@ -1041,10 +1069,10 @@
                       }
                       StringBuffer respBuffer =
                           new StringBuffer(128)
  -                                .append("220 ")
  -                                .append(article.getArticleNumber())
  -                                .append(" ")
  -                                .append(articleID);
  +                        .append("220 ")
  +                        .append(article.getArticleNumber())
  +                        .append(" ")
  +                        .append(articleID);
                       writeLoggedFlushedResponse(respBuffer.toString());
                   }
               }
  @@ -1075,10 +1103,10 @@
               NNTPArticle article = group.getArticle(currentArticleNumber);
               StringBuffer respBuffer =
                   new StringBuffer(64)
  -                        .append("223 ")
  -                        .append(article.getArticleNumber())
  -                        .append(" ")
  -                        .append(article.getUniqueID());
  +                .append("223 ")
  +                .append(article.getArticleNumber())
  +                .append(" ")
  +                .append(article.getUniqueID());
               writeLoggedFlushedResponse(respBuffer.toString());
           }
       }
  @@ -1104,10 +1132,10 @@
               NNTPArticle article = group.getArticle(currentArticleNumber);
               StringBuffer respBuffer =
                   new StringBuffer(64)
  -                        .append("223 ")
  -                        .append(article.getArticleNumber())
  -                        .append(" ")
  -                        .append(article.getUniqueID());
  +                .append("223 ")
  +                .append(article.getArticleNumber())
  +                .append(" ")
  +                .append(article.getUniqueID());
               writeLoggedFlushedResponse(respBuffer.toString());
           }
       }
  @@ -1147,15 +1175,15 @@
               }
               StringBuffer respBuffer =
                   new StringBuffer(128)
  -                        .append("211 ")
  -                        .append(articleCount)
  -                        .append(" ")
  -                        .append(lowWaterMark)
  -                        .append(" ")
  -                        .append(highWaterMark)
  -                        .append(" ")
  -                        .append(group.getName())
  -                        .append(" group selected");
  +                .append("211 ")
  +                .append(articleCount)
  +                .append(" ")
  +                .append(lowWaterMark)
  +                .append(" ")
  +                .append(highWaterMark)
  +                .append(" ")
  +                .append(group.getName())
  +                .append(" group selected");
               writeLoggedFlushedResponse(respBuffer.toString());
           }
       }
  @@ -1183,7 +1211,7 @@
       private void doMODEREADER(String argument) {
           // 7.2
           writeLoggedFlushedResponse(theConfigData.getNNTPRepository().isReadOnly()
  -                       ? "201 Posting Not Permitted" : "200 Posting Permitted");
  +                                   ? "201 Posting Not Permitted" : "200 Posting 
Permitted");
       }
   
       /**
  @@ -1304,9 +1332,9 @@
                   }
                   StringBuffer hdrBuffer =
                       new StringBuffer(128)
  -                            .append(article[i].getArticleNumber())
  -                            .append(" ")
  -                            .append(val);
  +                    .append(article[i].getArticleNumber())
  +                    .append(" ")
  +                    .append(val);
                   writeLoggedResponse(hdrBuffer.toString());
               }
               writeLoggedFlushedResponse(".");
  @@ -1387,9 +1415,9 @@
           try {
               StringBuffer dateStringBuffer =
                   new StringBuffer(64)
  -                    .append(date)
  -                    .append(" ")
  -                    .append(time);
  +                .append(date)
  +                .append(" ")
  +                .append(time);
               Date dt = DF_RFC977.parse(dateStringBuffer.toString());
               if ( utc ) {
                   dt = new Date(dt.getTime()+UTC_OFFSET);
  @@ -1398,12 +1426,12 @@
           } catch ( ParseException pe ) {
               StringBuffer exceptionBuffer =
                   new StringBuffer(128)
  -                    .append("Date extraction failed: ")
  -                    .append(date)
  -                    .append(",")
  -                    .append(time)
  -                    .append(",")
  -                    .append(utc);
  +                .append("Date extraction failed: ")
  +                .append(date)
  +                .append(",")
  +                .append(time)
  +                .append(",")
  +                .append(utc);
               throw new NNTPException(exceptionBuffer.toString());
           }
       }
  @@ -1506,7 +1534,7 @@
           } else {
               return false;
           }
  -   }
  +    }
   
       /**
        * This method logs at a "DEBUG" level the response string that
  
  
  
  1.14      +5 -4      
jakarta-james/src/java/org/apache/james/nntpserver/repository/NNTPArticleImpl.java
  
  Index: NNTPArticleImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-james/src/java/org/apache/james/nntpserver/repository/NNTPArticleImpl.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- NNTPArticleImpl.java      14 Jan 2003 13:41:50 -0000      1.13
  +++ NNTPArticleImpl.java      18 Jan 2003 19:01:45 -0000      1.14
  @@ -145,14 +145,15 @@
               String references = hdr.getHeader("References",null);
               long byteCount = articleFile.length();
               long lineCount = -1;
  -            StringBuffer line=new StringBuffer(128)
  +            StringBuffer line=new StringBuffer(256)
  +                .append(getArticleNumber())      .append("\t")
                   .append(cleanHeader(subject))    .append("\t")
                   .append(cleanHeader(author))     .append("\t")
                   .append(cleanHeader(date))       .append("\t")
                   .append(cleanHeader(msgId))      .append("\t")
                   .append(cleanHeader(references)) .append("\t")
  -                .append(byteCount + "\t")
  -                .append(lineCount + "");
  +                .append(byteCount)               .append("\t")
  +                .append(lineCount);
               prt.println(line.toString());
           } catch(Exception ex) { throw new NNTPException(ex); }
       }
  @@ -185,7 +186,7 @@
           StringBuffer sb = new StringBuffer(field);
           for( int i=0 ; i<sb.length() ; i++ ) {
               char c = sb.charAt(i);
  -            if( (c=='\n') || (c=='\t') ) {
  +            if( (c=='\n') || (c=='\t') || (c=='\r') ) {
                   sb.setCharAt(i, ' ');
               }
           }
  
  
  
  1.11      +12 -3     
jakarta-james/src/java/org/apache/james/nntpserver/repository/NNTPGroupImpl.java
  
  Index: NNTPGroupImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-james/src/java/org/apache/james/nntpserver/repository/NNTPGroupImpl.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- NNTPGroupImpl.java        14 Jan 2003 13:41:50 -0000      1.10
  +++ NNTPGroupImpl.java        18 Jan 2003 19:01:45 -0000      1.11
  @@ -205,10 +205,19 @@
               throws IOException {
           File articleFile = null;
           synchronized (this) {
  -            int artNum = getLastArticleNumber();
  -            articleFile = new File(root,(artNum + 1)+"");
  +            int artNum;
  +            if (numOfArticles < 0)
  +                throw new IllegalStateException("NNTP Group is corrupt (articles < 
0).");
  +            else if (numOfArticles == 0) {
  +                firstArticle = 1;
  +                artNum = 1;
  +            } else {
  +                artNum = getLastArticleNumber() + 1;
  +            }
  +            
  +            articleFile = new File(root, artNum + "");
               articleFile.createNewFile();
  -            lastArticle++;
  +            lastArticle = artNum;
               numOfArticles++;
           }
           if (getLogger().isDebugEnabled()) {
  
  
  
  1.14      +28 -8     
jakarta-james/src/java/org/apache/james/nntpserver/repository/NNTPSpooler.java
  
  Index: NNTPSpooler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-james/src/java/org/apache/james/nntpserver/repository/NNTPSpooler.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- NNTPSpooler.java  14 Jan 2003 13:41:50 -0000      1.13
  +++ NNTPSpooler.java  18 Jan 2003 19:01:45 -0000      1.14
  @@ -23,6 +23,7 @@
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
  +import java.io.IOException;
   import java.util.Properties;
   
   /**
  @@ -200,7 +201,7 @@
            * if it loses it tries to lock and process the next article.
            */
           public void run() {
  -            getLogger().debug("in spool thread");
  +            getLogger().debug(Thread.currentThread().getName() + " is the NNTP 
spooler thread.");
               try {
                   while ( Thread.currentThread().interrupted() == false ) {
                       String[] list = spoolPath.list();
  @@ -218,7 +219,9 @@
                                   lock.unlock(list[i]);
                               }
                           }
  +                        list[i] = null;
                       }
  +                    list = null;
                       // this is good for other non idle threads
                       try {
                           Thread.currentThread().sleep(threadIdleTime);
  @@ -249,23 +252,34 @@
               // TODO: Why is this a block?
               {   // Get the message for copying to destination groups.
                   FileInputStream fin = new FileInputStream(spoolFile);
  -                msg = new MimeMessage(null,fin);
  -                fin.close();
  +                try {
  +                    msg = new MimeMessage(null,fin);
  +                } finally {
  +                    try {
  +                        fin.close();
  +                    } catch (IOException _) { /* ignore close error */ }
  +                }
   
                   // ensure no duplicates exist.
                   String[] idheader = msg.getHeader("Message-Id");
                   articleID = ((idheader != null && (idheader.length > 0))? 
idheader[0] : null);
                   if ((articleID != null) && ( articleIDRepo.isExists(articleID))) {
                       getLogger().debug("Message already exists: "+articleID);
  -                    spoolFile.delete();
  +                    if (spoolFile.delete() == false)
  +                        getLogger().error("Could not delete duplicate message from 
spool: " + spoolFile.getAbsolutePath());
                       return;
                   }
                   if ( articleID == null ) {
                       articleID = articleIDRepo.generateArticleID();
                       msg.setHeader("Message-Id", articleID);
                       FileOutputStream fout = new FileOutputStream(spoolFile);
  -                    msg.writeTo(fout);
  -                    fout.close();
  +                    try {
  +                        msg.writeTo(fout);
  +                    } finally {
  +                        try {
  +                            fout.close();
  +                        } catch (IOException _) { /* ignore close error */ }
  +                    }
                   }
               }
   
  @@ -281,8 +295,14 @@
                       }
   
                       FileInputStream newsStream = new FileInputStream(spoolFile);
  -                    NNTPArticle article = group.addArticle(newsStream);
  -                    prop.setProperty(group.getName(),article.getArticleNumber() + 
"");
  +                    try {
  +                        NNTPArticle article = group.addArticle(newsStream);
  +                        prop.setProperty(group.getName(),article.getArticleNumber() 
+ "");
  +                    } finally {
  +                        try {
  +                            newsStream.close();
  +                        } catch (IOException _) { /* ignore close error */ }
  +                    }
                   }
               }
               articleIDRepo.addArticle(articleID,prop);
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to