HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"News Today, Gone Tomorrow"
Vol. 3, Issue 1, p. 64

	

Listing 1: Header.
 
Path: news.wisper.net!peer.news.zetnet.net!dispose.news.demon.net! 
From: [email protected] (AR Williamson) 
Newsgroups: comp.lang.java.programmer,comp.lang.java.help,comp.lang.java.gui 
Subject: Java Servlets 
Date: 28 Nov 1997 13:23:02 -0800 
Organization: N-ARY Limited 
Message-ID: <[email protected]> 
NNTP-Posting-Host: n-ary.com 
X-Trace: 880752184 11160 pvdl  206.184.139.132 
Lines: 31
 
Listing 2: getArticle() method.
 
public cArticle getArticle( String ID ) throws IOException 
{ 
  cArticle NG = new cArticle(); 
  String LineIn; 
  boolean bBad = false; 

  //- Get the Header 
  sendMessage( "HEAD " + ID ); 
  while ( (LineIn=In.readLine()) != null ) 
  { 
    if ( LineIn.charAt(0) == '.' ) 
      break; 
    else if ( LineIn.indexOf("423") == 0 || LineIn. 
     indexOf("430") == 0 ) 
      return null; 
    else if ( LineIn.indexOf("Subject:") != -1 ) 
      NG.Subject = LineIn.substring( LineIn.indexOf("Subject:")+9,LineIn.length() ); 
    else if ( LineIn.indexOf("Date:") != -1 ) { 
      try{ 
        NG.EntryDate = new Date( LineIn.substring( LineIn.indexOf("Date:")+6, 
                       LineIn.length() ) ).getTime(); 
      } 
      catch( Exception E ){ 
        NG.EntryDate = new Date().getTime(); 
      } 
    } 
    else if ( LineIn.indexOf("From:") != -1 ){ 
      NG.Author = LineIn.substring( LineIn.indexOf("From:")+6,LineIn.length() ); 
      NG.Author = NG.Author.replace( '"', ' ' ); 
    } 
    else if ( LineIn.indexOf("Lines:") != -1 ){ 
      try{ 
        int Lines = Integer.parseInt( LineIn.substring(LineIn.indexOf("Lines:")+7,LineIn.length()) ); 
        if ( Lines > 120 ) 
          bBad = true; 
      }catch(Exception E){} 
    } 
  } 

  //- Get the Body 
  sendMessage( "BODY " + ID ); 
  NG.vBody = new Vector(30,5); 
  LineIn = In.readLine(); 
  if ( LineIn == null || LineIn.indexOf("222") == -1 )  return null; 

  while ( (LineIn=In.readLine()) != null ) 
  { 
    if ( LineIn.length() != 0 && LineIn.charAt(0) == '.' && LineIn.length() == 1 ) 
        break; 
    else if ( LineIn.length() != 0 && LineIn. 
       indexOf("Content-type: ") != -1 ){ 
      if ( LineIn.indexOf("text/plain") != -1 ) 
          NG.vBody.addElement( LineIn + "\n" ); 
      else 
          bBad = true; 
    } 
    else if ( LineIn.length() != 0 && LineIn. 
       indexOf("begin 600") != -1 ) 
      bBad = true; 
    else if ( bBad == false ) 
      NG.vBody.addElement( LineIn + "\n"); 
  } 

  if ( bBad ) return null; 
  NG.Newsgroup = Location + Type + cArticle.USENET; 
  return NG; 
} 

Listing 3: postArticle() method.
 
public void postArticle( String _NG, String _from, String _Subject, Vector _Body ) throws IOException 
{ 
  String LineIn; 

  //- Get the Header 
  sendMessage( "POST" ); 
  LineIn = In.readLine(); 
  if ( LineIn == null || LineIn.indexOf("340") == -1 ) return; 

  //- Post the Header fields 
  sendMessage( "Path: n-ary" ); 
  sendMessage( "From: " + _from ); 
  sendMessage( "Newsgroups: " + _NG ); 
  sendMessage( "Subject: " + _Subject ); 
  sendMessage( "Message-ID: <" + System.currentTimeMillis() + "@n-ary.com>" ); 
  sendMessage( "Date: " + new SimpleDateFormat( "dd MMM yy HH:mm:ss").format(new Date()) + " GMT"); 
  sendMessage( "Organization: N-ARY Limited" ); 
  sendMessage( "Reply-To:" + _from + "\r\n" ); 

  //- Post the Body 
  Enumeration E = _Body.elements(); 
  while ( E.hasMoreElements() ) 
    sendMessage( (String)E.nextElement() ); 

  sendMessage( "\r\n.\r\n" ); 
  In.readLine(); 
} 

Listing 4.
 
public Vector getArticleList(String _Name, long _date) throws IOException 
{ 
  String D = new SimpleDateFormat( "yyMMdd HHmmss").format(new Date(_date)); 

  Vector V = new Vector( 50, 10 ); 
  sendMessage( "NEWNEWS " + _Name + " " + D ); 
  String LineIn; 
  while ( (LineIn=In.readLine()) != null ){ 
    if ( LineIn.charAt(0) == '.' ) 
      return V; 
    else if ( LineIn.indexOf( "230" ) == 0 ) 
      continue; 
    else 
      V.addElement( LineIn ); 
  } 
  return null; 
} 

Listing 5: Complete listing.
 
import java.applet.*; 
import java.io.*; 
import java.util.*; 
import java.net.*; 
import java.text.*; 

