1 17 18 package org.apache.james.nntpserver.repository; 19 20 import org.apache.james.core.MailHeaders; 21 import org.apache.james.nntpserver.NNTPException; 22 import org.apache.avalon.excalibur.io.IOUtil; 23 24 import javax.mail.internet.InternetHeaders ; 25 import java.io.BufferedReader ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.io.OutputStream ; 31 32 35 class NNTPArticleImpl implements NNTPArticle { 36 37 40 private final File articleFile; 41 42 45 private final NNTPGroup group; 46 47 53 NNTPArticleImpl(NNTPGroup group, File f) { 54 articleFile = f; 55 this.group = group; 56 } 57 58 61 public NNTPGroup getGroup() { 62 return group; 63 } 64 65 68 public int getArticleNumber() { 69 return Integer.parseInt(articleFile.getName()); 70 } 71 72 75 public String getUniqueID() { 76 FileInputStream fin = null; 77 try { 78 fin = new FileInputStream (articleFile); 79 InternetHeaders headers = new InternetHeaders (fin); 80 String [] idheader = headers.getHeader("Message-Id"); 81 fin.close(); 82 return ( idheader.length > 0 ) ? idheader[0] : null; 83 } catch(Exception ex) { 84 throw new NNTPException(ex); 85 } finally { 86 IOUtil.shutdownStream(fin); 87 } 88 } 89 90 93 public void writeArticle(OutputStream out) { 94 FileInputStream fileStream = null; 95 try { 96 fileStream = new FileInputStream (articleFile); 97 byte[] readBuffer = new byte[1024]; 98 int read = 0; 99 while ((read = fileStream.read(readBuffer)) > 0) { 100 out.write(readBuffer, 0, read); 101 } 102 } catch(IOException ex) { 103 throw new NNTPException(ex); 104 } finally { 105 if (fileStream != null) { 106 try { 107 fileStream.close(); 108 } catch (IOException ioe) { 109 } 111 } 112 } 113 } 114 115 118 public void writeHead(OutputStream out) { 119 FileInputStream fileStream = null; 120 try { 121 fileStream = new FileInputStream (articleFile); 122 MailHeaders headers = new MailHeaders(fileStream); 123 byte[] headerBuffer = headers.toByteArray(); 124 int headerBufferLength = headerBuffer.length; 125 if (headerBufferLength > 2) { 127 out.write(headerBuffer, 0, (headerBufferLength - 2)); 128 } 129 } catch(Exception ex) { 130 throw new NNTPException(ex); 131 } finally { 132 if (fileStream != null) { 133 try { 134 fileStream.close(); 135 } catch (IOException ioe) { 136 } 138 } 139 } 140 } 141 142 145 public void writeBody(OutputStream out) { 146 FileInputStream fileStream = null; 147 try { 148 fileStream = new FileInputStream (articleFile); 149 MailHeaders headers = new MailHeaders(fileStream); 150 byte[] readBuffer = new byte[1024]; 151 int read = 0; 152 while ((read = fileStream.read(readBuffer)) > 0) { 153 out.write(readBuffer, 0, read); 154 } 155 } catch(Exception ex) { 156 throw new NNTPException(ex); 157 } finally { 158 if (fileStream != null) { 159 try { 160 fileStream.close(); 161 } catch (IOException ioe) { 162 } 164 } 165 } 166 } 167 168 171 public void writeOverview(OutputStream out) { 172 FileInputStream fileStream = null; 173 try { 174 fileStream = new FileInputStream (articleFile); 175 InternetHeaders hdr = new InternetHeaders (fileStream); 176 String subject = hdr.getHeader("Subject",null); 177 String author = hdr.getHeader("From",null); 178 String date = hdr.getHeader("Date",null); 179 String msgId = hdr.getHeader("Message-Id",null); 180 String references = hdr.getHeader("References",null); 181 long byteCount = articleFile.length(); 182 183 String lineCount = hdr.getHeader("Lines",null); 185 if (lineCount == null) { 186 BufferedReader rdr = new BufferedReader (new FileReader (fileStream.getFD())); 187 int lines = 0; 188 while (rdr.readLine() != null) { 189 lines++; 190 } 191 192 lineCount = Integer.toString(lines); 193 rdr.close(); 194 } 195 196 StringBuffer line=new StringBuffer (256) 197 .append(getArticleNumber()) .append("\t") 198 .append(cleanHeader(subject)) .append("\t") 199 .append(cleanHeader(author)) .append("\t") 200 .append(cleanHeader(date)) .append("\t") 201 .append(cleanHeader(msgId)) .append("\t") 202 .append(cleanHeader(references)) .append("\t") 203 .append(byteCount) .append("\t") 204 .append(lineCount).append("\r\n"); 205 String lineString = line.toString(); 206 out.write(lineString.getBytes("ASCII")); 207 } catch(Exception ex) { 208 throw new NNTPException(ex); 209 } finally { 210 if (fileStream != null) { 211 try { 212 fileStream.close(); 213 } catch (IOException ioe) { 214 } 216 } 217 } 218 } 219 220 223 public String getHeader(String header) { 224 try { 225 FileInputStream fin = new FileInputStream (articleFile); 226 InternetHeaders hdr = new InternetHeaders (fin); 227 fin.close(); 228 return hdr.getHeader(header,null); 229 } catch(Exception ex) { 230 throw new NNTPException(ex); 231 } 232 } 233 234 242 private String cleanHeader(String field) { 243 if ( field == null ) 244 field = ""; 245 StringBuffer sb = new StringBuffer (field); 246 for( int i=0 ; i<sb.length() ; i++ ) { 247 char c = sb.charAt(i); 248 if( (c=='\n') || (c=='\t') || (c=='\r')) { 249 sb.setCharAt(i, ' '); 250 } 251 } 252 return sb.toString(); 253 } 254 } 255 | Popular Tags |