001: package pygmy.nntp;
002:
003: import org.apache.lucene.search.Hits;
004: import org.apache.lucene.queryParser.ParseException;
005: import org.apache.lucene.document.Document;
006:
007: import java.io.IOException;
008: import java.io.FileNotFoundException;
009:
010: public class ArticleHandler extends NntpHandler {
011:
012: Forum forum;
013:
014: public ArticleHandler(Forum forum) {
015: this .forum = forum;
016: }
017:
018: public boolean handleNntp(NntpRequest nntpRequest,
019: NntpResponse nntpResponse) throws IOException {
020: try {
021: if (nntpRequest.getCommand().equalsIgnoreCase("article")) {
022: readArticle(nntpRequest, nntpResponse, true, true);
023: } else if (nntpRequest.getCommand()
024: .equalsIgnoreCase("head")) {
025: readArticle(nntpRequest, nntpResponse, true, false);
026: } else if (nntpRequest.getCommand()
027: .equalsIgnoreCase("body")) {
028: readArticle(nntpRequest, nntpResponse, false, true);
029: } else if (nntpRequest.getCommand()
030: .equalsIgnoreCase("next")) {
031: gotoNextArticle(nntpRequest, nntpResponse);
032: } else if (nntpRequest.getCommand()
033: .equalsIgnoreCase("last")) {
034: gotoPreviousArticle(nntpRequest, nntpResponse);
035: } else if (nntpRequest.getCommand()
036: .equalsIgnoreCase("post")) {
037: postArticle(nntpRequest, nntpResponse);
038: } else if (nntpRequest.getCommand()
039: .equalsIgnoreCase("stat")) {
040: retrieveStatistics(nntpRequest, nntpResponse);
041: } else if (nntpRequest.getCommand()
042: .equalsIgnoreCase("mode")) {
043: if (nntpRequest.getParameter(0).equalsIgnoreCase(
044: "reader")) {
045: nntpResponse.sendResponse(200,
046: "Hello you can post.");
047: }
048: } else if (nntpRequest.getCommand().equalsIgnoreCase(
049: "xover")) {
050: nntpResponse.sendResponse(224, "Header follows");
051: NewsGroup group = forum.getNewsgroup(nntpRequest
052: .getCurrentNewsgroup());
053: Hits hits = forum.getOverview(group);
054: for (int i = 0; i < hits.length(); i++) {
055: Document doc = hits.doc(i);
056: NntpOutputStream stream = nntpResponse
057: .getOutputStream();
058: stream.print(doc.getField(
059: "article." + group.getName()
060: + ".article-number").stringValue());
061: stream.print("\t");
062: stream.print(doc.getField("article.subject")
063: .stringValue());
064: stream.print("\t");
065: stream.print(doc.getField("article.from")
066: .stringValue());
067: stream.print("\t");
068: stream.print(doc.getField("article.date")
069: .stringValue());
070: stream.print("\t");
071: stream.print(doc.getField("article.message-id")
072: .stringValue());
073: stream.print("\t");
074: stream.print(doc.getField("article.references")
075: .stringValue());
076: stream.print("\t");
077: stream.print(doc.getField("article.bytes")
078: .stringValue());
079: stream.print("\t");
080: stream.print(doc.getField("article.lines")
081: .stringValue());
082: stream.print("\t");
083: stream.print(doc.getField("article.xref")
084: .stringValue());
085: }
086: nntpResponse.getOutputStream().printEnd();
087: // } else if( nntpRequest.getCommand().equalsIgnoreCase("xhdr") ) {
088: // for( int i = 0; i < nntpRequest.parameterLength(); i++ ) {
089: // System.out.println( nntpRequest.getParameter( i ) );
090: // }
091: // nntpResponse.sendResponse( 224, "Overview information follows" );
092: // nntpResponse.getOutputStream().printEnd();
093: } else {
094: return false;
095: }
096: } catch (NoCurrentNewsgroupException e) {
097: respondNoNewsGroup(nntpResponse);
098: } catch (NoCurrentArticleException e) {
099: respondNoCurrentArticle(nntpResponse);
100: } catch (FileNotFoundException e) {
101: respondNoSuchArticle(nntpResponse);
102: } catch (ParseException e) {
103: respondSyntaxError(nntpResponse);
104: }
105: return true;
106: }
107:
108: private void retrieveStatistics(NntpRequest nntpRequest,
109: NntpResponse nntpResponse)
110: throws NoCurrentNewsgroupException, IOException {
111: String index = nntpRequest.getParameter(0);
112: Article article = null;
113: if (index.startsWith("<")) {
114: article = forum.getArticle(index);
115: } else {
116: NewsGroup group = forum.getNewsgroup(nntpRequest
117: .getCurrentNewsgroup());
118: article = group.getMessage(Integer.parseInt(index));
119: nntpRequest.setCurrentArticle(index);
120: }
121: respondNextArticleFound(article, nntpResponse);
122: }
123:
124: private void postArticle(NntpRequest nntpRequest,
125: NntpResponse nntpResponse) throws IOException {
126: try {
127: if (isPostingAllowed(nntpRequest)) {
128: respondSendArticleToBePosted(nntpResponse);
129: Article article = readArticle(nntpRequest);
130: forum.addArticle(article, nntpRequest.getProperty(
131: "Host", ""));
132: respondArticleTransferedOk(nntpResponse);
133: } else {
134: respondPostingNotAllowed(nntpResponse);
135: }
136: } catch (IOException ioe) {
137: respondPostingFailed(nntpResponse);
138: }
139: }
140:
141: private Article readArticle(NntpRequest nntpRequest)
142: throws IOException {
143: Article article = new Article(nntpRequest.getInput());
144: return article;
145: }
146:
147: private void gotoPreviousArticle(NntpRequest nntpRequest,
148: NntpResponse nntpResponse)
149: throws NoCurrentArticleException,
150: NoCurrentNewsgroupException, IOException {
151: int index = getCurrentArticleAsInt(nntpRequest);
152: if (index - 1 < forum.getNewsgroup(
153: nntpRequest.getCurrentNewsgroup()).getFirstIndex()) {
154: respondNoPreviousArticle(nntpResponse);
155: return;
156: }
157: sendNextArticleIndex(index - 1, nntpRequest, nntpResponse);
158: }
159:
160: private void gotoNextArticle(NntpRequest nntpRequest,
161: NntpResponse nntpResponse) throws IOException,
162: NoCurrentNewsgroupException, NoCurrentArticleException {
163: int index = getCurrentArticleAsInt(nntpRequest);
164: if (index + 1 > forum.getNewsgroup(
165: nntpRequest.getCurrentNewsgroup()).getLastIndex()) {
166: respondNoNextArticle(nntpResponse);
167: return;
168: }
169: sendNextArticleIndex(index + 1, nntpRequest, nntpResponse);
170: }
171:
172: private void sendNextArticleIndex(int newIndex,
173: NntpRequest nntpRequest, NntpResponse nntpResponse)
174: throws NoCurrentNewsgroupException, IOException {
175: nntpRequest.setCurrentArticle(String.valueOf(newIndex));
176: Article article = forum.getNewsgroup(
177: nntpRequest.getCurrentNewsgroup()).getMessage(newIndex);
178: respondNextArticleFound(article, nntpResponse);
179: }
180:
181: private int getCurrentArticleAsInt(NntpRequest nntpRequest)
182: throws NoCurrentArticleException {
183: String next = nntpRequest.getCurrentArticle();
184: return Integer.parseInt(next);
185: }
186:
187: private void readArticle(NntpRequest nntpRequest,
188: NntpResponse nntpResponse, boolean head, boolean body)
189: throws IOException, NoCurrentNewsgroupException,
190: NoCurrentArticleException {
191: String article = nntpRequest.getParameter(0);
192: if (article == null) {
193: article = nntpRequest.getCurrentArticle();
194: }
195: if (article.startsWith("<")) {
196: readArticleByMessageId(article, nntpResponse, head, body);
197: } else {
198: readArticleByIndex(nntpRequest.getCurrentNewsgroup(),
199: article, nntpRequest, nntpResponse, head, body);
200: }
201: }
202:
203: private void readArticleByIndex(String newsgroup, String article,
204: NntpRequest request, NntpResponse nntpResponse,
205: boolean head, boolean body) throws IOException {
206: Article message = forum.getNewsgroup(newsgroup).getMessage(
207: Integer.parseInt(article));
208: if (message == null) {
209: respondNoSuchArticleIndex(nntpResponse);
210: } else {
211: request.setCurrentArticle(article);
212: sendArticle(message, nntpResponse, head, body);
213: }
214: }
215:
216: private void readArticleByMessageId(String article,
217: NntpResponse nntpResponse, boolean head, boolean body)
218: throws IOException {
219: Article message = forum.getArticle(article);
220: sendArticle(message, nntpResponse, head, body);
221: }
222:
223: private void sendArticle(Article message,
224: NntpResponse nntpResponse, boolean head, boolean body)
225: throws IOException {
226: respondArticleRetrieved(message, nntpResponse);
227: NntpOutputStream out = nntpResponse.getOutputStream();
228: if (head) {
229: message.getHeader().print(out);
230: }
231: if (body) {
232: out.print(message.getBody());
233: }
234: out.println();
235: out.printEnd();
236: out.flush();
237: }
238: }
|