001: package pygmy.nntp;
002:
003: import pygmy.core.HttpHeaders;
004: import pygmy.core.InternetOutputStream;
005:
006: import java.io.IOException;
007: import java.util.Date;
008: import java.util.StringTokenizer;
009: import java.text.ParseException;
010:
011: import org.apache.lucene.document.Document;
012: import org.apache.lucene.document.Field;
013:
014: public class Article {
015:
016: int articleNumber;
017: HttpHeaders headers;
018: String body;
019:
020: public Article(NntpInputStream stream) throws IOException {
021: headers = new HttpHeaders(stream);
022: body = stream.readText();
023: }
024:
025: public int getArticleNumber() {
026: return articleNumber;
027: }
028:
029: public String getMessageId() {
030: return headers.get("Message-ID");
031: }
032:
033: public HttpHeaders getHeader() {
034: return headers;
035: }
036:
037: public String getBody() {
038: return body;
039: }
040:
041: public String getSubject() {
042: return headers.get("Subject");
043: }
044:
045: public String[] getNewsgroups() {
046: return headers.get("Newsgroups").split(",\\s?");
047: }
048:
049: public String getFrom() {
050: return headers.get("From");
051: }
052:
053: public Date getDate() throws ParseException {
054: return NntpUtil.toDate(headers.get("Date"));
055: }
056:
057: public void setArticleNumber(int index) {
058: articleNumber = index;
059: }
060:
061: public void save(InternetOutputStream stream) throws IOException {
062: headers.print(stream);
063: stream.print(body);
064: }
065:
066: public void setMessageId(String uuid) {
067: headers.put("Message-ID", uuid);
068: }
069:
070: public void addXRef(NewsGroup newsGroup, int articleNumber) {
071: if (!headers.contains("XRef")) {
072: headers.put("XRef", "localhost "); //todo this should change to be the right host
073: }
074: String xref = getXRef();
075: xref += " " + newsGroup.getName() + ":" + articleNumber;
076: headers.put("XRef", xref);
077: }
078:
079: public void addPath(String host) {
080: String path = headers.get("Path");
081: headers.put("Path", host + "!" + path);
082: }
083:
084: public void setDateReceived(Date date) {
085: headers.put("Date-Received", NntpUtil.toDateString(date));
086: }
087:
088: public String getXRef() {
089: return headers.get("Xref", "");
090: }
091:
092: public String getReferences() {
093: return headers.get("References", "");
094: }
095:
096: public String getLines() {
097: return headers.get("Lines", "");
098: }
099:
100: public Document getOveriewDocument() {
101: Document doc = new Document();
102: doc.add(Field.Keyword("article.message-id", getMessageId()));
103: doc.add(Field.Text("article.subject", getSubject()));
104: doc.add(Field.Keyword("article.from", getFrom()));
105: try {
106: doc.add(Field.Keyword("article.date", getDate()));
107: } catch (ParseException e) {
108: }
109: doc.add(Field.Text("article.references", getReferences()));
110: doc.add(Field.UnIndexed("article.bytes", String
111: .valueOf(getBody().length())));
112: doc.add(Field.UnIndexed("article.lines", getLines()));
113: doc.add(Field.UnIndexed("article.xref", getXRef()));
114: addNewsgroupArticleNumbers(doc);
115: return doc;
116: }
117:
118: private void addNewsgroupArticleNumbers(Document doc) {
119: StringTokenizer tokenizer = new StringTokenizer(getXRef(), " ");
120: tokenizer.nextToken();
121: while (tokenizer.hasMoreTokens()) {
122: String newsgroup = tokenizer.nextToken(":");
123: String articleNumber = tokenizer.nextToken();
124: doc.add(Field.Keyword("article." + newsgroup
125: + ".article-number", articleNumber));
126: }
127: }
128: }
|