001: package pygmy.nntp;
002:
003: import java.io.IOException;
004: import java.util.Iterator;
005: import java.util.Date;
006: import java.util.TimeZone;
007: import java.util.List;
008: import java.text.SimpleDateFormat;
009: import java.text.ParseException;
010:
011: public class NewsHandler extends NntpHandler {
012:
013: Forum forum;
014:
015: public NewsHandler(Forum forum) {
016: this .forum = forum;
017: }
018:
019: public boolean handleNntp(NntpRequest nntpRequest,
020: NntpResponse nntpResponse) throws IOException {
021: try {
022: if (nntpRequest.getCommand().equalsIgnoreCase("ihave")) {
023: retrieveArticleFromClient(nntpRequest.getParameter(0),
024: nntpRequest, nntpResponse);
025: } else if (nntpRequest.getCommand().equalsIgnoreCase(
026: "newgroups")) {
027: Date since = getNntpDate(nntpRequest, 0);
028: searchNewsgroups(since,
029: getDistributionList(nntpRequest), nntpResponse);
030: } else if (nntpRequest.getCommand().equalsIgnoreCase(
031: "newnews")) {
032: Date since = getNntpDate(nntpRequest, 0);
033: searchForNews(nntpRequest.getParameter(0), since,
034: getDistributionList(nntpRequest), nntpResponse);
035: } else {
036: return false;
037: }
038: } catch (ParseException e) {
039: respondSyntaxError(nntpResponse);
040: }
041: return true;
042: }
043:
044: private void searchForNews(String newsgroup, Date since,
045: String[] distributionList, NntpResponse nntpResponse) {
046: List list = forum.getArticle(since);
047: }
048:
049: private void searchNewsgroups(Date since, String[] distributions,
050: NntpResponse response) throws IOException {
051: List newsgroups = forum.getNewsgroups(since, distributions);
052: NntpOutputStream writer = response.getOutputStream();
053: respondListOfNewsgroups(response);
054: for (Iterator i = newsgroups.iterator(); i.hasNext();) {
055: writer.println(((NewsGroup) i.next()).getName());
056: }
057: writer.printEnd();
058: }
059:
060: private Date getNntpDate(NntpRequest nntpRequest, int startingIndex)
061: throws ParseException {
062: String dateStr = nntpRequest.getParameter(startingIndex);
063: String timeStr = nntpRequest.getParameter(startingIndex + 1);
064: TimeZone timeZone = TimeZone.getDefault();
065: if (startingIndex + 2 < nntpRequest.parameterLength()) {
066: String zone = nntpRequest.getParameter(startingIndex + 2);
067: if (zone.equalsIgnoreCase("gmt")) {
068: timeZone = TimeZone.getTimeZone(zone);
069: }
070: }
071:
072: SimpleDateFormat formatter = new SimpleDateFormat(
073: "yyMMdd HHmmss z");
074: return formatter.parse(dateStr + " " + timeStr + " "
075: + timeZone.getID());
076: }
077:
078: private String[] getDistributionList(NntpRequest nntpRequest) {
079: String[] distributions = null;
080: for (int i = 0; i < nntpRequest.parameterLength(); i++) {
081: String distribs = nntpRequest.getParameter(i);
082: if (distribs.startsWith("<") && distribs.endsWith(">")) {
083: distributions = distribs.split(",");
084: distributions[0] = distributions[0].substring(1);
085: String last = distributions[distributions.length - 1];
086: distributions[distributions.length - 1] = last
087: .substring(0, last.length() - 1);
088: break;
089: }
090: }
091: return distributions;
092: }
093:
094: private void retrieveArticleFromClient(String messageId,
095: NntpRequest nntpRequest, NntpResponse nntpResponse)
096: throws IOException {
097: if (isArticlePresent(messageId)) {
098: respondArticleNotWanted(nntpResponse);
099: } else {
100: respondArticleWanted(nntpResponse);
101: retrieveArticle(nntpRequest, nntpResponse);
102: }
103: }
104:
105: private void retrieveArticle(NntpRequest nntpRequest,
106: NntpResponse nntpResponse) throws IOException {
107: try {
108: Article article = new Article(nntpRequest.getInput());
109: forum.addArticle(article, nntpRequest.getProperty("Host",
110: ""));
111: respondArticleTransferedOk(nntpResponse);
112: } catch (IOException e) {
113: respondTryTransferAgain(nntpResponse);
114: }
115: }
116:
117: private boolean isArticlePresent(String messageId) {
118: try {
119: forum.getArticle(messageId);
120: return true;
121: } catch (IOException e) {
122: return false;
123: }
124: }
125: }
|