You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/06/28 04:39:02 UTC
svn commit: r417640 -
/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/TrackbackServlet.java
Author: agilliland
Date: Tue Jun 27 19:39:01 2006
New Revision: 417640
URL: http://svn.apache.org/viewvc?rev=417640&view=rev
Log:
a little code tidy up.
Modified:
incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/TrackbackServlet.java
Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/TrackbackServlet.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/TrackbackServlet.java?rev=417640&r1=417639&r2=417640&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/TrackbackServlet.java (original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/TrackbackServlet.java Tue Jun 27 19:39:01 2006
@@ -67,16 +67,6 @@
/** Request parameter for the trackback "blog_name" */
private static final String TRACKBACK_BLOG_NAME_PARAM = "blog_name";
- /** Key under which the trackback return code will be placed
- * (example: on the request for the JSPDispatcher) */
- public static final String TRACKBACK_RETURN_CODE =
- "BLOJSOM_TRACKBACK_RETURN_CODE";
-
- /** Key under which the trackback error message will be placed
- * (example: on the request for the JSPDispatcher) */
- public static final String TRACKBACK_MESSAGE =
- "BLOJSOM_TRACKBACK_MESSAGE";
-
/**
* Handle incoming http GET requests.
@@ -98,6 +88,9 @@
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+ String error = null;
+ PrintWriter pw = response.getWriter();
+
String url = request.getParameter(TRACKBACK_URL_PARAM);
String title = request.getParameter(TRACKBACK_TITLE_PARAM);
String excerpt = request.getParameter(TRACKBACK_EXCERPT_PARAM);
@@ -116,102 +109,99 @@
}
}
- String error = null;
+
+ if(!RollerRuntimeConfig.getBooleanProperty("users.trackbacks.enabled")) {
+ error = "Trackbacks are disabled for this site";
+ } else if (title==null || url==null || excerpt==null || blogName==null) {
+ error = "title, url, excerpt, and blog_name not specified.";
+ }
+
+ if(error != null) {
+ pw.println(this.getErrorResponse(error));
+ return;
+ }
+
boolean verified = true;
- PrintWriter pw = new PrintWriter(response.getOutputStream());
try {
- if(!RollerRuntimeConfig.getBooleanProperty("users.trackbacks.enabled")) {
- error = "Trackbacks are disabled for this site";
- }
- else if (title==null || url==null || excerpt==null || blogName==null) {
- error = "title, url, excerpt, and blog_name not specified.";
- }
- else {
- RollerRequest rreq = RollerRequest.getRollerRequest(request);
- WeblogEntryData entry = rreq.getWeblogEntry();
- WebsiteData website = entry.getWebsite();
- boolean siteAllows = website.getAllowComments().booleanValue();
+ RollerRequest rreq = RollerRequest.getRollerRequest(request);
+ WeblogEntryData entry = rreq.getWeblogEntry();
+ WebsiteData website = entry.getWebsite();
+ boolean siteAllows = website.getAllowComments().booleanValue();
+
+ if (entry!=null && siteAllows && entry.getCommentsStillAllowed()) {
- if (entry!=null && siteAllows && entry.getCommentsStillAllowed()) {
-
- // Track trackbacks as comments
- CommentData comment = new CommentData();
- comment.setContent("[Trackback] "+excerpt);
- comment.setName(blogName);
- comment.setUrl(url);
- comment.setWeblogEntry(entry);
- comment.setNotify(Boolean.FALSE);
- comment.setPostTime(new Timestamp(new Date().getTime()));
+ // Track trackbacks as comments
+ CommentData comment = new CommentData();
+ comment.setContent("[Trackback] "+excerpt);
+ comment.setName(blogName);
+ comment.setUrl(url);
+ comment.setWeblogEntry(entry);
+ comment.setNotify(Boolean.FALSE);
+ comment.setPostTime(new Timestamp(new Date().getTime()));
+
+ // If comment contains blacklisted text, mark as spam
+ SpamChecker checker = new SpamChecker();
+ if (checker.checkTrackback(comment)) {
+ comment.setSpam(Boolean.TRUE);
+ logger.debug("Trackback blacklisted: "+comment.getUrl());
+ error = "REJECTED: trackback contains spam words";
+ }
+ // Else, if trackback verification is on...
+ else if (RollerRuntimeConfig.getBooleanProperty(
+ "site.trackbackVerification.enabled")) {
- // If comment contains blacklisted text, mark as spam
- SpamChecker checker = new SpamChecker();
- if (checker.checkTrackback(comment)) {
- comment.setSpam(Boolean.TRUE);
- logger.debug("Trackback blacklisted: "+comment.getUrl());
- error = "REJECTED: trackback contains spam words";
- }
- // Else, if trackback verification is on...
- else if (RollerRuntimeConfig.getBooleanProperty(
- "site.trackbackVerification.enabled")) {
-
- // ...ensure trackbacker actually links to us
- RollerContext rctx= RollerContext.getRollerContext();
- String absurl = rctx.getAbsoluteContextUrl(request);
- LinkbackExtractor linkback = new LinkbackExtractor(
+ // ...ensure trackbacker actually links to us
+ RollerContext rctx= RollerContext.getRollerContext();
+ String absurl = rctx.getAbsoluteContextUrl(request);
+ LinkbackExtractor linkback = new LinkbackExtractor(
comment.getUrl(), absurl + entry.getPermaLink());
- if (linkback.getExcerpt() == null) {
- comment.setPending(Boolean.TRUE);
- comment.setApproved(Boolean.FALSE);
- verified = false;
- // if we can't verify trackback, then reject it
- error = "REJECTED: trackback failed verification";
- logger.debug("Trackback failed verification: "+comment.getUrl());
- }
+ if (linkback.getExcerpt() == null) {
+ comment.setPending(Boolean.TRUE);
+ comment.setApproved(Boolean.FALSE);
+ verified = false;
+ // if we can't verify trackback, then reject it
+ error = "REJECTED: trackback failed verification";
+ logger.debug("Trackback failed verification: "+comment.getUrl());
+ }
+ }
+
+ if (error == null) {
+ // If comment moderation is on, set comment as pending
+ if (verified && website.getCommentModerationRequired()) {
+ comment.setPending(Boolean.TRUE);
+ comment.setApproved(Boolean.FALSE);
+ } else if (verified) {
+ comment.setPending(Boolean.FALSE);
+ comment.setApproved(Boolean.TRUE);
}
- if (error == null) {
- // If comment moderation is on, set comment as pending
- if (verified && website.getCommentModerationRequired()) {
- comment.setPending(Boolean.TRUE);
- comment.setApproved(Boolean.FALSE);
- } else if (verified) {
- comment.setPending(Boolean.FALSE);
- comment.setApproved(Boolean.TRUE);
- }
-
- // save, commit, send response
- WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
- mgr.saveComment(comment);
- RollerFactory.getRoller().flush();
-
- // Clear all caches associated with comment
- CacheManager.invalidate(comment);
-
- // Send email notifications
- RollerContext rc = RollerContext.getRollerContext();
- String rootURL = rc.getAbsoluteContextUrl(request);
- if (rootURL == null || rootURL.trim().length()==0) {
- rootURL = RequestUtils.serverURL(request) + request.getContextPath();
- }
- CommentServlet.sendEmailNotification(comment, rootURL);
-
- pw.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
- pw.println("<response>");
- pw.println("<error>0</error>");
- if (comment.getPending().booleanValue()) {
- pw.println("<message>Trackback sumitted to moderation</message>");
- } else {
- pw.println("<message>Trackback accepted</message>");
- }
- pw.println("</response>");
- pw.flush();
+ // save, commit, send response
+ WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
+ mgr.saveComment(comment);
+ RollerFactory.getRoller().flush();
+
+ // Clear all caches associated with comment
+ CacheManager.invalidate(comment);
+
+ // Send email notifications
+ RollerContext rc = RollerContext.getRollerContext();
+ String rootURL = rc.getAbsoluteContextUrl(request);
+ if (rootURL == null || rootURL.trim().length()==0) {
+ rootURL = RequestUtils.serverURL(request) + request.getContextPath();
}
+ CommentServlet.sendEmailNotification(comment, rootURL);
- } else if (entry!=null) {
- error = "Comments and Trackbacks are disabled for the entry you specified.";
- } else {
- error = "Entry not specified.";
+ if(comment.getPending().booleanValue()) {
+ pw.println(this.getSuccessResponse("Trackback submitted to moderator"));
+ } else {
+ pw.println(this.getSuccessResponse("Trackback accepted"));
+ }
}
+
+ } else if (entry!=null) {
+ error = "Comments and Trackbacks are disabled for the entry you specified.";
+ } else {
+ error = "Entry not specified.";
}
} catch (Exception e) {
@@ -221,17 +211,42 @@
}
}
- if ( error!= null ) {
- pw.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
- pw.println("<response>");
- pw.println("<error>1</error>");
- pw.println("<message>ERROR: "+error+"</message>");
- pw.println("</response>");
- pw.flush();
+ if(error!= null) {
+ pw.println(this.getErrorResponse(error));
}
- response.flushBuffer();
- // TODO : FindBugs thinks 'pw' should close
+ }
+
+
+ private String getSuccessResponse(String message) {
+
+ StringBuffer output = new StringBuffer();
+
+ output.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
+ output.append("<response>");
+ output.append("<error>0</error>");
+ output.append("<message>");
+ output.append(message);
+ output.append("</message>");
+ output.append("</response>");
+
+ return output.toString();
+ }
+
+
+ private String getErrorResponse(String message) {
+
+ StringBuffer output = new StringBuffer();
+
+ output.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
+ output.append("<response>");
+ output.append("<error>1</error>");
+ output.append("<message>ERROR: ");
+ output.append(message);
+ output.append("</message>");
+ output.append("</response>");
+
+ return output.toString();
}
}