1 31 package org.blojsom.extension.comment; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.blog.Blog; 36 import org.blojsom.blog.Comment; 37 import org.blojsom.blog.Entry; 38 import org.blojsom.event.EventBroadcaster; 39 import org.blojsom.fetcher.Fetcher; 40 import org.blojsom.fetcher.FetcherException; 41 import org.blojsom.plugin.comment.event.CommentAddedEvent; 42 import org.blojsom.plugin.common.ResponseConstants; 43 import org.blojsom.servlet.ServletConfigFactoryBean; 44 import org.blojsom.util.BlojsomConstants; 45 import org.blojsom.util.BlojsomUtils; 46 import org.springframework.context.support.ClassPathXmlApplicationContext; 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.Element ; 49 import org.w3c.dom.Node ; 50 import org.w3c.dom.NodeList ; 51 import org.xml.sax.SAXException ; 52 53 import javax.mail.internet.AddressException ; 54 import javax.mail.internet.InternetAddress ; 55 import javax.servlet.ServletConfig ; 56 import javax.servlet.ServletException ; 57 import javax.servlet.http.HttpServlet ; 58 import javax.servlet.http.HttpServletRequest ; 59 import javax.servlet.http.HttpServletResponse ; 60 import javax.xml.parsers.DocumentBuilder ; 61 import javax.xml.parsers.DocumentBuilderFactory ; 62 import javax.xml.parsers.FactoryConfigurationError ; 63 import javax.xml.parsers.ParserConfigurationException ; 64 import java.io.IOException ; 65 import java.io.PrintWriter ; 66 import java.io.UnsupportedEncodingException ; 67 import java.net.HttpURLConnection ; 68 import java.util.Date ; 69 import java.util.HashMap ; 70 71 83 public class CommentAPIServlet extends HttpServlet { 84 85 private Log _logger = LogFactory.getLog(CommentAPIServlet.class); 86 87 90 private static final String COMMENTAPI_TITLE = "title"; 91 92 95 private static final String COMMENTAPI_LINK = "link"; 96 97 100 private static final String COMMENTAPI_DESCRIPTION = "description"; 101 102 105 private static final String COMMENTAPI_AUTHOR = "author"; 106 107 110 private static final String COMMENTAPI_DC_CREATOR = "dc:creator"; 111 112 private static final String COMMENTAPI_TITLE_METADATA = "comment-api-metadata-title"; 113 114 private static final String COMMENTAPI_ACCEPTS_ONLY_POSTS_MESSAGE = "Comment API server only accepts POST requests."; 115 116 private String [] BLOJSOM_CONFIGURATION_FILES = {"blojsom-commentapi.xml"}; 117 118 private ClassPathXmlApplicationContext _classPathXmlApplicationContext; 119 120 123 public CommentAPIServlet() { 124 } 125 126 132 public void init(ServletConfig servletConfig) throws ServletException { 133 super.init(servletConfig); 134 135 ServletConfigFactoryBean.setServletConfig(servletConfig); 136 137 _classPathXmlApplicationContext = new ClassPathXmlApplicationContext(BLOJSOM_CONFIGURATION_FILES); 138 servletConfig.getServletContext().setAttribute(BlojsomConstants.BLOJSOM_COMMENTAPI_APPLICATION_CONTEXT, _classPathXmlApplicationContext); 139 140 if (_logger.isDebugEnabled()) { 141 _logger.debug("blojsom Comment API: All Your Blog Are Belong To Us"); 142 } 143 } 144 145 153 protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException , IOException { 154 try { 155 httpServletRequest.setCharacterEncoding(BlojsomConstants.UTF8); 156 } catch (UnsupportedEncodingException e) { 157 _logger.error(e); 158 } 159 160 if (!"post".equalsIgnoreCase(httpServletRequest.getMethod())) { 161 httpServletResponse.setContentType("text/html; charset=UTF-8"); 162 httpServletResponse.setContentLength(COMMENTAPI_ACCEPTS_ONLY_POSTS_MESSAGE.length()); 163 httpServletResponse.setStatus(HttpURLConnection.HTTP_BAD_METHOD); 164 PrintWriter printWriter = httpServletResponse.getWriter(); 165 printWriter.print(COMMENTAPI_ACCEPTS_ONLY_POSTS_MESSAGE); 166 printWriter.flush(); 167 168 return; 169 } 170 171 String commentAuthor = null; 172 String commentEmail = null; 173 String commentLink = null; 174 String commentText = null; 175 String commentTitle = null; 176 String commentDCCreator = null; 177 178 String blogId = BlojsomUtils.getBlogFromPath(httpServletRequest.getPathInfo()); 180 if (BlojsomUtils.checkNullOrBlank(blogId) || "/".equals(blogId)) { 181 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "Requested blog not found: " + blogId); 182 183 return; 184 } 185 186 Fetcher fetcher = (Fetcher) _classPathXmlApplicationContext.getBean("fetcher"); 187 188 Blog blog; 189 try { 190 blog = fetcher.loadBlog(blogId); 191 } catch (FetcherException e) { 192 if (_logger.isErrorEnabled()) { 193 _logger.error(e); 194 } 195 196 return; 197 } 198 199 if (blog.getProperty(BlojsomConstants.USE_DYNAMIC_BLOG_URLS) != null) { 200 BlojsomUtils.resolveDynamicBaseAndBlogURL(httpServletRequest, blog, blogId); 201 } 202 203 if (blog.getBlogCommentsEnabled().booleanValue() && httpServletRequest.getContentLength() > 0) { 204 String permalink = httpServletRequest.getParameter(BlojsomConstants.PERMALINK_PARAM); 205 206 try { 207 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 208 Document document = builder.parse(httpServletRequest.getInputStream()); 209 210 Element docElement = document.getDocumentElement(); 212 if (docElement.hasChildNodes()) { 213 NodeList comment = docElement.getChildNodes(); 214 if (comment.getLength() > 0) { 215 for (int x = 0; x < comment.getLength(); x++) { 216 Node node = comment.item(x); 217 if (node.getNodeType() == Node.ELEMENT_NODE) { 218 if (node.getNodeName().equals(COMMENTAPI_LINK) && (node.getFirstChild() != null)) { 219 commentLink = node.getFirstChild().getNodeValue(); 220 } 221 222 if (node.getNodeName().equals(COMMENTAPI_TITLE) && (node.getFirstChild() != null)) { 223 commentTitle = node.getFirstChild().getNodeValue(); 224 } 225 226 if (node.getNodeName().equals(COMMENTAPI_AUTHOR) && (node.getFirstChild() != null)) { 227 commentAuthor = node.getFirstChild().getNodeValue(); 228 } 229 230 if (node.getNodeName().equals(COMMENTAPI_DESCRIPTION) && (node.getFirstChild() != null)) { 231 commentText = node.getFirstChild().getNodeValue(); 232 } 233 234 if (node.getNodeName().equals(COMMENTAPI_DC_CREATOR) && (node.getFirstChild() != null)) { 235 commentDCCreator = node.getFirstChild().getNodeValue(); 236 } 237 } 238 } 239 } 240 } 241 } catch (ParserConfigurationException e) { 242 if (_logger.isErrorEnabled()) { 243 _logger.error(e); 244 } 245 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 246 247 return; 248 } catch (FactoryConfigurationError e) { 249 if (_logger.isErrorEnabled()) { 250 _logger.error(e); 251 } 252 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 253 254 return; 255 } catch (SAXException e) { 256 if (_logger.isErrorEnabled()) { 257 _logger.error(e); 258 } 259 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 260 261 return; 262 } catch (IOException e) { 263 if (_logger.isErrorEnabled()) { 264 _logger.error(e); 265 } 266 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 267 268 return; 269 } 270 271 if (commentAuthor != null) { 274 try { 275 InternetAddress emailaddress = new InternetAddress (commentAuthor); 276 commentEmail = emailaddress.getAddress(); 277 commentAuthor = emailaddress.getPersonal(); 278 if (commentAuthor == null) { 279 commentAuthor = commentEmail; 280 } 281 } catch (AddressException e) { 282 if (_logger.isErrorEnabled()) { 283 _logger.error(e); 284 } 285 } 286 } else { 287 commentAuthor = ""; 288 commentEmail = ""; 289 } 290 291 if (commentLink == null) { 293 commentLink = ""; 294 } 295 296 if (commentDCCreator != null) { 298 commentAuthor = commentDCCreator; 299 } 300 301 if (commentText != null) { 302 if (_logger.isDebugEnabled()) { 303 _logger.debug("Comment API =============================================="); 304 _logger.debug(" Blog: " + blog.getBlogId()); 305 _logger.debug(" Permalink: " + permalink); 306 _logger.debug(" Commenter: " + commentAuthor); 307 _logger.debug("Cmtr Email: " + commentEmail); 308 _logger.debug(" Link: " + commentLink); 309 _logger.debug(" Comment: \n" + commentText); 310 } 311 312 Comment comment = fetcher.newComment(); 314 try { 315 Entry entry = fetcher.loadEntry(blog, permalink); 316 317 HashMap commentMetaData = new HashMap (); 318 commentMetaData.put(COMMENTAPI_TITLE_METADATA, commentTitle); 319 320 comment.setAuthor(commentAuthor); 321 comment.setAuthorEmail(commentEmail); 322 comment.setAuthorURL(commentLink); 323 comment.setBlogId(blog.getId()); 324 comment.setComment(commentText); 325 comment.setCommentDate(new Date ()); 326 comment.setEntry(entry); 327 comment.setIp(httpServletRequest.getRemoteAddr()); 328 comment.setMetaData(commentMetaData); 329 comment.setStatus(ResponseConstants.NEW_STATUS); 330 331 fetcher.saveComment(blog, comment); 332 333 EventBroadcaster eventBroadcaster = (EventBroadcaster) _classPathXmlApplicationContext.getBean("eventBroadcaster"); 334 335 eventBroadcaster.broadcastEvent(new CommentAddedEvent(this, new Date (), comment, blog)); 336 337 httpServletResponse.setStatus(HttpServletResponse.SC_OK); 338 } catch (FetcherException e) { 339 _logger.error(e); 340 341 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "Could not find blog entry: " + permalink); 342 } 343 } else { 344 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "No comment text available"); 345 } 346 } else { 347 httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Blog comments not enabled or No content in request"); 348 } 349 } 350 351 354 public void destroy() { 355 super.destroy(); 356 357 _classPathXmlApplicationContext.destroy(); 358 359 if (_logger.isDebugEnabled()) { 360 _logger.debug("blojsom Comment API destroyed"); 361 } 362 } 363 } 364 365 | Popular Tags |