roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agillil...@apache.org
Subject svn commit: r417661 - in /incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering: servlets/CommentServlet.java util/WeblogCommentRequest.java
Date Wed, 28 Jun 2006 03:39:39 GMT
Author: agilliland
Date: Tue Jun 27 20:39:39 2006
New Revision: 417661

URL: http://svn.apache.org/viewvc?rev=417661&view=rev
Log:
added new parsed object for weblog comment request and updated comment servlet to make use
of it.

NOTE: comment servlet is still partially broken because if there is and error or status message
it tries to forward to the permalink url, which won't work.  this will be fixed soon.


Added:
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/util/WeblogCommentRequest.java
Modified:
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/CommentServlet.java

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/CommentServlet.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/CommentServlet.java?rev=417661&r1=417660&r2=417661&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/CommentServlet.java
(original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/CommentServlet.java
Tue Jun 27 20:39:39 2006
@@ -46,6 +46,7 @@
 import org.apache.roller.config.RollerRuntimeConfig;
 import org.apache.roller.model.IndexManager;
 import org.apache.roller.model.RollerFactory;
+import org.apache.roller.model.UserManager;
 import org.apache.roller.model.WeblogManager;
 import org.apache.roller.pojos.CommentData;
 import org.apache.roller.pojos.UserData;
@@ -57,6 +58,7 @@
 import org.apache.roller.ui.rendering.model.UtilitiesHelper;
 import org.apache.roller.ui.rendering.util.CommentAuthenticator;
 import org.apache.roller.ui.rendering.util.DefaultCommentAuthenticator;
+import org.apache.roller.ui.rendering.util.WeblogCommentRequest;
 import org.apache.roller.util.GenericThrottle;
 import org.apache.roller.util.IPBanList;
 import org.apache.roller.util.MailUtil;
@@ -79,7 +81,7 @@
  * email sent to the blog owner and all who have commented on the same post.
  *
  * @web.servlet name="CommentServlet"
- * @web.servlet-mapping url-pattern="/comment/*"
+ * @web.servlet-mapping url-pattern="/roller-ui/rendering/comment/*"
  */
 public class CommentServlet extends HttpServlet {
     
@@ -96,9 +98,11 @@
     /** 
      * Initialization.
      */
-    public void init(ServletConfig config) throws ServletException {
+    public void init(ServletConfig servletConfig) throws ServletException {
         
-        super.init(config);
+        super.init(servletConfig);
+        
+        log.info("Initializing CommentServlet");
         
         // lookup the authenticator we are going to use and instantiate it
         try {
@@ -171,6 +175,9 @@
         String message = null;
         String entry_permalink = request.getContextPath();
         
+        WebsiteData weblog = null;
+        WeblogEntryData entry = null;
+        
         // are we doing a preview?  or a post?
         String method = request.getParameter("method");
         boolean preview = (method != null && method.equals("preview")) ? true : false;
@@ -184,40 +191,47 @@
             response.sendError(HttpServletResponse.SC_NOT_FOUND);
             return;
         }
-
-        // lookup the entry being commented on and validate it
-        WeblogEntryData entry = null;
-        String entryid = request.getParameter("entryid");
+        
+        WeblogCommentRequest commentRequest = null;
         try {
+            commentRequest = new WeblogCommentRequest(request);
+            
+            // lookup weblog specified by comment request
+            UserManager uMgr = RollerFactory.getRoller().getUserManager();
+            weblog = uMgr.getWebsiteByHandle(commentRequest.getWeblogHandle());
+            
+            if(weblog == null) {
+                throw new RollerException("unable to lookup weblog: "+
+                        commentRequest.getWeblogHandle());
+            }
+            
+            // lookup entry specified by comment request
             WeblogManager weblogMgr = RollerFactory.getRoller().getWeblogManager();
-            entry = weblogMgr.getWeblogEntry(entryid);
+            entry = weblogMgr.getWeblogEntryByAnchor(weblog, commentRequest.getWeblogAnchor());
             
-            // if we couldn't find the entry then we are done
             if(entry == null) {
-                log.debug("Entry was null: "+entryid);
-                response.sendRedirect(request.getContextPath());
-                return;
+                throw new RollerException("unable to lookup entry: "+
+                        commentRequest.getWeblogAnchor());
             }
             
             // we know what the weblog entry is, so setup our permalink url
             entry_permalink = entry.getPermaLink();
-            
-        } catch(RollerException re) {
-            log.error("Error looking up entry: "+entryid, re);
-            response.sendRedirect(request.getContextPath());
+        } catch (Exception e) {
+            // some kind of error parsing the request or looking up weblog
+            log.debug("error creating page request", e);
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
             return;
         }
         
-        log.debug("Doing comment posting for entry = "+entry_permalink);
         
-        WebsiteData website = entry.getWebsite();
+        log.debug("Doing comment posting for entry = "+entry_permalink);
         
         // check if site is allowing comments
         if(!RollerRuntimeConfig.getBooleanProperty("users.comments.enabled"))
             error = "Comments are disabled for this site.";
         
         // check if weblog and entry are allowing comments
-        if (!website.getAllowComments().booleanValue() ||
+        if (!weblog.getAllowComments().booleanValue() ||
                 !entry.getCommentsStillAllowed())
             error = "Comments not allowed on this entry";
         
@@ -235,11 +249,11 @@
         // fields: name, email, url, content, notify
         // TODO: data validation on collected comment data
         CommentData comment = new CommentData();
-        comment.setName(request.getParameter("name"));
-        comment.setEmail(request.getParameter("email"));
-        comment.setUrl(request.getParameter("url"));
-        comment.setContent(request.getParameter("content"));
-        comment.setNotify(new Boolean((request.getParameter("notify") != null)));
+        comment.setName(commentRequest.getName());
+        comment.setEmail(commentRequest.getEmail());
+        comment.setUrl(commentRequest.getUrl());
+        comment.setContent(commentRequest.getContent());
+        comment.setNotify(new Boolean(commentRequest.isNotify()));
         comment.setWeblogEntry(entry);
         comment.setRemoteHost(request.getRemoteHost());
         comment.setPostTime(new Timestamp(System.currentTimeMillis()));
@@ -280,7 +294,7 @@
                 }
                 
                 // If comment moderation is on, set comment as pending
-                if (website.getCommentModerationRequired()) {
+                if (weblog.getCommentModerationRequired()) {
                     comment.setPending(Boolean.TRUE);
                     comment.setApproved(Boolean.FALSE);
                     message = bundle.getString("commentServlet.submittedToModerator");

Added: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/util/WeblogCommentRequest.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/util/WeblogCommentRequest.java?rev=417661&view=auto
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/util/WeblogCommentRequest.java
(added)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/util/WeblogCommentRequest.java
Tue Jun 27 20:39:39 2006
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.ui.rendering.util;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * Represents a request for a weblog preview.
+ */
+public class WeblogCommentRequest extends WeblogRequest {
+    
+    private static Log log = LogFactory.getLog(WeblogCommentRequest.class);
+    
+    private static final String COMMENT_SERVLET = "/roller-ui/rendering/comment";
+    
+    private String name = null;
+    private String email = null;
+    private String url = null;
+    private String content = null;
+    private boolean notify = false;
+    private String weblogAnchor = null;
+    
+    
+    public WeblogCommentRequest() {}
+    
+    
+    public WeblogCommentRequest(HttpServletRequest request) 
+            throws InvalidRequestException {
+        
+        // let our parent take care of their business first
+        // parent determines weblog handle and locale if specified
+        super(request);
+        
+        String servlet = request.getServletPath();
+        
+        // we only want the path info left over from after our parents parsing
+        String pathInfo = this.getPathInfo();
+        
+        // was this request bound for the comment servlet?
+        if(servlet == null || !COMMENT_SERVLET.equals(servlet)) {
+            throw new InvalidRequestException("not a weblog comment request, "+
+                    request.getRequestURL());
+        }
+        
+        
+        /*
+         * parse path info.  we expect ...
+         *
+         * /entry/<anchor> - permalink
+         */
+        if(pathInfo != null && pathInfo.trim().length() > 0) {
+            
+            // we should only ever get 2 path elements
+            String[] pathElements = pathInfo.split("/");
+            if(pathElements.length == 2) {
+                
+                String context = pathElements[0];
+                if("entry".equals(context)) {
+                    try {
+                        this.weblogAnchor = 
+                                URLDecoder.decode(pathElements[1], "UTF-8");
+                    } catch (UnsupportedEncodingException ex) {
+                        // should never happen
+                        log.error(ex);
+                    }
+                    
+                } else {
+                    throw new InvalidRequestException("bad path info, "+
+                            request.getRequestURL());
+                }
+                
+            } else {
+                throw new InvalidRequestException("bad path info, "+
+                        request.getRequestURL());
+            }
+            
+        } else {
+            // bad request
+            throw new InvalidRequestException("bad path info, "+
+                    request.getRequestURL());
+        }
+        
+        
+        /*
+         * parse request parameters
+         *
+         * the only params we currently care about are:
+         *   name - comment author
+         *   email - comment email
+         *   url - comment referring url
+         *   content - comment contents
+         *   notify - if commenter wants to receive notifications
+         */
+        if(request.getParameter("name") != null) {
+            this.name = request.getParameter("name");
+        }
+        
+        if(request.getParameter("email") != null) {
+            this.email = request.getParameter("email");
+        }
+        
+        if(request.getParameter("url") != null) {
+            this.url = request.getParameter("url");
+        }
+        
+        if(request.getParameter("content") != null) {
+            this.content = request.getParameter("content");
+        }
+        
+        if(request.getParameter("notify") != null) {
+            this.notify = Boolean.valueOf(request.getParameter("notify")).booleanValue();
+        }
+        
+        if(log.isDebugEnabled()) {
+            log.debug("name = "+this.name);
+            log.debug("email = "+this.email);
+            log.debug("url = "+this.url);
+            log.debug("content = "+this.content);
+            log.debug("notify = "+this.notify);
+            log.debug("weblogAnchor = "+this.weblogAnchor);
+        }
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getEmail() {
+        return email;
+    }
+
+    public void setEmail(String email) {
+        this.email = email;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public boolean isNotify() {
+        return notify;
+    }
+
+    public void setNotify(boolean notify) {
+        this.notify = notify;
+    }
+
+    public String getWeblogAnchor() {
+        return weblogAnchor;
+    }
+
+    public void setWeblogAnchor(String weblogAnchor) {
+        this.weblogAnchor = weblogAnchor;
+    }
+    
+}



Mime
View raw message