001: package pygmy.nntp.http;
002:
003: import pygmy.nntp.Forum;
004: import pygmy.nntp.NewsGroup;
005: import pygmy.nntp.http.ForumView;
006: import pygmy.core.HttpRequest;
007: import pygmy.core.HttpResponse;
008: import pygmy.core.UUID;
009:
010: import java.io.IOException;
011: import java.net.HttpURLConnection;
012:
013: public class ForumHandler {//extends AbstractXmlHandler {
014:
015: public static final String CSS = "css";
016:
017: private Forum forum;
018:
019: public ForumHandler() {
020: // forum = new Forum();
021: //
022: // NewsGroup t = forum.createNewsgroup( "Foo", "Dude" );
023: // System.out.println("t.getGuid() = " + t.getGuid());
024: // ForumMessage message = new ForumMessage("This is a subject", "charlie", "Read my contents.");
025: // ForumMessage message1 = new ForumMessage( message.getGuid(), "Bring it", "charlie", "Read my contents.");
026: // ForumMessage message2 = new ForumMessage("What is this", "charlie", "Read my contents.");
027: // ForumMessage message3 = new ForumMessage( message.getGuid(), "DotCrap!", "charlie", "Read my contents.");
028: // ForumMessage message4 = new ForumMessage("What is this shit", "charlie", "Read my contents.");
029: // ForumMessage message5 = new ForumMessage("Football is cool.", "charlie", "Read my contents.");
030: // ForumMessage message6 = new ForumMessage("I have nothing of interest to say", "charlie", "Read my contents.");
031: // ForumMessage message7 = new ForumMessage("work sucks. I know.", "charlie", "Read my contents.");
032:
033: // t.addMessage( message );
034: // t.addMessage( message1 );
035: // t.addMessage( message2 );
036: // t.addMessage( message3 );
037: // t.addMessage( message4 );
038: // t.addMessage( message5 );
039: // t.addMessage( message6 );
040: // t.addMessage( message7 );
041: }
042:
043: protected boolean handleBody(HttpRequest request,
044: HttpResponse response) throws IOException {
045: if (request.getUrl().endsWith("post")
046: && request.getMethod().equalsIgnoreCase("GET")) {
047: return createPost(request, response);
048: } else if (request.getUrl().endsWith("post")
049: && request.getMethod().equalsIgnoreCase("POST")) {
050: return postMessage(request, response);
051: } else if (request.getUrl().endsWith("read")) {
052: return readMessage(request, response);
053: } else if (request.getUrl().endsWith("topic")) {
054: return readTopic(request, response);
055: } else {
056: return readForum(request, response);
057: }
058: }
059:
060: private boolean postMessage(HttpRequest request,
061: HttpResponse response) throws IOException {
062: String subject = request.getParameter("subject");
063: String author = request.getParameter("author");
064: String messageUuidStr = request.getParameter("message");
065: String topicUuidStr = request.getParameter("topic");
066: String contents = request.getParameter("contents");
067:
068: // if( topicUuidStr == null ) return readForum( request, response );
069:
070: UUID topicUuid = UUID.parse(topicUuidStr);
071: UUID parentMessage = null;
072: if (messageUuidStr != null) {
073: parentMessage = UUID.parse(messageUuidStr);
074: }
075:
076: // NewsGroup topic = forum.getTopic( topicUuid );
077: if (request.getParameter("preview") == null) {
078: // showMessage( topicUuid, topic.addMessage( new ForumMessage( parentMessage, subject, author, contents ) ), request, response );
079: } else {
080: // previewMessage( topic, parentMessage, author, subject, contents, request, response );
081: }
082: return true;
083: }
084:
085: // private void previewMessage(NewsGroup topic, UUID parentMessage, String author, String subject, String contents, HttpRequest request, HttpResponse response ) throws IOException {
086: // CompositeView composite = new CompositeView( getUrlPrefix(), "<p>" );
087: // ForumMessage preview = new ForumMessage( parentMessage, subject, author, contents );
088: // composite.addView( createPostView( topic, preview ) );
089: // composite.addView( new MessageView( getUrlPrefix(), topic, preview ) );
090: // View view = new DocumentView( getUrlPrefix(), getStyleSheet(request), composite );
091: // response.sendResponse( HttpURLConnection.HTTP_OK, "text/html", view.render( request ) );
092: // }
093: //
094: private boolean readForum(HttpRequest request, HttpResponse response)
095: throws IOException {
096: // ForumView forumView = new ForumView( getUrlPrefix(), forum );
097: // NavigationTabsView tabs = new NavigationTabsView( getUrlPrefix(), forumView, "Forums", new String[] { "Forums" }, new String[] { forumView.getForumUrl() } );
098: // View view = new DocumentView( getUrlPrefix(), getStyleSheet(request), tabs );
099: // response.sendResponse( HttpURLConnection.HTTP_OK, "text/html", view.render( request ) );
100: return true;
101: }
102:
103: private boolean readTopic(HttpRequest request, HttpResponse response)
104: throws IOException {
105: String messageStart = request.getParameter("messageStart");
106: String numberToDisplay = request.getParameter("display");
107: UUID topicUuid = UUID.parse(request.getParameter("topic"));
108:
109: UUID startingMessage = null;
110: if (messageStart == null) {
111: // startingMessage = forum.getTopic( topicUuid ).first();
112: } else {
113: startingMessage = UUID.parse(messageStart);
114: }
115:
116: int displayLength = 20;
117: if (numberToDisplay != null) {
118: displayLength = Integer.parseInt(numberToDisplay);
119: }
120:
121: // NewsGroup topic = forum.getTopic( topicUuid );
122: // TopicView topicView = new TopicView( getUrlPrefix(), topic, startingMessage, displayLength );
123: // NavigationTabsView tabs = new NavigationTabsView( getUrlPrefix(), topicView, "NewsGroup", new String[] { "Forums", "NewsGroup", "Post" }, new String[] { getUrlPrefix() + "/", null, getUrlPrefix() + "/post?topic=" + topic.getGuid() } );
124:
125: // View view = new DocumentView( getUrlPrefix(), getStyleSheet(request), tabs );
126: // response.sendResponse( HttpURLConnection.HTTP_OK, "text/html", view.render(request) );
127: return true;
128: }
129:
130: private boolean readMessage(HttpRequest request,
131: HttpResponse response) throws IOException {
132: String messageStr = request.getParameter("message");
133: String topicStr = request.getParameter("topic");
134: if (messageStr == null)
135: return false;
136:
137: UUID message = UUID.parse(messageStr);
138: UUID topicUuid = UUID.parse(topicStr);
139:
140: showMessage(topicUuid, message, request, response);
141: return true;
142: }
143:
144: private void showMessage(UUID topicUuid, UUID messageGuid,
145: HttpRequest request, HttpResponse response)
146: throws IOException {
147: // NewsGroup topic = forum.getTopic( topicUuid );
148: // CompositeView composite = new CompositeView( getUrlPrefix(), "<p>" );
149: // ForumMessage message = topic.getMessage( messageGuid );
150: // MessageControls controls = new MessageControls( getUrlPrefix(), new MessageView( getUrlPrefix(), topic, message ), message, topic );
151: // NavigationTabsView tabs = new NavigationTabsView( getUrlPrefix(), controls, "Message", new String[] { "Forums", "NewsGroup", "Message" }, new String[] { getUrlPrefix() + "/", getUrlPrefix() + topic.getUrl(), null } );
152: // composite.addView( tabs );
153: // composite.addView( new ThreadMapView( getUrlPrefix(), topic, message ) );
154: // View view = new DocumentView( getUrlPrefix(), getStyleSheet(request), composite );
155: // response.sendResponse( HttpURLConnection.HTTP_OK, "text/html", view.render( request ) );
156: }
157:
158: private boolean createPost(HttpRequest request,
159: HttpResponse response) throws IOException {
160: // String messageStr = request.getParameter("message");
161: // String topicStr = request.getParameter("topic");
162: //
163: // UUID topicUuid = UUID.parse( topicStr );
164: // NewsGroup topic = forum.getTopic( topicUuid );
165: //
166: // UUID parentGuid = null;
167: // if( messageStr != null ) {
168: // parentGuid = UUID.parse( messageStr );
169: // }
170: //
171: // ForumMessage message = null;
172: // if( parentGuid != null ) {
173: // ForumMessage parentMessage = topic.getMessage( parentGuid );
174: // message = new ForumMessage( parentMessage.getGuid(), "Re: " + parentMessage.getSubject(), "", parentMessage.getContents() );
175: // }
176: // View tabs = createPostView(topic, message);
177: // View view = new DocumentView( getUrlPrefix(), getStyleSheet(request), tabs );
178: // response.sendResponse( HttpURLConnection.HTTP_OK, "text/html", view.render( request ) );
179: return true;
180: }
181:
182: private View createPostView(NewsGroup topic, ForumMessage message) {
183: // String[] headings = null;
184: // String[] links = null;
185: // if( message == null ) {
186: // headings = new String[] { "Forums", "NewsGroup", "Post" };
187: // links = new String[] { getUrlPrefix() + "/", getUrlPrefix() + topic.getUrl(), null };
188: // } else {
189: // headings = new String[] { "Forums", "NewsGroup", "Message", "Post" };
190: // links = new String[] { getUrlPrefix() + "/", getUrlPrefix() + topic.getUrl(), getUrlPrefix() + topic.getMessage( message.getParentGuid() ).getUrl() , null };
191: // }
192: // PostView postView = new PostView( getUrlPrefix(), topic, message );
193: // NavigationTabsView tabs = new NavigationTabsView( getUrlPrefix(), postView, "Post", headings, links );
194: // return tabs;
195: return null;
196: }
197:
198: // private String getStyleSheet(HttpRequest request) throws IOException {
199: // return request.createUrl( server.getHandlerProperty( handlerName, CSS, "/style_ie.css" ) );
200: // }
201: }
|