KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > TrackbackServlet


1 /*
2  * Created on Apr 13, 2003
3  */

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 JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServlet JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17
18
19 /**
20  * Roller's Trackback server implementation. POSTing to this Servlet will add a
21  * Trackback to a Weblog Entrty. For more info on Trackback, read the spec:
22  * <a HREF="http://www.movabletype.org/docs/mttrackback.html>MT Trackback</a>.
23  *
24  * @web.servlet name="TrackbackServlet"
25  * @web.servlet-mapping url-pattern="/trackback/*"
26  *
27  * @author David M Johnson
28  */

29 public class TrackbackServlet extends HttpServlet JavaDoc
30 {
31     /** Request parameter to indicate a trackback "tb" */
32     //private static final String TRACKBACK_PARAM = "tb";
33

34     /** Request parameter for the trackback "title" */
35     private static final String JavaDoc TRACKBACK_TITLE_PARAM = "title";
36
37     /** Request parameter for the trackback "excerpt" */
38     private static final String JavaDoc TRACKBACK_EXCERPT_PARAM = "excerpt";
39
40     /** Request parameter for the trackback "url" */
41     private static final String JavaDoc TRACKBACK_URL_PARAM = "url";
42
43     /** Request parameter for the trackback "blog_name" */
44     private static final String JavaDoc TRACKBACK_BLOG_NAME_PARAM = "blog_name";
45
46     /** Key under which the trackback return code will be placed
47      * (example: on the request for the JSPDispatcher) */

48     public static final String JavaDoc BLOJSOM_TRACKBACK_RETURN_CODE =
49             "BLOJSOM_TRACKBACK_RETURN_CODE";
50
51     /** Key under which the trackback error message will be placed
52      * (example: on the request for the JSPDispatcher) */

53     public static final String JavaDoc BLOJSOM_TRACKBACK_MESSAGE =
54             "BLOJSOM_TRACKBACK_MESSAGE";
55
56     /** Trackback success page */
57     //private static final String TRACKBACK_SUCCESS_PAGE = "trackback-success";
58

59     /** Trackback failure page */
60     //private static final String TRACKBACK_FAILURE_PAGE = "trackback-failure";
61

62     /**
63      * Constructor.
64      */

65     public TrackbackServlet()
66     {
67         super();
68     }
69
70     /**
71      * POSTing to this Servlet will add a Trackback to a Weblog Entrty.
72      */

73     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
74                    throws ServletException JavaDoc, IOException JavaDoc
75     {
76         doPost(req,res);
77     }
78     
79     /**
80      * POSTing to this Servlet will add a Trackback to a Weblog Entrty.
81      */

82     protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
83                    throws ServletException JavaDoc, IOException JavaDoc
84     {
85         try
86         {
87             // insure that incoming data is parsed as UTF-8
88
req.setCharacterEncoding("UTF-8");
89         }
90         catch (UnsupportedEncodingException JavaDoc e)
91         {
92             throw new ServletException JavaDoc("Can't set incoming encoding to UTF-8");
93         }
94
95         String JavaDoc url = req.getParameter(TRACKBACK_URL_PARAM);
96         String JavaDoc title = req.getParameter(TRACKBACK_TITLE_PARAM);
97         String JavaDoc excerpt = req.getParameter(TRACKBACK_EXCERPT_PARAM);
98         String JavaDoc 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 JavaDoc error = null;
119         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(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 JavaDoc 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         // TODO : FindBugs thinks 'pw' should close
174
}
175 }
Popular Tags