1 4 package org.roller.presentation.weblog; 5 6 import org.roller.pojos.WeblogEntryData; 7 import org.roller.presentation.RollerRequest; 8 9 import java.io.IOException ; 10 import java.io.PrintWriter ; 11 12 import javax.servlet.ServletException ; 13 import javax.servlet.http.HttpServlet ; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 import java.io.UnsupportedEncodingException ; 17 18 19 29 public class TrackbackServlet extends HttpServlet 30 { 31 32 34 35 private static final String TRACKBACK_TITLE_PARAM = "title"; 36 37 38 private static final String TRACKBACK_EXCERPT_PARAM = "excerpt"; 39 40 41 private static final String TRACKBACK_URL_PARAM = "url"; 42 43 44 private static final String TRACKBACK_BLOG_NAME_PARAM = "blog_name"; 45 46 48 public static final String BLOJSOM_TRACKBACK_RETURN_CODE = 49 "BLOJSOM_TRACKBACK_RETURN_CODE"; 50 51 53 public static final String BLOJSOM_TRACKBACK_MESSAGE = 54 "BLOJSOM_TRACKBACK_MESSAGE"; 55 56 57 59 60 62 65 public TrackbackServlet() 66 { 67 super(); 68 } 69 70 73 protected void doGet(HttpServletRequest req, HttpServletResponse res) 74 throws ServletException , IOException 75 { 76 doPost(req,res); 77 } 78 79 82 protected void doPost(HttpServletRequest req, HttpServletResponse res) 83 throws ServletException , IOException 84 { 85 try 86 { 87 req.setCharacterEncoding("UTF-8"); 89 } 90 catch (UnsupportedEncodingException e) 91 { 92 throw new ServletException ("Can't set incoming encoding to UTF-8"); 93 } 94 95 String url = req.getParameter(TRACKBACK_URL_PARAM); 96 String title = req.getParameter(TRACKBACK_TITLE_PARAM); 97 String excerpt = req.getParameter(TRACKBACK_EXCERPT_PARAM); 98 String blogName = req.getParameter(TRACKBACK_BLOG_NAME_PARAM); 99 100 if ((title == null) || "".equals(title)) 101 { 102 title = url; 103 } 104 105 if (excerpt == null) 106 { 107 excerpt = ""; 108 } 109 else 110 { 111 if (excerpt.length() >= 255) 112 { 113 excerpt = excerpt.substring(0, 252); 114 excerpt += "..."; 115 } 116 } 117 118 String error = null; 119 PrintWriter pw = new PrintWriter (res.getOutputStream()); 120 try 121 { 122 if ( title==null || url==null || excerpt==null || blogName==null ) 123 { 124 error = "title, url, excerpt, and blog_name not specified."; 125 } 126 else 127 { 128 RollerRequest rreq = RollerRequest.getRollerRequest(req); 129 WeblogEntryData entry = rreq.getWeblogEntry(); 130 131 if (entry!=null && entry.getCommentsStillAllowed()) 132 { 133 entry.addTrackback(url,title,excerpt,blogName); 134 rreq.getRoller().commit(); 135 136 pw.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); 137 pw.println("<response>"); 138 pw.println("<error>0</error>"); 139 pw.println("</response>"); 140 pw.flush(); 141 } 142 else if (entry!=null) 143 { 144 error = "Comments and Trackbacks are disabled for the entry you specified."; 145 } 146 else 147 { 148 error = "Entry not specified."; 149 } 150 } 151 152 } 153 catch (Exception e) 154 { 155 error = e.getMessage(); 156 if ( error == null ) 157 { 158 error = e.getClass().getName(); 159 } 160 } 161 162 if ( error!= null ) 163 { 164 pw.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); 165 pw.println("<response>"); 166 pw.println("<error>1</error>"); 167 pw.println("<message>ERROR: "+error+"</message>"); 168 pw.println("</response>"); 169 pw.flush(); 170 } 171 res.flushBuffer(); 172 173 } 175 } | Popular Tags |