01: package pygmy.nntp;
02:
03: import java.io.IOException;
04: import java.util.Iterator;
05:
06: public class GroupHandler extends NntpHandler {
07:
08: Forum forum;
09:
10: public GroupHandler(Forum forum) {
11: this .forum = forum;
12: }
13:
14: public boolean handleNntp(NntpRequest nntpRequest,
15: NntpResponse nntpResponse) throws IOException {
16: if (nntpRequest.getCommand().equalsIgnoreCase("list")) {
17: respondNewsgroupList(nntpResponse);
18: sendGroupList(nntpResponse.getOutputStream());
19: } else if (nntpRequest.getCommand().equalsIgnoreCase("group")) {
20: selectGroup(nntpRequest, nntpResponse);
21: } else {
22: return false;
23: }
24: return true;
25: }
26:
27: private void selectGroup(NntpRequest nntpRequest,
28: NntpResponse nntpResponse) throws IOException {
29: String groupName = nntpRequest.getParameter(0);
30: if (groupName == null || forum.getNewsgroup(groupName) == null) {
31: respondNoSuchGroup(nntpResponse);
32: } else {
33: NewsGroup group = forum.getNewsgroup(groupName);
34: nntpRequest.setCurrentNewsgroup(groupName);
35: nntpRequest.setCurrentArticle(String.valueOf(group
36: .getFirstIndex()));
37: respondGroupSelected(group, nntpResponse);
38: }
39: }
40:
41: private void sendGroupList(NntpOutputStream writer)
42: throws IOException {
43: for (Iterator i = forum.newsgroupIterator(); i.hasNext();) {
44: NewsGroup group = (NewsGroup) i.next();
45: writer.print(group.getName());
46: writer.print(" ");
47: writer.print(group.getLastIndex());
48: writer.print(" ");
49: writer.print(group.getFirstIndex());
50: writer.print(" ");
51: writer.println((group.getPostingAllowed() ? "y" : "n"));
52: }
53: writer.printEnd();
54: }
55: }
|