Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/roller/presentation/weblog/TrackbackServlet.java


1   /*
2    * Created on Apr 13, 2003
3    */
4   package org.roller.presentation.weblog;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.roller.pojos.WeblogEntryData;
9   import org.roller.presentation.RollerRequest;
10  
11  import java.io.IOException;
12  import java.io.PrintWriter;
13  
14  import javax.servlet.ServletException;
15  import javax.servlet.http.HttpServlet;
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  
19  
20  /**
21   * Roller's Trackback server implementation. POSTing to this Servlet will add a
22   * Trackback to a Weblog Entrty. For more info on Trackback, read the spec: 
23   * <a href="http://www.movabletype.org/docs/mttrackback.html>MT Trackback</a>.
24   * 
25   * @web.servlet name="TrackbackServlet"
26   * @web.servlet-mapping url-pattern="/trackback/*"
27   * 
28   * @author David M Johnson
29   */
30  public class TrackbackServlet extends HttpServlet
31  {
32      /** Request parameter to indicate a trackback "tb" */
33      private static final String TRACKBACK_PARAM = "tb";
34  
35      /** Request parameter for the trackback "title" */
36      private static final String TRACKBACK_TITLE_PARAM = "title";
37  
38      /** Request parameter for the trackback "excerpt" */
39      private static final String TRACKBACK_EXCERPT_PARAM = "excerpt";
40  
41      /** Request parameter for the trackback "url" */
42      private static final String TRACKBACK_URL_PARAM = "url";
43  
44      /** Request parameter for the trackback "blog_name" */
45      private static final String TRACKBACK_BLOG_NAME_PARAM = "blog_name";
46  
47      /** Key under which the trackback return code will be placed
48       * (example: on the request for the JSPDispatcher) */
49      public static final String BLOJSOM_TRACKBACK_RETURN_CODE = 
50              "BLOJSOM_TRACKBACK_RETURN_CODE";
51  
52      /** Key under which the trackback error message will be placed
53       * (example: on the request for the JSPDispatcher) */
54      public static final String BLOJSOM_TRACKBACK_MESSAGE = 
55              "BLOJSOM_TRACKBACK_MESSAGE";
56  
57      /** Trackback success page */
58      private static final String TRACKBACK_SUCCESS_PAGE = "trackback-success";
59  
60      /** Trackback failure page */
61      private static final String TRACKBACK_FAILURE_PAGE = "trackback-failure";
62  
63      /**
64       * Constructor.
65       */
66      public TrackbackServlet()
67      {
68          super();
69      }
70  
71      /** 
72       * POSTing to this Servlet will add a Trackback to a Weblog Entrty.
73       */
74      protected void doGet(HttpServletRequest req, HttpServletResponse res)
75                     throws ServletException, IOException
76      {
77          doPost(req,res);
78      }
79      
80      /** 
81       * POSTing to this Servlet will add a Trackback to a Weblog Entrty.
82       */
83      protected void doPost(HttpServletRequest req, HttpServletResponse res)
84                     throws ServletException, IOException
85      {
86          String url = req.getParameter(TRACKBACK_URL_PARAM);
87          String title = req.getParameter(TRACKBACK_TITLE_PARAM);
88          String excerpt = req.getParameter(TRACKBACK_EXCERPT_PARAM);
89          String blogName = req.getParameter(TRACKBACK_BLOG_NAME_PARAM);
90  
91          if ((title == null) || "".equals(title))
92          {
93              title = url;
94          }
95  
96          if (excerpt == null)
97          {
98              excerpt = "";
99          }
100         else
101         {
102             if (excerpt.length() >= 255)
103             {
104                 excerpt = excerpt.substring(0, 252);
105                 excerpt += "...";
106             }
107         }
108                
109         String error = null;
110         PrintWriter pw = new PrintWriter(res.getOutputStream());
111         try
112         {
113             if ( title==null || url==null || excerpt==null || blogName==null )
114             {
115                 error = "title, url, excerpt, and blog_name not specified.";
116             }
117             else
118             {
119                 RollerRequest rreq = RollerRequest.getRollerRequest(req);
120                 WeblogEntryData entry = rreq.getWeblogEntry();
121                 
122                 if ( entry != null )
123                 {
124                     entry.setRoller(rreq.getRoller());            
125                     entry.addTrackback(url,title,excerpt,blogName);
126             
127                     pw.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
128                     pw.println("<response>");
129                     pw.println("<error>0</error>");
130                     pw.println("</response>");
131                     pw.flush();
132                 }
133                 else
134                 {
135                     error = "Entry not specified.";
136                 }                
137             }
138             
139         }
140         catch (Exception e)
141         {
142             error = e.getMessage();
143             if ( error == null )
144             {   
145                 error = e.getClass().getName();
146             }
147         }
148         
149         if ( error!= null )
150         {
151             pw.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
152             pw.println("<response>");
153             pw.println("<error>1</error>");
154             pw.println("<message>ERROR: "+error+"</message>");
155             pw.println("</response>");
156             pw.flush();
157         }
158         res.flushBuffer();
159     }
160 }