public class newscomms 
{ 
  DataInputStream In; 
  DataOutputStream Out; 
  Socket  OutPort; 

  public newscomms(){} 

  public boolean openPort(String _Host){ 
      try{ 
          OutPort = new Socket( _Host, 119 ); 
          In      = new DataInputStream( OutPort.getInputStream() ); 
          Out     = new DataOutputStream( OutPort.getOutputStream() ); 
          In.readLine(); 
          return true; 
      }catch( IOException E ){ 
          System.out.println( "newscomms.openPort(.): " + E );     
      } 
      return false; 
  } 

  public void closePort(){ 
    try{ 
        sendMessage( "QUIT" ); 
        OutPort.close(); 
    }catch( IOException E){} 
  } 

  public void sendMessage( String _M ) throws IOException{ 
    Out.writeBytes( _M + "\r\n" ); 
  } 

  public Vector getArticleList(String _Name, long _date) throws IOException{ 
    String D = new SimpleDateFormat( "yyMMdd HHmmss").format(new Date(_date)); 

    Vector V = new Vector( 50, 10 ); 
    sendMessage( "NEWNEWS " + _Name + " " + D ); 
    String LineIn; 
    while ( (LineIn=In.readLine()) != null ) { 
      if ( LineIn.charAt(0) == '.' ) 
        return V; 
      else if ( LineIn.indexOf( "230" ) == 0 ) 
        continue; 
      else 
        V.addElement( LineIn ); 
    } 
    return null; 
  } 

  public cArticle getArticle( String ID ) throws IOException { 
    cArticle NG = new cArticle(); 
    String LineIn; 
    boolean bBad = false; 

    //- Default the Newsgroup 
    int Location    = cArticle.UK; 
    int Type        = cArticle.PERMANENT; 

    //- Get the Header 
    sendMessage( "HEAD " + ID ); 
    while ( (LineIn=In.readLine()) != null ) 
    { 
        if ( LineIn.charAt(0) == '.' ) 
          break; 
        else if ( LineIn.indexOf("423") == 0 ||  
         LineIn.indexOf("430") == 0 ) 
          return null; 
        else if ( LineIn.indexOf("Subject:") != -1 ) 
          NG.Subject = LineIn.substring( LineIn.indexOf("Subject:")+9,LineIn.length() ); 
        else if ( LineIn.indexOf("Date:") != -1 ) { 
          try{ 
            NG.EntryDate=new  Date(LineIn.substring(LineIn.indexOf 
             ("Date:")+6,LineIn.length())).getTime(); 
          }catch( Exception E ){ 
            NG.EntryDate = new Date().getTime(); 
          } 
        } 
        else if ( LineIn.indexOf("From:") != -1 ){ 
          NG.Author = LineIn.substring( LineIn.indexOf("From:")+6,LineIn.length() ); 
          NG.Author = NG.Author.replace( '"', ' ' ); 
        } 
        else if ( LineIn.indexOf("Lines:") != -1 ){ 
          try{ 
            int Lines=Integer.parseInt(LineIn.substring(LineIn. 
               indexOf("Lines:")+7,LineIn.length())); 
            if ( Lines > 120 ) 
              bBad = true; 
          }catch(Exception E){} 
        } 
    } 

    //- Get the Body 
    sendMessage( "BODY " + ID ); 
    NG.vBody = new Vector(30,5); 
    LineIn = In.readLine(); 
    if ( LineIn == null || LineIn.indexOf("222") == -1 )  return null; 

    while ( (LineIn=In.readLine()) != null ){ 
      if ( LineIn.length() != 0 && LineIn.charAt(0) == '.' && LineIn.length() == 1 ) 
        break; 
     else if ( LineIn.length() != 0 &&  
          LineIn.indexOf("Content-type: ") != -1 ){ 
        if ( LineIn.indexOf("text/plain") != -1 ) 
          NG.vBody.addElement( LineIn + "\n" ); 
        else 
          bBad = true; 
      } 
      else if ( LineIn.length() != 0 && LineIn.indexOf("begin 600") != -1 ) 
        bBad = true; 
      else if ( bBad == false ) 
        NG.vBody.addElement( LineIn + "\n"); 
    } 

    if ( bBad ) return null; 
    NG.Newsgroup = Location + Type + cArticle.USENET; 
    return NG; 
  } 

  public void postArticle( String _NG, String _from, String _Subject, Vector _Body )  
                                                                         throws IOException { 
    String LineIn; 

    //- Get the Header 
    sendMessage( "POST" ); 
    LineIn = In.readLine(); 
    if ( LineIn == null || LineIn.indexOf("340") == -1 ) return; 

    //- Post the Header fields 
    sendMessage( "Path: n-ary" ); 
    sendMessage( "From: " + _form ); 
    sendMessage( "Newsgroups: " + _NG ); 
    sendMessage( "Subject: " + _Subject ); 
    sendMessage( "Message-ID: <" + System.currentTimeMillis() + "@n-ary.com>" ); 
    sendMessage( "Date: " + new SimpleDateFormat( "dd MMM yy HH:mm:ss").format(new Date()) + " GMT"); 
    sendMessage( "Organization: N-ARY Limited" ); 
    sendMessage( "Reply-To:" + _from + "\r\n" ); 

    //- Post the Body 
    Enumeration E = _Body.elements(); 
    while ( E.hasMoreElements() ) 
      sendMessage( (String)E.nextElement() ); 

    sendMessage( "\r\n.\r\n" ); 
    In.readLine(); 
  } 
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.