FileDocCategorySizeDatePackage
NNTPArticleImpl.javaAPI DocApache James 2.3.18890Fri Jan 12 12:56:24 GMT 2007org.apache.james.nntpserver.repository

NNTPArticleImpl

public class NNTPArticleImpl extends Object implements NNTPArticle
Please see NNTPArticle for comments

Fields Summary
private final File
articleFile
The file that stores the article data
private final NNTPGroup
group
The newsgroup containing this article.
Constructors Summary
NNTPArticleImpl(NNTPGroup group, File f)
The sole constructor for this class.

param
group the news group containing this article
param
f the file that stores the article data

        articleFile = f;
        this.group = group;
    
Methods Summary
private java.lang.StringcleanHeader(java.lang.String field)
Strips out newlines and tabs, converting them to spaces. rfc2980: 2.8 XOVER requires newline and tab to be converted to spaces

param
the input String
return
the cleaned string

        if ( field == null )
            field = "";
        StringBuffer sb = new StringBuffer(field);
        for( int i=0 ; i<sb.length() ; i++ ) {
            char c = sb.charAt(i);
            if( (c=='\n") || (c=='\t") || (c=='\r")) {
                sb.setCharAt(i, ' ");
            }
        }
        return sb.toString();
    
public intgetArticleNumber()

see
org.apache.james.nntpserver.repository.NNTPArticle#getArticleNumber()

        return Integer.parseInt(articleFile.getName());
    
public NNTPGroupgetGroup()

see
org.apache.james.nntpserver.repository.NNTPArticle#getGroup()

        return group;
    
public java.lang.StringgetHeader(java.lang.String header)

see
org.apache.james.nntpserver.repository.NNTPArticle#getHeader(String)

        try {
            FileInputStream fin = new FileInputStream(articleFile);
            InternetHeaders hdr = new InternetHeaders(fin);
            fin.close();
            return hdr.getHeader(header,null);
        } catch(Exception ex) {
            throw new NNTPException(ex);
        }
    
public java.lang.StringgetUniqueID()

see
org.apache.james.nntpserver.repository.NNTPArticle#getUniqueID()

        FileInputStream fin = null;
        try {
            fin = new FileInputStream(articleFile);
            InternetHeaders headers = new InternetHeaders(fin);
            String[] idheader = headers.getHeader("Message-Id");
            fin.close();
            return ( idheader.length > 0 ) ? idheader[0] : null;
        } catch(Exception ex) {
            throw new NNTPException(ex);
        } finally {
            IOUtil.shutdownStream(fin);
        }
    
public voidwriteArticle(java.io.OutputStream out)

see
org.apache.james.nntpserver.repository.NNTPArticle#writeArticle(OutputStream)

        FileInputStream fileStream = null;
        try {
            fileStream = new FileInputStream(articleFile);
            byte[] readBuffer = new byte[1024];
            int read = 0;
            while ((read = fileStream.read(readBuffer)) > 0) {
                out.write(readBuffer, 0, read);
            }
        } catch(IOException ex) { 
            throw new NNTPException(ex);
        } finally {
            if (fileStream != null) {
                try {
                    fileStream.close();
                } catch (IOException ioe) {
                    // Ignored
                }
            }
        }
    
public voidwriteBody(java.io.OutputStream out)

see
org.apache.james.nntpserver.repository.NNTPArticle#writeBody(OutputStream)

        FileInputStream fileStream = null;
        try {
            fileStream = new FileInputStream(articleFile);
            MailHeaders headers = new MailHeaders(fileStream);
            byte[] readBuffer = new byte[1024];
            int read = 0;
            while ((read = fileStream.read(readBuffer)) > 0) {
                out.write(readBuffer, 0, read);
            }
        } catch(Exception ex) {
            throw new NNTPException(ex);
        } finally {
            if (fileStream != null) {
                try {
                    fileStream.close();
                } catch (IOException ioe) {
                    // Ignored
                }
            }
        }
    
public voidwriteHead(java.io.OutputStream out)

see
org.apache.james.nntpserver.repository.NNTPArticle#writeHead(OutputStream)

        FileInputStream fileStream = null;
        try {
            fileStream = new FileInputStream(articleFile);
            MailHeaders headers = new MailHeaders(fileStream);
            byte[] headerBuffer = headers.toByteArray();
            int headerBufferLength = headerBuffer.length;
            // Write the headers excluding the final CRLF pair
            if (headerBufferLength > 2) {
                out.write(headerBuffer, 0, (headerBufferLength - 2));
            }
        } catch(Exception ex) { 
            throw new NNTPException(ex);
        } finally {
            if (fileStream != null) {
                try {
                    fileStream.close();
                } catch (IOException ioe) {
                    // Ignored
                }
            }
        }
    
public voidwriteOverview(java.io.OutputStream out)

see
org.apache.james.nntpserver.repository.NNTPArticle#writeOverview(OutputStream)

        FileInputStream fileStream = null;
        try {
            fileStream = new FileInputStream(articleFile);
            InternetHeaders hdr = new InternetHeaders(fileStream);
            String subject = hdr.getHeader("Subject",null);
            String author = hdr.getHeader("From",null);
            String date = hdr.getHeader("Date",null);
            String msgId = hdr.getHeader("Message-Id",null);
            String references = hdr.getHeader("References",null);
            long byteCount = articleFile.length();

            // get line count, if not set, count the lines
            String lineCount = hdr.getHeader("Lines",null);
            if (lineCount == null) {
                BufferedReader rdr = new BufferedReader(new FileReader(fileStream.getFD()));
                int lines = 0;
                while (rdr.readLine() != null) {
                    lines++;
                }

                lineCount = Integer.toString(lines);
                rdr.close();
            }

            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)               .append("\t")
                .append(lineCount).append("\r\n");
            String lineString = line.toString();
            out.write(lineString.getBytes("ASCII"));
        } catch(Exception ex) {
            throw new NNTPException(ex);
        } finally {
            if (fileStream != null) {
                try {
                    fileStream.close();
                } catch (IOException ioe) {
                    // Ignored
                }
            }
        }