001: package pygmy.nntp;
002:
003: import pygmy.core.Handler;
004: import pygmy.core.Server;
005: import pygmy.core.Request;
006: import pygmy.core.Response;
007:
008: import java.io.IOException;
009:
010: public abstract class NntpHandler implements Handler {
011:
012: private String name;
013: private Server server;
014:
015: public boolean initialize(String handlerName, Server aServer) {
016: name = handlerName;
017: server = aServer;
018: return true;
019: }
020:
021: public String getName() {
022: return name;
023: }
024:
025: public boolean isPostingAllowed(Request nntpRequest) {
026: return Boolean.valueOf(
027: nntpRequest.getProperty("posting", "true"))
028: .booleanValue();
029: }
030:
031: public boolean handle(Request request, Response response)
032: throws IOException {
033: if (request instanceof NntpRequest) {
034: return handleNntp((NntpRequest) request,
035: (NntpResponse) response);
036: }
037: return false;
038: }
039:
040: public abstract boolean handleNntp(NntpRequest nntpRequest,
041: NntpResponse nntpResponse) throws IOException;
042:
043: public boolean shutdown(Server server) {
044: return true;
045: }
046:
047: public void respondGroupSelected(NewsGroup group,
048: NntpResponse nntpResponse) throws IOException {
049: StringBuffer buffer = new StringBuffer();
050: buffer.append(group.size());
051: buffer.append(" ");
052: buffer.append(group.getFirstIndex());
053: buffer.append(" ");
054: buffer.append(group.getLastIndex());
055: buffer.append(" ");
056: buffer.append(group.getName());
057: buffer.append(" group selected.");
058: nntpResponse.sendResponse(211, buffer.toString());
059: }
060:
061: public void respondNewsgroupList(NntpResponse nntpResponse)
062: throws IOException {
063: nntpResponse.sendResponse(215, "list of newsgroups follows");
064: }
065:
066: public void respondArticleRetrieved(Article message,
067: NntpResponse nntpResponse) throws IOException {
068: nntpResponse.sendResponse(220, new String[] {
069: Integer.toString(message.getArticleNumber()),
070: message.getMessageId(),
071: "article retrieved - head and body follow" });
072: }
073:
074: public void respondNextArticleFound(Article article,
075: NntpResponse nntpResponse) throws IOException {
076: nntpResponse.sendResponse(223, new String[] {
077: String.valueOf(article.getArticleNumber()),
078: article.getMessageId(),
079: "article retrieved - request text separately" });
080: }
081:
082: public void respondListOfNews(NntpResponse response)
083: throws IOException {
084: response.sendResponse(230,
085: "list of new articles by message-id follows");
086: }
087:
088: public void respondListOfNewsgroups(NntpResponse nntpResponse)
089: throws IOException {
090: nntpResponse
091: .sendResponse(231, "list of new newsgroups follows");
092: }
093:
094: public void respondArticleTransferedOk(NntpResponse nntpResponse)
095: throws IOException {
096: nntpResponse.sendResponse(235, "article transferred ok");
097: }
098:
099: public void respondPostingOk(NntpResponse nntpResponse)
100: throws IOException {
101: nntpResponse.sendResponse(240, "article posted ok");
102: }
103:
104: public void respondArticleWanted(NntpResponse nntpResponse)
105: throws IOException {
106: nntpResponse
107: .sendResponse(335,
108: "send article to be transferred. End with <CR-LF>.<CR-LF>");
109: }
110:
111: public void respondSendArticleToBePosted(NntpResponse nntpResponse)
112: throws IOException {
113: nntpResponse.sendResponse(340,
114: "send article to be posted. End with <CR-LF>.<CR-LF>");
115: }
116:
117: public void respondNoSuchGroup(NntpResponse nntpResponse)
118: throws IOException {
119: nntpResponse.sendResponse(411, "no such news group");
120: }
121:
122: public void respondNoNewsGroup(NntpResponse nntpResponse)
123: throws IOException {
124: nntpResponse
125: .sendResponse(412, "no newsgroup has been selected");
126: }
127:
128: public void respondNoCurrentArticle(NntpResponse nntpResponse)
129: throws IOException {
130: nntpResponse.sendResponse(420,
131: "no current article has been selected");
132: }
133:
134: public void respondNoNextArticle(NntpResponse nntpResponse)
135: throws IOException {
136: nntpResponse.sendResponse(421, "no next article in this group");
137: }
138:
139: public void respondNoPreviousArticle(NntpResponse nntpResponse)
140: throws IOException {
141: nntpResponse.sendResponse(422,
142: "no previous article in this group");
143: }
144:
145: public void respondNoSuchArticleIndex(NntpResponse nntpResponse)
146: throws IOException {
147: nntpResponse.sendResponse(423,
148: "no such article number in this group");
149: }
150:
151: public void respondNoSuchArticle(NntpResponse nntpResponse)
152: throws IOException {
153: nntpResponse.sendResponse(430, "no such article found");
154: }
155:
156: public void respondArticleNotWanted(NntpResponse nntpResponse)
157: throws IOException {
158: nntpResponse.sendResponse(435,
159: "article not wanted - do not send it");
160: }
161:
162: public void respondTryTransferAgain(NntpResponse nntpResponse)
163: throws IOException {
164: nntpResponse.sendResponse(436,
165: "transfer failed - try again later");
166: }
167:
168: public void respondTransferRejected(NntpResponse nntpResponse)
169: throws IOException {
170: nntpResponse.sendResponse(437,
171: "article rejected - do not try again");
172: }
173:
174: public void respondPostingNotAllowed(NntpResponse nntpResponse)
175: throws IOException {
176: nntpResponse.sendResponse(440, "posting not allowed");
177: }
178:
179: public void respondPostingFailed(NntpResponse nntpResponse)
180: throws IOException {
181: nntpResponse.sendResponse(441, "posting failed");
182: }
183:
184: public void respondSyntaxError(NntpResponse nntpResponse)
185: throws IOException {
186: nntpResponse.sendResponse(501, "command syntax error");
187: }
188: }
|